universal-matrix 中文文档教程
矩阵
安装
$ npm i universal-matrix --save
使用
import Matrix from 'universal-matrix';
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
const Matrix = require('universal-matrix');
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
API
所有方法都是可链接的,并且直接作用于矩阵数据(clone
除外)。
[静态] Matrix.zero(rows[, cols])
创建大小为 rows * cols
的零矩阵,或大小为 rows * rows< 的方阵/代码>。
参数
- rows {Number} :行数
- cols {Number} (可选):列数(如果未指定则等于行)
返回
- {Matrix}:大小为
行*列
的零矩阵。
示例
const square = Matrix.zero(3);
// 0 0 0
// 0 0 0
// 0 0 0
const matrix = Matrix.zero(2, 4);
// 0 0 0 0
// 0 0 0 0
[静态] Matrix.identity(n)
创建大小为n * n
的单位矩阵。
参数
- n {Number} :单位矩阵的大小
返回
- {Matrix} :大小为
n
。
示例
const matrix = Matrix.identity(3);
// 1 0 0
// 0 1 0
// 0 0 1
[静态] Matrix.randomize(matrix[[, min][, max]])
随机化给定的矩阵数据。
参数
- matrix {Matrix} :随机化的矩阵
- min {Number} (可选):最小值 < em>(默认
0
) - max {Number}(可选):最大值(默认
1
)
返回
- {Matrix} :方法链接的矩阵实例
示例
const matrices = [
// random matrix of size 3 x 3, with values between [0, 1[
Matrix.randomize(Matrix.zero(3)),
// random matrix of size 3 x 3, with values between [0, 10[
Matrix.randomize(Matrix.zero(3), 10),
// random matrix of size 3 x 3, with values between [-3, 5[
Matrix.randomize(Matrix.zero(3), -3, 5),
];
[静态] Matrix.determinant(matrix)
返回矩阵行列式。给定的矩阵必须是方阵,并且包含 至少 2 行。
参数
- matrix {Matrix}:计算行列式的矩阵
返回
- {Number}:行列式
示例
const matrix = new Matrix([6, 1, 1], [4, -2, 5], [2, 8, 7]);
Matrix.determinant(matrix);
// -306
[构造函数] Matrix(...rows)
使用给定的行值创建一个新矩阵。每行必须相同 大小,否则会抛出错误。
参数
- …rows {Array} :标识矩阵每一行的数组列表
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
// 1 2 3
// 4 5 6
// 7 8 9
matrix.rows;
// 3
matrix.columns;
// 3
matrix.data;
// [1, 2, 3, 4, 5, 6, 7, 8, 9];
matrix.get(row, col)
从矩阵中获取值。行和列都是从 0 开始的。
参数
- 行
- row {Number} :从
0
到matrix.rows - 1
col 的 strong> {Number} :从0
到matrix.columns - 1
的列
返回
- {Number} :矩阵值
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.get(2, 0);
// 7
matrix.getRow(row)
从矩阵中获取整行。行是从 0 开始的。
参数
- row {Number} :从
0
到matrix.rows - 1
的行
返回
- {Array } :矩阵行
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.getRow(2);
// [7 8 9]
matrix.getColumn(col)
从矩阵中获取整个列。这些列是从 0 开始的。
参数
- col {Number} :从
0
到matrix.columns - 1
的列
返回
- {Array } :矩阵列
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.getColumn(2);
// [3, 6, 9]
matrix.set(row, col, value)
设置矩阵的值。行和列都是从 0 开始的。
参数
- 行
- row {Number} :从
0
到matrix.rows - 1
col 的 strong> {Number} :从0
到matrix.columns - 1
- 值 {Number }:要设置的值
返回
- {Matrix}:用于方法链接的矩阵实例
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.set(2, 1, -88);
// 1 2 3
// 4 5 6
// 7 -88 9
matrix.setRow(row, data)
设置矩阵的整行。行是从 0 开始的。
参数
- 行
- row {Number} :从
0
到matrix.rows - 1
data 的 strong> {Array} :与matrix.columns
长度相同的数据数组
返回
- {Matrix} :用于方法链接的矩阵实例
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.setRow(2, [-9, -8, -7]);
// 1 2 3
// 4 5 6
// -9 -8 -7
matrix.setColumn(col, data)
设置矩阵的整个列。这些列是从 0 开始的。
参数
- col {Number} :从
0
到matrix.columns - 1
- data 的 col strong> {Array} :与
matrix.rows
长度相同的数据数组
返回
- {Matrix} :用于方法链接的矩阵实例
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.setColumn(2, [11, 12, 13]);
// 1 2 11
// 4 5 12
// 7 8 13
matrix.apply(fn)
将给定函数应用于矩阵的给定值。该函数将 接收三 (3) 个参数:
- val {Number} :将函数应用到
- row {Number} 的 矩阵值em> :矩阵中的当前行
- col {Number} :矩阵中的当前列 参数
- fn {Function}< /em>:调用每个值的函数
返回
- {Matrix}:用于方法链接的矩阵实例
示例
const matrix = new Matrix([1, 2], [3, 4]);
// matrix.apply(function (val) { return val * 2; });
matrix.apply(val => val * 2);
// 2 4
// 6 8
matrix.add(n|matrix)
添加一个标量值n
到每个矩阵元素,或将两个矩阵加在一起。 对于矩阵加法,两者必须具有相同的行
数和列
数。
参数
- n {Number} | matrix {Matrix}:添加到当前矩阵元素的标量或矩阵
返回
- {Matrix}:用于方法链接的矩阵实例示例
矩阵
const matrix = new Matrix([-6, 2], [4, 0]);
matrix.add(-3);
// -9 -1
// 1 -3
matrix.add(new Matrix([0, 1], [-1, 0]));
// -9 0
// 0 -3
。 multiply(n|matrix)
与给定标量n或指定矩阵相乘。对于矩阵乘法, 每个矩阵的行和列必须兼容。
参数
- n {Number} | matrix {Matrix}:与当前矩阵元素相乘的标量或矩阵
返回
- {Matrix}:用于方法链接的矩阵实例示例
矩阵
const matrix = new Matrix([2, 3, 4], [1, 0, 0]);
matrix.multiply(3);
// 6 9 12
// 3 0 0
matrix.multiply(new Matrix([0, 1000], [1, 100], [0, 10]));
// 9 7020
// 0 3000
。 mergeRows(removeRow, addToRow)
获取一行并将其合并(添加)到另一行。所有行都是从零开始的,这意味着 第一行位于位置 0
,最后一行位于位置 rows - 1
参数
- removeRow {Number} :要从矩阵中删除的行
- addToRow {Number} :将删除的行添加到的行
Return
- {Matrix} :矩阵方法链接的实例 示例
matrix.multiplyRow
const a = new Matrix([1, 2, 3], [0, 1, -6], [8, 2, 1]);
// merge the second row into the third row
a.mergeRows(1, 2);
// 1 2 3
// 8 3 -5
a.mergeRows(1, 0);
// 9 5 -2
(row, multiplier)
仅将单个行乘以标量。
参数
- row {Number} :要相乘的行
- n {Number} :要与该行相乘的标量值with
Return
- {Matrix} :用于方法链接的矩阵实例
示例
const matrix = new Matrix([1, 2, 3], [0, 0, 7]);
matrix.multiplyRow(1, 1 / 7);
// 1 2 3
// 0 0 1
matrix.removeRow(row)
从矩阵中删除整行。
参数
- row {Number} :要删除的行
返回
- {Matrix} :用于方法链接的矩阵实例 示例
matrix.removeColumn
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.removeRow(0);
// 4 5 6
// 7 8 9
(col)
删除一个矩阵的整个列。
参数
- col {Number} :要删除的列
返回
- {Matrix} :用于方法链接的矩阵实例
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.removeColumn(1);
// 1 3
// 4 6
// 7 9
matrix.transpose()
转置矩阵侧面,交换行和列。
返回
- {Matrix}:方法链接的矩阵实例
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6]);
matrix.transpose();
// 1 4
// 2 5
// 3 6
matrix.inverse()
返回逆矩阵。矩阵必须是方阵,并且包含 至少 2 行。
返回
- {Matrix}:用于方法链接的矩阵实例示例
matrix.clone
const matrix = new Matrix([3, 0, 2], [2, 0, -2], [0, 1, 1]);
matrix.inverse();
// 0.2 0.2 0
// -0.2 0.3 1
// 0.2 -0.3 0
matrix.inverse();
// 3 0 2
// 2 0 -2
// 0 1 1
()
因为所有矩阵运算都直接对数据进行操作,所以某些情况可能会 需要在不同的实例上工作以保留原始数据。
返回
- {Matrix}:方法链接的矩阵实例
示例
const matrix = new Matrix([1, 2, 3], [4, 5, 6]);
const copy = matrix.clone();
copy.multiply(-3);
// -3 -6 -9
// -12 -15 -18
matrix.add(1);
// 2 3 4
// 5 6 7
许可
MIT
Matrix
Installation
$ npm i universal-matrix --save
Usage
import Matrix from 'universal-matrix';
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
const Matrix = require('universal-matrix');
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
API
All methods are chainable and work directly on the matrix data (except for clone
).
[static] Matrix.zero(rows[, cols])
Create a zero matrix of size rows * cols
, or a square matrix of size rows * rows
.
Arguments
- rows {Number} : the number or rows
- cols {Number} (optional) : the number of columns (equals to rows if not specified)
Return
- {Matrix} : a zero matrix of size
rows * columns
.
Example
const square = Matrix.zero(3);
// 0 0 0
// 0 0 0
// 0 0 0
const matrix = Matrix.zero(2, 4);
// 0 0 0 0
// 0 0 0 0
[static] Matrix.identity(n)
Create an identity matrix of size n * n
.
Arguments
- n {Number} : the size of the identity matrix
Return
- {Matrix} : an identity matrix of size
n
.
Example
const matrix = Matrix.identity(3);
// 1 0 0
// 0 1 0
// 0 0 1
[static] Matrix.randomize(matrix[[, min][, max]])
Randomize the given matrix data.
Arguments
- matrix {Matrix} : the matrix to randomize
- min {Number} (optional) : the minimum value (default
0
) - max {Number} (optional) : the maximum value (default
1
)
Return
- {Matrix} : matrix instance for method chaining
Example
const matrices = [
// random matrix of size 3 x 3, with values between [0, 1[
Matrix.randomize(Matrix.zero(3)),
// random matrix of size 3 x 3, with values between [0, 10[
Matrix.randomize(Matrix.zero(3), 10),
// random matrix of size 3 x 3, with values between [-3, 5[
Matrix.randomize(Matrix.zero(3), -3, 5),
];
[static] Matrix.determinant(matrix)
Return the matrix determinant. The given matrix must be square, and contain at least 2 rows.
Arguments
- matrix {Matrix} : the matrix to calculate the determinant for
Return
- {Number} : the determinant
Example
const matrix = new Matrix([6, 1, 1], [4, -2, 5], [2, 8, 7]);
Matrix.determinant(matrix);
// -306
[constructor] Matrix(…rows)
Create a new matrix with the given row values. Every row must be of same size, otherwise an error will be thrown.
Arguments
- …rows {Array} : a list of arrays identifying each rows of the Matrix
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
// 1 2 3
// 4 5 6
// 7 8 9
matrix.rows;
// 3
matrix.columns;
// 3
matrix.data;
// [1, 2, 3, 4, 5, 6, 7, 8, 9];
matrix.get(row, col)
Get the value from the matrix. The rows and columns are 0-based.
arguments
- row {Number} : the row from
0
tomatrix.rows - 1
- col {Number} : the column from
0
tomatrix.columns - 1
Return
- {Number} : the matrix value
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.get(2, 0);
// 7
matrix.getRow(row)
Get the entire row from the matrix. The rows are 0-based.
arguments
- row {Number} : the row from
0
tomatrix.rows - 1
Return
- {Array} : the matrix row
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.getRow(2);
// [7 8 9]
matrix.getColumn(col)
Get the entire column from the matrix. The columns are 0-based.
arguments
- col {Number} : the col from
0
tomatrix.columns - 1
Return
- {Array} : the matrix column
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.getColumn(2);
// [3, 6, 9]
matrix.set(row, col, value)
Set the value of the matrix. The rows and columns are 0-based.
arguments
- row {Number} : the row from
0
tomatrix.rows - 1
- col {Number} : the column from
0
tomatrix.columns - 1
- value {Number} : the value to set
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.set(2, 1, -88);
// 1 2 3
// 4 5 6
// 7 -88 9
matrix.setRow(row, data)
Set the entire row of the matrix. The rows are 0-based.
arguments
- row {Number} : the row from
0
tomatrix.rows - 1
- data {Array} : an array of data having the same length as
matrix.columns
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.setRow(2, [-9, -8, -7]);
// 1 2 3
// 4 5 6
// -9 -8 -7
matrix.setColumn(col, data)
Set the entire column of the matrix. The columns are 0-based.
arguments
- col {Number} : the col from
0
tomatrix.columns - 1
- data {Array} : an array of data having the same length as
matrix.rows
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.setColumn(2, [11, 12, 13]);
// 1 2 11
// 4 5 12
// 7 8 13
matrix.apply(fn)
Apply a given function to the given values of the matrix. The function will receive three (3) arguments:
- val {Number} : the matrix value to apply the function to
- row {Number} : the current row in the Matrix
- col {Number} : the current column in the matrix
Arguments
- fn {Function} : the function to call on each value
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([1, 2], [3, 4]);
// matrix.apply(function (val) { return val * 2; });
matrix.apply(val => val * 2);
// 2 4
// 6 8
matrix.add(n|matrix)
Add a scalar value n
to every matrix element, or add two matrices together. For matrix additions, both must have the same number of rows
and columns
.
Arguments
- n {Number} | matrix {Matrix} : a scalar or matrix to add to the current matrix elements
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([-6, 2], [4, 0]);
matrix.add(-3);
// -9 -1
// 1 -3
matrix.add(new Matrix([0, 1], [-1, 0]));
// -9 0
// 0 -3
matrix.multiply(n|matrix)
Multiply with a given scalar n
or a specified matrix. For matrix multiplication, both rows and columns from each matrices must be compatible.
Arguments
- n {Number} | matrix {Matrix} : a scalar or matrix to multiply to the current matrix elements
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([2, 3, 4], [1, 0, 0]);
matrix.multiply(3);
// 6 9 12
// 3 0 0
matrix.multiply(new Matrix([0, 1000], [1, 100], [0, 10]));
// 9 7020
// 0 3000
matrix.mergeRows(removeRow, addToRow)
Take one row and merge (add) it to another row. All rows are zero-based, meaning that the first row is at position 0
, and the last row is at position rows - 1
Arguments
- removeRow {Number} : the row to remove from the matrix
- addToRow {Number} : the row to add the removed row to
Return
- {Matrix} : matrix instance for method chaining
Example
const a = new Matrix([1, 2, 3], [0, 1, -6], [8, 2, 1]);
// merge the second row into the third row
a.mergeRows(1, 2);
// 1 2 3
// 8 3 -5
a.mergeRows(1, 0);
// 9 5 -2
matrix.multiplyRow(row, multiplier)
Multiply only a single row by a scalar.
Arguments
- row {Number} : the row to multiply
- n {Number} : the scalar value to multiply the row with
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([1, 2, 3], [0, 0, 7]);
matrix.multiplyRow(1, 1 / 7);
// 1 2 3
// 0 0 1
matrix.removeRow(row)
Delete an entire row from the Matrix.
Arguments
- row {Number} : the row to remove
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.removeRow(0);
// 4 5 6
// 7 8 9
matrix.removeColumn(col)
Delete an entire column from the Matrix.
Arguments
- col {Number} : the column to remove
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.removeColumn(1);
// 1 3
// 4 6
// 7 9
matrix.transpose()
Transpose the matrix side ways, swaping rows and columns.
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6]);
matrix.transpose();
// 1 4
// 2 5
// 3 6
matrix.inverse()
Return the inverse matrix. The matrix must be square, and contain at least 2 rows.
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([3, 0, 2], [2, 0, -2], [0, 1, 1]);
matrix.inverse();
// 0.2 0.2 0
// -0.2 0.3 1
// 0.2 -0.3 0
matrix.inverse();
// 3 0 2
// 2 0 -2
// 0 1 1
matrix.clone()
Because all matrix operations operate directly on the data, some cases may require working on a different instance to preserve original data.
Return
- {Matrix} : matrix instance for method chaining
Example
const matrix = new Matrix([1, 2, 3], [4, 5, 6]);
const copy = matrix.clone();
copy.multiply(-3);
// -3 -6 -9
// -12 -15 -18
matrix.add(1);
// 2 3 4
// 5 6 7
License
MIT