Slices a matrices into columns, rows, diagonals and submatrices.
JavaScript utility for slicing a matrix into elements, columns, rows, diagonals and submatrices.
$ npm install matrix-slicer --save
$ yarn add matrix-slicer
$ bower install matrix-slicer --save
Install Bower package or just put the script in your project.
<script src="/lib/matrix-slicer/index.js"></script>
<!-- In case of Bower: <script src="/bower_components/matrix-slicer/index.js"></script> -->
<script>
var m = new Matrix([
[1, 2],
[3, 4],
]);
</script>
Install Bower package or just put the script in your project.
require([
'matrix-slicer',
], function (Matrix) {
var m = new Matrix([
[1, 2],
[3, 4],
]);
});
Install npm package and use it.
var Matrix = require('matrix-slicer');
var m = new Matrix([
[1, 2],
[3, 4],
]);
Install npm package and use it.
import Matrix from 'matrix-slicer';
const m = new Matrix([
[1, 2],
[3, 4],
]);
Syntax
new Matrix(matrix width, height[, element = 0 callback])
Where:
Example
// Regular matrix
const m = new Matrix([
['bird', 'dog'],
['cat', 'elephant'],
]); // => instance of matrix [['bird', 'dog'], ['cat', 'elephant']]
// By dimensions
const m = new Matrix(3, 2); // => instance of matrix [[0, 0, 0], [0, 0, 0]]
// By dimensions and filler
const m = new Matrix(2, 2, 'Foo'); // => instance of matrix [['Foo', 'Foo'], ['Foo', 'Foo']]
// By dimensions and callback function to generate elements
const m = new Matrix(2, 2, function (i, j, m, n, matrix) {
return i + j;
}); // => instance of matrix [[0, 1], [1, 2]]
Syntax
m.get()
Example
m.get(); // => [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]
Syntax
m.getElem(x, y)
Where:
m.getElems(fromX, fromY[, toX = <width>, toY = <height>])
Where:
Example
// Get an Element by coordinates (zero-based)
m.getElem(0, 1); // => 'D'
// Get a set of Elements
m.getElems(1, 0, 2, 1); // => ['B', 'C', 'D', 'E', 'F']
Syntax
m.getRow(y)
Where:
m.getRows(fromY[, toY = <height>])
Where:
Example
// Get a Row by index (zero-based)
m.getRow(1); // => ['D', 'E', 'F']
// Get a set of Rows
m.getRows(0, 2); // => [['A', 'B', 'C'], ['D', 'E', 'F']]
Syntax
m.getColumn(x)
Where:
m.getColumns(fromX[, toX = <width>])
Where:
Example
// Get a Column by index (zero-based)
m.getColumn(2); // => ['C', 'F', 'I']
// Get a set of Columns
m.getColumns(1, 3); // => [['B', 'E', 'H'], ['C', 'F', 'I']]
Syntax
m.getDiagonalMaj(index)
Where:
m.getDiagonalsMaj(fromIndex[, toIndex = <diagonals_ammount>])
Where:
Example
// Get a major Diagonal by index (zero-based)
m.getDiagonalMaj(1); // => ['B', 'F']
// Get a set of major Diagonals
m.getDiagonalsMaj(2, 4); // => [['A', 'E', 'I'], ['D', 'H']]
Syntax
m.getDiagonalMin(index)
Where:
m.getDiagonalsMin(fromIndex[, toIndex = <diagonals_ammount>])
Where:
Example
// Get a minor Diagonal by index (zero-based)
m.getDiagonalMin(4); // => ['I']
// Get a set of major Diagonals
m.getDiagonalsMin(-3, -1); // => [['B', 'D'], ['C', 'E', 'G']]
Syntax
m.getSubmatrix(fromX, fromY[, toX = <width>, toY = <height>])
Where:
Example
// Get a Submatrix
m.getSubmatrix(1, 1, 3, 3); // => [['E', 'F'], ['H', 'I']]
For more details see description of tests.
$ git clone https://github.com/ahtohbi4/matrix-slicer.git
$ cd matrix-slicer
$ npm install
$ npm test
MIT © Alexander Antonov alexandr-post@yandex.ru