99 transformEqualityInExpression ,
1010 expressionsCanBeCompared ,
1111 solveQuadraticEquation ,
12+ hasSymbolicExponent ,
13+ areNumericallyEquivalent ,
1214} from "./utils" ;
1315
1416const m : any = mathjs ;
@@ -49,49 +51,6 @@ export const compareEquations = (
4951 let firstEquationCoefficients : number [ ] ;
5052 let secondEquationCoefficients : number [ ] ;
5153
52- const hasSymbolicExponent = ( node : MathNode ) : boolean => {
53- let found = false ;
54-
55- node . traverse ( ( n ) => {
56- if (
57- n . isOperatorNode &&
58- n . op === "^" &&
59- ! n . args [ 1 ] ?. isConstantNode // Exponent is symbolic or compound
60- ) {
61- found = true ;
62- }
63- } ) ;
64-
65- return found ;
66- } ;
67-
68- const areNumericallyEquivalent = (
69- exprA : MathNode ,
70- exprB : MathNode ,
71- variables : string [ ] ,
72- tolerance : number = 1e-10
73- ) : boolean => {
74- const compiledA = m . compile ( exprA . toString ( ) ) ;
75- const compiledB = m . compile ( exprB . toString ( ) ) ;
76-
77- const testValues = [ 1 , 2 , 3 , 4 , 5 ] ;
78-
79- return testValues . every ( ( val ) => {
80- const scope = variables . reduce ( ( acc , v ) => {
81- acc [ v ] = val ;
82- return acc ;
83- } , { } as Record < string , number > ) ;
84-
85- try {
86- const resultA = compiledA . evaluate ( scope ) ;
87- const resultB = compiledB . evaluate ( scope ) ;
88- return Math . abs ( resultA - resultB ) < tolerance ;
89- } catch {
90- return false ;
91- }
92- } ) ;
93- } ;
94-
9554 if (
9655 hasSymbolicExponent ( firstExpression ) ||
9756 hasSymbolicExponent ( secondExpression )
@@ -101,13 +60,7 @@ export const compareEquations = (
10160 secondExpression ,
10261 firstEquationVariablesName
10362 ) ;
104- console . log (
105- "Symbolic exponent detected, fallback to numeric equivalence:" ,
106- firstExpression . toString ( ) ,
107- secondExpression . toString ( ) ,
108- "=>" ,
109- isEquivalent
110- ) ;
63+
11164 return isEquivalent ;
11265 }
11366
0 commit comments