UNPKG

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