matrix-slicer
JavaScript utility for slicing a matrix into elements, columns, rows, diagonals and submatrices.
Installation
Npm
$ npm install matrix-slicer --save
Yarn
$ yarn add matrix-slicer
Bower
$ bower install matrix-slicer --save
Usage
Javascript
Install Bower package or just put the script in your project.
<!-- In case of Bower: <script src="/bower_components/matrix-slicer/index.js"></script> -->
AMD
Install Bower package or just put the script in your project.
;
CommonJS
Install npm package and use it.
var Matrix = ; var m = 1 2 3 4;
ES6
Install npm package and use it.
; const m = 1 2 3 4;
Methods
1. Creating instance of the Matrix
Syntax
new Matrix(matrix | width, height[, element = 0 | callback])
Where:
- matrix - regular matrix (an array of arrays with similar lengths);
- width - number of columns;
- height - number of rows;
- element - an element with which the matrix will be filled. By default, this is 0;
- callback - function that produces an element of the matrix, taking three arguments:
- i - index (zero-based) of the column of generated the element;
- j - index (zero-based) of the row of generated the element;
- m - the width of generated matrix was passed earlier;
- n - the height of generated matrix was passed earlier;
- matrix - the matrix with previously generated elements.
Example
// Regular matrixconst m = 'bird' 'dog' 'cat' 'elephant'; // => instance of matrix [['bird', 'dog'], ['cat', 'elephant']] // By dimensionsconst m = 3 2; // => instance of matrix [[0, 0, 0], [0, 0, 0]] // By dimensions and fillerconst m = 2 2 'Foo'; // => instance of matrix [['Foo', 'Foo'], ['Foo', 'Foo']] // By dimensions and callback function to generate elementsconst m = 2 2 { return i + j;}; // => instance of matrix [[0, 1], [1, 2]]
2. Get Matrix
Syntax
m.get()
Example
m; // => [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]
3. Get Element(s)
Syntax
m.getElem(x, y)
Where:
- x - index (zero-based) of the column. If it is negative, the coordinate calculated from the end (width + x).
- y - index (zero-based) of the row. If it is negative, the coordinate calculated from the end (height + y).
m.getElems(fromX, fromY[, toX = <width>, toY = <height>])
Where:
- fromX - index (zero-based) of the column of start element. If it is negative, the coordinate calculated from the end (width + fromX).
- fromY - index (zero-based) of the row of start element. If it is negative, the coordinate calculated from the end (height + fromY).
- toX - index (zero-based) of the column till which extraction will be going. If it is negative, the coordinate calculated from the end (width + toX).
- toY - index (zero-based) of the row till which extraction will be going. If it is negative, the coordinate calculated from the end (height + toY).
Example
// Get an Element by coordinates (zero-based)m; // => 'D' // Get a set of Elementsm; // => ['B', 'C', 'D', 'E', 'F']
4. Get Row(s)
Syntax
m.getRow(y)
Where:
- y - index (zero-based) of the row. If it is negative, the coordinate calculated from the end (height + y).
m.getRows(fromY[, toY = <height>])
Where:
- fromY - index (zero-based) of the start row. If it is negative, the coordinate calculated from the end (height + fromY).
- toY - index (zero-based) of the row till which extraction will be going. If it is negative, the coordinate calculated from the end (height + toY).
Example
// Get a Row by index (zero-based)m; // => ['D', 'E', 'F'] // Get a set of Rowsm; // => [['A', 'B', 'C'], ['D', 'E', 'F']]
5. Get Column(s)
Syntax
m.getColumn(x)
Where:
- x - index (zero-based) of the column. If it is negative, the coordinate calculated from the end (width + x).
m.getColumns(fromX[, toX = <width>])
Where:
- fromX - index (zero-based) of the start column. If it is negative, the coordinate calculated from the end (width + fromX).
- toX - index (zero-based) of the column till which extraction will be going. If it is negative, the coordinate calculated from the end (width + toX).
Example
// Get a Column by index (zero-based)m; // => ['C', 'F', 'I'] // Get a set of Columnsm; // => [['B', 'E', 'H'], ['C', 'F', 'I']]
6. Get Major Diagonal(s)
Syntax
m.getDiagonalMaj(index)
Where:
- index - index (zero-based) of the diagonal. If it is negative, the coordinate calculated from the end (diagonals_amount + index).
m.getDiagonalsMaj(fromIndex[, toIndex = <diagonals_ammount>])
Where:
- fromIndex - index (zero-based) of the start major diagonal. If it is negative, the value calculated from the end (diagonals_amount + fromIndex).
- toIndex - index (zero-based) of the major diagonal till which extraction will be going. If it is negative, the value calculated from the end (diagonals_amount + toIndex).
Example
// Get a major Diagonal by index (zero-based)m; // => ['B', 'F'] // Get a set of major Diagonalsm; // => [['A', 'E', 'I'], ['D', 'H']]
7. Get Minor Diagonal(s)
Syntax
m.getDiagonalMin(index)
Where:
- index - index (zero-based) of the diagonal. If it is negative, the coordinate calculated from the end (diagonals_amount + index).
m.getDiagonalsMin(fromIndex[, toIndex = <diagonals_ammount>])
Where:
- fromIndex - index (zero-based) of the start minor diagonal. If it is negative, the value calculated from the end (diagonals_amount + fromIndex).
- toIndex - index (zero-based) of the minor diagonal till which extraction will be going. If it is negative, the value calculated from the end (diagonals_amount + toIndex).
Example
// Get a minor Diagonal by index (zero-based)m; // => ['I'] // Get a set of major Diagonalsm; // => [['B', 'D'], ['C', 'E', 'G']]
8. Get Submatrix
Syntax
m.getSubmatrix(fromX, fromY[, toX = <width>, toY = <height>])
Where:
- fromX - index (zero-based) of the column of start element. If it is negative, the coordinate calculated from the end (width + fromX).
- fromY - index (zero-based) of the row of start element. If it is negative, the coordinate calculated from the end (height + fromY).
- toX - index (zero-based) of the column till which extraction will be going. If it is negative, the coordinate calculated from the end (width + toX).
- toY - index (zero-based) of the row till which extraction will be going. If it is negative, the coordinate calculated from the end (height + toY).
Example
// Get a Submatrixm; // => [['E', 'F'], ['H', 'I']]
For more details see description of tests.
Test
$ git clone https://github.com/ahtohbi4/matrix-slicer.git$ cd matrix-slicer$ npm install$ npm test
License
MIT © Alexander Antonov alexandr-post@yandex.ru