%mathpiper,def="TestEquivalent"

//Retract("TestEquivalent",*);
//Retract("TestTwoLists",*);

Macro("TestEquivalent",{left,right})
[
    Local(leftEval,rightEval,diff,vars,isEquiv);
    If(InVerboseMode(),[Tell(TestEquivalent,{left,right});]);
    leftEval  := @left;
    rightEval := @right;
    If(InVerboseMode(),
      [ NewLine(); Tell("    ",leftEval); Tell("   ",rightEval); ]);
    If( IsList(leftEval),
      [
          If( IsList(rightEval),
            [
                // both are lists
                If(InVerboseMode(),Tell("     both are lists "));
                isEquiv := TestTwoLists(leftEval,rightEval);
            ],
            isEquiv := False
          );
      ],
      [
          If( IsList(rightEval), 
            isEquiv := False,
            [
                // neither is a list, so check equality of diff
                If(InVerboseMode(),Tell("     neither is list "));
                If(IsEquation(leftEval),
                  [
                      If(IsEquation(rightEval),
                        [
                            If(InVerboseMode(),Tell("      both are equations"));
                            Local(dLHs,dRHS);
                            dLHS := Simplify(EquationLeft(leftEval) - EquationLeft(rightEval));
                            dRHS := Simplify(EquationRight(leftEval) - EquationRight(rightEval));
                            If(InVerboseMode(),Tell("      ",{dLHS,dRHS}));
                            isEquiv := dLHS=0 And dRHS=0;
                        ],
                        isEquiv := False
                      );
                  ],
                  [
                     If(IsEquation(rightEval),
                        isEquiv := False,
                        [
                            If(InVerboseMode(),Tell("      neither is equation"));
                            diff := Simplify(leftEval - rightEval);
                            vars := VarList(diff);
                            If(InVerboseMode(),
                              [
                                 Tell("    ",{leftEval,rightEval});
                                 Tell("    ",vars);
                                 Tell("    ",diff);
                              ]
                            );
                            isEquiv   := ( IsZero(diff) Or IsZeroVector(diff) );
                        ]
                      );
                   ]
                );
            ]
          );
      ]
    );
    If(InVerboseMode(),Tell("     Equivalence = ",isEquiv));
    If ( Not isEquiv,
      [
		  WriteString("******************");          NewLine();
		  WriteString("L.H.S. evaluates to: ");
		  Write(leftEval);                            NewLine();
		  WriteString("which differs from   ");
		  Write(rightEval);                           NewLine();
		  WriteString(" by                  "); 
		  Write(diff);                                NewLine();
		  WriteString("******************");          NewLine();
      ]
    );
    isEquiv;
];


10 # TestTwoLists( L1_IsList, L2_IsList ) <--
[
    If(InVerboseMode(),[Tell("   TestTwoLists");Tell("     ",L1);Tell("     ",L2);]);
    If(Length(L1)=1 And Length(L2)=1,
      [
          TestEquivalent(L1[1],L2[1]);
      ],
      [
          EqualAsSets(L1,L2);
      ]
    );
];

%/mathpiper

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


