Skip to content

Latest commit

 

History

History
123 lines (97 loc) · 3.93 KB

File metadata and controls

123 lines (97 loc) · 3.93 KB

Implementation Summary: Algebra Solver

Problem Statement

"algebra (apply operations to both sides)" Example: (Y+5)/2 is 2

Solution Overview

Implemented a comprehensive algebraic equation solver that can solve equations by automatically applying inverse operations to both sides to isolate the variable.

Implementation Details

Core Module: algebra_solver.pl

  • Main predicates:
    • solve_equation(+Equation, -Solution) - Solve equation auto-detecting variable
    • solve_equation(+Equation, -Variable, -Solution) - Solve for specific variable
    • solve_for_var(+Expression, +Value, ?Variable, -Solution) - Apply inverse operations
    • contains_var(+Expr, -Variable) - Detect variables in expressions

Supported Operations

The solver handles the following operations by applying their inverses:

Operation Inverse Example
Division (/) Multiply both sides Y/2 = 3Y = 6
Multiplication (*) Divide both sides 2*Y = 8Y = 4
Addition (+) Subtract from both sides Y+3 = 7Y = 4
Subtraction (-) Add to both sides Y-2 = 5Y = 7
Power (**) Take nth root Y**2 = 16Y = 4

Algorithm

The solver works recursively:

  1. Check which side contains the variable
  2. Identify the outermost operation
  3. Apply the inverse operation to both sides
  4. Recursively solve the simplified equation
  5. Base case: variable is isolated

Example Walkthrough

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]

Files Created

Core Implementation

  • algebra_solver.pl - Main algebra solver module with solving logic

Tests

  • tests/test_algebra_solver.pl - Comprehensive test suite (13 tests)
  • tests/test_integrated_algebra.pl - Integration tests with starlog

Demos

  • demo_algebra_solver.pl - Demonstration of various equation types
  • demo_problem_statement.pl - Specific demo for the problem statement

Documentation

  • ALGEBRA_SOLVER_GUIDE.md - Comprehensive user guide with examples
  • README.md - Updated with algebra solver section

Integration

  • starlog.pl - Updated to export algebra solver predicates

Test Results

Problem Statement Test

?- solve_equation((Y+5)/2 is 2, Y, Solution).
Solution = -1.

All Tests Pass

  • ✓ 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

Key Features

  1. Automatic Operation Detection: Identifies operations and applies inverses automatically
  2. Variable Auto-detection: Can determine which variable to solve for
  3. Multi-step Solving: Handles complex nested expressions
  4. Commutative Operations: Correctly handles both Y+3 and 3+Y
  5. Non-commutative Operations: Properly handles 10-Y vs Y-10
  6. Full Integration: Exported through main starlog module

Usage Example

:- 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.

Verification

All existing tests continue to pass, confirming no regressions:

  • ✓ test_basic.pl
  • ✓ test_nested.pl
  • ✓ test_arithmetic_is.pl
  • ✓ examples.pl

Conclusion

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".