baskin-lib
Version:
baskin robbins 31 web game library
126 lines (125 loc) • 3.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.matToString = exports.matVecDot = exports.vecMatDot = exports.vecVecDot = exports.matSliceCol = exports.sliceRev = exports.vecNormalize = exports.matShiftToLast = exports.vecShiftToLast = exports.vecCumSum = exports.getUnitVec = exports.vecFindMin = void 0;
/**
*
* @param vec input vector
* @returns minimum value of `vec`
*/
function vecFindMin(vec) {
return vec.reduce((lowest, current) => lowest > current ? current : lowest, Infinity);
}
exports.vecFindMin = vecFindMin;
/**
*
* @param len length of the output vector
* @returns unit vector of length `len`. [1, 0, 0, ..., 0]
*/
function getUnitVec(len) {
const result = Array(len).fill(0);
result[0] = 1;
return result;
}
exports.getUnitVec = getUnitVec;
/**
*
* @param vec input vector
* @returns cumulative sum of `vec`. ex) [1, 2, 3] => [1, 3, 6]
*/
function vecCumSum(vec) {
return vec.map((sum => value => sum += value)(0));
}
exports.vecCumSum = vecCumSum;
/**
*
* @param vec input vector
* @returns Shifted vector. Makes deep copy. ex) [1, 2, 3] => [3, 1, 2].
*/
function vecShiftToLast(vec) {
if (vec.length <= 1) {
return vec;
}
const copied = vec.slice(0, -1);
copied.unshift(vec[vec.length - 1]);
return copied;
}
exports.vecShiftToLast = vecShiftToLast;
/**
*
* @param mat input matrix
* @returns Row-wise shifted matrix. Makes deep copy.
*/
function matShiftToLast(mat) {
return mat.map((row) => vecShiftToLast(row));
}
exports.matShiftToLast = matShiftToLast;
/**
*
* @param vec input vector
* @returns normalized vector such that sum(vector) = 1.
*/
function vecNormalize(vec) {
const sum = vec.reduce((accum, item) => accum + item, 0);
return vec.map(x => x / sum);
}
exports.vecNormalize = vecNormalize;
/**
*
* @param revArr reversed array
* @param startRow slice starting row index
* @param endRow slice ending row index
* @returns Slice reversed array as if it is normal.
* (noraml array).slice(startRow, endRow).reverse() = sliceRev((reversed array), startRow, endRow)
*/
function sliceRev(revArr, startRow, endRow) {
const newStartRow = revArr.length - endRow;
const newEndRow = revArr.length - startRow;
return revArr.slice(newStartRow, newEndRow);
}
exports.sliceRev = sliceRev;
/**
*
* @param mat input matrix
* @param colIdx column index to slice
* @returns sliced vector.
*/
function matSliceCol(mat, colIdx) {
return mat.reduce((accum, row) => accum.concat(row[colIdx]), []);
}
exports.matSliceCol = matSliceCol;
// multiplication
/**
*
* @param vec1 (1 x n) row vector
* @param vec2 (n X 1) column vector
* @returns (vec1 . vec2) = scalar
*/
function vecVecDot(vec1, vec2) {
return vec1.reduce((accum, value, idx) => accum + (value * vec2[idx]), 0);
}
exports.vecVecDot = vecVecDot;
/**
*
* @param vec (1 x n) row vector.
* @param mat (n x m) matrix. Should be in row-major order.
* @returns (vec . mat) = (1 x m) row vector
*/
function vecMatDot(vec, mat) {
return mat.reduce((accumRow, arrRow, rowIdx) => accumRow.map((colVal, colIdx) => colVal + arrRow[colIdx] * vec[rowIdx]), new Array(mat[0].length).fill(0));
}
exports.vecMatDot = vecMatDot;
/**
*
* @param mat (n x m) matrix. Should be in row-major order.
* @param vec (m x 1) column vector.
* @returns (mat . vec) = (n x l) column vector
*/
function matVecDot(mat, vec) {
return mat.map((vec2) => vecVecDot(vec2, vec));
}
exports.matVecDot = matVecDot;
// debug
function matToString(mat) {
return "[" + mat.map((x) => x.join(", ")).join("] [") + "]";
}
exports.matToString = matToString;