-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
import.js
93 lines (80 loc) · 2.47 KB
/
import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Math.js can easily be extended with functions and variables using the
* `import` function. The function `import` accepts a module name or an object
* containing functions and variables.
*/
import { create, all } from '../lib/esm/index.js'
const math = create(all)
/**
* Define new functions and variables
*/
math.import({
myConstant: 42,
hello: function (name) {
return 'hello, ' + name + '!'
}
})
// defined methods can be used in both JavaScript as well as the parser
print(math.myConstant * 2) // 84
print(math.hello('user')) // 'hello, user!'
print(math.evaluate('myConstant + 10')) // 52
print(math.evaluate('hello("user")')) // 'hello, user!'
/**
* Import the math library numbers.js, https://github.com/sjkaliski/numbers.js
* The library must be installed first using npm:
* npm install numbers
*/
try {
// load the numbers.js library
const numbers = require('numbers')
// import the numbers.js library into math.js
math.import(numbers, { wrap: true, silent: true })
if (math.fibonacci) {
// calculate fibonacci
print(math.fibonacci(7)) // 13
print(math.evaluate('fibonacci(7)')) // 13
}
} catch (err) {
console.log('Warning: To use numbers.js, the library must ' +
'be installed first via `npm install numbers`.')
}
/**
* Import the math library numeric.js, https://github.com/sloisel/numeric
* The library must be installed first using npm:
* npm install numeric
*/
try {
// load the numeric.js library
const numeric = require('numeric')
// import the numeric.js library into math.js
math.import(numeric, { wrap: true, silent: true })
if (math.eig) {
// calculate eigenvalues of a matrix
print(math.evaluate('eig([1, 2; 4, 3])').lambda.x) // [5, -1]
// solve AX = b
const A = math.evaluate('[1, 2, 3; 2, -1, 1; 3, 0, -1]')
const b = [9, 8, 3]
print(math.solve(A, b)) // [2, -1, 3]
}
} catch (err) {
console.log('Warning: To use numeric.js, the library must ' +
'be installed first via `npm install numeric`.')
}
/**
* By default, the function import does not allow overriding existing functions.
* Existing functions can be overridden by specifying option `override: true`
*/
math.import({
pi: 3.14
}, {
override: true
})
print(math.pi) // returns 3.14 instead of 3.141592653589793
/**
* Helper function to output a value in the console. Value will be formatted.
* @param {*} value
*/
function print (value) {
const precision = 14
console.log(math.format(value, precision))
}