Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | 1556x 1556x 1556x 52x 1556x 342x 1556x 1556x 1556x 1556x 5897x 5897x 4159x 4159x 1738x 1738x 429x 375x 375x 377x 799x 375x 54x 54x 54x 54x 54x 162x 264x 264x 264x 54x 250x 250x 250x 1x 3x 9x 1x 249x 249x 249x 251x 533x 249x 12x 12x 12x 36x 6x 6x 6x 6x 6x 1136x 267x 2x 6x 267x 265x 795x 267x 265x 795x 267x 2x 6x 267x 267x 267x 801x 267x 71x 82x 63x 12x 12x 12x 12x 12x 36x 12x 8x 8x 8x 8x 274x 4x 4x 8x 8x 12x 4x 12x 8x 4x 11193x 11584x 8x 8x 23x 8x | import { AugmentedMatrix } from "./augmented-matrix.mjs";
export class Matrix {
/**
*
* @param {number} rows
* @param {number} cols
* @param {Float32Array} data
* @param {number} dataRowSize
* @param {number} rowOffset
* @param {number} colOffset
*/
constructor(rows, cols, data = undefined, dataRowSize = undefined, rowOffset = 0, colOffset = 0) {
this._rows = rows;
this._cols = cols;
if (!data) {
data = new Float32Array(rows * cols);
}
if (!dataRowSize) {
dataRowSize = cols;
}
this._dataRowSize = dataRowSize;
this._rowOffset = rowOffset;
this._colOffset = colOffset;
this._data = data;
}
/**
*
* @param {number} col
* @param {number} row
* @throws {RangeError}
*/
_checkBounds(col, row) {
Iif (row < 0 || col < 0 || row >= this.rows || col >= this.cols) {
throw RangeError(`(${col},${row}) is out of range for ${this}`);
}
}
/**
*
* @param {number} col
* @param {number} row
*/
_computeDataIndex(col, row) {
return this._dataRowSize * (row + this._rowOffset) + col + this._colOffset;
}
/**
*
* @param {number} col
* @param {number} row
* @returns {number}
*/
get(col, row) {
this._checkBounds(col, row);
return this._data[this._computeDataIndex(col, row)];
}
/**
*
* @param {number} col
* @param {number} row
* @param {number} value
*
*/
set(col, row, value) {
this._checkBounds(col, row);
this._data[this._computeDataIndex(col, row)] = value;
}
/**
*
* @param {number|Matrix} other
* @param {Matrix=} outputMatrix
* @return {Matrix}
*/
multiply(other, outputMatrix) {
if (typeof other === 'number') {
const output = outputMatrix ?? new Matrix(this.rows, this.cols);
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
output.set(col, row, this.get(col, row) * other);
}
}
return output;
}
Iif (!(other instanceof Matrix)) {
throw Error('Can only multiply by matrix.');
}
Iif (other.rows !== this.cols) {
throw Error(`The left matrices number of columns must equal the right's number of rows`);
}
Iif (outputMatrix && (outputMatrix.rows !== this.rows || outputMatrix.cols !== other.cols)) {
throw Error('A output matrix with invalid dimensions was provided.');
}
const output = outputMatrix ?? new Matrix(this.rows, other.cols);
for (let row = 0; row < output.rows; row++) {
for (let col = 0; col < output.cols; col++) {
const myRow = this.slice(0, this.cols, row, row+1);
const otherCol = other.slice(col, col+1, 0, other.rows);
output.set(col, row, myRow.dot(otherCol));
}
}
return output;
}
/**
*
* @param {number|Matrix} other
* @param {Matrix=} outputMatrix
* @return {Matrix}
*/
add(other, outputMatrix) {
Iif (outputMatrix && (outputMatrix.rows !== this.rows || outputMatrix.cols !== this.cols)) {
throw Error('Output matrix provided with the wrong dimensions.');
}
const output = outputMatrix ?? new Matrix(this.rows, this.cols);
if (typeof other === 'number') {
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
output.set(col, row, this.get(col, row) + other);
}
}
return output;
}
Iif (!(other instanceof Matrix)) {
throw Error('Can only add a number or matrix.');
}
Iif (other.rows !== this.rows || other.cols !== this.cols) {
throw Error(`The matrices must have the same dimensions`);
}
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
output.set(col, row, this.get(col, row) + other.get(col, row));
}
}
return output;
}
/**
* Assumes data from other matrix
* @param {Matrix} other
*/
assume(other) {
Iif (other.cols !== this.cols || other.rows !== this.rows) {
throw Error('Can only assume data from matrix with same dimensions.');
}
for (let row = 0; row < this.rows; row++) {
for (let col = 0; col < this.cols; col++) {
this.set(col, row, other.get(col, row));
}
}
}
/**
*
* @param {number} rowIndexOne
* @param {number} rowIndexTwo
*/
rowSwap(rowIndexOne, rowIndexTwo) {
const rowOne = this.slice(0, this.cols+1, rowIndexOne, rowIndexOne+1);
const rowOneCopy = rowOne.copy();
const rowTwo = this.slice(0, this.cols+1, rowIndexTwo, rowIndexTwo+1);
rowOne.assume(rowTwo);
rowTwo.assume(rowOneCopy);
}
/**
* Returns a view of a sub matrix.
* IT'S NOT A COPY! mutations to the view will mutate the matrix.
* This is intentional as it allows for easily performing elementary row operations on linear systems
* @param {number} colStart
* @param {number} colEnd
* @param {number} rowStart
* @param {number} rowEnd
* @returns {Matrix}
*/
slice(colStart, colEnd, rowStart, rowEnd) {
return new Matrix(rowEnd - rowStart, colEnd - colStart, this._data, this._dataRowSize, rowStart, colStart);
}
/**
*
* @param {Matrix} vector
* @return {number}
*/
dot(vector) {
/** @type {number|undefined} */
let myDimensions;
/** @type {(i: number) => number|undefined} */
let getMyValue;
/** @type {number|undefined} */
let vectorDimensions;
/** @type {(i: number) => number|undefined} */
let getVectorValue;
if (this.cols === 1) {
myDimensions = this.rows;
getMyValue = i => this.get(0, i);
}
if (this.rows === 1) {
myDimensions = this.cols;
getMyValue = i => this.get(i, 0);
}
if (vector.cols === 1) {
vectorDimensions = vector.rows;
getVectorValue = i => vector.get(0, i);
}
if (vector.rows === 1) {
vectorDimensions = vector.cols;
getVectorValue = i => vector.get(i, 0);
}
Iif (vectorDimensions === undefined || myDimensions === undefined || vectorDimensions !== myDimensions) {
throw Error('Invalid matrices provided for dot product');
}
let dotProduct = 0;
for (let i = 0; i < myDimensions; i++) {
dotProduct += getMyValue(i) * getVectorValue(i);
}
return dotProduct;
}
/**
* Finds the index of the first row with a pivot
* @param {number} column
* @return {number|undefined} index if found otherwise undefined
*/
findFirstRowWithPivot(column) {
for (let row = column; row < this.rows; row++) {
if (Math.abs(this.get(column, row)) >= Number.EPSILON) {
return row;
}
}
}
det() {
Iif (this.rows !== this.cols) {
throw Error('Can only compute the determinant of a square matrix');
}
const augmentedMatrix = new AugmentedMatrix(this.copy());
const { leftHandSide, scaledDown } = augmentedMatrix.gje();
let determinant = 1;
for (let i = 0; i < this.rows; i++) {
determinant *= leftHandSide.get(i, i);
}
return determinant * scaledDown;
}
/**
*
* @returns {Matrix}
*/
inverse() {
Iif (this.rows !== this.cols) {
throw Error('Can only compute inverse of a square matrix');
}
const augmentedMatrix = new AugmentedMatrix(this.copy(), Matrix.identity(this.rows));
const { rightHandSide } = augmentedMatrix.gje();
return rightHandSide;
}
copy() {
return new Matrix(this.rows, this.cols, new Float32Array(this._data), this._dataRowSize, this._rowOffset, this._colOffset);
}
toString() {
let matrixBody = '';
for (let row = 0; row < this.rows; row++) {
matrixBody += '\t';
for (let col = 0; col < this.cols; col++) {
if (col !== 0) {
matrixBody += ', ';
}
matrixBody += this.get(col, row);
}
matrixBody += '\n';
}
return `Matrix(rows: ${this.rows}, cols:${this.cols}) {\n${matrixBody}}`;
}
/**
* @returns {number}
*/
get rows() {
return this._rows;
}
/**
* @returns {number}
*/
get cols() {
return this._cols;
}
/**
* @param {number} size
* @returns {Matrix}
*/
static identity(size) {
const identity = new Matrix(size, size);
for (let i = 0; i < size; i++) {
identity.set(i, i, 1);
}
return identity;
}
}
|