UNPKG

3.76 kBMarkdownView Raw
1# Algebra (symbolic computation)
2
3math.js has built-in support for symbolic computation ([CAS](https://www.wikiwand.com/en/Computer_algebra_system)). It can parse expressions into an expression tree and do algebraic operations like simplification and derivation on the tree.
4
5> It's worth mentioning an excellent extension on math.js here: [mathsteps](https://github.com/socraticorg/mathsteps), a step-by-step math solver library that is focused on pedagogy (how best to teach). The math problems it focuses on are pre-algebra and algebra problems involving simplifying expressions.
6
7
8## Simplify
9
10The function [`math.simplify`](../reference/functions/simplify.md) simplifies an expression tree:
11
12```js
13// simplify an expression
14console.log(math.simplify('3 + 2 / 4').toString()) // '7 / 2'
15console.log(math.simplify('2x + 3x').toString()) // '5 * x'
16console.log(math.simplify('x^2 + x + 3 + x^2').toString()) // '2 * x ^ 2 + x + 3'
17console.log(math.simplify('x * y * -x / (x ^ 2)').toString()) // '-y'
18```
19
20The function accepts either a string or an expression tree (`Node`) as input, and outputs a simplified expression tree (`Node`). This node tree can be transformed and evaluated as described in detail on the page [Expression trees](expression_trees.md).
21
22```js
23// work with an expression tree, evaluate results
24const f = math.parse('2x + x')
25const simplified = math.simplify(f)
26console.log(simplified.toString()) // '3 * x'
27console.log(simplified.evaluate({x: 4})) // 12
28```
29
30For more details on the theory of expression simplification, see:
31
32- [Strategies for simplifying math expressions (Stackoverflow)](https://stackoverflow.com/questions/7540227/strategies-for-simplifying-math-expressions)
33- [Symbolic computation - Simplification (Wikipedia)](https://en.wikipedia.org/wiki/Symbolic_computation#Simplification)
34
35
36## Derivative
37
38The function [`math.derivative`](../reference/functions/derivative.md) finds the symbolic derivative of an expression:
39
40```js
41// calculate a derivative
42console.log(math.derivative('2x^2 + 3x + 4', 'x').toString()) // '4 * x + 3'
43console.log(math.derivative('sin(2x)', 'x').toString()) // '2 * cos(2 * x)'
44```
45
46Similar to the function `math.simplify`, `math.derivative` accepts either a string or an expression tree (`Node`) as input, and outputs a simplified expression tree (`Node`).
47
48```js
49// work with an expression tree, evaluate results
50const h = math.parse('x^2 + x')
51const x = math.parse('x')
52const dh = math.derivative(h, x)
53console.log(dh.toString()) // '2 * x + 1'
54console.log(dh.evaluate({x: 3})) // '7'
55```
56
57The rules used by `math.derivative` can be found on Wikipedia:
58
59- [Differentiation rules (Wikipedia)](https://en.wikipedia.org/wiki/Differentiation_rules)
60
61
62## Rationalize
63
64The function [`math.transform`](../reference/functions/transform.md) transforms a rationalizable expression in a rational fraction.
65If rational fraction is one variable polynomial then converts the numerator and denominator in canonical form, with decreasing exponents, returning the coefficients of numerator.
66
67```js
68
69math.rationalize('2x/y - y/(x+1)')
70 // (2*x^2-y^2+2*x)/(x*y+y)
71math.rationalize('(2x+1)^6')
72 // 64*x^6+192*x^5+240*x^4+160*x^3+60*x^2+12*x+1
73math.rationalize('2x/( (2x-1) / (3x+2) ) - 5x/ ( (3x+4) / (2x^2-5) ) + 3')
74 // -20*x^4+28*x^3+104*x^2+6*x-12)/(6*x^2+5*x-4)
75
76math.rationalize('x+x+x+y',{y:1}) // 3*x+1
77math.rationalize('x+x+x+y',{}) // 3*x+y
78
79const ret = math.rationalize('x+x+x+y',{},true)
80 // ret.expression=3*x+y, ret.variables = ["x","y"]
81const ret = math.rationalize('-2+5x^2',{},true)
82 // ret.expression=5*x^2-2, ret.variables = ["x"], ret.coefficients=[-2,0,5]
83```
\No newline at end of file