ml-matrix
Version:
Matrix manipulation and computation library
31 lines (25 loc) • 758 B
JavaScript
import { AbstractMatrix } from '../matrix';
export default class WrapperMatrix1D extends AbstractMatrix {
constructor(data, options = {}) {
const { rows = 1 } = options;
if (data.length % rows !== 0) {
throw new Error('the data length is not divisible by the number of rows');
}
super();
this.rows = rows;
this.columns = data.length / rows;
this.data = data;
}
set(rowIndex, columnIndex, value) {
let index = this._calculateIndex(rowIndex, columnIndex);
this.data[index] = value;
return this;
}
get(rowIndex, columnIndex) {
let index = this._calculateIndex(rowIndex, columnIndex);
return this.data[index];
}
_calculateIndex(row, column) {
return row * this.columns + column;
}
}