%mathpiper

//Retract("IsEquation",*);

//Retract("*==",*);
//Retract("/==",*);
//Retract("+==",*);
//Retract("-==",*);
//Retract("==+",*);
//Retract("==-",*);

10 # IsEquation(expr_IsAtom) <-- False;
12 # IsEquation(_expr) <-- 
[
    Local(EL,res);
    EL  := FunctionToList(expr);
    res := (EL[1] = == ); 
];

10 # *==(_num,eqn_IsEquation) <-- 
[
    Local(EL,LHS,RHS);
    EL  := FunctionToList(eqn);
    LHS := Expand(Simplify( num * EL[2] ));
    RHS := Expand(Simplify( num * EL[3] ));
    LHS == RHS;
];

10 # *==(eqn_IsEquation,_num) <-- 
[
    Local(EL,LHS,RHS);
    EL  := FunctionToList(eqn);
    LHS := Expand(Simplify( num * EL[2] ));
    RHS := Expand(Simplify( num * EL[3] ));
    LHS == RHS;
];

10 # /==(eqn_IsEquation,_num) <-- 
[
    Local(EL,LHS,RHS);
    EL  := FunctionToList(eqn);
    LHS := Expand(Simplify( EL[2] / num ));
    RHS := Expand(Simplify( EL[3] / num ));
    LHS == RHS;
];

10 # +==(_num,eqn_IsEquation) <-- 
[
    Local(EL,LHS,RHS);
    EL  := FunctionToList(eqn);
    LHS := Expand(Simplify( EL[2] + num ));
    RHS := Expand(Simplify( EL[3] + num ));
    LHS == RHS;
];

10 # +==(eqn_IsEquation,_num) <-- 
[
    Local(EL,LHS,RHS);
    EL  := FunctionToList(eqn);
    LHS := Expand(Simplify( EL[2] + num ));
    RHS := Expand(Simplify( EL[3] + num ));
    LHS == RHS;
];

10 # -==(eqn_IsEquation,_num) <-- 
[
    Local(EL,LHS,RHS);
    EL  := FunctionToList(eqn);
    LHS := Expand(Simplify( EL[2] - num ));
    RHS := Expand(Simplify( EL[3] - num ));
    LHS == RHS;
];

10 # -==(_num,eqn_IsEquation) <-- 
[
    Local(EL,LHS,RHS);
    EL  := FunctionToList(eqn);
    LHS := Expand(Simplify( num - EL[2] ));
    RHS := Expand(Simplify( num - EL[3] ));
    LHS == RHS;
];

12 # ==+(eqn1_IsEquation,eqn2_IsEquation) <-- 
[
    Local(EL1,LHS,RHS,EL2);
    EL1 := FunctionToList(eqn1);
    EL2 := FunctionToList(eqn2);
    LHS := Expand(Simplify( EL1[2] + EL2[2] ));
    RHS := Expand(Simplify( EL1[3] + EL2[3] ));
    LHS == RHS;
];

12 # ==-(eqn1_IsEquation,eqn2_IsEquation) <-- 
[
    Local(EL1,LHS,RHS,EL2);
    EL1 := FunctionToList(eqn1);
    EL2 := FunctionToList(eqn2);
    LHS := Expand(Simplify( EL1[2] - EL2[2] ));
    RHS := Expand(Simplify( EL1[3] - EL2[3] ));
    LHS == RHS;
];

%/mathpiper

    %output,preserve="false"
      Result: True
.   %/output




///////////////////////////////////////////////////////////////////////////////

%mathpiper,scope="nobuild",subtype="manual_test"

Unbind(eqns,eq1,eq2,eq3,eq5,eq6,eq7,X,Y,solution);

/* Wade & Taylor, page 222, Example 2 */
//  Solve the pair of equations
//       2*x + 3*y == 7
//       3*x - 2*y == 4

eqns := { 2*x+3*y==7, 3*x-2*y==4 };
Tell(0,eqns);
NewLine();

// multiply each side of eqns[1] by 2:
eq1 := *==(2,eqns[1]);
// multiply each side of eqns[2] by 3:
eq2 := *==(3,eqns[2]);
Tell(1,eq1);
Tell(2,eq2);
NewLine();
// add the two equations together
eq3 := ==+(eq1,eq2);
Tell(Eq2+Eq3,eq3);
// solve eq3 for x
X := Solve(eq3,x);
Tell(4,X);
NewLine();

// now multiply each side of eqns[1] by 3:
eq5 := *==(3,eqns[1]);
// multiply each side of eqns[2] by 2:
eq6 := *==(2,eqns[2]);
Tell(5,eq5);
Tell(6,eq6);
NewLine();
// subtract eq6 from eq5
eq7 := ==-(eq5,eq6);
Tell(Eq5-Eq6,eq7);
// solve eq7 for y
Y := Solve(eq7,y);
Tell(8,Y);
NewLine();

solution := {X,Y};
Tell(9,solution);


%/mathpiper

    %output,preserve="false"
      Result: True
      
      Side effects:
      << 0 >> eqns {2*x+3*y==7,3*x-2*y==4} 
      
      << 1 >> eq1 4*x+6*y==14 
      << 2 >> eq2 9*x-6*y==12 
      
      << Eq2+Eq3 >> eq3 13*x==26 
      << 4 >> X {x==2} 
      
      << 5 >> eq5 6*x+9*y==21 
      << 6 >> eq6 6*x-4*y==8 
      
      << Eq5-Eq6 >> eq7 13*y==13 
      << 8 >> Y {y==1} 
      
      << 9 >> solution {{x==2},{y==1}}
.   %/output





