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