Skip to content

Commit 209391c

Browse files
committed
refactor
1 parent 79adc27 commit 209391c

2 files changed

Lines changed: 48 additions & 52 deletions

File tree

src/symbolic/compare-equations.ts

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
transformEqualityInExpression,
1010
expressionsCanBeCompared,
1111
solveQuadraticEquation,
12+
hasSymbolicExponent,
13+
areNumericallyEquivalent,
1214
} from "./utils";
1315

1416
const 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

src/symbolic/utils.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export const expressionsCanBeCompared = (
2828

2929
firstEquation.traverse(function (node, path, parent) {
3030
if (node.isSymbolNode) {
31-
firstSymbolNode = true
32-
seriesNode = seriesNode || node.name.includes("[")
31+
firstSymbolNode = true;
32+
seriesNode = seriesNode || node.name.includes("[");
3333
}
3434
noFunctionOrArray =
3535
noFunctionOrArray || node.isFunctionNode || node.isArrayNode;
@@ -46,6 +46,49 @@ export const expressionsCanBeCompared = (
4646
return noFunctionOrArray && symbolNode && !seriesNode;
4747
};
4848

49+
export const hasSymbolicExponent = (node: MathNode): boolean => {
50+
let found = false;
51+
52+
node.traverse((n) => {
53+
if (
54+
n.isOperatorNode &&
55+
n.op === "^" &&
56+
!n.args[1]?.isConstantNode // Exponent is symbolic or compound
57+
) {
58+
found = true;
59+
}
60+
});
61+
62+
return found;
63+
};
64+
65+
export const areNumericallyEquivalent = (
66+
exprA: MathNode,
67+
exprB: MathNode,
68+
variables: string[],
69+
tolerance: number = 1e-10
70+
): boolean => {
71+
const compiledA = m.compile(exprA.toString());
72+
const compiledB = m.compile(exprB.toString());
73+
74+
const testValues = [1, 2, 3, 4, 5];
75+
76+
return testValues.every((val) => {
77+
const scope = variables.reduce((acc, v) => {
78+
acc[v] = val;
79+
return acc;
80+
}, {} as Record<string, number>);
81+
82+
try {
83+
const resultA = compiledA.evaluate(scope);
84+
const resultB = compiledB.evaluate(scope);
85+
return Math.abs(resultA - resultB) < tolerance;
86+
} catch {
87+
return false;
88+
}
89+
});
90+
};
91+
4992
// move the terms of the equations to the left hand side
5093
export const transformEqualityInExpression = (equality: MathNode) =>
5194
// remove added/subtracted numbers/variables from both sides of the equation

0 commit comments

Comments
 (0)