matrix-slicer

Slices a matrices into columns, rows, diagonals and submatrices.

View the Project on GitHub

matrix-slicer

Bower version npm version devDependency Status Travis Build Status Appveyor Build Status Coverage Status

JavaScript utility for slicing a matrix into elements, columns, rows, diagonals and submatrices.

  1. Installation
  2. Usage
  3. Methods
  4. Test
  5. License

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.

<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>

AMD

Install Bower package or just put the script in your project.

require([
    'matrix-slicer',
], function (Matrix) {
    var m = new Matrix([
        [1, 2],
        [3, 4],
    ]);
});

CommonJS

Install npm package and use it.

var Matrix = require('matrix-slicer');

var m = new Matrix([
    [1, 2],
    [3, 4],
]);

ES6

Install npm package and use it.

import Matrix from 'matrix-slicer';

const m = new Matrix([
    [1, 2],
    [3, 4],
]);

Methods

1. Creating instance of the Matrix

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]]

2. Get Matrix

Syntax

m.get()

Example

m.get(); // => [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]

3. Get Element(s)

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']

4. Get Row(s)

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']]

5. Get Column(s)

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']]

6. Get Major Diagonal(s)

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']]

7. Get Minor Diagonal(s)

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']]

8. Get Submatrix

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.

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