mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
This function returns the number of leaves in the parse tree of an expression. The motivation is to provide an initial complexity measure that could be used to decide whether or not to apply some simplification rules. Docs, embedded docs, and test cases are provided. Resolves #2389.
21 lines
724 B
JavaScript
21 lines
724 B
JavaScript
import assert from 'assert'
|
|
import math from '../../../../src/defaultInstance.js'
|
|
|
|
describe('leafCount', function () {
|
|
it('handles nodes', function () {
|
|
assert.strictEqual(math.leafCount(new math.SymbolNode('x')), 1)
|
|
assert.strictEqual(math.leafCount(math.parse('2+y')), 2)
|
|
assert.strictEqual(math.leafCount(math.parse('[3,a+5,2/2,z]')), 6)
|
|
})
|
|
|
|
it('handles strings', function () {
|
|
assert.strictEqual(math.leafCount('3x^2-7x+2'), 6)
|
|
assert.strictEqual(math.leafCount('13 < m+n < abs(n^2-m^2)'), 8)
|
|
})
|
|
|
|
it('can be used in an expression', function () {
|
|
assert(math.evaluate('leafCount("identity(2)[0,1]") == 4'))
|
|
assert.strictEqual(math.evaluate('leafCount("{a: 7, b: x}.b")'), 3)
|
|
})
|
|
})
|