-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilMatrix.js
135 lines (123 loc) · 3.35 KB
/
utilMatrix.js
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
import { isAnyArray } from './indexIsAnyArray.js';
/**
* @private
* Check that a row index is not out of bounds
* @param {Matrix} matrix
* @param {number} index
* @param {boolean} [outer]
*/
export function checkRowIndex(matrix, index, outer) {
let max = outer ? matrix.rows : matrix.rows - 1;
if (index < 0 || index > max) {
throw new RangeError('Row index out of range');
}
}
/**
* @private
* Check that a column index is not out of bounds
* @param {Matrix} matrix
* @param {number} index
* @param {boolean} [outer]
*/
export function checkColumnIndex(matrix, index, outer) {
let max = outer ? matrix.columns : matrix.columns - 1;
if (index < 0 || index > max) {
throw new RangeError('Column index out of range');
}
}
/**
* @private
* Check that the provided vector is an array with the right length
* @param {Matrix} matrix
* @param {Array|Matrix} vector
* @return {Array}
* @throws {RangeError}
*/
export function checkRowVector(matrix, vector) {
if (vector.to1DArray) {
vector = vector.to1DArray();
}
if (vector.length !== matrix.columns) {
throw new RangeError(
'vector size must be the same as the number of columns',
);
}
return vector;
}
/**
* @private
* Check that the provided vector is an array with the right length
* @param {Matrix} matrix
* @param {Array|Matrix} vector
* @return {Array}
* @throws {RangeError}
*/
export function checkColumnVector(matrix, vector) {
if (vector.to1DArray) {
vector = vector.to1DArray();
}
if (vector.length !== matrix.rows) {
throw new RangeError('vector size must be the same as the number of rows');
}
return vector;
}
export function checkRowIndices(matrix, rowIndices) {
if (!isAnyArray(rowIndices)) {
throw new TypeError('row indices must be an array');
}
for (let i = 0; i < rowIndices.length; i++) {
if (rowIndices[i] < 0 || rowIndices[i] >= matrix.rows) {
throw new RangeError('row indices are out of range');
}
}
}
export function checkColumnIndices(matrix, columnIndices) {
if (!isAnyArray(columnIndices)) {
throw new TypeError('column indices must be an array');
}
for (let i = 0; i < columnIndices.length; i++) {
if (columnIndices[i] < 0 || columnIndices[i] >= matrix.columns) {
throw new RangeError('column indices are out of range');
}
}
}
export function checkRange(matrix, startRow, endRow, startColumn, endColumn) {
if (arguments.length !== 5) {
throw new RangeError('expected 4 arguments');
}
checkNumber('startRow', startRow);
checkNumber('endRow', endRow);
checkNumber('startColumn', startColumn);
checkNumber('endColumn', endColumn);
if (
startRow > endRow ||
startColumn > endColumn ||
startRow < 0 ||
startRow >= matrix.rows ||
endRow < 0 ||
endRow >= matrix.rows ||
startColumn < 0 ||
startColumn >= matrix.columns ||
endColumn < 0 ||
endColumn >= matrix.columns
) {
throw new RangeError('Submatrix indices are out of range');
}
}
export function newArray(length, value = 0) {
let array = [];
for (let i = 0; i < length; i++) {
array.push(value);
}
return array;
}
function checkNumber(name, value) {
if (typeof value !== 'number') {
throw new TypeError(`${name} must be a number`);
}
}
export function checkNonEmpty(matrix) {
if (matrix.isEmpty()) {
throw new Error('Empty matrix has no elements to index');
}
}