1 |
|
2 |
|
3 |
|
4 | const math = require('..')
|
5 |
|
6 |
|
7 |
|
8 | console.log('create a matrix')
|
9 | const a = math.matrix([1, 4, 9, 16, 25])
|
10 | print(a)
|
11 | const b = math.matrix(math.ones([2, 3]))
|
12 | print(b)
|
13 | print(b.size())
|
14 |
|
15 |
|
16 | const array = a.valueOf()
|
17 | print(array)
|
18 |
|
19 |
|
20 | const clone = a.clone()
|
21 | print(clone)
|
22 | console.log()
|
23 |
|
24 |
|
25 | console.log('perform operations')
|
26 | print(math.sqrt(a))
|
27 | const c = [1, 2, 3, 4, 5]
|
28 | print(math.factorial(c))
|
29 | console.log()
|
30 |
|
31 |
|
32 | console.log('manipulate matrices')
|
33 | const d = [[1, 2], [3, 4]]
|
34 | print(d)
|
35 | const e = math.matrix([[5, 6], [1, 1]])
|
36 | print(e)
|
37 |
|
38 |
|
39 |
|
40 | e.subset(math.index(1, [0, 1]), [[7, 8]])
|
41 | print(e)
|
42 | const f = math.multiply(d, e)
|
43 | print(f)
|
44 | const g = f.subset(math.index(1, 0))
|
45 | print(g)
|
46 | console.log()
|
47 |
|
48 |
|
49 |
|
50 | console.log('get a sub matrix')
|
51 | const h = math.diag(math.range(1, 4))
|
52 | print(h)
|
53 | print(h.subset(math.index([1, 2], [1, 2])))
|
54 | const i = math.range(1, 6)
|
55 | print(i)
|
56 | print(i.subset(math.index(math.range(1, 4))))
|
57 | console.log()
|
58 |
|
59 |
|
60 | console.log('resizing a matrix')
|
61 | const j = math.matrix()
|
62 | let defaultValue = 0
|
63 | j.resize([2, 2, 2], defaultValue)
|
64 | print(j)
|
65 | print(j.size())
|
66 | j.resize([2, 2])
|
67 | print(j)
|
68 | print(j.size())
|
69 | console.log()
|
70 |
|
71 |
|
72 |
|
73 | console.log('set a value outside a matrices range')
|
74 | const k = math.matrix()
|
75 | k.subset(math.index(2), 6)
|
76 | print(k)
|
77 | console.log()
|
78 |
|
79 | console.log('set a value outside a matrices range, setting other new entries to null')
|
80 | const m = math.matrix()
|
81 | defaultValue = null
|
82 | m.subset(math.index(2), 6, defaultValue)
|
83 | print(m)
|
84 | console.log()
|
85 |
|
86 |
|
87 | console.log('create ranges')
|
88 | print(math.range(1, 6))
|
89 | print(math.range(0, 18, 3))
|
90 | print(math.range('2:-1:-3'))
|
91 | print(math.factorial(math.range('1:6')))
|
92 | console.log()
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function print (value) {
|
99 | const precision = 14
|
100 | console.log(math.format(value, precision))
|
101 | }
|