mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-16 11:02:11 +08:00
Add typings to mathjs MathNode class
Furthermore removed all trailing whitespace
This commit is contained in:
@@ -11,20 +11,20 @@ Basic usage examples
|
||||
math.log(10000, 10); // 4
|
||||
math.sqrt(-4); // 2i
|
||||
math.pow([[-1, 2], [3, 1]], 2); // [[7, 0], [0, 7]]
|
||||
|
||||
|
||||
// expressions
|
||||
math.eval('1.2 * (2 + 4.5)'); // 7.8
|
||||
math.eval('5.08 cm to inch'); // 2 inch
|
||||
math.eval('sin(45 deg) ^ 2'); // 0.5
|
||||
math.eval('9 / 3 + 2i'); // 3 + 2i
|
||||
math.eval('det([-1, 2; 3, 1])'); // -7
|
||||
|
||||
|
||||
// chained operations
|
||||
var a = math.chain(3)
|
||||
.add(4)
|
||||
.multiply(2)
|
||||
.done(); // 14
|
||||
|
||||
|
||||
// mixed use of different data types in functions
|
||||
console.log('mixed use of data types');
|
||||
math.add(4, [5, 6]); // number + Array, [9, 10]
|
||||
@@ -44,22 +44,22 @@ Bignumbers examples
|
||||
// 'number' (default), 'bignumber', or 'fraction'
|
||||
precision: 20 // Number of significant digits for BigNumbers
|
||||
});
|
||||
|
||||
|
||||
console.log('round-off errors with numbers');
|
||||
math.add(0.1, 0.2); // number, 0.30000000000000004
|
||||
math.divide(0.3, 0.2); // number, 1.4999999999999998
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('no round-off errors with BigNumbers');
|
||||
math.add(math.bignumber(0.1), math.bignumber(0.2)); // BigNumber, 0.3
|
||||
math.divide(math.bignumber(0.3), math.bignumber(0.2)); // BigNumber, 1.5
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('create BigNumbers from strings when exceeding the range of a number');
|
||||
math.bignumber(1.2e+500); // BigNumber, Infinity WRONG
|
||||
math.bignumber('1.2e+500'); // BigNumber, 1.2e+500
|
||||
console.log();
|
||||
|
||||
|
||||
// one can work conveniently with BigNumbers using the expression parser.
|
||||
// note though that BigNumbers are only supported in arithmetic functions
|
||||
console.log('use BigNumbers in the expression parser');
|
||||
@@ -80,33 +80,33 @@ Chaining examples
|
||||
.add(4)
|
||||
.multiply(2)
|
||||
.done(); // 14
|
||||
|
||||
|
||||
// Another example, calculate square(sin(pi / 4))
|
||||
var b = math.chain(math.pi)
|
||||
.divide(4)
|
||||
.sin()
|
||||
.square()
|
||||
.done(); // 0.5
|
||||
|
||||
|
||||
// A chain has a few special methods: done, toString, valueOf, get, and set.
|
||||
// these are demonstrated in the following examples
|
||||
|
||||
|
||||
// toString will return a string representation of the chain's value
|
||||
var chain = math.chain(2).divide(3);
|
||||
var str = chain.toString(); // "0.6666666666666666"
|
||||
|
||||
|
||||
// a chain has a function .valueOf(), which returns the value hold by the chain.
|
||||
// This allows using it in regular operations. The function valueOf() acts the
|
||||
// same as function done().
|
||||
chain.valueOf(); // 0.66666666666667
|
||||
|
||||
|
||||
|
||||
|
||||
// the function subset can be used to get or replace sub matrices
|
||||
var array = [[1, 2], [3, 4]];
|
||||
var v = math.chain(array)
|
||||
.subset(math.index(1, 0))
|
||||
.done(); // 3
|
||||
|
||||
|
||||
var m = math.chain(array)
|
||||
.subset(math.index(0, 0), 8)
|
||||
.multiply(3)
|
||||
@@ -119,38 +119,38 @@ Complex numbers examples
|
||||
*/
|
||||
(function(){
|
||||
var a = math.complex(2, 3); // 2 + 3i
|
||||
|
||||
|
||||
// read the real and complex parts of the complex number
|
||||
a.re; // 2
|
||||
a.im; // 3
|
||||
|
||||
|
||||
// clone a complex value
|
||||
var clone = a.clone(); // 2 + 3i
|
||||
|
||||
|
||||
// adjust the complex value
|
||||
a.re = 5; // 5 + 3i
|
||||
|
||||
|
||||
// create a complex number by providing a string with real and complex parts
|
||||
var b = math.complex('3 - 7i'); // 3 - 7i
|
||||
console.log();
|
||||
|
||||
|
||||
// perform operations with complex numbers
|
||||
console.log('perform operations');
|
||||
math.add(a, b); // 8 - 4i
|
||||
math.multiply(a, b); // 36 - 26i
|
||||
math.sin(a); // -9.6541254768548 + 2.8416922956064i
|
||||
|
||||
|
||||
// some operations will return a complex number depending on the arguments
|
||||
math.sqrt(4); // 2
|
||||
math.sqrt(-4); // 2i
|
||||
|
||||
|
||||
// create a complex number from polar coordinates
|
||||
console.log('create complex numbers with polar coordinates');
|
||||
var c = math.complex({r: math.sqrt(2), phi: math.pi / 4}); // 1 + i
|
||||
|
||||
|
||||
// get polar coordinates of a complex number
|
||||
var d = math.complex(3, 4);
|
||||
d.toPolar(); // { r: 5, phi: 0.9272952180016122 }
|
||||
d.toPolar(); // { r: 5, phi: 0.9272952180016122 }
|
||||
}());
|
||||
|
||||
|
||||
@@ -166,14 +166,14 @@ Expressions examples
|
||||
// JavaScript Object. The scope will be used to resolve symbols, and to write
|
||||
// assigned variables or function.
|
||||
console.log('1. USING FUNCTION MATH.EVAL');
|
||||
|
||||
|
||||
// evaluate expressions
|
||||
console.log('\nevaluate expressions');
|
||||
math.eval('sqrt(3^2 + 4^2)'); // 5
|
||||
math.eval('sqrt(-4)'); // 2i
|
||||
math.eval('2 inch to cm'); // 5.08 cm
|
||||
math.eval('cos(45 deg)'); // 0.70711
|
||||
|
||||
|
||||
// evaluate multiple expressions at once
|
||||
console.log('\nevaluate multiple expressions at once');
|
||||
math.eval([
|
||||
@@ -181,34 +181,34 @@ Expressions examples
|
||||
'g = 4',
|
||||
'f * g'
|
||||
]); // [3, 4, 12]
|
||||
|
||||
|
||||
// provide a scope (just a regular JavaScript Object)
|
||||
console.log('\nevaluate expressions providing a scope with variables and functions');
|
||||
var scope: any = {
|
||||
a: 3,
|
||||
b: 4
|
||||
};
|
||||
|
||||
|
||||
// variables can be read from the scope
|
||||
math.eval('a * b', scope); // 12
|
||||
|
||||
|
||||
// variable assignments are written to the scope
|
||||
math.eval('c = 2.3 + 4.5', scope); // 6.8
|
||||
scope.c; // 6.8
|
||||
|
||||
|
||||
// scope can contain both variables and functions
|
||||
scope["hello"] = function (name: string) {
|
||||
return 'hello, ' + name + '!';
|
||||
};
|
||||
math.eval('hello("hero")', scope); // "hello, hero!"
|
||||
|
||||
|
||||
// define a function as an expression
|
||||
var f = math.eval('f(x) = x ^ a', scope);
|
||||
f(2); // 8
|
||||
scope.f(2); // 8
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 2. using function math.parse
|
||||
//
|
||||
// Function `math.parse` parses expressions into a node tree. The syntax is
|
||||
@@ -219,16 +219,16 @@ Expressions examples
|
||||
// scope. This scope is a regular JavaScript Object. The scope will be used
|
||||
// to resolve symbols, and to write assigned variables or function.
|
||||
console.log('\n2. USING FUNCTION MATH.PARSE');
|
||||
|
||||
|
||||
// parse an expression
|
||||
console.log('\nparse an expression into a node tree');
|
||||
var node1 = math.parse('sqrt(3^2 + 4^2)');
|
||||
node1.toString(); // "sqrt((3 ^ 2) + (4 ^ 2))"
|
||||
|
||||
|
||||
// compile and evaluate the compiled code
|
||||
// you could also do this in two steps: node1.compile().eval()
|
||||
node1.eval(); // 5
|
||||
|
||||
|
||||
// provide a scope
|
||||
console.log('\nprovide a scope');
|
||||
var node2 = math.parse('x^a');
|
||||
@@ -239,12 +239,12 @@ Expressions examples
|
||||
a: 2
|
||||
};
|
||||
code2.eval(scope); // 9
|
||||
|
||||
|
||||
// change a value in the scope and re-evaluate the node
|
||||
scope.a = 3;
|
||||
code2.eval(scope); // 27
|
||||
|
||||
|
||||
|
||||
|
||||
// 3. using function math.compile
|
||||
//
|
||||
// Function `math.compile` compiles expressions into a node tree. The syntax is
|
||||
@@ -255,14 +255,14 @@ Expressions examples
|
||||
// be provided. This scope will be used to resolve symbols, and to write
|
||||
// assigned variables or function.
|
||||
console.log('\n3. USING FUNCTION MATH.COMPILE');
|
||||
|
||||
|
||||
// parse an expression
|
||||
console.log('\ncompile an expression');
|
||||
var code3 = math.compile('sqrt(3^2 + 4^2)');
|
||||
|
||||
|
||||
// evaluate the compiled code
|
||||
code3.eval(); // 5
|
||||
|
||||
|
||||
// provide a scope for the variable assignment
|
||||
console.log('\nprovide a scope');
|
||||
var code2 = math.compile('a = a + 3');
|
||||
@@ -271,8 +271,8 @@ Expressions examples
|
||||
};
|
||||
code2.eval(scope);
|
||||
scope.a; // 10
|
||||
|
||||
|
||||
|
||||
|
||||
// 4. using a parser
|
||||
//
|
||||
// In addition to the static functions `math.eval` and `math.parse`, math.js
|
||||
@@ -281,21 +281,21 @@ Expressions examples
|
||||
// some convenience methods to get, set, and remove variables from memory.
|
||||
console.log('\n4. USING A PARSER');
|
||||
var parser = math.parser();
|
||||
|
||||
|
||||
// evaluate with parser
|
||||
console.log('\nevaluate expressions');
|
||||
parser.eval('sqrt(3^2 + 4^2)'); // 5
|
||||
parser.eval('sqrt(-4)'); // 2i
|
||||
parser.eval('2 inch to cm'); // 5.08 cm
|
||||
parser.eval('cos(45 deg)'); // 0.70711
|
||||
|
||||
|
||||
// define variables and functions
|
||||
console.log('\ndefine variables and functions');
|
||||
parser.eval('x = 7 / 2'); // 3.5
|
||||
parser.eval('x + 3'); // 6.5
|
||||
parser.eval('f(x, y) = x^y'); // f(x, y)
|
||||
parser.eval('f(2, 3)'); // 8
|
||||
|
||||
|
||||
// manipulate matrices
|
||||
// Note that matrix indexes in the expression parser are one-based with the
|
||||
// upper-bound included. On a JavaScript level however, math.js uses zero-based
|
||||
@@ -308,7 +308,7 @@ Expressions examples
|
||||
parser.eval('m = k * l'); // [[19, 22], [43, 50]]
|
||||
parser.eval('n = m[2, 1]'); // 43
|
||||
parser.eval('n = m[:, 1]'); // [[19], [43]]
|
||||
|
||||
|
||||
// get and set variables and functions
|
||||
console.log('\nget and set variables and function in the scope of the parser');
|
||||
var x = parser.get('x');
|
||||
@@ -317,14 +317,14 @@ Expressions examples
|
||||
console.log('f =', math.format(f)); // f = f(x, y)
|
||||
var g = f(3, 3);
|
||||
console.log('g =', g); // g = 27
|
||||
|
||||
|
||||
parser.set('h', 500);
|
||||
parser.eval('h / 2'); // 250
|
||||
parser.set('hello', function (name: string) {
|
||||
return 'hello, ' + name + '!';
|
||||
});
|
||||
parser.eval('hello("hero")'); // "hello, hero!"
|
||||
|
||||
|
||||
// clear defined functions and variables
|
||||
parser.clear();
|
||||
}());
|
||||
@@ -339,7 +339,7 @@ Fractions examples
|
||||
number: 'fraction' // Default type of number:
|
||||
// 'number' (default), 'bignumber', or 'fraction'
|
||||
});
|
||||
|
||||
|
||||
console.log('basic usage');
|
||||
math.fraction(0.125); // Fraction, 1/8
|
||||
math.fraction(0.32); // Fraction, 8/25
|
||||
@@ -348,23 +348,23 @@ Fractions examples
|
||||
math.fraction(2, 3); // Fraction, 2/3
|
||||
math.fraction('0.(285714)'); // Fraction, 2/7
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('round-off errors with numbers');
|
||||
math.add(0.1, 0.2); // number, 0.30000000000000004
|
||||
math.divide(0.3, 0.2); // number, 1.4999999999999998
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('no round-off errors with fractions :)');
|
||||
math.add(math.fraction(0.1), math.fraction(0.2)); // Fraction, 3/10
|
||||
math.divide(math.fraction(0.3), math.fraction(0.2)); // Fraction, 3/2
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('represent an infinite number of repeating digits');
|
||||
math.fraction('1/3'); // Fraction, 0.(3)
|
||||
math.fraction('2/7'); // Fraction, 0.(285714)
|
||||
math.fraction('23/11'); // Fraction, 2.(09)
|
||||
console.log();
|
||||
|
||||
|
||||
// one can work conveniently with fractions using the expression parser.
|
||||
// note though that Fractions are only supported by basic arithmetic functions
|
||||
console.log('use fractions in the expression parser');
|
||||
@@ -372,7 +372,7 @@ Fractions examples
|
||||
math.eval('0.3 / 0.2'); // Fraction, 3/2
|
||||
math.eval('23 / 11'); // Fraction, 23/11
|
||||
console.log();
|
||||
|
||||
|
||||
// output formatting
|
||||
console.log('output formatting of fractions');
|
||||
var a = math.fraction('2/3');
|
||||
@@ -380,7 +380,7 @@ Fractions examples
|
||||
console.log(math.format(a, {fraction: 'ratio'})); // Fraction, 2/3
|
||||
console.log(math.format(a, {fraction: 'decimal'})); // Fraction, 0.(6)
|
||||
console.log(a.toString()); // Fraction, 0.(6)
|
||||
console.log();
|
||||
console.log();
|
||||
}());
|
||||
|
||||
/*
|
||||
@@ -393,33 +393,33 @@ Matrices examples
|
||||
var a = math.matrix([1, 4, 9, 16, 25]); // [1, 4, 9, 16, 25]
|
||||
var b = math.matrix(math.ones([2, 3])); // [[1, 1, 1], [1, 1, 1]]
|
||||
b.size(); // [2, 3]
|
||||
|
||||
|
||||
// the Array data of a Matrix can be retrieved using valueOf()
|
||||
var array = a.valueOf(); // [1, 4, 9, 16, 25]
|
||||
|
||||
|
||||
// Matrices can be cloned
|
||||
var clone = a.clone(); // [1, 4, 9, 16, 25]
|
||||
console.log();
|
||||
|
||||
|
||||
// perform operations with matrices
|
||||
console.log('perform operations');
|
||||
math.sqrt(a); // [1, 2, 3, 4, 5]
|
||||
var c = [1, 2, 3, 4, 5];
|
||||
math.factorial(c); // [1, 2, 6, 24, 120]
|
||||
console.log();
|
||||
|
||||
|
||||
// create and manipulate matrices. Arrays and Matrices can be used mixed.
|
||||
console.log('manipulate matrices');
|
||||
var d = [[1, 2], [3, 4]]; // [[1, 2], [3, 4]]
|
||||
var e = math.matrix([[5, 6], [1, 1]]); // [[5, 6], [1, 1]]
|
||||
|
||||
|
||||
// set a submatrix.
|
||||
// Matrix indexes are zero-based.
|
||||
e.subset(math.index(1, [0, 1]), [[7, 8]]); // [[5, 6], [7, 8]]
|
||||
var f = math.multiply(d, e); // [[19, 22], [43, 50]]
|
||||
var g = f.subset(math.index(1, 0)); // 43
|
||||
console.log();
|
||||
|
||||
|
||||
// get a sub matrix
|
||||
// Matrix indexes are zero-based.
|
||||
console.log('get a sub matrix');
|
||||
@@ -428,8 +428,8 @@ Matrices examples
|
||||
var i = math.range(1,6); // [1, 2, 3, 4, 5]
|
||||
i.subset(math.index(math.range(1,4))); // [2, 3, 4]
|
||||
console.log();
|
||||
|
||||
|
||||
|
||||
|
||||
// resize a multi dimensional matrix
|
||||
console.log('resizing a matrix');
|
||||
var j = math.matrix();
|
||||
@@ -439,20 +439,20 @@ Matrices examples
|
||||
j.resize([2, 2]); // [[0, 0], [0, 0]]
|
||||
j.size(); // [2, 2]
|
||||
console.log();
|
||||
|
||||
|
||||
// setting a value outside the matrices range will resize the matrix.
|
||||
// new elements will be initialized with zero.
|
||||
console.log('set a value outside a matrices range');
|
||||
var k = math.matrix();
|
||||
k.subset(math.index(2), 6); // [0, 0, 6]
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('set a value outside a matrices range, leaving new entries uninitialized');
|
||||
var m = math.matrix();
|
||||
defaultValue = math.uninitialized;
|
||||
m.subset(math.index(2), 6, defaultValue); // [undefined, undefined, 6]
|
||||
console.log();
|
||||
|
||||
|
||||
// create ranges
|
||||
console.log('create ranges');
|
||||
math.range(1, 6); // [1, 2, 3, 4, 5]
|
||||
@@ -469,14 +469,14 @@ Sparse matrices examples
|
||||
// create a sparse matrix
|
||||
console.log('creating a 1000x1000 sparse matrix...');
|
||||
var a = math.eye(1000, 1000, 'sparse');
|
||||
|
||||
|
||||
// do operations with a sparse matrix
|
||||
console.log('doing some operations on the sparse matrix...');
|
||||
var b = math.multiply(a, a);
|
||||
var c = math.multiply(b, math.complex(2, 2));
|
||||
var d = math.transpose(c);
|
||||
var e = math.multiply(d, a);
|
||||
|
||||
|
||||
// we will not print the output, but doing the same operations
|
||||
// with a dense matrix are very slow, try it for yourself.
|
||||
console.log('already done');
|
||||
@@ -493,7 +493,7 @@ Units examples
|
||||
var a = math.unit(45, 'cm'); // 450 mm
|
||||
var b = math.unit('0.1m'); // 100 mm
|
||||
console.log();
|
||||
|
||||
|
||||
// units can be added, subtracted, and multiplied or divided by numbers and by other units
|
||||
console.log('perform operations');
|
||||
math.add(a, b); // 0.55 m
|
||||
@@ -501,7 +501,7 @@ Units examples
|
||||
math.divide(math.unit('1 m'), math.unit('1 s')); // 1 m / s
|
||||
math.pow(math.unit('12 in'), 3); // 1728 in^3
|
||||
console.log();
|
||||
|
||||
|
||||
// units can be converted to a specific type, or to a number
|
||||
console.log('convert to another type or to a number');
|
||||
b.to('cm'); // 10 cm Alternatively: math.to(b, 'cm')
|
||||
@@ -509,25 +509,25 @@ Units examples
|
||||
b.toNumber('cm'); // 10
|
||||
math.number(b, 'cm'); // 10
|
||||
console.log();
|
||||
|
||||
|
||||
// the expression parser supports units too
|
||||
console.log('parse expressions');
|
||||
math.eval('2 inch to cm'); // 5.08 cm
|
||||
math.eval('cos(45 deg)'); // 0.70711...
|
||||
math.eval('90 km/h to m/s'); // 25 m / s
|
||||
console.log();
|
||||
|
||||
|
||||
// convert a unit to a number
|
||||
// A second parameter with the unit for the exported number must be provided
|
||||
math.eval('number(5 cm, mm)'); // number, 50
|
||||
console.log();
|
||||
|
||||
|
||||
// simplify units
|
||||
console.log('simplify units');
|
||||
math.eval('100000 N / m^2'); // 100 kPa
|
||||
math.eval('9.81 m/s^2 * 100 kg * 40 m'); // 39.24 kJ
|
||||
console.log();
|
||||
|
||||
|
||||
// example engineering calculations
|
||||
console.log('compute molar volume of ideal gas at 65 Fahrenheit, 14.7 psi in L/mol');
|
||||
var Rg = math.unit('8.314 N m / (mol K)');
|
||||
@@ -539,7 +539,7 @@ Units examples
|
||||
console.log('T = ' + format(T));
|
||||
console.log('v = Rg * T / P = ' + format(math.to(v, 'L/mol'))); // 23.910... L / mol
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('compute speed of fluid flowing out of hole in a container');
|
||||
var g = math.unit('9.81 m / s^2');
|
||||
var h = math.unit('1 m');
|
||||
@@ -548,17 +548,17 @@ Units examples
|
||||
console.log('h = ' + format(h));
|
||||
console.log('v = (2 g h) ^ 0.5 = ' + format(v2)); // 4.429... m / s
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('electrical power consumption:');
|
||||
var expr = '460 V * 20 A * 30 days to kWh';
|
||||
console.log(expr + ' = ' + math.eval(expr)); // 6624 kWh
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('circuit design:');
|
||||
var expr = '24 V / (6 mA)';
|
||||
console.log(expr + ' = ' + math.eval(expr)); // 4 kohm
|
||||
console.log();
|
||||
|
||||
|
||||
console.log('operations on arrays:');
|
||||
var B = math.eval('[1, 0, 0] T');
|
||||
var v3 = math.eval('[0, 1, 0] m/s');
|
||||
@@ -568,7 +568,7 @@ Units examples
|
||||
console.log('v (particle velocity) = ' + format(v3)); // [0 m / s, 1 m / s, 0 m / s]
|
||||
console.log('q (particle charge) = ' + format(q)); // 1 C
|
||||
console.log('F (force) = q (v cross B) = ' + format(F)); // [0 N, 0 N, -1 N]
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to format an output a value.
|
||||
* @param {*} value
|
||||
@@ -578,4 +578,62 @@ Units examples
|
||||
var precision = 14;
|
||||
return math.format(value, precision);
|
||||
}
|
||||
}());
|
||||
}());
|
||||
|
||||
|
||||
/*
|
||||
Expression tree examples
|
||||
*/
|
||||
(function(){
|
||||
|
||||
// Filter an expression tree
|
||||
console.log('Filter all symbol nodes "x" in the expression "x^2 + x/4 + 3*y"');
|
||||
var node = math.parse('x^2 + x/4 + 3*y');
|
||||
var filtered = node.filter(function (node) {
|
||||
return node.isSymbolNode && node.name == 'x';
|
||||
});
|
||||
// returns an array with two entries: two SymbolNodes 'x'
|
||||
|
||||
filtered.forEach(function (node) {
|
||||
console.log(node.type, node.toString())
|
||||
});
|
||||
// outputs:
|
||||
// SymbolNode x
|
||||
// SymbolNode x
|
||||
|
||||
|
||||
// Traverse an expression tree
|
||||
console.log();
|
||||
console.log('Traverse the expression tree of expression "3 * x + 2"');
|
||||
var node1 = math.parse('3 * x + 2');
|
||||
node1.traverse(function (node, path, parent) {
|
||||
switch (node.type) {
|
||||
case 'OperatorNode': console.log(node.type, node.op); break;
|
||||
case 'ConstantNode': console.log(node.type, node.value); break;
|
||||
case 'SymbolNode': console.log(node.type, node.name); break;
|
||||
default: console.log(node.type);
|
||||
}
|
||||
});
|
||||
// outputs:
|
||||
// OperatorNode +
|
||||
// OperatorNode *
|
||||
// ConstantNode 3
|
||||
// SymbolNode x
|
||||
// ConstantNode 2
|
||||
|
||||
|
||||
// transform an expression tree
|
||||
console.log();
|
||||
console.log('Replace all symbol nodes "x" in expression "x^2 + 5*x" with a constant 3');
|
||||
var node2 = math.parse('x^2 + 5*x');
|
||||
var transformed = node2.transform(function (node, path, parent) {
|
||||
if (node.isSymbolNode && node.name == 'x') {
|
||||
return new math.expression.node.ConstantNode(3);
|
||||
}
|
||||
else {
|
||||
return node;
|
||||
}
|
||||
});
|
||||
console.log(transformed.toString());
|
||||
// outputs: '(3 ^ 2) + (5 * 3)'
|
||||
}());
|
||||
|
||||
939
mathjs/mathjs.d.ts
vendored
939
mathjs/mathjs.d.ts
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user