mathjs/test/unit-tests/function/algebra/leafCount.test.js
Glen Whitney 790a5941c3
feat: Add leafCount function (#2411)
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.
2022-02-16 09:31:38 +01:00

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)
})
})