Skip to content

Commit 8b24d71

Browse files
committed
enable numerical comparison of vecs
1 parent e8424fd commit 8b24d71

File tree

7 files changed

+34
-11
lines changed

7 files changed

+34
-11
lines changed

build/math-expressions.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/math-expressions_umd.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/expression/analytic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { operators } from './variables';
33
import { functions } from './variables';
44
import { normalize_function_names, normalize_applied_functions, subscripts_to_strings } from './normalization';
55

6-
var analytic_operators = ['+', '-', '*', '/', '^', 'tuple', 'vector', 'altvector', 'list', 'array', 'matrix', 'interval'];
6+
var analytic_operators = ['+', '-', '*', '/', '^', 'tuple', 'vector', 'altvector', 'list', 'array', 'matrix', 'interval', 'vec'];
77
var non_analytic_functions = ["abs", "sign", 'arg'];
88

99
var relation_operators = ['=', 'le', 'ge', '<', '>'];

lib/expression/equality/numerical.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export const equals = function ({ expr, other, randomBindings,
127127
}
128128

129129

130+
130131
const component_equals = function ({ expr, other, randomBindings,
131132
expr_context, other_context,
132133
allow_proportional = false, require_positive_proportion = false,
@@ -165,8 +166,12 @@ const component_equals = function ({ expr, other, randomBindings,
165166
// for functions that didn't get exponents moved outside
166167
// rename function with simple exponents as a single string
167168
// so that f^2(x) will be treated as a single function and compare equal another instance of f^2(x)
168-
expr = expr.context.fromAst(rename_functions_with_exponents(expr.tree));
169-
other = other.context.fromAst(rename_functions_with_exponents(other.tree));
169+
let exprTree = rename_functions_with_exponents(expr.tree);
170+
let otherTree = rename_functions_with_exponents(other.tree);
171+
172+
// rename vec to single string
173+
expr = expr_context.fromAst(rename_vecs(exprTree));
174+
other = other_context.fromAst(rename_vecs(otherTree));
170175

171176
// Get set of variables mentioned in at least one of the two expressions
172177
var variables = [...new Set([...expr.variables(), ...other.variables()])];
@@ -692,4 +697,20 @@ function rename_functions_with_exponents(tree) {
692697

693698
return [operator, ...operands.map(rename_functions_with_exponents)];
694699

695-
}
700+
}
701+
702+
703+
function rename_vecs(tree) {
704+
if (!Array.isArray(tree)) {
705+
return tree;
706+
}
707+
708+
let operator = tree[0];
709+
let operands = tree.slice(1);
710+
711+
if (operator === "vec" && operands.length === 1) {
712+
return '__vec_' + astToText.convert(operands[0])
713+
} else {
714+
return [operator, ...operands.map(rename_vecs)]
715+
}
716+
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "math-expressions",
33
"description": "Perform basic equality testing and symbolic computations on mathematical expressions involving transcendental functions",
4-
"version": "2.0.0-alpha56",
4+
"version": "2.0.0-alpha57",
55
"author": {
66
"name": "Jim Fowler",
77
"email": "[email protected]",

spec/slow_math-expressions.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ describe("expression", function () {
392392
['A ⊆ B', 'B ⊇ A'],
393393
['A ⊄ B', 'B ⊅ A'],
394394
['A ⊈ B', 'B ⊉ A'],
395+
['vec(x)+vec(x)', '2vec(x)'],
396+
['sin^2(vec(x)*vec(y)) + cos^2(vec(x)*vec(y))', '1'],
395397
];
396398

397399
_.each(equivalences, function (equiv) {

0 commit comments

Comments
 (0)