mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
* refactor: Remove the automatic conversion from number to string. (#2482) This is a breaking change. However, nothing in the unit tests or examples actually depended on such a conversion, and it's difficult to construct situations in which it's necessary. The best such example is e.g. `count(57)` which formerly gave the number of digits in its numeric argument. Of course, after this commit, that behavior can still be obtained by the just slightly longer expression `count(string(57))` The change is proposed in preparation for an addition of new facilities/ handlers to allow symbolic computation in a couple of different ways (see #2475 and #2470). * feat(simplifyCore): convert equivalent function calls into operators (#2466) * feat(simplifyCore): convert equivalent function calls into operators Resolves #2415. * docs: Every operator has a function form Also documents the new behavior of simplifyCore to convert function calls into any equivalent operator form they may have. Also fixes the syntax errors so that simplifyCore will successfully doctest. * docs: Fix table syntax for operator->function correspondence * fix(parse): Implement amended "Rule 2" As per the discussion in #2370, the amended "Rule 2" is "when having a division followed by an implicit multiplication, the division gets higher precedence over the implicit multiplication when (a) the numerator is a constant with optionally a prefix operator (-, +, ~), and (b) the denominator is a constant." This commit implements that behavior and adds tests for it. Resolves #2370. * fix: OperatorNode.toString() outputs match implicit multiplication parsing Also greatly extends the tests on OperatorNode.toString() and .toTex(), and ensures that all tests are performed on both. (toHTML() is still a testing stepchild.) Also fixes other small bugs in .toString() and .toTex() revealed by the new tests. Resolves #1431. * test(parse): More cases of implicit multiplication * refactor: Alter the precedence of implicit multiplication This greatly simplifies OperatorNode:calculateNecessaryParentheses, as opposed to trying to correct for the change in precedence after the fact. * Fix broken unit test * Replace `options && options.implicit` with `options?.implicit` * Replace `options?.implicit` with `options && options.implicit` again, it breaks the Node 12 tests * chore: Prevent confusion with standard matrix functions. (#2465) * chore: Prevent consfusion with standard matrix functions. Prior to this commit, many functions operated elementwise on matrices even though in standard mathematical usage they have a different meaning on square matrices. Since the elementwise operation is easily recoverable using `math.map`, this commit removes the elementwise operation on arrays and matrices from these functions. Affected functions include all trigonometric functions, exp, log, gamma, square, sqrt, cube, and cbrt. Resolves #2440. * chore(typescript): Revise usages in light of changes sqrt() is now correctly typed as `number | Complex` and so must be explicitly cast to number when called on a positive and used where a Complex is disallowed; sqrt() no longer applies to matrices at all. * feat: Provide better error messages for v10 -> v11 transition Uses new `typed.onMismatch` handler so that matrix calls that used to work will suggest a replacement. * Fix #2412: let function diff return an empty matrix when the input contains only one element (#2422) * Fix #2412: let function diff return an empty matrix when the input has only one element * Undo changes in History in this fixme * Add TypeScript definitions for src/utils/is.js (#2432) This is a first step toward full publication of these functions, that were already being exported by mathjs but had not yet had the associated actions (documentation/available in parser/typed, etc.) Also, makes most of them into TypeScript type guards, and adds Matrix as a constructor type. Resolved #2431. Co-authored-by: Glen Whitney <glen@studioinfinity.org> * test: add two-dimensional test cases for diff of length 1 Co-authored-by: Chris Chudzicki <christopher.chudzicki@gmail.com> Co-authored-by: Glen Whitney <glen@studioinfinity.org> * Refactor/simplify core cleanup (#2490) * refactor: don't simplify constants in simplifyCore Keeps the operation of simplifyCore cleanly separate from simplifyConstant. * fix; handle multiple consecutive operations in simplifyCore() Also adds support for logical operators. Resolves #2484. * feat: export simplifyConstant Now that simplifyCore does not do any constant folding, clients may wish to access that behavior via simplifyConstant. Moreover, exporting it makes it easier to use in custom rule lists for simplify(). Also adds docs, embedded docs, and tests for simplifyConstant(). Also fixes simplifyCore() on logical functions (they always return boolean, rather than "short-circuiting"). Resolves #2459. * refactor: Rename matrix algorithms to stay sane in next refactor * refactor: Create a generator for boilerplate matrix versions of operations This reduces code length and duplication, and significantly reduces the number of instances of 'this' that will require replacement when moving on top of typed-function v3. * refactor: add automatic conversion from string to Node Eliminates many `this` calls in src/function/algebra, which will help conversion to typed-function v3a. Also make `resolve` into a typed function so that it will now work on strings as well, and adds a test that it does. * refactor: Use temporary conversions to simplify typed-function definitions Specifically, temporarily converting Object to Map eases the definition of 'simplify' and a new, generally ignored type 'identifier' (a subtype of 'string') with a temporary conversion to 'SymbolNode' simplifies the definition of 'derivative'. These refactors eliminate multiple instances of this, which will ease conversion to typed-function v3a. * refactor: Speed up utils/is.js typeOf function In preparation for using it as the function selector for the Unit class. Also fixes the inconsistency between the `typed` type hierarchy 'function' and typeOf returning 'Function' in favor of 'function', again to minimize the special cases in typeOf * feat(Unit): Add a method giving the (string name of the) type of the value E.g. `math.unit('5cm').valType()` returns `number`. Also uses this for an internal method that directly gives the number converter for a Unit. Also fixes lint errors from previous commit (not clean, I know, I forgot that build-and-test does not run lint). Adds tests for unit.valType() * refactor: Eliminate hyperbolic functions operating on angles There is no mathematical meaning to a hyperbolic function operating on an angle (the proper units of its argument is actually area), and it eliminates a number of uses of `this`, so remove such arguments. * refactor: Remove miscellaneous unnecessary typed-function this refs * refactor: Adapt to typed-function v3a Mostly this involves replaceing instances of 'this' with used of (preferably) typed.referTo() or typed.referToSelf(). Some repeated batterns of boilerpolate signatures within different divisions of functions (bitwise, relational, trigonometry) were factored out into their own files and reused in several of the individual functions. * tests: Only require that derivative tests mention the proper node type * refactor: remove typed.ignore * chore: Update to typed-function 3.0 Also had to deal with new typing for `resolve()` in that it now accepts strings and Matrices; added tests for the new possibilities for `resolve()`, and eliminated empty comments from the Node representation of parsed strings as they can't really be doing anyone any good and they are a pain for testing. Also updates the TypeScript declarations and tests for `resolve()` * chore: Object.hasOwn not supported in Node 14 Also removes 'resolve' from the known failing doc tests, now that it handles strings. * chore: Drop ES5 / IE 11 support. * fix(types): Remove no-longer-implementd matrix overloads * test(identifier): As requested in review item 2 * refactor(Unit): valType => valueType as per review item 3 * test(hasNumericValue): Test boolean arguments as per review item 4 * refactor(Node): Use class syntax rather than assigning prototypes This change simplifies the typeOf() function, because now all subclasses of Node have the expected constructor name. Also, reformats the documentation of the typeOf() function so that the doc test of that function will serve as an exhaustive test that the bundle returns the proper types. * Prevent chain functions from matching stored value with a rest parameter (#2559) * chore: Prevent confusion with standard matrix functions. (#2465) * chore: Prevent consfusion with standard matrix functions. Prior to this commit, many functions operated elementwise on matrices even though in standard mathematical usage they have a different meaning on square matrices. Since the elementwise operation is easily recoverable using `math.map`, this commit removes the elementwise operation on arrays and matrices from these functions. Affected functions include all trigonometric functions, exp, log, gamma, square, sqrt, cube, and cbrt. Resolves #2440. * chore(typescript): Revise usages in light of changes sqrt() is now correctly typed as `number | Complex` and so must be explicitly cast to number when called on a positive and used where a Complex is disallowed; sqrt() no longer applies to matrices at all. * feat: Provide better error messages for v10 -> v11 transition Uses new `typed.onMismatch` handler so that matrix calls that used to work will suggest a replacement. * fix: prevent chain from matching rest parameter with stored value Since the revised code needs the isTypedFunction predicate, switch to using the typed-function implementation for that throughout mathjs, rather than rolling our own here. Also adds a test that chain() no longer allows this kind of usage. Removes the two type declarations in types/index.d.ts that were allowing this sort of "split rest" call and added tests that such calls are forbidden. Adds to the chaining documentation page that such "split" calls are not allowed. * chore: Refresh this PR to reflect underlying changes Also addresses the review request with a detailed comment on the correctness of a new code section. Note that it reverts some changes to the TypeScript signatures of the matrix functions ones() and zeros() -- they do not actually have a typed-function signature of two numbers and an optional format specifically for two dimensions. What they have is a single rest parameter, from which the format is extracted if present. Hence, due to the ban on breaking rest parameters, it is not valid to call math.chain(3).zeros(2) to make a 3-by-2 matrix of zeros, which seems like a perfectly valid ban as the division of the dimensions is very confusing; this should be written as math.chain([3,2]).zeros(). The TypeScript signatures are fixed accordingly, along with the edge case of no arguments to ones() and zeros() at all, which does work to produce the "empty matrix". * Unit test `typeOf` on the minified bundle (currently failing) * Update AUTHORS * Improve testing of typeOf on browser bundle (WIP) * fix #2621: Module "mathjs" has no exported member "count" .ts(2305) (#2622) * fix #2621: Module "mathjs" has no exported member "count" .ts(2305) * feat: Update comments of count * feat: update the signature for count * feat: add usage example for count and sum * chore: Ensure type info remains in bundling Co-authored-by: Glen Whitney <glen@studioinfinity.org> Co-authored-by: Chris Chudzicki <christopher.chudzicki@gmail.com> Co-authored-by: Hansuku <1556207795@qq.com>
695 lines
34 KiB
JavaScript
695 lines
34 KiB
JavaScript
// test OperatorNode
|
|
import assert from 'assert'
|
|
|
|
import math from '../../../../src/defaultInstance.js'
|
|
const Node = math.Node
|
|
const ConstantNode = math.ConstantNode
|
|
const SymbolNode = math.SymbolNode
|
|
const OperatorNode = math.OperatorNode
|
|
const ConditionalNode = math.ConditionalNode
|
|
// Set up a bunch of expression pieces that are used over and over:
|
|
const one = new ConstantNode(1)
|
|
const two = new ConstantNode(2)
|
|
const three = new ConstantNode(3)
|
|
const four = new ConstantNode(4)
|
|
const five = new ConstantNode(5)
|
|
const add23 = new OperatorNode('+', 'add', [two, three])
|
|
const sub23 = new OperatorNode('-', 'subtract', [two, three])
|
|
|
|
const asym = new SymbolNode('a')
|
|
const bsym = new SymbolNode('b')
|
|
const csym = new SymbolNode('c')
|
|
const dsym = new SymbolNode('d')
|
|
const xsym = new SymbolNode('x')
|
|
const ysym = new SymbolNode('y')
|
|
|
|
describe('OperatorNode', function () {
|
|
it('should create an OperatorNode', function () {
|
|
const n = new OperatorNode('op', 'fn', [])
|
|
assert(n instanceof OperatorNode)
|
|
assert(n instanceof Node)
|
|
assert.strictEqual(n.type, 'OperatorNode')
|
|
})
|
|
|
|
it('should have isOperatorNode', function () {
|
|
const node = new OperatorNode('op', 'fn', [])
|
|
assert(node.isOperatorNode)
|
|
})
|
|
|
|
it('should throw an error when calling without new operator', function () {
|
|
assert.throws(
|
|
function () { OperatorNode('+', 'add', [two, three]) }, TypeError)
|
|
})
|
|
|
|
it('should compile an OperatorNode', function () {
|
|
assert.strictEqual(add23.compile().evaluate(), 5)
|
|
})
|
|
|
|
it('should test whether a unary or binary operator', function () {
|
|
const n1 = new OperatorNode('-', 'unaryMinus', [two])
|
|
assert.strictEqual(n1.isUnary(), true)
|
|
assert.strictEqual(n1.isBinary(), false)
|
|
|
|
// change the args of an operator node (bad practice, but should keep working correctly)
|
|
n1.args.push(three)
|
|
assert.strictEqual(n1.isUnary(), false)
|
|
assert.strictEqual(n1.isBinary(), true)
|
|
|
|
const n2 = new OperatorNode('+', 'add', [two, three])
|
|
assert.strictEqual(n2.isUnary(), false)
|
|
assert.strictEqual(n2.isBinary(), true)
|
|
|
|
const n3 = new OperatorNode('+', 'add', [two, three, four])
|
|
assert.strictEqual(n3.isUnary(), false)
|
|
assert.strictEqual(n3.isBinary(), false)
|
|
|
|
// change the args of an operator node (bad practice, but should keep working correctly)
|
|
n3.args.splice(2, 1)
|
|
assert.strictEqual(n3.isUnary(), false)
|
|
assert.strictEqual(n3.isBinary(), true)
|
|
})
|
|
|
|
it('should throw an error in case of unresolved operator function', function () {
|
|
const n = new OperatorNode('***', 'foo', [two, three])
|
|
|
|
assert.throws(function () {
|
|
n.compile()
|
|
}, /Function foo missing in provided namespace/)
|
|
})
|
|
|
|
it('should filter an OperatorNode', function () {
|
|
assert.deepStrictEqual(add23.filter(function (node) { return node instanceof OperatorNode }), [add23])
|
|
assert.deepStrictEqual(add23.filter(function (node) { return node instanceof SymbolNode }), [])
|
|
assert.deepStrictEqual(add23.filter(function (node) { return node instanceof ConstantNode }), [two, three])
|
|
assert.deepStrictEqual(add23.filter(function (node) { return node instanceof ConstantNode && node.value === 2 }), [two])
|
|
assert.deepStrictEqual(add23.filter(function (node) { return node instanceof ConstantNode && node.value === 4 }), [])
|
|
})
|
|
|
|
it('should filter an OperatorNode without contents', function () {
|
|
const n = new OperatorNode('op', 'fn', [])
|
|
|
|
assert.deepStrictEqual(n.filter(function (node) { return node instanceof OperatorNode }), [n])
|
|
assert.deepStrictEqual(n.filter(function (node) { return node instanceof SymbolNode }), [])
|
|
})
|
|
|
|
it('should run forEach on an OperatorNode', function () {
|
|
// x^2-x
|
|
const c = new OperatorNode('^', 'pow', [xsym, two])
|
|
const d = new SymbolNode('x') // to make sure it's different from xsym
|
|
const e = new OperatorNode('-', 'subtract', [c, d])
|
|
|
|
const nodes = []
|
|
const paths = []
|
|
e.forEach(function (node, path, parent) {
|
|
nodes.push(node)
|
|
paths.push(path)
|
|
assert.strictEqual(parent, e)
|
|
})
|
|
|
|
assert.strictEqual(nodes.length, 2)
|
|
assert.strictEqual(nodes[0], c)
|
|
assert.strictEqual(nodes[1], d)
|
|
assert.deepStrictEqual(paths, ['args[0]', 'args[1]'])
|
|
})
|
|
|
|
it('should map an OperatorNode', function () {
|
|
// x^2-x
|
|
const c = new OperatorNode('^', 'pow', [xsym, two])
|
|
const d = new SymbolNode('x') // to make sure it's different from xsym
|
|
const e = new OperatorNode('-', 'subtract', [c, d])
|
|
|
|
const nodes = []
|
|
const paths = []
|
|
const g = e.map(function (node, path, parent) {
|
|
nodes.push(node)
|
|
paths.push(path)
|
|
assert.strictEqual(parent, e)
|
|
|
|
return node instanceof SymbolNode && node.name === 'x' ? three : node
|
|
})
|
|
|
|
assert.strictEqual(nodes.length, 2)
|
|
assert.strictEqual(nodes[0], c)
|
|
assert.strictEqual(nodes[1], d)
|
|
assert.deepStrictEqual(paths, ['args[0]', 'args[1]'])
|
|
|
|
assert.notStrictEqual(g, e)
|
|
assert.strictEqual(g.args[0], e.args[0])
|
|
assert.strictEqual(g.args[0].args[0], xsym) // nested x is not replaced
|
|
assert.deepStrictEqual(g.args[0].args[1], two)
|
|
assert.deepStrictEqual(g.args[1], three)
|
|
})
|
|
|
|
it('should map an implicit OperatorNode', function () {
|
|
const product = new OperatorNode('*', 'multiply', [xsym, ysym], true /* implicit */)
|
|
|
|
assert.deepStrictEqual(product.map(function (x) { return x }), product)
|
|
})
|
|
|
|
it('should throw an error when the map callback does not return a node', function () {
|
|
const c = new OperatorNode('^', 'pow', [xsym, two])
|
|
|
|
assert.throws(function () {
|
|
c.map(function () { return undefined })
|
|
}, /Callback function must return a Node/)
|
|
})
|
|
|
|
it('should transform an OperatorNodes parameters', function () {
|
|
// x^2-x
|
|
const c = new OperatorNode('^', 'pow', [xsym, two])
|
|
const d = new SymbolNode('x') // to make sure it's different from xsym
|
|
const e = new OperatorNode('-', 'subtract', [c, d])
|
|
|
|
const g = e.transform(function (node) {
|
|
return node instanceof SymbolNode && node.name === 'x' ? three : node
|
|
})
|
|
|
|
assert.deepStrictEqual(g.args[1], three)
|
|
})
|
|
|
|
it('should transform an OperatorNode itself', function () {
|
|
const c = new OperatorNode('+', 'add', [xsym, two])
|
|
|
|
const g = c.transform(function (node) {
|
|
return node instanceof OperatorNode ? three : node
|
|
})
|
|
|
|
assert.notStrictEqual(g, c)
|
|
assert.deepStrictEqual(g, three)
|
|
})
|
|
|
|
it('should clone an OperatorNode', function () {
|
|
const c = new OperatorNode('+', 'add', [xsym, two])
|
|
|
|
const d = c.clone()
|
|
assert(d instanceof OperatorNode)
|
|
assert.deepStrictEqual(d, c)
|
|
assert.notStrictEqual(d, c)
|
|
assert.notStrictEqual(d.args, c.args)
|
|
assert.strictEqual(d.args[0], c.args[0])
|
|
assert.strictEqual(d.args[1], c.args[1])
|
|
})
|
|
|
|
it('should clone implicit multiplications', function () {
|
|
const node = new OperatorNode('*', 'multiply', [two, xsym], true)
|
|
|
|
assert.strictEqual('2 x', node.toString())
|
|
assert.strictEqual(true, node.clone().implicit)
|
|
assert.strictEqual(node.toString(), node.clone().toString())
|
|
})
|
|
|
|
it('test equality another Node', function () {
|
|
// not using the standard instances to make sure everything is fresh
|
|
const a = new OperatorNode('+', 'add', [new SymbolNode('x'), new ConstantNode(2)])
|
|
const b = new OperatorNode('+', 'add', [new SymbolNode('x'), new ConstantNode(2)])
|
|
const c = new OperatorNode('*', 'multiply', [new SymbolNode('x'), new ConstantNode(2)])
|
|
const d = new OperatorNode('*', 'add', [new SymbolNode('x'), new ConstantNode(3)])
|
|
const e = new OperatorNode('*', 'add', [new SymbolNode('x'), new ConstantNode(2), new ConstantNode(4)])
|
|
|
|
assert.strictEqual(a.equals(null), false)
|
|
assert.strictEqual(a.equals(undefined), false)
|
|
assert.strictEqual(a.equals(b), true)
|
|
assert.strictEqual(a.equals(c), false)
|
|
assert.strictEqual(a.equals(d), false)
|
|
assert.strictEqual(a.equals(e), false)
|
|
})
|
|
|
|
// Put a given expression through formatting paces: test its consistency,
|
|
// and results under toString and toTex, with various options:
|
|
// example is a object with either a key 'i' for input, or 'n' for Node
|
|
// (in the former case the value of 'i' is parsed to get the Node to test)
|
|
// and keys 's' and 'l' for the string and LaTex output, respectively.
|
|
// If the output is expected to differ for paren values of 'keep' and 'auto',
|
|
// then the keys 'skeep', 'sauto', 'lkeep', and 'lauto' can be used instead
|
|
// Takes optional 2nd argument that gives the list of paren values to try,
|
|
// defaults to ['keep', 'auto']
|
|
function ex (example, parens = ['keep', 'auto']) {
|
|
const hasi = 'i' in example
|
|
const expr = hasi ? math.parse(example.i) : example.n
|
|
const orig = hasi
|
|
? example.i
|
|
: `${expr.getIdentifier()}${expr.args.map(arg => arg.getIdentifier())}`
|
|
for (const paren of parens) {
|
|
const prefix = `${orig},${paren}: ` // eases reading of failure output
|
|
const skey = 's' in example ? 's' : 's' + paren
|
|
const lkey = 'l' in example ? 'l' : 'l' + paren
|
|
assert.strictEqual(
|
|
prefix + expr.toString({ parenthesis: paren }), prefix + example[skey])
|
|
assert.strictEqual(
|
|
prefix + expr.toTex({ parenthesis: paren }), prefix + example[lkey])
|
|
}
|
|
}
|
|
|
|
describe('toString and toTex', function () {
|
|
it('on an OperatorNode', function () {
|
|
ex({ n: add23, s: '2 + 3', l: '2+3' })
|
|
})
|
|
|
|
it('on an OperatorNode with factorial', function () {
|
|
ex({ n: new OperatorNode('!', 'factorial', [two]), s: '2!', l: '2!' })
|
|
})
|
|
|
|
it('on an OperatorNode with unary minus', function () {
|
|
ex({ n: new OperatorNode('-', 'unaryMinus', [two]), s: '-2', l: '-2' })
|
|
})
|
|
|
|
it('on an OperatorNode with zero arguments', function () {
|
|
ex({ n: new OperatorNode('foo', 'foo', []), s: 'foo()', l: '\\mathrm{foo}\\left(\\right)' })
|
|
})
|
|
|
|
it('on an OperatorNode with more than two operators', function () {
|
|
ex({
|
|
n: new OperatorNode('foo', 'foo', [two, three, four]),
|
|
s: 'foo(2, 3, 4)',
|
|
l: '\\mathrm{foo}\\left(2,3,4\\right)'
|
|
})
|
|
})
|
|
|
|
it('on addition and multiplication with more than two operands', function () {
|
|
// This is slightly different than most of the tests, so not using `ex`
|
|
const add = new OperatorNode('+', 'add', [asym, bsym, csym])
|
|
const multiply = new OperatorNode('*', 'multiply', [asym, bsym, csym])
|
|
const implicitMultiply = new OperatorNode('*', 'multiply', [asym, bsym, csym], true)
|
|
assert.strictEqual(add.toString(), 'a + b + c')
|
|
assert.strictEqual(multiply.toString(), 'a * b * c')
|
|
// The first two verify that implicit: hide is indeed the default
|
|
assert.strictEqual(implicitMultiply.toString(), 'a b c')
|
|
assert.strictEqual(implicitMultiply.toString({ implicit: 'hide' }), 'a b c')
|
|
assert.strictEqual(implicitMultiply.toString({ implicit: 'show' }), 'a * b * c')
|
|
|
|
assert.strictEqual(add.toTex(), ' a+\\mathrm{b}+ c')
|
|
assert.strictEqual(multiply.toTex(), ' a\\cdot\\mathrm{b}\\cdot c')
|
|
// The first two verify that implicit: hide is indeed the default
|
|
assert.strictEqual(implicitMultiply.toTex(), ' a~\\mathrm{b}~ c')
|
|
assert.strictEqual(implicitMultiply.toTex({ implicit: 'hide' }), ' a~\\mathrm{b}~ c')
|
|
assert.strictEqual(implicitMultiply.toTex({ implicit: 'show' }), ' a\\cdot\\mathrm{b}\\cdot c')
|
|
})
|
|
|
|
it('on addition and multiplication with more than two operands including OperatorNode', function () {
|
|
const mult = new OperatorNode('*', 'multiply', [asym, bsym])
|
|
const add = new OperatorNode('+', 'add', [asym, bsym])
|
|
|
|
const multipleMultWithMult = new OperatorNode('*', 'multiply', [csym, mult, dsym])
|
|
const multipleMultWithAdd = new OperatorNode('*', 'multiply', [csym, add, dsym])
|
|
const multipleAddWithMult = new OperatorNode('+', 'add', [csym, mult, dsym])
|
|
const multipleAddWithAdd = new OperatorNode('+', 'add', [csym, add, dsym])
|
|
|
|
ex({ n: multipleMultWithMult, s: 'c * a * b * d', l: ' c\\cdot a\\cdot\\mathrm{b}\\cdot d' })
|
|
ex({ n: multipleMultWithAdd, s: 'c * (a + b) * d', l: ' c\\cdot\\left( a+\\mathrm{b}\\right)\\cdot d' })
|
|
ex({ n: multipleAddWithMult, s: 'c + a * b + d', l: ' c+ a\\cdot\\mathrm{b}+ d' })
|
|
ex({ n: multipleAddWithAdd, s: 'c + a + b + d', l: ' c+ a+\\mathrm{b}+ d' })
|
|
})
|
|
|
|
it('on an OperatorNode that contains an operatornode with more than two operands', function () {
|
|
const mult = new OperatorNode('*', 'multiply', [asym, bsym, csym])
|
|
const add = new OperatorNode('+', 'add', [asym, bsym, csym])
|
|
|
|
const addWithMult = new OperatorNode('+', 'add', [mult, dsym])
|
|
const addWithAdd = new OperatorNode('+', 'add', [add, dsym])
|
|
const multWithMult = new OperatorNode('*', 'multiply', [mult, dsym])
|
|
const multWithAdd = new OperatorNode('*', 'multiply', [add, dsym])
|
|
|
|
ex({ n: addWithMult, s: 'a * b * c + d', l: ' a\\cdot\\mathrm{b}\\cdot c+ d' })
|
|
ex({ n: addWithAdd, s: 'a + b + c + d', l: ' a+\\mathrm{b}+ c+ d' })
|
|
ex({ n: multWithMult, s: 'a * b * c * d', l: ' a\\cdot\\mathrm{b}\\cdot c\\cdot d' })
|
|
ex({ n: multWithAdd, s: '(a + b + c) * d', l: '\\left( a+\\mathrm{b}+ c\\right)\\cdot d' })
|
|
})
|
|
|
|
it('on an OperatorNode with nested operator nodes', function () {
|
|
const sub45 = new OperatorNode('-', 'subtract', [four, five])
|
|
const prod1 = new OperatorNode('*', 'multiply', [add23, sub45])
|
|
const prod2 = new OperatorNode('*', 'multiply', [add23, four])
|
|
const diff1 = new OperatorNode('-', 'subtract', [prod2, five])
|
|
|
|
ex({ n: sub45, s: '4 - 5', l: '4-5' })
|
|
ex({ n: prod1, s: '(2 + 3) * (4 - 5)', l: '\\left(2+3\\right)\\cdot\\left(4-5\\right)' })
|
|
ex({ n: diff1, s: '(2 + 3) * 4 - 5', l: '\\left(2+3\\right)\\cdot4-5' })
|
|
})
|
|
|
|
it('on left associative OperatorNodes that are associative with another Node', function () {
|
|
ex({ i: '(a+b)+c', skeep: '(a + b) + c', sauto: 'a + b + c', lkeep: '\\left( a+\\mathrm{b}\\right)+ c', lauto: ' a+\\mathrm{b}+ c' })
|
|
ex({ i: 'a+(b+c)', skeep: 'a + (b + c)', sauto: 'a + b + c', lkeep: ' a+\\left(\\mathrm{b}+ c\\right)', lauto: ' a+\\mathrm{b}+ c' })
|
|
ex({ i: '(a+b)-c', skeep: '(a + b) - c', sauto: 'a + b - c', lkeep: '\\left( a+\\mathrm{b}\\right)- c', lauto: ' a+\\mathrm{b}- c' })
|
|
ex({ i: 'a+(b-c)', skeep: 'a + (b - c)', sauto: 'a + b - c', lkeep: ' a+\\left(\\mathrm{b}- c\\right)', lauto: ' a+\\mathrm{b}- c' })
|
|
|
|
ex({ i: '(a*b)*c', skeep: '(a * b) * c', sauto: 'a * b * c', lkeep: '\\left( a\\cdot\\mathrm{b}\\right)\\cdot c', lauto: ' a\\cdot\\mathrm{b}\\cdot c' })
|
|
ex({ i: 'a*(b*c)', skeep: 'a * (b * c)', sauto: 'a * b * c', lkeep: ' a\\cdot\\left(\\mathrm{b}\\cdot c\\right)', lauto: ' a\\cdot\\mathrm{b}\\cdot c' })
|
|
ex({ i: '(a*b)/c', skeep: '(a * b) / c', sauto: 'a * b / c', lkeep: '\\frac{\\left( a\\cdot\\mathrm{b}\\right)}{ c}', lauto: '\\frac{ a\\cdot\\mathrm{b}}{ c}' })
|
|
ex({ i: 'a*(b/c)', skeep: 'a * (b / c)', sauto: 'a * b / c', lkeep: ' a\\cdot\\left(\\frac{\\mathrm{b}}{ c}\\right)', lauto: ' a\\cdot\\frac{\\mathrm{b}}{ c}' })
|
|
})
|
|
|
|
it('on left associative OperatorNodes that are not associative with another Node', function () {
|
|
ex({ i: '(a-b)-c', skeep: '(a - b) - c', sauto: 'a - b - c', lkeep: '\\left( a-\\mathrm{b}\\right)- c', lauto: ' a-\\mathrm{b}- c' })
|
|
ex({ i: 'a-(b-c)', s: 'a - (b - c)', l: ' a-\\left(\\mathrm{b}- c\\right)' })
|
|
ex({ i: '(a-b)+c', skeep: '(a - b) + c', sauto: 'a - b + c', lkeep: '\\left( a-\\mathrm{b}\\right)+ c', lauto: ' a-\\mathrm{b}+ c' })
|
|
ex({ i: 'a-(b+c)', s: 'a - (b + c)', l: ' a-\\left(\\mathrm{b}+ c\\right)' })
|
|
|
|
ex({ i: '(a/b)/c', skeep: '(a / b) / c', sauto: 'a / b / c', lkeep: '\\frac{\\left(\\frac{ a}{\\mathrm{b}}\\right)}{ c}', lauto: '\\frac{\\frac{ a}{\\mathrm{b}}}{ c}' })
|
|
ex({ i: 'a/(b/c)', s: 'a / (b / c)', lkeep: '\\frac{ a}{\\left(\\frac{\\mathrm{b}}{ c}\\right)}', lauto: '\\frac{ a}{\\frac{\\mathrm{b}}{ c}}' })
|
|
ex({ i: '(a/b)*c', skeep: '(a / b) * c', sauto: 'a / b * c', lkeep: '\\left(\\frac{ a}{\\mathrm{b}}\\right)\\cdot c', lauto: '\\frac{ a}{\\mathrm{b}}\\cdot c' })
|
|
ex({ i: 'a/(b*c)', s: 'a / (b * c)', lkeep: '\\frac{ a}{\\left(\\mathrm{b}\\cdot c\\right)}', lauto: '\\frac{ a}{\\mathrm{b}\\cdot c}' })
|
|
})
|
|
|
|
it('on right associative OperatorNodes that are not associative with another Node', function () {
|
|
ex({ i: '(a^b)^c', s: '(a ^ b) ^ c', l: '{\\left({ a}^{\\mathrm{b}}\\right)}^{ c}' })
|
|
ex({ i: 'a^(b^c)', skeep: 'a ^ (b ^ c)', sauto: 'a ^ b ^ c', lkeep: '{ a}^{\\left({\\mathrm{b}}^{ c}\\right)}', lauto: '{ a}^{{\\mathrm{b}}^{ c}}' })
|
|
})
|
|
|
|
it('on unary OperatorNodes containing a binary OperatorNode', function () {
|
|
ex({ i: '(a*b)!', s: '(a * b)!', l: '\\left( a\\cdot\\mathrm{b}\\right)!' })
|
|
ex({ i: '-(a*b)', s: '-(a * b)', l: '-\\left( a\\cdot\\mathrm{b}\\right)' })
|
|
ex({ i: '-(a+b)', s: '-(a + b)', l: '-\\left( a+\\mathrm{b}\\right)' })
|
|
})
|
|
|
|
it('on unary OperatorNodes containing a unary OperatorNode', function () {
|
|
ex({ i: '(-a)!', s: '(-a)!', l: '\\left(- a\\right)!' })
|
|
ex({ i: '-(a!)', skeep: '-(a!)', sauto: '-a!', lkeep: '-\\left( a!\\right)', lauto: '- a!' })
|
|
ex({ i: '-(-a)', s: '-(-a)', l: '-\\left(- a\\right)' })
|
|
})
|
|
})
|
|
|
|
it('should stringify an OperatorNode with custom toString', function () {
|
|
// Also checks if the custom functions get passed on to the children
|
|
const customFunction = function (node, options) {
|
|
if (node.type === 'OperatorNode') {
|
|
return node.op + node.fn + '(' +
|
|
node.args[0].toString(options) +
|
|
', ' + node.args[1].toString(options) + ')'
|
|
} else if (node.type === 'ConstantNode') {
|
|
return 'const(' + node.value + ', ' + math.typeOf(node.value) + ')'
|
|
}
|
|
}
|
|
|
|
const n2 = new OperatorNode('-', 'subtract', [one, two])
|
|
|
|
assert.strictEqual(add23.toString({ handler: customFunction }), '+add(const(2, number), const(3, number))')
|
|
assert.strictEqual(n2.toString({ handler: customFunction }), '-subtract(const(1, number), const(2, number))')
|
|
})
|
|
|
|
it('should stringify an OperatorNode with custom toString for a single operator', function () {
|
|
// Also checks if the custom functions get passed on to the children
|
|
const customFunction = function (node, options) {
|
|
if ((node.type === 'OperatorNode') && (node.fn === 'add')) {
|
|
return node.args[0].toString(options) +
|
|
node.op + node.fn + node.op +
|
|
node.args[1].toString(options)
|
|
} else if (node.type === 'ConstantNode') {
|
|
return 'const(' + node.value + ', ' + math.typeOf(node.value) + ')'
|
|
}
|
|
}
|
|
|
|
assert.strictEqual(add23.toString({ handler: customFunction }), 'const(2, number)+add+const(3, number)')
|
|
})
|
|
|
|
it('should respect the \'all\' parenthesis option', function () {
|
|
ex({ i: '1+1+1', s: '(1 + 1) + 1', l: '\\left(1+1\\right)+1' }, ['all'])
|
|
})
|
|
|
|
it('should correctly format fractions in \'all\' parenthesis mode', function () {
|
|
ex({ i: '1/2/3', s: '(1 / 2) / 3', l: '\\frac{\\left(\\frac{1}{2}\\right)}{3}' },
|
|
['all'])
|
|
})
|
|
|
|
it('should format an OperatorNode with factorial of an OperatorNode', function () {
|
|
const mult23 = new OperatorNode('*', 'multiply', [two, three])
|
|
const div23 = new OperatorNode('/', 'divide', [two, three])
|
|
|
|
const n1 = new OperatorNode('!', 'factorial', [sub23])
|
|
const n2 = new OperatorNode('!', 'factorial', [add23])
|
|
const n3 = new OperatorNode('!', 'factorial', [mult23])
|
|
const n4 = new OperatorNode('!', 'factorial', [div23])
|
|
ex({ n: n1, s: '(2 - 3)!', l: '\\left(2-3\\right)!' })
|
|
ex({ n: n2, s: '(2 + 3)!', l: '\\left(2+3\\right)!' })
|
|
ex({ n: n3, s: '(2 * 3)!', l: '\\left(2\\cdot3\\right)!' })
|
|
ex({ n: n4, s: '(2 / 3)!', l: '\\frac{2}{3}!' })
|
|
})
|
|
|
|
it('should format an OperatorNode with unary minus', function () {
|
|
const n2 = new OperatorNode('-', 'unaryMinus', [sub23])
|
|
const n3 = new OperatorNode('-', 'unaryMinus', [add23])
|
|
|
|
ex({ n: n2, s: '-(2 - 3)', l: '-\\left(2-3\\right)' })
|
|
ex({ n: n3, s: '-(2 + 3)', l: '-\\left(2+3\\right)' })
|
|
})
|
|
|
|
it('should format an OperatorNode that subtracts an OperatorNode', function () {
|
|
const n1 = new OperatorNode('-', 'subtract', [one, sub23])
|
|
const n2 = new OperatorNode('-', 'subtract', [one, add23])
|
|
|
|
ex({ n: n1, s: '1 - (2 - 3)', l: '1-\\left(2-3\\right)' })
|
|
ex({ n: n2, s: '1 - (2 + 3)', l: '1-\\left(2+3\\right)' })
|
|
})
|
|
|
|
it('should format fractions with operators that are enclosed in parenthesis', function () {
|
|
ex({ n: new OperatorNode('/', 'divide', [add23, four]), s: '(2 + 3) / 4', l: '\\frac{2+3}{4}' })
|
|
})
|
|
|
|
it('should have an identifier', function () {
|
|
assert.strictEqual(add23.getIdentifier(), 'OperatorNode:add')
|
|
})
|
|
|
|
it('should LaTeX an OperatorNode with custom toTex', function () {
|
|
// Also checks if the custom functions get passed on to the children
|
|
const customFunction = function (node, options) {
|
|
if (node.type === 'OperatorNode') {
|
|
return node.op + node.fn + '(' +
|
|
node.args[0].toTex(options) +
|
|
', ' + node.args[1].toTex(options) + ')'
|
|
} else if (node.type === 'ConstantNode') {
|
|
return 'const\\left(' + node.value + ', ' + math.typeOf(node.value) + '\\right)'
|
|
}
|
|
}
|
|
|
|
assert.strictEqual(add23.toTex({ handler: customFunction }), '+add(const\\left(2, number\\right), const\\left(3, number\\right))')
|
|
assert.strictEqual(sub23.toTex({ handler: customFunction }), '-subtract(const\\left(2, number\\right), const\\left(3, number\\right))')
|
|
})
|
|
|
|
it('should LaTeX an OperatorNode with custom toTex for a single operator', function () {
|
|
// Also checks if the custom functions get passed on to the children
|
|
const customFunction = function (node, options) {
|
|
if ((node.type === 'OperatorNode') && (node.fn === 'add')) {
|
|
return node.args[0].toTex(options) +
|
|
node.op + node.fn + node.op +
|
|
node.args[1].toTex(options)
|
|
} else if (node.type === 'ConstantNode') {
|
|
return 'const\\left(' + node.value + ', ' + math.typeOf(node.value) + '\\right)'
|
|
}
|
|
}
|
|
|
|
assert.strictEqual(add23.toTex({ handler: customFunction }), 'const\\left(2, number\\right)+add+const\\left(3, number\\right)')
|
|
})
|
|
|
|
it('should format powers of fractions with parentheses', function () {
|
|
const frac = new OperatorNode('/', 'divide', [one, one])
|
|
const pow = new OperatorNode('^', 'pow', [frac, one])
|
|
|
|
ex({ n: pow, s: '(1 / 1) ^ 1', l: '\\left({\\frac{1}{1}}\\right)^{1}' })
|
|
})
|
|
|
|
it('should format powers of conditions with parentheses', function () {
|
|
const cond = new ConditionalNode(one, one, one)
|
|
const pow = new OperatorNode('^', 'pow', [cond, one])
|
|
|
|
ex({ n: pow, s: '(1 ? 1 : 1) ^ 1', l: '\\left({\\begin{cases} {1}, &\\quad{\\text{if }\\;1}\\\\{1}, &\\quad{\\text{otherwise}}\\end{cases}}\\right)^{1}' })
|
|
})
|
|
|
|
it('should format simple expressions in \'auto\' mode', function () {
|
|
// this covers a bug that was triggered previously
|
|
ex({ i: '1+(1+1)', skeep: '1 + (1 + 1)', sauto: '1 + 1 + 1', lkeep: '1+\\left(1+1\\right)', lauto: '1+1+1' })
|
|
})
|
|
|
|
// Variant of the `ex` tester that also tests implicit hide and show
|
|
function exhs (example, parens = ['keep', 'auto']) {
|
|
const imps = ['hide', 'show']
|
|
const hasi = 'i' in example
|
|
const expr = hasi ? math.parse(example.i) : example.n
|
|
const orig = hasi
|
|
? example.i
|
|
: `${expr.getIdentifier()}${expr.args.map(arg => arg.getIdentifier())}`
|
|
for (const paren of parens) {
|
|
const skey = 's' in example ? 's' : 's' + paren
|
|
const lkey = 'l' in example ? 'l' : 'l' + paren
|
|
for (const i of [0, 1]) {
|
|
const prefix = `${orig},${paren},${imps[i]}: ` // eases reading of failure output
|
|
assert.strictEqual(
|
|
prefix + expr.toString({ parenthesis: paren, implicit: imps[i] }),
|
|
prefix + example[skey][i])
|
|
assert.strictEqual(
|
|
prefix + expr.toTex({ parenthesis: paren, implicit: imps[i] }),
|
|
prefix + example[lkey][i])
|
|
}
|
|
}
|
|
}
|
|
|
|
it('should format implicit multiplications', function () {
|
|
exhs({ i: '4a', s: ['4 a', '4 * a'], l: ['4~ a', '4\\cdot a'] })
|
|
exhs({ i: '4 a', s: ['4 a', '4 * a'], l: ['4~ a', '4\\cdot a'] })
|
|
exhs({ i: 'a b', s: ['a b', 'a * b'], l: [' a~\\mathrm{b}', ' a\\cdot\\mathrm{b}'] })
|
|
exhs({ i: '2a b', s: ['2 a b', '2 * a * b'], l: ['2~ a~\\mathrm{b}', '2\\cdot a\\cdot\\mathrm{b}'] })
|
|
exhs({ i: 'a b c', s: ['a b c', 'a * b * c'], l: [' a~\\mathrm{b}~ c', ' a\\cdot\\mathrm{b}\\cdot c'] })
|
|
exhs({ i: '(2+3)a', s: ['(2 + 3) a', '(2 + 3) * a'], l: ['\\left(2+3\\right)~ a', '\\left(2+3\\right)\\cdot a'] })
|
|
exhs({ i: '(2+3)2', s: ['(2 + 3) 2', '(2 + 3) * 2'], l: ['\\left(2+3\\right)~2', '\\left(2+3\\right)\\cdot2'] })
|
|
exhs({ i: '2(3+4)', s: ['2 (3 + 4)', '2 * (3 + 4)'], l: ['2~\\left(3+4\\right)', '2\\cdot\\left(3+4\\right)'] })
|
|
exhs({ i: 'a / b c', s: ['a / b c', 'a / (b * c)'], l: ['\\frac{ a}{\\mathrm{b}~ c}', '\\frac{ a}{\\mathrm{b}\\cdot c}'] })
|
|
exhs({ i: 'a / b c d', s: ['a / b c d', 'a / (b * c * d)'], l: ['\\frac{ a}{\\mathrm{b}~ c~ d}', '\\frac{ a}{\\mathrm{b}\\cdot c\\cdot d}'] })
|
|
exhs({ i: '1/2 a', s: ['1 / 2 a', '1 / 2 * a'], l: ['\\frac{1}{2}~ a', '\\frac{1}{2}\\cdot a'] })
|
|
exhs({ i: '-2/3 a', s: ['-2 / 3 a', '-2 / 3 * a'], l: ['\\frac{-2}{3}~ a', '\\frac{-2}{3}\\cdot a'] })
|
|
exhs({ i: '2!/3 a', s: ['2! / 3 a', '2! / (3 * a)'], l: ['\\frac{2!}{3~ a}', '\\frac{2!}{3\\cdot a}'] })
|
|
exhs({ i: '+2!/3 a', s: ['+2! / 3 a', '+2! / (3 * a)'], l: ['\\frac{+2!}{3~ a}', '\\frac{+2!}{3\\cdot a}'] })
|
|
exhs({ i: '2/3! a', s: ['2 / 3! a', '2 / (3! * a)'], l: ['\\frac{2}{3!~ a}', '\\frac{2}{3!\\cdot a}'] })
|
|
exhs({ i: '-2!/+3! a', s: ['-2! / +3! a', '-2! / (+3! * a)'], l: ['\\frac{-2!}{+3!~ a}', '\\frac{-2!}{+3!\\cdot a}'] })
|
|
exhs({ i: '2/-3 a', s: ['2 / -3 a', '2 / (-3 * a)'], l: ['\\frac{2}{-3~ a}', '\\frac{2}{-3\\cdot a}'] })
|
|
exhs({ i: '-(2+3)/3x', s: ['-(2 + 3) / 3 x', '-(2 + 3) / (3 * x)'], l: ['\\frac{-\\left(2+3\\right)}{3~ x}', '\\frac{-\\left(2+3\\right)}{3\\cdot x}'] })
|
|
exhs({ i: '-2/(3+4)x', s: ['-2 / (3 + 4) x', '-2 / ((3 + 4) * x)'], l: ['\\frac{-2}{\\left(3+4\\right)~ x}', '\\frac{-2}{\\left(3+4\\right)\\cdot x}'] })
|
|
exhs({
|
|
i: '(2)/3x',
|
|
skeep: ['(2) / 3 x', '(2) / (3 * x)'],
|
|
sauto: ['2 / (3 x)', '2 / (3 * x)'],
|
|
lkeep: ['\\frac{\\left(2\\right)}{3~ x}', '\\frac{\\left(2\\right)}{3\\cdot x}'],
|
|
lauto: ['\\frac{2}{3~ x}', '\\frac{2}{3\\cdot x}']
|
|
})
|
|
exhs({
|
|
i: '2/(3)x',
|
|
skeep: ['2 / (3) x', '2 / ((3) * x)'],
|
|
sauto: ['2 / (3 x)', '2 / (3 * x)'],
|
|
lkeep: ['\\frac{2}{\\left(3\\right)~ x}', '\\frac{2}{\\left(3\\right)\\cdot x}'],
|
|
lauto: ['\\frac{2}{3~ x}', '\\frac{2}{3\\cdot x}']
|
|
})
|
|
exhs({
|
|
i: '(2)/(3)x',
|
|
skeep: ['(2) / (3) x', '(2) / ((3) * x)'],
|
|
sauto: ['2 / (3 x)', '2 / (3 * x)'],
|
|
lkeep: ['\\frac{\\left(2\\right)}{\\left(3\\right)~ x}', '\\frac{\\left(2\\right)}{\\left(3\\right)\\cdot x}'],
|
|
lauto: ['\\frac{2}{3~ x}', '\\frac{2}{3\\cdot x}']
|
|
})
|
|
exhs({
|
|
i: '(2!)/(3)x',
|
|
skeep: ['(2!) / (3) x', '(2!) / ((3) * x)'],
|
|
sauto: ['2! / 3 x', '2! / (3 * x)'],
|
|
lkeep: ['\\frac{\\left(2!\\right)}{\\left(3\\right)~ x}', '\\frac{\\left(2!\\right)}{\\left(3\\right)\\cdot x}'],
|
|
lauto: ['\\frac{2!}{3~ x}', '\\frac{2!}{3\\cdot x}']
|
|
})
|
|
exhs({
|
|
i: '(2!)/3x',
|
|
skeep: ['(2!) / 3 x', '(2!) / (3 * x)'],
|
|
sauto: ['2! / 3 x', '2! / (3 * x)'],
|
|
lkeep: ['\\frac{\\left(2!\\right)}{3~ x}', '\\frac{\\left(2!\\right)}{3\\cdot x}'],
|
|
lauto: ['\\frac{2!}{3~ x}', '\\frac{2!}{3\\cdot x}']
|
|
})
|
|
})
|
|
|
|
it('toJSON and fromJSON', function () {
|
|
// There is no such thing as an implicit add node, really, but
|
|
// put toJSON really through its paces
|
|
const node = new OperatorNode('+', 'add', [one, two], true)
|
|
|
|
const json = node.toJSON()
|
|
|
|
assert.deepStrictEqual(json, {
|
|
mathjs: 'OperatorNode',
|
|
op: '+',
|
|
fn: 'add',
|
|
args: [one, two],
|
|
implicit: true,
|
|
isPercentage: false
|
|
})
|
|
|
|
const parsed = OperatorNode.fromJSON(json)
|
|
assert.deepStrictEqual(parsed, node)
|
|
})
|
|
|
|
it('should HTML operators', function () {
|
|
assert.strictEqual(math.parse('2 + 3').toHTML(),
|
|
'<span class="math-number">2</span>' +
|
|
'<span class="math-operator math-binary-operator math-explicit-binary-operator">+</span>' +
|
|
'<span class="math-number">3</span>'
|
|
)
|
|
|
|
assert.strictEqual(math.parse('not 5').toHTML(),
|
|
'<span class="math-operator math-unary-operator math-lefthand-unary-operator">not</span>' +
|
|
'<span class="math-number">5</span>'
|
|
)
|
|
|
|
assert.strictEqual(math.parse('5!').toHTML(),
|
|
'<span class="math-number">5</span>' +
|
|
'<span class="math-operator math-unary-operator math-righthand-unary-operator">!</span>'
|
|
)
|
|
|
|
assert.strictEqual(math.parse('5\'').toHTML(),
|
|
'<span class="math-number">5</span>' +
|
|
'<span class="math-operator math-unary-operator math-righthand-unary-operator">'</span>'
|
|
)
|
|
})
|
|
|
|
it('should format implicit multiplications between ConstantNodes with parentheses', function () {
|
|
ex({ i: '(3)x', skeep: '(3) x', sauto: '3 x', lkeep: '\\left(3\\right)~ x', lauto: '3~ x' })
|
|
ex({
|
|
i: '(4)(4)(4)(4)',
|
|
skeep: '(4) (4) (4) (4)',
|
|
sauto: '4 (4) (4) (4)',
|
|
lkeep: '\\left(4\\right)~\\left(4\\right)~\\left(4\\right)~\\left(4\\right)',
|
|
lauto: '4~\\left(4\\right)~\\left(4\\right)~\\left(4\\right)'
|
|
})
|
|
ex({ i: '4b*4(4)', s: '4 b * 4 (4)', l: '4~\\mathrm{b}\\cdot4~\\left(4\\right)' })
|
|
ex({
|
|
i: '(4(4(4)))',
|
|
skeep: '(4 (4 (4)))',
|
|
sauto: '4 (4 (4))',
|
|
lkeep: '\\left(4~\\left(4~\\left(4\\right)\\right)\\right)',
|
|
lauto: '4~\\left(4~\\left(4\\right)\\right)'
|
|
})
|
|
})
|
|
|
|
it('should stringify implicit multiplications recoverably and to preserve their values', function () {
|
|
const m1 = new OperatorNode('-', 'unaryMinus', [one])
|
|
const m2 = new OperatorNode('-', 'unaryMinus', [two])
|
|
const p1 = new OperatorNode('+', 'unaryPlus', [one])
|
|
const p2 = new OperatorNode('+', 'unaryPlus', [two])
|
|
const onetwo = new OperatorNode('/', 'divide', [one, two])
|
|
const m1two = new OperatorNode('/', 'divide', [m1, two])
|
|
const p1two = new OperatorNode('/', 'divide', [p1, two])
|
|
const onem2 = new OperatorNode('/', 'divide', [one, m2])
|
|
const onep2 = new OperatorNode('/', 'divide', [one, p2])
|
|
const onePlus2 = new OperatorNode('+', 'add', [one, two])
|
|
const onePlusm2 = new OperatorNode('+', 'add', [one, m2])
|
|
const onePlus2over2 = new OperatorNode('/', 'divide', [
|
|
new OperatorNode('+', 'add', [one, two]), two])
|
|
const twoOver1plus2 = new OperatorNode('/', 'divide', [
|
|
two, new OperatorNode('+', 'add', [one, two])])
|
|
const avar = new SymbolNode('a')
|
|
const ascope = { a: 2 }
|
|
const cs = [
|
|
onetwo, m1two, p1two, onem2, onep2,
|
|
onePlus2, onePlusm2, onePlus2over2, twoOver1plus2]
|
|
for (const paren of ['auto', 'keep']) {
|
|
for (const coeff of cs) {
|
|
let expr = new math.OperatorNode('*', 'multiply', [coeff, avar], true)
|
|
let estring = expr.toString({ parenthesis: paren, implicit: 'hide' })
|
|
const rexpr = math.parse(estring)
|
|
const rstring = rexpr.toString({ parenthesis: 'all' })
|
|
// Make sure parsing the string version gives back the same grouping as the
|
|
// original:
|
|
assert.strictEqual(rstring, expr.toString({ parenthesis: 'all' }))
|
|
// And make sure that it produces the same value
|
|
assert.strictEqual(rexpr.evaluate(ascope), expr.evaluate(ascope))
|
|
// And make sure that's the same value you get with a constant in the expression
|
|
expr = new math.OperatorNode('*', 'multiply', [coeff, two], true)
|
|
estring = expr.toString({ parenthesis: paren, implicit: 'hide' })
|
|
assert.strictEqual(math.evaluate(estring, {}), expr.evaluate(ascope))
|
|
}
|
|
}
|
|
})
|
|
|
|
it('should HTML implicit multiplications between ConstantNodes with parentheses', function () {
|
|
const z = math.parse('(3)x')
|
|
const a = math.parse('(4)(4)(4)(4)')
|
|
const b = math.parse('4b*4(4)')
|
|
const c = math.parse('(4(4(4)))')
|
|
|
|
assert.strictEqual(z.toHTML({ implicit: 'hide', parenthesis: 'auto' }), '<span class="math-number">3</span><span class="math-operator math-binary-operator math-implicit-binary-operator"></span><span class="math-symbol">x</span>')
|
|
assert.strictEqual(a.toHTML({ implicit: 'hide', parenthesis: 'auto' }), '<span class="math-number">4</span><span class="math-operator math-binary-operator math-implicit-binary-operator"></span><span class="math-parenthesis math-round-parenthesis">(</span><span class="math-number">4</span><span class="math-parenthesis math-round-parenthesis">)</span><span class="math-operator math-binary-operator math-implicit-binary-operator"></span><span class="math-parenthesis math-round-parenthesis">(</span><span class="math-number">4</span><span class="math-parenthesis math-round-parenthesis">)</span><span class="math-operator math-binary-operator math-implicit-binary-operator"></span><span class="math-parenthesis math-round-parenthesis">(</span><span class="math-number">4</span><span class="math-parenthesis math-round-parenthesis">)</span>')
|
|
assert.strictEqual(b.toHTML({ implicit: 'hide', parenthesis: 'auto' }), '<span class="math-number">4</span><span class="math-operator math-binary-operator math-implicit-binary-operator"></span><span class="math-symbol">b</span><span class="math-operator math-binary-operator math-explicit-binary-operator">*</span><span class="math-number">4</span><span class="math-operator math-binary-operator math-implicit-binary-operator"></span><span class="math-parenthesis math-round-parenthesis">(</span><span class="math-number">4</span><span class="math-parenthesis math-round-parenthesis">)</span>')
|
|
assert.strictEqual(c.toHTML({ implicit: 'hide', parenthesis: 'auto' }), '<span class="math-number">4</span><span class="math-operator math-binary-operator math-implicit-binary-operator"></span><span class="math-parenthesis math-round-parenthesis">(</span><span class="math-number">4</span><span class="math-operator math-binary-operator math-implicit-binary-operator"></span><span class="math-parenthesis math-round-parenthesis">(</span><span class="math-number">4</span><span class="math-parenthesis math-round-parenthesis">)</span><span class="math-parenthesis math-round-parenthesis">)</span>')
|
|
})
|
|
})
|