Skip to content

ahtohbi4/matrix-slicer

Repository files navigation

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:

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

  • 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.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:

  • 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.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:

  • 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.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:

  • 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.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:

  • 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.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:

  • 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 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 [email protected]

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published