The two-dimensional matrix resembling a Cartesian coordinate system
npm install coord-matrix2dtypescript
import { Matrix } from 'coord-matrix2d'
const matrix = Matrix.Create(3, 3, [
1,2,3,
4,5,6,
7,8,9
])
matrix.elements // [1,2,3,4,5,6,7,8,9]
matrix.size // 9
const eight = matrix.getElement(3, 2) // 8
const outOfRange = matrix.getElement(5, 5) // error!
const sourceMatrix = Matrix.Create(5, 5, [
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20
])
const center = sourceMatrix.getElement(3, 4) // 14
// Gets the elements around a given row and column and returns the matrix.
const localMatrix = Matrix.GetLocalMatrix(sourceMatrix, 3, 4)
localMatrix
// [
// 8, 9, 10,
// 13, 14, 15,
// 18, 19, 20
// ]
`
`typescript
// Matrix scalar product
import { Matrix } from 'coord-matrix2d'
const matrix = Matrix.Create(3, 3, [
1, 1, 1,
2, 2, 2,
3, 3, 3
])
const scalar3 = matrix.clone.fill(3) // create same sized matrix and fill all to 3.
const result = Matrix.Mul(matrix, scalar3)
// [
// 3, 3, 3,
// 6, 6, 6,
// 9, 9, 9
// ]
`
Install
`bash
npm i coord-matrix2d
`
or
`html
`
Matrix Static functions
$3
Creates a new matrix instance with same row and col size.
$3
Create matrix instance from 2d-array data type.
$3
Returns added result matrix between both matrix. It will returns a + b.
$3
Returns subtracted result matrix between both matrix. It will returns a - b.
$3
Returns multiplied result matrix between both matrix. It will returns a * b.
WARNING! This method is not product matrix. It's just a multiply each element of matrix.
If you want to product matrix, use Prod method.
$3
Returns divided result matrix between both matrix. It will returns a / b.
It's just a divide each element of matrix.
$3
Returns multiplied result matrix between both matrix.
The matrix product must be same a.col and b.row size. If not, it will throw an error.
$3
Returns the scalar product results of two matrices.
WARNING! This does NOT distinguish between the rows and columns of the matrix. List all the elements of the matrix and convert it to a vector, Returns the value that adds all the elements.
$3
Calculate and return cosine similarity between the two matrices.
WARNING! This does NOT distinguish between the rows and columns of the matrix. List all the elements of the matrix and convert it to a vector, then compare the similarity.
$3
Returns whether the matrix has same size. It calculates with row and col properties.
$3
Get new matrix from part of source matrix. It returns neighbor elements from point of coordinates of source matrix.
The size of new matrix is multiple of row and column of arguments.
`typescript
const sourceMatrix = Matrix.Create(5, 5, [
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20
])
const center = sourceMatrix.getElement(3, 4) // 14
// Gets the elements around a given row and column and returns the matrix.
const localMatrix = Matrix.GetLocalMatrix(sourceMatrix, 3, 4)
localMatrix.elements
// [
// 8, 9, 10,
// 13, 14, 15,
// 18, 19, 20
// ]
`
Instance methods
$3
Returns all elements in matrix's row vector as array.
$3
Returns all elements in matrix's column vector as array.
$3
Get elements index of matrix.elements property with calculates row and column.
$3
Sets element for matrix.
$3
Returns row index of matrix from calculated with element index.
$3
Returns column index of matrix of calculated with element index.
$3
Returns whether the positions of rows and columns are within the range of the current matrix.
If the position is within the range of the matrix, it returns true. Otherwise, it returns false.
You can use this method to get element safely.
`typescript
if (matrix.reachable(rowIndex, colIndex)) {
const element = matrix.getElement(rowIndex, colIndex)
}
`
$3
Returns element what you find in point of coordinates in matrix.
$3
Fill matrix with a element.
It is useful with Matrix.Add, Matrix.Sub, Matrix.Mul, Matrix.Div like methods.
`typescript
const mat = Matrix.Create(3, 3, [1,2,3,4,5,6,7,8,9])
const result = Matrix.Mul(mat, mat.clone.fill(3))
``