表达式树 #

通过 math.parse(expr) 解析表达式时,math.js 会生成一个表达式树并返回树的根节点。表达式树可用于分析、操作和求值表达式。

示例

const node = math.parse('sqrt(2 + x)')

在这种情况下,表达式 sqrt(2 + x) 被解析为

  FunctionNode    sqrt
                   |
  OperatorNode     +
                  / \
  ConstantNode   2   x   SymbolNode

或者,可以通过手动创建节点来构建此表达式树

const node1 = new math.ConstantNode(2)
const node2 = new math.SymbolNode('x')
const node3 = new math.OperatorNode('+', 'add', [node1, node2])
const node4 = new math.FunctionNode('sqrt', [node3])

生成的根节点为 node4 的表达式树等于通过 math.parse('sqrt(2 + x)') 生成的表达式树。

API #

方法 #

所有节点都具有以下方法

属性 #

每个 Node 都具有以下属性

节点 #

math.js 具有以下类型的节点。所有节点都可以在命名空间 math 中访问。

AccessorNode #

构造

new AccessorNode(object: Node, index: IndexNode)
new AccessorNode(object: Node, index: IndexNode, optionalChaining: boolean)

可以提供一个可选属性 optionalChaining,用于指示访问器是使用可选链 a?.b 还是使用方括号表示法 a?.["b"] 编写的。默认值为 false。如果给定的对象未定义或为 null,则强制求值为 undefined。

属性

示例

const node1 = math.parse('a[3]')

const object = new math.SymbolNode('a')
const constant3 = new math.ConstantNode(3)
const index = new math.IndexNode([constant3])
const node2 = new math.AccessorNode(object, index)

ArrayNode #

构造

new ArrayNode(items: Node[])

属性

示例

const node1 = math.parse('[1, 2, 3]')

const one    = new math.ConstantNode(1)
const two    = new math.ConstantNode(2)
const three  = new math.ConstantNode(3)
const node2  = new math.ArrayNode([one, two, three])

AssignmentNode #

构造

new AssignmentNode(object: SymbolNode, value: Node)
new AssignmentNode(object: SymbolNode | AccessorNode, index: IndexNode, value: Node)

属性

示例

const node1 = math.parse('a = 3')

const object = new math.SymbolNode('a')
const value = new math.ConstantNode(3)
const node2 = new math.AssignmentNode(object, value)

BlockNode #

当解析多行表达式(如 a=2;b=3a=2\nb=3)时,会创建 BlockNode。求值 BlockNode 会返回一个 ResultSet。结果可以通过 ResultSet.entriesResultSet.valueOf() 检索,其中包含一个 Array,其中的元素是可见行的(即不以分号结尾的行)的结果。

构造

block = new BlockNode(Array.<{node: Node} | {node: Node, visible: boolean}>)

属性

示例

const block1 = math.parse('a=1; b=2; c=3')

const a = new math.SymbolNode('a')
const one = new math.ConstantNode(1)
const ass1 = new math.AssignmentNode(a, one)

const b = new math.SymbolNode('b')
const two = new math.ConstantNode(2)
const ass2 = new math.AssignmentNode(b, two)

const c = new math.SymbolNode('c')
const three = new math.ConstantNode(3)
const ass3 = new math.AssignmentNode(c, three)

const block2 = new BlockNode([
  {node: ass1, visible: false},
  {node: ass2, visible: false},
  {node: ass3, visible: true}
])

ConditionalNode #

构造

new ConditionalNode(condition: Node, trueExpr: Node, falseExpr: Node)

属性

示例

const node1 = math.parse('a > 0 ? a : -a')

const a         = new math.SymbolNode('a')
const zero      = new math.ConstantNode(0)
const condition = new math.OperatorNode('>', 'larger', [a, zero])
const trueExpr  = a
const falseExpr = new math.OperatorNode('-', 'unaryMinus', [a])
const node2     = new math.ConditionalNode(condition, trueExpr, falseExpr)

ConstantNode #

构造

new ConstantNode(value: *)

属性

示例

const node1 = math.parse('2.4')

const node2 = new math.ConstantNode(2.4)
const node3 = new math.ConstantNode('foo')

FunctionAssignmentNode #

构造

new FunctionAssignmentNode(name: string, params: string[], expr: Node)

属性

示例

const node1 = math.parse('f(x) = x^2')

const x      = new math.SymbolNode('x')
const two    = new math.ConstantNode(2)
const expr   = new math.OperatorNode('^', 'pow', [x, 2])
const node2  = new math.FunctionAssignmentNode('f', ['x'], expr)

FunctionNode #

构造

new FunctionNode(fn: Node | string, args: Node[])

属性

静态函数

示例

const node1 = math.parse('sqrt(4)')

const four  = new math.ConstantNode(4)
const node3 = new math.FunctionNode(new SymbolNode('sqrt'), [four])

IndexNode #

构造

new IndexNode(dimensions: Node[])
new IndexNode(dimensions: Node[], dotNotation: boolean)

每个维度可以是一个单一值、一个范围或一个属性。索引的值从一开始,包括范围的结束。

可以提供一个可选属性 dotNotation,用于描述索引是使用点表示法(如 a.b)还是使用方括号表示法(如 a["b"])编写的。默认值为 false。此信息用于字符串化 IndexNode。

属性

示例

const node1 = math.parse('A[1:3, 2]')

const A     = new math.SymbolNode('A')
const one   = new math.ConstantNode(1)
const two   = new math.ConstantNode(2)
const three = new math.ConstantNode(3)

const range = new math.RangeNode(one, three)
const index = new math.IndexNode([range, two])
const node2 = new math.AccessorNode(A, index)

ObjectNode #

构造

new ObjectNode(properties: Object.<string, Node>)

属性

示例

const node1 = math.parse('{a: 1, b: 2, c: 3}')

const a = new math.ConstantNode(1)
const b = new math.ConstantNode(2)
const c = new math.ConstantNode(3)
const node2 = new math.ObjectNode({a: a, b: b, c: c})

OperatorNode #

构造

new OperatorNode(op: string, fn: string, args: Node[], implicit: boolean = false)

附加方法

属性

示例

const node1 = math.parse('2.3 + 5')

const a     = new math.ConstantNode(2.3)
const b     = new math.ConstantNode(5)
const node2 = new math.OperatorNode('+', 'add', [a, b])

ParenthesisNode #

构造

new ParenthesisNode(content: Node)

属性

示例

const node1 = math.parse('(1)')

const a     = new math.ConstantNode(1)
const node2 = new math.ParenthesisNode(a)

RangeNode #

构造

new RangeNode(start: Node, end: Node [, step: Node])

属性

示例

const node1 = math.parse('1:10')
const node2 = math.parse('0:2:10')

const zero = new math.ConstantNode(0)
const one = new math.ConstantNode(1)
const two = new math.ConstantNode(2)
const ten = new math.ConstantNode(10)

const node3 = new math.RangeNode(one, ten)
const node4 = new math.RangeNode(zero, ten, two)

RelationalNode #

构造

new RelationalNode(conditionals: string[], params: Node[])

conditionals 是一个字符串数组,每个字符串可以是 'smaller'、'larger'、'smallerEq'、'largerEq'、'equal' 或 'unequal'。 conditionals 数组必须比 params 少一个元素。

属性

一个 RelationalNode 高效地表示带有两个或多个比较运算符的链式条件表达式,例如 10 < x <= 50。该表达式等同于 10 < x and x <= 50,不同之处在于 x 只计算一次,并且一旦任何条件测试为 false,计算就会停止(“短路”)。可以进行链式比较的运算符是 <><=>===!=。为了向后兼容,如果只存在单个条件(例如 x > 2),math.parse 将返回一个 OperatorNode

示例


const ten = new math.ConstantNode(10)
const x = new math.SymbolNode('x')
const fifty = new math.ConstantNode(50)

const node1 = new math.RelationalNode(['smaller', 'smallerEq'], [ten, x, fifty])
const node2 = math.parse('10 < x <= 50')

SymbolNode #

构造

new SymbolNode(name: string)

属性

静态函数

示例

const node = math.parse('x')

const x = new math.SymbolNode('x')
Fork me on GitHub