UNPKG

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