Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add leafCount function #2411

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/expression/embeddedDocs/embeddedDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ import { qrDocs } from './function/algebra/qr.js'
import { usolveDocs } from './function/algebra/usolve.js'
import { usolveAllDocs } from './function/algebra/usolveAll.js'
import { sluDocs } from './function/algebra/slu.js'
import { leafCountDocs } from './function/algebra/leafCount.js'
import { rationalizeDocs } from './function/algebra/rationalize.js'
import { simplifyDocs } from './function/algebra/simplify.js'
import { lupDocs } from './function/algebra/lup.js'
Expand Down Expand Up @@ -326,6 +327,7 @@ export const embeddedDocs = {
lsolveAll: lsolveAllDocs,
lup: lupDocs,
lusolve: lusolveDocs,
leafCount: leafCountDocs,
simplify: simplifyDocs,
rationalize: rationalizeDocs,
slu: sluDocs,
Expand Down
11 changes: 11 additions & 0 deletions src/expression/embeddedDocs/function/algebra/leafCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const leafCountDocs = {
name: 'leafCount',
category: 'Algebra',
syntax: ['leafCount(expr)'],
description: 'Computes the number of leaves in the parse tree of the given expression',
examples: [
'leafCount("e^(i*pi)-1")',
'leafCount(parse("{a: 22/7, b: 10^(1/2)}"))'
],
seealso: ['simplify']
}
1 change: 1 addition & 0 deletions src/factoriesAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ export { createStirlingS2 } from './function/combinatorics/stirlingS2.js'
export { createBellNumbers } from './function/combinatorics/bellNumbers.js'
export { createCatalan } from './function/combinatorics/catalan.js'
export { createComposition } from './function/combinatorics/composition.js'
export { createLeafCount } from './function/algebra/leafCount.js'
export { createSimplify } from './function/algebra/simplify.js'
export { createDerivative } from './function/algebra/derivative.js'
export { createRationalize } from './function/algebra/rationalize.js'
Expand Down
59 changes: 59 additions & 0 deletions src/function/algebra/leafCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { factory } from '../../utils/factory.js'

const name = 'leafCount'
const dependencies = [
'parse',
'typed'
]

export const createLeafCount = /* #__PURE__ */ factory(name, dependencies, ({
parse,
typed
}) => {
// This does the real work, but we don't have to recurse through
// a typed call if we separate it out
function countLeaves (node) {
let count = 0
node.forEach(n => { count += countLeaves(n) })
return count || 1
}

/**
* Gives the number of "leaf nodes" in the parse tree of the given expression
* A leaf node is one that has no subexpressions, essentially either a
* symbol or a constant. Note that `5!` has just one leaf, the '5'; the
* unary factorial operator does not add a leaf. On the other hand,
* function symbols do add leaves, so `sin(x)/cos(x)` has four leaves.
*
* The `simplify()` function should generally not increase the `leafCount()`
* of an expression, although currently there is no guarantee that it never
* does so. In many cases, `simplify()` reduces the leaf count.
*
* Syntax:
*
* leafCount(expr)
*
* Examples:
*
* math.leafCount('x') // 1
* math.leafCount(math.parse('a*d-b*c')) // 4
* math.leafCount('[a,b;c,d][0,1]') // 6
*
* See also:
*
* simplify
*
* @param {Node|string} expr The expression to count the leaves of
*
* @return {number} The number of leaves of `expr`
*
*/
return typed(name, {
string: function (expr) {
return this(parse(expr))
},
Node: function (expr) {
return countLeaves(expr)
}
})
})
20 changes: 20 additions & 0 deletions test/unit-tests/function/algebra/leafCount.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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)
})
})