The two-dimensional matrix resembling a Cartesian coordinate system.
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
// ]
// 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
// ]
npm i coord-matrix2d
or
<script type="module">
import { Matrix } from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/esm/index.js'
</script>
Creates a new matrix instance with same row and col size.
Create matrix instance from 2d-array data type.
Returns added result matrix between both matrix. It will returns a + b
.
Returns subtracted result matrix between both matrix. It will returns a - b
.
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.
Returns divided result matrix between both matrix. It will returns a / b
.
It's just a divide each element of matrix.
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.
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.
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.
Returns whether the matrix has same size. It calculates with row
and col
properties.
Matrix.GetLocalMatrix<T>
(source: Matrix<T>
, rowIndex: number
, colIndex: number
, row = 3
, col = 3
, fill: any
= null
): Matrix<T>
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.
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
// ]
Returns all elements in matrix's row vector as array.
Returns all elements in matrix's column vector as array.
Get elements index of matrix.elements
property with calculates row and column.
Sets element for matrix.
Returns row index of matrix from calculated with element index.
Returns column index of matrix of calculated with element index.
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.
if (matrix.reachable(rowIndex, colIndex)) {
const element = matrix.getElement(rowIndex, colIndex)
}
Returns element what you find in point of coordinates in matrix.
Fill matrix with a element.
It is useful with Matrix.Add
, Matrix.Sub
, Matrix.Mul
, Matrix.Div
like methods.
const mat = Matrix.Create(3, 3, [1,2,3,4,5,6,7,8,9])
const result = Matrix.Mul(mat, mat.clone.fill(3))
Returns all elements in matrix as 2d-array data type.
Returns a clone of matrix.
Returns a length of matrix. This follows the calculation of Euclidean norm.
The row size of matrix
The column size of matrix.
The elements length of matrix. It's NOT magnitude of matrix.
All of matrix's elements.
MIT license