Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const range = (count) => Array.from(Array(count).keys());
- class Vector {
- static Add(A, B) {
- const hA = A.length;
- const hB = B.length;
- if (hA != hB)
- {
- throw "A height != B height";
- }
- return range(hA).map((_, i) => A[i] + B[i]);
- }
- static Sub(A, B) {
- const hA = A.length;
- const hB = B.length;
- if (hA != hB)
- {
- throw "A height != B height";
- }
- return range(hA).map((_, i) => A[i] - B[i]);
- }
- static Translate(A, shift) {
- const wA = A.length;
- const R = range(wA).map((_, i) => A[i] + shift);
- return R;
- }
- static Scale(A, scale) {
- const wA = A.length;
- const R = range(wA).map((_, i) => A[i] * scale);
- return R;
- }
- static FromMatrixRow(A, k = 0)
- {
- const wA = A.length;
- const C = range(wA).map((_, i) =>A[i][k]);
- return C;
- }
- static FromMatrixColumn(A, k = 0)
- {
- if (!A)
- {
- throw "No matrix";
- }
- if (k >= A.length)
- {
- throw "Out of index";
- }
- if (!A[k])
- {
- throw "No Data at index: " + k;
- }
- const wA = A[k].length;
- const C = range(wA).map((_, i) =>A[k][i]);
- return C;
- }
- static IndexOfMaxValue(A)
- {
- const wA = A.length;
- var index = 0;
- var v = A[0];
- for (let i = 1; i < wA; i++)
- {
- if (A[i] > v)
- {
- v = A[i];
- index = i;
- }
- }
- return index;
- }
- static MaxValueOf(A)
- {
- const wA = A.length;
- var v = A[0];
- for (let i = 1; i < wA; i++)
- {
- if (A[i] > v)
- {
- v = A[i];
- }
- }
- return v;
- }
- }
- 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 Mul_ValueByVector(A, B) {
- // Dot production
- const wA = A[0].length;
- const hA = A.length;
- const wB = B[0].length;
- const hB = B.length;
- if (hA != hB)
- {
- throw "A height != B height";
- }
- const C = range(hA).map((_, i) => range(wA).map((_, j) => Vector.Scale(B[i], A[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 Transpose(A) {
- const wA = A[0].length;
- const hA = A.length;
- const T = range(wA).map((_, j) => range(hA).map((_, i) => A[i][j]));
- return T;
- }
- static Random(w, h) {
- const m = range(h).map((_, i) => range(w).map((_, j) => Math.random()));
- return m;
- }
- static Scale(A, scale) {
- const wA = A[0].length;
- const hA = A.length;
- const R = range(hA).map((_, i) => range(wA).map((_, j) => A[i][j] * scale));
- return R;
- }
- 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 Average(A) {
- // A is array of matrixes
- const hA = A.length;
- let C = A[0];
- for (let i = 1; i < hA; i++)
- {
- C = Matrix.Add(C, A[i]);
- }
- C = Matrix.Scale(C, 1/hA);
- return C;
- }
- 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;
- }
- static SigmoidDerivative(A) {
- const wA = A[0].length;
- const hA = A.length;
- const R = range(hA).map((_, i) => range(wA).map((_, j) => {
- const sigmoid = 1 / (1 + Math.exp(-A[i][j]));
- return sigmoid * (1 - sigmoid);
- }));
- return R;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement