1 | // matrices
|
2 |
|
3 | // load math.js (using node.js)
|
4 | var math = require('../index');
|
5 |
|
6 |
|
7 | // create matrices and arrays. a matrix is just a wrapper around an Array,
|
8 | // providing some handy utilities.
|
9 | console.log('create a matrix');
|
10 | var a = math.matrix([1, 4, 9, 16, 25]);
|
11 | print(a); // [1, 4, 9, 16, 25]
|
12 | var b = math.matrix(math.ones([2, 3]));
|
13 | print(b); // [[1, 1, 1], [1, 1, 1]]
|
14 | print(b.size()); // [2, 3]
|
15 |
|
16 | // the Array data of a Matrix can be retrieved using valueOf()
|
17 | var array = a.valueOf();
|
18 | print(array); // [1, 4, 9, 16, 25]
|
19 |
|
20 | // Matrices can be cloned
|
21 | var clone = a.clone();
|
22 | print(clone); // [1, 4, 9, 16, 25]
|
23 | console.log();
|
24 |
|
25 | // perform operations with matrices
|
26 | console.log('perform operations');
|
27 | print(math.sqrt(a)); // [1, 2, 3, 4, 5]
|
28 | var c = [1, 2, 3, 4, 5];
|
29 | print(math.factorial(c)); // [1, 2, 6, 24, 120]
|
30 | console.log();
|
31 |
|
32 | // create and manipulate matrices. Arrays and Matrices can be used mixed.
|
33 | console.log('manipulate matrices');
|
34 | var d = [[1, 2], [3, 4]];
|
35 | print(d); // [[1, 2], [3, 4]]
|
36 | var e = math.matrix([[5, 6], [1, 1]]);
|
37 | print(e); // [[5, 6], [1, 1]]
|
38 |
|
39 | // set a submatrix.
|
40 | // Matrix indexes are zero-based.
|
41 | e.subset(math.index(1, [0, 1]), [[7, 8]]);
|
42 | print(e); // [[5, 6], [7, 8]]
|
43 | var f = math.multiply(d, e);
|
44 | print(f); // [[19, 22], [43, 50]]
|
45 | var g = f.subset(math.index(1, 0));
|
46 | print(g); // 43
|
47 | console.log();
|
48 |
|
49 | // get a sub matrix
|
50 | // Matrix indexes are zero-based.
|
51 | console.log('get a sub matrix');
|
52 | var h = math.diag(math.range(1,4));
|
53 | print(h); // [[1, 0, 0], [0, 2, 0], [0, 0, 3]]
|
54 | print(h.subset( math.index([1, 2], [1, 2]))); // [[2, 0], [0, 3]]
|
55 | var i = math.range(1,6);
|
56 | print(i); // [1, 2, 3, 4, 5]
|
57 | print(i.subset(math.index(math.range(1,4)))); // [2, 3, 4]
|
58 | console.log();
|
59 |
|
60 |
|
61 | // resize a multi dimensional matrix
|
62 | console.log('resizing a matrix');
|
63 | var j = math.matrix();
|
64 | var defaultValue = 0;
|
65 | j.resize([2, 2, 2], defaultValue);
|
66 | print(j); // [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
|
67 | print(j.size()); // [2, 2, 2]
|
68 | j.resize([2, 2]);
|
69 | print(j); // [[0, 0], [0, 0]]
|
70 | print(j.size()); // [2, 2]
|
71 | console.log();
|
72 |
|
73 | // setting a value outside the matrices range will resize the matrix.
|
74 | // new elements will be initialized with zero.
|
75 | console.log('set a value outside a matrices range');
|
76 | var k = math.matrix();
|
77 | k.subset(math.index(2), 6);
|
78 | print(k); // [0, 0, 6]
|
79 | console.log();
|
80 |
|
81 | console.log('set a value outside a matrices range, setting other new entries to null');
|
82 | var m = math.matrix();
|
83 | defaultValue = null;
|
84 | m.subset(math.index(2), 6, defaultValue);
|
85 | print(m); // [null, null, 6]
|
86 | console.log();
|
87 |
|
88 | // create ranges
|
89 | console.log('create ranges');
|
90 | print(math.range(1, 6)); // [1, 2, 3, 4, 5]
|
91 | print(math.range(0, 18, 3)); // [0, 3, 6, 9, 12, 15]
|
92 | print(math.range('2:-1:-3')); // [2, 1, 0, -1, -2]
|
93 | print(math.factorial(math.range('1:6'))); // [1, 2, 6, 24, 120]
|
94 | console.log();
|
95 |
|
96 |
|
97 | /**
|
98 | * Helper function to output a value in the console. Value will be formatted.
|
99 | * @param {*} value
|
100 | */
|
101 | function print (value) {
|
102 | var precision = 14;
|
103 | console.log(math.format(value, precision));
|
104 | }
|