UNPKG

tsgammon-core

Version:

A Backgammon library for Typescript, formerly developed as a part of tsgammon-ui

49 lines (48 loc) 1.2 kB
"use strict"; /** * * 行列計算の簡易な実装 * * @module */ Object.defineProperty(exports, "__esModule", { value: true }); exports.add = exports.product = exports.matrix2d = void 0; function matrix2d(value) { return { arr: value, rowsN() { return this.arr.length; }, colsN() { return this.arr.length === 0 ? 0 : this.arr[0].length; }, }; } exports.matrix2d = matrix2d; function product(m1, m2) { if (m1.colsN() !== m2.rowsN()) { throw Error(); } const mp = m1.arr.map((rowValues, i) => { // rowValues = [a, b] const newRow = Array(m2.colsN()).fill(0); return newRow.map((v, j) => { return m1.arr[i] .map((v, ii) => { return m2.arr[ii][j] * v; }) .reduce((a, b) => a + b); }); }); return matrix2d(mp); } exports.product = product; function add(m1, m2) { if (m1.rowsN() !== m2.rowsN() || m1.colsN() !== m2.colsN()) { throw Error(); } return matrix2d(m1.arr.map((row, i) => { return row.map((value, j) => m2.arr[i][j] + value); })); } exports.add = add;