- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
const range = (count) => Array.from(Array(count).keys());
class Matrix {
static Dot(A, B) {
// Dot production
const wA = A[0].length;
const hA = A.length;
const wB = B[0].length;
const hB = B.length;
if (wA != hB)
{
throw "A width != B height";
}
const C = range(hA).map((_, i) => range(wB).map((_, j) => 0));
for (let i = 0; i < hA; ++i)
for (let j = 0; j < wB; ++j) {
let sum = 0;
for (let k = 0; k < wA; ++k) {
const a = A[i][k];
const b = B[k][j];
sum += a * b;
}
C[i][j] = sum;
}
return C;
}
static Mul(A, B) {
// Dot production
const wA = A[0].length;
const hA = A.length;
const wB = B[0].length;
const hB = B.length;
if (wA != wB || hA != hB)
{
throw "A width != B width, A height != B height";
}
const C = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] * B[i][j]));
return C;
}
static Add(A, B) {
const wA = A[0].length;
const hA = A.length;
const wB = B[0].length;
const hB = B.length;
if (wA != wB || hA != hB)
{
throw "A width != B width, A height != B height";
}
const C = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] + B[i][j]));
return C;
}
static Sub(A, B) {
const wA = A[0].length;
const hA = A.length;
const wB = B[0].length;
const hB = B.length;
if (wA != wB || hA != hB)
{
throw "A width != B width, A height != B height";
}
const C = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] - B[i][j]));
return C;
}
static Translate(A, shift) {
const wA = A[0].length;
const hA = A.length;
const R = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] + shift));
return R;
}
static Sigmoid(A) {
const wA = A[0].length;
const hA = A.length;
const R = range(hA).map((_, i) => range(wA).map((_, j) => 1 / (1 + Math.exp(-A[i][j]))));
return R;
}
//...
}