-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,984 additions
and
1,464 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
if (!Symbol.species) { | ||
Symbol.species = Symbol.for('@@species'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @private | ||
* Check that a row index is not out of bounds | ||
* @param {Matrix} matrix | ||
* @param {number} index | ||
* @param {boolean} [outer] | ||
*/ | ||
exports.checkRowIndex = function checkRowIndex(matrix, index, outer) { | ||
var 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] | ||
*/ | ||
exports.checkColumnIndex = function checkColumnIndex(matrix, index, outer) { | ||
var 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 | ||
* @returns {Array} | ||
* @throws {RangeError} | ||
*/ | ||
exports.checkRowVector = 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 | ||
* @returns {Array} | ||
* @throws {RangeError} | ||
*/ | ||
exports.checkColumnVector = 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; | ||
}; | ||
|
||
exports.checkIndices = function checkIndices(matrix, rowIndices, columnIndices) { | ||
var rowOut = rowIndices.some(r => { | ||
return r < 0 || r >= matrix.rows; | ||
|
||
}); | ||
|
||
var columnOut = columnIndices.some(c => { | ||
return c < 0 || c >= matrix.columns; | ||
}); | ||
|
||
if (rowOut || columnOut) { | ||
throw new RangeError('Indices are out of range') | ||
} | ||
|
||
if (typeof rowIndices !== 'object' || typeof columnIndices !== 'object') { | ||
throw new TypeError('Unexpected type for row/column indices'); | ||
} | ||
if (!Array.isArray(rowIndices)) rowIndices = Array.from(rowIndices); | ||
if (!Array.isArray(columnIndices)) rowIndices = Array.from(columnIndices); | ||
|
||
return { | ||
row: rowIndices, | ||
column: columnIndices | ||
}; | ||
}; | ||
|
||
exports.checkRange = function checkRange(matrix, startRow, endRow, startColumn, endColumn) { | ||
if (arguments.length !== 5) throw new TypeError('Invalid argument type'); | ||
var notAllNumbers = Array.from(arguments).slice(1).some(function (arg) { | ||
return typeof arg !== 'number'; | ||
}); | ||
if (notAllNumbers) throw new TypeError('Invalid argument type'); | ||
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'); | ||
} | ||
}; | ||
|
||
exports.getRange = function getRange(from, to) { | ||
var arr = new Array(to - from + 1); | ||
for (var i = 0; i < arr.length; i++) { | ||
arr[i] = from + i; | ||
} | ||
return arr; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
var abstractMatrix = require('../abstractMatrix'); | ||
var Matrix; | ||
|
||
class BaseView extends abstractMatrix() { | ||
constructor(matrix, rows, columns) { | ||
super(); | ||
this.matrix = matrix; | ||
this.rows = rows; | ||
this.columns = columns; | ||
} | ||
|
||
static get [Symbol.species]() { | ||
if (!Matrix) { | ||
Matrix = require('../matrix'); | ||
} | ||
return Matrix; | ||
} | ||
} | ||
|
||
module.exports = BaseView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
var BaseView = require('./base'); | ||
|
||
class MatrixColumnView extends BaseView { | ||
constructor(matrix, column) { | ||
super(matrix, matrix.rows, 1); | ||
this.column = column; | ||
} | ||
|
||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(rowIndex, this.column, value); | ||
return this; | ||
} | ||
|
||
get(rowIndex, columnIndex) { | ||
return this.matrix.get(rowIndex, this.column); | ||
} | ||
} | ||
|
||
module.exports = MatrixColumnView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
var BaseView = require('./base'); | ||
|
||
class MatrixFlipColumnView extends BaseView { | ||
constructor(matrix) { | ||
super(matrix, matrix.rows, matrix.columns); | ||
} | ||
|
||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(rowIndex, this.columns - columnIndex - 1, value); | ||
return this; | ||
} | ||
|
||
get(rowIndex, columnIndex) { | ||
return this.matrix.get(rowIndex, this.columns - columnIndex - 1); | ||
} | ||
} | ||
|
||
module.exports = MatrixFlipColumnView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
var BaseView = require('./base'); | ||
|
||
class MatrixFlipRowView extends BaseView { | ||
constructor(matrix) { | ||
super(matrix, matrix.rows, matrix.columns); | ||
} | ||
|
||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(this.rows - rowIndex - 1, columnIndex, value); | ||
return this; | ||
} | ||
|
||
get(rowIndex, columnIndex) { | ||
return this.matrix.get(this.rows - rowIndex - 1, columnIndex); | ||
} | ||
} | ||
|
||
module.exports = MatrixFlipRowView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
var BaseView = require('./base'); | ||
|
||
class MatrixRowView extends BaseView { | ||
constructor(matrix, row) { | ||
super(matrix, 1, matrix.columns); | ||
this.row = row; | ||
} | ||
|
||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(this.row, columnIndex, value); | ||
return this; | ||
} | ||
|
||
get(rowIndex, columnIndex) { | ||
return this.matrix.get(this.row, columnIndex); | ||
} | ||
} | ||
|
||
module.exports = MatrixRowView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
var BaseView = require('./base'); | ||
var util = require('../util'); | ||
|
||
class MatrixSelectionView extends BaseView { | ||
constructor(matrix, rowIndices, columnIndices) { | ||
var indices = util.checkIndices(matrix, rowIndices, columnIndices); | ||
super(matrix, indices.row.length, indices.column.length); | ||
this.rowIndices = indices.row; | ||
this.columnIndices = indices.column; | ||
} | ||
|
||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(this.rowIndices[rowIndex], this.columnIndices[columnIndex] , value); | ||
return this; | ||
} | ||
|
||
get(rowIndex, columnIndex) { | ||
return this.matrix.get(this.rowIndices[rowIndex], this.columnIndices[columnIndex]); | ||
} | ||
} | ||
|
||
module.exports = MatrixSelectionView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
var BaseView = require('./base'); | ||
var util = require('../util'); | ||
|
||
class MatrixSubView extends BaseView { | ||
constructor(matrix, startRow, endRow, startColumn, endColumn) { | ||
util.checkRange(matrix, startRow, endRow, startColumn, endColumn); | ||
super(matrix, endRow - startRow + 1, endColumn - startColumn + 1); | ||
this.startRow = startRow; | ||
this.startColumn = startColumn; | ||
} | ||
|
||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(this.startRow + rowIndex, this.startColumn + columnIndex , value); | ||
return this; | ||
} | ||
|
||
get(rowIndex, columnIndex) { | ||
return this.matrix.get(this.startRow + rowIndex, this.startColumn + columnIndex); | ||
} | ||
} | ||
|
||
module.exports = MatrixSubView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
var BaseView = require('./base'); | ||
|
||
class MatrixTransposeView extends BaseView { | ||
constructor(matrix) { | ||
super(matrix, matrix.columns, matrix.rows); | ||
} | ||
|
||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(columnIndex, rowIndex, value); | ||
return this; | ||
} | ||
|
||
get(rowIndex, columnIndex) { | ||
return this.matrix.get(columnIndex, rowIndex); | ||
} | ||
} | ||
|
||
module.exports = MatrixTransposeView; |
Oops, something went wrong.