matrices.js (2917B)
1 // matrices 2 3 // load math.js (using node.js) 4 const math = require('..') 5 6 // create matrices and arrays. a matrix is just a wrapper around an Array, 7 // providing some handy utilities. 8 console.log('create a matrix') 9 const a = math.matrix([1, 4, 9, 16, 25]) 10 print(a) // [1, 4, 9, 16, 25] 11 const b = math.matrix(math.ones([2, 3])) 12 print(b) // [[1, 1, 1], [1, 1, 1]] 13 print(b.size()) // [2, 3] 14 15 // the Array data of a Matrix can be retrieved using valueOf() 16 const array = a.valueOf() 17 print(array) // [1, 4, 9, 16, 25] 18 19 // Matrices can be cloned 20 const clone = a.clone() 21 print(clone) // [1, 4, 9, 16, 25] 22 console.log() 23 24 // perform operations with matrices 25 console.log('perform operations') 26 print(math.sqrt(a)) // [1, 2, 3, 4, 5] 27 const c = [1, 2, 3, 4, 5] 28 print(math.factorial(c)) // [1, 2, 6, 24, 120] 29 console.log() 30 31 // create and manipulate matrices. Arrays and Matrices can be used mixed. 32 console.log('manipulate matrices') 33 const d = [[1, 2], [3, 4]] 34 print(d) // [[1, 2], [3, 4]] 35 const e = math.matrix([[5, 6], [1, 1]]) 36 print(e) // [[5, 6], [1, 1]] 37 38 // set a submatrix. 39 // Matrix indexes are zero-based. 40 e.subset(math.index(1, [0, 1]), [[7, 8]]) 41 print(e) // [[5, 6], [7, 8]] 42 const f = math.multiply(d, e) 43 print(f) // [[19, 22], [43, 50]] 44 const g = f.subset(math.index(1, 0)) 45 print(g) // 43 46 console.log() 47 48 // get a sub matrix 49 // Matrix indexes are zero-based. 50 console.log('get a sub matrix') 51 const h = math.diag(math.range(1, 4)) 52 print(h) // [[1, 0, 0], [0, 2, 0], [0, 0, 3]] 53 print(h.subset(math.index([1, 2], [1, 2]))) // [[2, 0], [0, 3]] 54 const i = math.range(1, 6) 55 print(i) // [1, 2, 3, 4, 5] 56 print(i.subset(math.index(math.range(1, 4)))) // [2, 3, 4] 57 console.log() 58 59 // resize a multi dimensional matrix 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) // [[[0, 0], [0, 0]], [[0, 0], [0, 0]]] 65 print(j.size()) // [2, 2, 2] 66 j.resize([2, 2]) 67 print(j) // [[0, 0], [0, 0]] 68 print(j.size()) // [2, 2] 69 console.log() 70 71 // setting a value outside the matrices range will resize the matrix. 72 // new elements will be initialized with zero. 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) // [0, 0, 6] 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) // [null, null, 6] 84 console.log() 85 86 // create ranges 87 console.log('create ranges') 88 print(math.range(1, 6)) // [1, 2, 3, 4, 5] 89 print(math.range(0, 18, 3)) // [0, 3, 6, 9, 12, 15] 90 print(math.range('2:-1:-3')) // [2, 1, 0, -1, -2] 91 print(math.factorial(math.range('1:6'))) // [1, 2, 6, 24, 120] 92 console.log() 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 const precision = 14 100 console.log(math.format(value, precision)) 101 }