ml-matrix 6.8.1 → 6.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/util.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { isAnyArray } from 'is-any-array';
2
+
1
3
  /**
2
4
  * @private
3
5
  * Check that a row index is not out of bounds
@@ -64,46 +66,28 @@ export function checkColumnVector(matrix, vector) {
64
66
  return vector;
65
67
  }
66
68
 
67
- export function checkIndices(matrix, rowIndices, columnIndices) {
68
- return {
69
- row: checkRowIndices(matrix, rowIndices),
70
- column: checkColumnIndices(matrix, columnIndices),
71
- };
72
- }
73
-
74
69
  export function checkRowIndices(matrix, rowIndices) {
75
- if (typeof rowIndices !== 'object') {
76
- throw new TypeError('unexpected type for row indices');
70
+ if (!isAnyArray(rowIndices)) {
71
+ throw new TypeError('row indices must be an array');
77
72
  }
78
73
 
79
- let rowOut = rowIndices.some((r) => {
80
- return r < 0 || r >= matrix.rows;
81
- });
82
-
83
- if (rowOut) {
84
- throw new RangeError('row indices are out of range');
74
+ for (let i = 0; i < rowIndices.length; i++) {
75
+ if (rowIndices[i] < 0 || rowIndices[i] >= matrix.rows) {
76
+ throw new RangeError('row indices are out of range');
77
+ }
85
78
  }
86
-
87
- if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices);
88
-
89
- return rowIndices;
90
79
  }
91
80
 
92
81
  export function checkColumnIndices(matrix, columnIndices) {
93
- if (typeof columnIndices !== 'object') {
94
- throw new TypeError('unexpected type for column indices');
82
+ if (!isAnyArray(columnIndices)) {
83
+ throw new TypeError('column indices must be an array');
95
84
  }
96
85
 
97
- let columnOut = columnIndices.some((c) => {
98
- return c < 0 || c >= matrix.columns;
99
- });
100
-
101
- if (columnOut) {
102
- throw new RangeError('column indices are out of range');
86
+ for (let i = 0; i < columnIndices.length; i++) {
87
+ if (columnIndices[i] < 0 || columnIndices[i] >= matrix.columns) {
88
+ throw new RangeError('column indices are out of range');
89
+ }
103
90
  }
104
- if (!Array.isArray(columnIndices)) columnIndices = Array.from(columnIndices);
105
-
106
- return columnIndices;
107
91
  }
108
92
 
109
93
  export function checkRange(matrix, startRow, endRow, startColumn, endColumn) {
@@ -4,7 +4,7 @@ import BaseView from './base';
4
4
 
5
5
  export default class MatrixColumnSelectionView extends BaseView {
6
6
  constructor(matrix, columnIndices) {
7
- columnIndices = checkColumnIndices(matrix, columnIndices);
7
+ checkColumnIndices(matrix, columnIndices);
8
8
  super(matrix, matrix.rows, columnIndices.length);
9
9
  this.columnIndices = columnIndices;
10
10
  }
@@ -4,7 +4,7 @@ import BaseView from './base';
4
4
 
5
5
  export default class MatrixRowSelectionView extends BaseView {
6
6
  constructor(matrix, rowIndices) {
7
- rowIndices = checkRowIndices(matrix, rowIndices);
7
+ checkRowIndices(matrix, rowIndices);
8
8
  super(matrix, rowIndices.length, matrix.columns);
9
9
  this.rowIndices = rowIndices;
10
10
  }
@@ -1,13 +1,14 @@
1
- import { checkIndices } from '../util';
1
+ import { checkRowIndices, checkColumnIndices } from '../util';
2
2
 
3
3
  import BaseView from './base';
4
4
 
5
5
  export default class MatrixSelectionView extends BaseView {
6
6
  constructor(matrix, rowIndices, columnIndices) {
7
- let indices = checkIndices(matrix, rowIndices, columnIndices);
8
- super(matrix, indices.row.length, indices.column.length);
9
- this.rowIndices = indices.row;
10
- this.columnIndices = indices.column;
7
+ checkRowIndices(matrix, rowIndices);
8
+ checkColumnIndices(matrix, columnIndices);
9
+ super(matrix, rowIndices.length, columnIndices.length);
10
+ this.rowIndices = rowIndices;
11
+ this.columnIndices = columnIndices;
11
12
  }
12
13
 
13
14
  set(rowIndex, columnIndex, value) {
package/src/wrap/wrap.js CHANGED
@@ -1,9 +1,11 @@
1
+ import { isAnyArray } from 'is-any-array';
2
+
1
3
  import WrapperMatrix1D from './WrapperMatrix1D';
2
4
  import WrapperMatrix2D from './WrapperMatrix2D';
3
5
 
4
6
  export function wrap(array, options) {
5
- if (Array.isArray(array)) {
6
- if (array[0] && Array.isArray(array[0])) {
7
+ if (isAnyArray(array)) {
8
+ if (array[0] && isAnyArray(array[0])) {
7
9
  return new WrapperMatrix2D(array);
8
10
  } else {
9
11
  return new WrapperMatrix1D(array, options);