|
| 1 | +import math from 'mathjs'; |
| 2 | + |
| 3 | +// Matrices and Vectors |
| 4 | + |
| 5 | +const matrixA = math.matrix([[0, 1], [2, 3], [4, 5]]); |
| 6 | +const matrixB = math.matrix([[0], [1], [2]]); |
| 7 | + |
| 8 | +console.log(`Matrix A Dimension: ${matrixA.size()[0]}x${matrixA.size()[1]}`); |
| 9 | +console.log(`Is vector: ${matrixA.size()[1] === 1}`); |
| 10 | + |
| 11 | +console.log(`Matrix B Dimension: ${matrixB.size()[0]}x${matrixB.size()[1]}`); |
| 12 | +console.log(`Is vector: ${matrixB.size()[1] === 1}`); |
| 13 | +console.log('\n'); |
| 14 | + |
| 15 | +// Matrix Addition |
| 16 | + |
| 17 | +const matrixC = math.matrix([[0, 1], [2, 3], [4, -5]]); |
| 18 | +const matrixD = math.matrix([[1, -1], [-2, 4], [-7, 4]]); |
| 19 | + |
| 20 | +const matrixAdditionCD = math.add(matrixC, matrixD); |
| 21 | + |
| 22 | +console.log('Matrix Addition:'); |
| 23 | +console.log(matrixAdditionCD.valueOf()); |
| 24 | +console.log('\n'); |
| 25 | + |
| 26 | +// Matrix Subtraction |
| 27 | + |
| 28 | +const matrixE = math.matrix([[0, 1], [2, 3], [4, -5]]); |
| 29 | +const matrixF = math.matrix([[1, -1], [-2, 4], [-7, 4]]); |
| 30 | + |
| 31 | +const matrixAdditionEF = math.subtract(matrixE, matrixF); |
| 32 | + |
| 33 | +console.log('Matrix Subtraction:'); |
| 34 | +console.log(matrixAdditionEF.valueOf()); |
| 35 | +console.log('\n'); |
| 36 | + |
| 37 | +// Matrix Scalar Multiplication |
| 38 | + |
| 39 | +const matrixG = math.matrix([[0, 1], [2, 3], [4, -5]]); |
| 40 | + |
| 41 | +const matrixG3 = math.multiply(3, matrixG); |
| 42 | + |
| 43 | +console.log('Matrix Scalar Multiplication:'); |
| 44 | +console.log(matrixG3.valueOf()); |
| 45 | +console.log('\n'); |
| 46 | + |
| 47 | +// Matrix Scalar Division |
| 48 | + |
| 49 | +const matrixH = math.matrix([[2, 4], [6, 2], [4, -4]]); |
| 50 | + |
| 51 | +const matrixH2 = math.divide(matrixH, 2); |
| 52 | + |
| 53 | +console.log('Matrix Scalar Division:'); |
| 54 | +console.log(matrixH2.valueOf()); |
| 55 | +console.log('\n'); |
| 56 | + |
| 57 | +// Matrix-Vector Multiplication |
| 58 | + |
| 59 | +const matrixI = math.matrix([[0, 1], [2, 3], [4, 5]]); |
| 60 | +const matrixJ = math.matrix([[2], [1]]); |
| 61 | + |
| 62 | +const matrixIJ = math.multiply(matrixI, matrixJ); |
| 63 | + |
| 64 | +console.log('Matrix-Vector Multiplication:'); |
| 65 | +console.log(matrixIJ.valueOf()); |
| 66 | +console.log('\n'); |
| 67 | + |
| 68 | +// Matrix Multiplication |
| 69 | + |
| 70 | +const matrixK = math.matrix([[0, 1], [2, 3], [4, 5]]); |
| 71 | +const matrixL = math.matrix([[2, 4], [6, 2]]); |
| 72 | + |
| 73 | +const matrixKL = math.multiply(matrixK, matrixL); |
| 74 | + |
| 75 | +console.log('Matrix Multiplication:'); |
| 76 | +console.log(matrixKL.valueOf()); |
| 77 | +console.log('\n'); |
| 78 | + |
| 79 | +// Matrix Property: Not Commutative |
| 80 | + |
| 81 | +const matrixN = math.matrix([[0, 1], [2, 3], [4, 5]]); |
| 82 | +const matrixO = math.matrix([[2, 4], [6, 2]]); |
| 83 | + |
| 84 | +const matrixNO = math.multiply(matrixN, matrixO); |
| 85 | +const matrixON = math.multiply(matrixO, matrixL); |
| 86 | + |
| 87 | +console.log('Are matrices commutative?'); |
| 88 | +console.log(math.equal(matrixNO.size(), matrixON.size())); |
| 89 | +console.log('\n'); |
| 90 | + |
| 91 | +// Matrix Property: But Associative |
| 92 | + |
| 93 | +const matrixP = math.matrix([[0, 1], [2, 3], [4, 5]]); |
| 94 | +const matrixQ = math.matrix([[2, 4], [6, 2]]); |
| 95 | +const matrixR = math.matrix([[5, 2], [2, -2]]); |
| 96 | + |
| 97 | +const matrixPQ_R = math.multiply(math.multiply(matrixP, matrixQ), matrixR); |
| 98 | +const matrixP_QR = math.multiply(matrixP, math.multiply(matrixQ, matrixR)); |
| 99 | + |
| 100 | +console.log('Are matrices associative?'); |
| 101 | +console.log(math.equal(matrixPQ_R.valueOf(), matrixP_QR.valueOf())); |
| 102 | +console.log('\n'); |
| 103 | + |
| 104 | +// Identity (I) Matrix |
| 105 | + |
| 106 | +const matrixM = math.eye(3); |
| 107 | + |
| 108 | +console.log('Identity Matrix:'); |
| 109 | +console.log(matrixM.valueOf()); |
| 110 | +console.log('\n'); |
| 111 | + |
| 112 | +// Inverse Matrix |
| 113 | + |
| 114 | +const matrixS = math.matrix([[0, 1], [2, 3]]); |
| 115 | + |
| 116 | +const matrixS_I = math.inv(matrixS); |
| 117 | + |
| 118 | +console.log('Inverse Matrix:'); |
| 119 | +console.log(matrixS_I.valueOf()); |
| 120 | +console.log('\n'); |
| 121 | + |
| 122 | +// Calculated Inverse Matrix with Identity Matrix |
| 123 | + |
| 124 | +const matrixT = math.matrix([[0, 1], [2, 3]]); |
| 125 | +const matrixU = math.eye(2); |
| 126 | + |
| 127 | +const matrixT_I = math.divide(matrixU, matrixT); |
| 128 | + |
| 129 | +console.log('Calculated Inverse Matrix with Identity Matrix:'); |
| 130 | +console.log(matrixT_I.valueOf()); |
| 131 | +console.log('\n'); |
| 132 | + |
| 133 | +// Transpose Matrix |
| 134 | + |
| 135 | +const matrixV = math.matrix([[0], [1], [2]]); |
| 136 | + |
| 137 | +const matrixV_T = math.transpose(matrixV); |
| 138 | + |
| 139 | +console.log('Transpose Matrix:'); |
| 140 | +console.log(matrixV_T.valueOf()); |
| 141 | +console.log('\n'); |
| 142 | + |
| 143 | +// Example: Predicting Housing Prices with 3 competing Hypotheses |
| 144 | + |
| 145 | +console.log('Predicting Housing Prices with 3 competing Hypotheses:'); |
| 146 | +console.log('const HOUSE_SIZES = [2104, 1416, 1534, 852];'); |
| 147 | +console.log('const h1 = x => -40 + 0.25 * x;'); |
| 148 | +console.log('const h2 = x => 200 + 0.1 * x;'); |
| 149 | +console.log('const h3 = x => -150 + 0.4 * x;'); |
| 150 | +console.log('\n'); |
| 151 | + |
| 152 | +const houseSizeMatrix = math.matrix([ |
| 153 | + [1, 2104], |
| 154 | + [1, 1416], |
| 155 | + [1, 1534], |
| 156 | + [1, 852], |
| 157 | +]); |
| 158 | + |
| 159 | +const hypothesesMatrix = math.matrix([ |
| 160 | + [-40, 200, -150], |
| 161 | + [0.25, 0.1, 0.4], |
| 162 | +]); |
| 163 | + |
| 164 | +const competingResults = math.multiply(houseSizeMatrix, hypothesesMatrix); |
| 165 | + |
| 166 | +console.log('Column: Result for each Hypothesis'); |
| 167 | +console.log('Row: Result for each House Size'); |
| 168 | +console.log(competingResults.valueOf()); |
| 169 | +console.log('\n'); |
0 commit comments