UNPKG

@formant/ava

Version:

A framework for automated visual analytics.

79 lines (78 loc) 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.inverseSecondOrderMatrix = exports.constructDiagonalMatrix = exports.multiMatrixMultiply = exports.matrixMultiply = exports.matrixTranspose = exports.vectorInnerProduct = exports.vectorSubtract = exports.vectorAdd = void 0; /** operation of vector or matrix */ var vectorAdd = function (x, y) { if (x.length !== y.length) return null; return x.map(function (xi, i) { return xi + y[i]; }); }; exports.vectorAdd = vectorAdd; var vectorSubtract = function (x, y) { if (x.length !== y.length) return null; return x.map(function (xi, i) { return xi - y[i]; }); }; exports.vectorSubtract = vectorSubtract; var vectorInnerProduct = function (x, y) { if (x.length !== y.length) return null; var result = 0; x.forEach(function (xi, i) { result += xi * y[i]; }); return result; }; exports.vectorInnerProduct = vectorInnerProduct; var matrixTranspose = function (x) { var result = []; var _loop_1 = function (j) { result.push(x.map(function (xi) { return xi[j]; })); }; for (var j = 0; j < x[0].length; j += 1) { _loop_1(j); } return result; }; exports.matrixTranspose = matrixTranspose; var matrixMultiply = function (x, y) { var _a; if (((_a = x[0]) === null || _a === void 0 ? void 0 : _a.length) !== y.length) return null; var result = []; var yT = (0, exports.matrixTranspose)(y); x.forEach(function (xi) { return result.push(yT.map(function (yj) { return (0, exports.vectorInnerProduct)(xi, yj); })); }); return result; }; exports.matrixMultiply = matrixMultiply; var multiMatrixMultiply = function (matrixSet) { var result = matrixSet[0]; for (var i = 1; i < matrixSet.length; i += 1) { result = (0, exports.matrixMultiply)(result, matrixSet[i]); } return result; }; exports.multiMatrixMultiply = multiMatrixMultiply; /** * Constructs a diagonal matrix where the values of the main diagonal are the values of the given vector */ var constructDiagonalMatrix = function (diagonalVector) { return diagonalVector.map(function (diagonalValue, i) { // ith row var row = Array(diagonalVector.length).fill(0); row[i] = diagonalValue; return row; }); }; exports.constructDiagonalMatrix = constructDiagonalMatrix; /** * Calculate the inverse matrix for second order matrix * */ var inverseSecondOrderMatrix = function (matrix) { var determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]; return [ [matrix[1][1] / determinant, -matrix[0][1] / determinant], [-matrix[1][0] / determinant, matrix[0][0] / determinant], ]; }; exports.inverseSecondOrderMatrix = inverseSecondOrderMatrix;