"algebra (apply operations to both sides)"
Example: (Y+5)/2 is 2
Implemented a comprehensive algebraic equation solver that can solve equations by automatically applying inverse operations to both sides to isolate the variable.
- Main predicates:
solve_equation(+Equation, -Solution)- Solve equation auto-detecting variablesolve_equation(+Equation, -Variable, -Solution)- Solve for specific variablesolve_for_var(+Expression, +Value, ?Variable, -Solution)- Apply inverse operationscontains_var(+Expr, -Variable)- Detect variables in expressions
The solver handles the following operations by applying their inverses:
| Operation | Inverse | Example |
|---|---|---|
Division (/) |
Multiply both sides | Y/2 = 3 → Y = 6 |
Multiplication (*) |
Divide both sides | 2*Y = 8 → Y = 4 |
Addition (+) |
Subtract from both sides | Y+3 = 7 → Y = 4 |
Subtraction (-) |
Add to both sides | Y-2 = 5 → Y = 7 |
Power (**) |
Take nth root | Y**2 = 16 → Y = 4 |
The solver works recursively:
- Check which side contains the variable
- Identify the outermost operation
- Apply the inverse operation to both sides
- Recursively solve the simplified equation
- Base case: variable is isolated
Solving (Y+5)/2 = 2:
Step 1: (Y+5)/2 = 2 [Original equation]
Step 2: Y+5 = 4 [Multiply both sides by 2]
Step 3: Y = -1 [Subtract 5 from both sides]
algebra_solver.pl- Main algebra solver module with solving logic
tests/test_algebra_solver.pl- Comprehensive test suite (13 tests)tests/test_integrated_algebra.pl- Integration tests with starlog
demo_algebra_solver.pl- Demonstration of various equation typesdemo_problem_statement.pl- Specific demo for the problem statement
ALGEBRA_SOLVER_GUIDE.md- Comprehensive user guide with examplesREADME.md- Updated with algebra solver section
starlog.pl- Updated to export algebra solver predicates
?- solve_equation((Y+5)/2 is 2, Y, Solution).
Solution = -1. ✓- ✓ Basic equation (problem statement)
- ✓ Simple addition, subtraction, multiplication, division
- ✓ Commutative operations (5+Y, 2*Y)
- ✓ Multi-step equations (2*Y+3)
- ✓ Nested equations ((Y+1)*2)
- ✓ Division and addition combined
- ✓ Subtraction from constant (10-Y)
- ✓ Power equations (Y**2)
- ✓ Variable on right side
- ✓ Integration with starlog module
- Automatic Operation Detection: Identifies operations and applies inverses automatically
- Variable Auto-detection: Can determine which variable to solve for
- Multi-step Solving: Handles complex nested expressions
- Commutative Operations: Correctly handles both
Y+3and3+Y - Non-commutative Operations: Properly handles
10-YvsY-10 - Full Integration: Exported through main starlog module
:- use_module(starlog).
% Direct usage
?- solve_equation((Y+5)/2 is 2, Y, Solution).
Y = -1,
Solution = -1.
% Auto-detect variable
?- solve_equation(3*X+7 is 22, Result).
Result = 5.
% Complex equation
?- solve_equation(((X+2)*3-1)/2 is 7, X, Result).
X = 3,
Result = 3.All existing tests continue to pass, confirming no regressions:
- ✓ test_basic.pl
- ✓ test_nested.pl
- ✓ test_arithmetic_is.pl
- ✓ examples.pl
Successfully implemented a fully functional algebraic equation solver that can solve equations by applying operations to both sides, exactly as specified in the problem statement "(Y+5)/2 is 2".