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