This is an npm package called "MatrixMath". This package was written a long time ago, when typescript was not yet created. However, today, this package is used in a project where typescript is used, and this package is missing the `matrixMath.d.ts` file. Produce the data type typescript file for the package:

Usage
-----

The module is created in the CommonJS format, exporting using module.exports, meaning you need a CommonJS require system to use it. It is published in npm, so including it in node.js is simple:

Install it in the command line:

```
npm install matrixmath

```

Then include the module in your code:

```
var Matrix = require('matrixmath/Matrix');

```

[](https://www.npmjs.com/package/matrixmath#api)API
---------------------------------------------------

### [](https://www.npmjs.com/package/matrixmath#overview)Overview

Constructor

- new Matrix(opt\_rows, opt\_cols, opt\_setInitial)

Instance properties

- rows
- cols
- length

Static methods

- add (matrix, matrix1[,...matrixN])
- subtract (matrix, matrix1[,...matrixN])
- multiply (matrix, matrix1[,...matrixN])
- divide (matrix, matrix1[,...matrixN])

Instance methods

- setIdentityData ()
- setEmptyData ()
- setData (data, opt\_rows, opt\_cols)
- getData ()
- toArray ()
- toLogString ()
- copy (matrix)
- clone ()
- add (matrix[,...matrixN])
- subtract (matrix[,...matrixN])
- multiply (matrix[,...matrixN])
- divide (matrix[,...matrixN])
- power (number)
- transpose ()
- invert ()
- getDeterminant ()
- equals (input)
- isIdentity ()

### [](https://www.npmjs.com/package/matrixmath#constructor)Constructor

Create a new Matrix instance by using the constructor:

#### [](https://www.npmjs.com/package/matrixmath#new-matrix)new Matrix()

```
var matrix = new Matrix();

```

#### [](https://www.npmjs.com/package/matrixmath#new-matrixopt\_rows-opt\_cols)new Matrix(opt\_rows, opt\_cols)

```
var matrix = new Matrix(3, 3);

```

```
> console.log(matrix.toLogString());
[
 1 0 0
 0 1 0
 0 0 1
]

```

#### [](https://www.npmjs.com/package/matrixmath#new-matrixopt\_rows-opt\_cols-opt\_setinitial)new Matrix(opt\_rows, opt\_cols, opt\_setInitial)

```
var matrix = new Matrix(3, 3, false);

```

```
> console.log(matrix.toLogString());
[
 undefined undefined undefined
 undefined undefined undefined
 undefined undefined undefined
]

```

\* \* \* \* \*

### [](https://www.npmjs.com/package/matrixmath#instance-properties)Instance properties

#### [](https://www.npmjs.com/package/matrixmath#matrixrows)matrix.rows

The rows property contains the number of rows in the matrix.

```
> console.log(matrix.rows);
3

```

#### [](https://www.npmjs.com/package/matrixmath#matrixcols)matrix.cols

The cols property contains the number of columns in the matrix.

```
> console.log(matrix.cols);
3

```

#### [](https://www.npmjs.com/package/matrixmath#matrixlength)matrix.length

The length property contains the number of values in the matrix.

```
> console.log(matrix.length);
9

```

\* \* \* \* \*

### [](https://www.npmjs.com/package/matrixmath#static-methods)Static methods

The following methods will return a new Matrix instance.

#### [](https://www.npmjs.com/package/matrixmath#matrixaddmatrix1-matrix2matrixn)Matrix.add(matrix1, matrix2[,...matrixN])

Creates a new matrix with the sum of the passed in matrices. All matrices must have the same size. If a matrix is found that is not the same size, it is skipped.

```
var matrix = Matrix.add(matrix1, matrix2);

```

#### [](https://www.npmjs.com/package/matrixmath#matrixsubtractmatrix1-matrix2matrixn)Matrix.subtract(matrix1, matrix2[,...matrixN])

Creates a new matrix with the difference of the passed in matrices. All matrices must have the same size. If a matrix is found that is not the same size, it is skipped.

```
var matrix = Matrix.subtract(matrix1, matrix2);

```

#### [](https://www.npmjs.com/package/matrixmath#matrixmultiplymatrix1-matrix2matrixn)Matrix.multiply(matrix1, matrix2[,...matrixN])

Creates a new matrix with the product of the passed in matrices. All matrices must either be square and of the same size, or the next matrix in line must have the same number of rows as the number of columns in the previous result.

The first argument must be a matrix instance, but the rest can be either matrices or numbers. If a number is seen, a scalar multiplication is made.

```
var matrix = Matrix.multiply(matrix1, matrix2);
// or
var matrix = Matrix.multiply(matrix1, 3);

```

#### [](https://www.npmjs.com/package/matrixmath#matrixdividematrix1-matrix2matrixn)Matrix.divide(matrix1, matrix2[,...matrixN])

Creates a new matrix with the quotient of the passed in matrices. All matrices must be square and of the same size.

```
var matrix = Matrix.divide(matrix1, matrix2);

```

\* \* \* \* \*

### [](https://www.npmjs.com/package/matrixmath#instance-methods)Instance methods

The following methods are available on all Matrix instances.

#### [](https://www.npmjs.com/package/matrixmath#matrixsetidentitydata)matrix.setIdentityData()

Set the data in the matrix to the identity matrix.

```
var matrix = new Matrix(3, 3, false).setIdentityData();

```

```
> console.log(matrix.toLogString());
[
 1 0 0
 0 1 0
 0 0 1
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixsetemptydata)matrix.setEmptyData()

Set the data in the matrix to be only zeros.

```
var matrix = new Matrix(3, 3, false).setEmptyData();

```

```
> console.log(matrix.toLogString());
[
 0 0 0
 0 0 0
 0 0 0
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixsetdatadata-opt\_rows\_-opt\_cols)matrix.setData(data, opt\_rows\_ opt\_cols)

Set the data in the matrix to the passed in data.

```
var matrix = new Matrix(3, 3);
matrix.setData([1, 2, 3, 4], 2, 2);

```

```
> console.log(matrix.toLogString());
[
 1 2
 3 4
]

```

```
var matrix = new Matrix(3, 3);
matrix.setData([1, 2, 3, 4, 5, 6, 7, 8, 9]);

```

```
> console.log(matrix.toLogString());
[
 1 2 3
 4 5 6
 7 8 9
]

```

```
var matrix = new Matrix(3, 3);
matrix.setData(1, 2, 3, 4, 5, 6, 7, 8, 9);

```

```
> console.log(matrix.toLogString());
[
 1 2 3
 4 5 6
 7 8 9
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixgetdata)matrix.getData()

Get the data in the matrix as an array with extra data.

```
var matrix = new Matrix(2, 2);

```

```
> console.log(matrix.getData());
[1, 0, 0, 1]
> console.log(matrix.getData().rows);
2
> console.log(matrix.getData().cols);
2

```

#### [](https://www.npmjs.com/package/matrixmath#matrixtoarray)matrix.toArray()

Get the data in the matrix as an array.

```
var matrix = new Matrix(2, 2);

```

```
> console.log(matrix.toArray());
[1, 0, 0, 1]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixtologstringopt\_indentation-opt\_separator-opt\_start-opt\_end)matrix.toLogString([opt\_indentation[, opt\_separator[, opt\_start[, opt\_end]]]])

Get the data for this matrix as a formatted string, which is useful for logging and debugging. It will be formatted with line breaks to visualize the rows and columns.

```
var matrix = new Matrix(3, 3);

```

```
> console.log(matrix.toLogString());
[
 1 0 0
 0 1 0
 0 0 1
]

```

```
> console.log(matrix.toLogString(5));
[
 1 0 0
 0 1 0
 0 0 1
]

```

```
> console.log(matrix.toLogString(' ', ' | '));
[
 1 | 0 | 0
 0 | 1 | 0
 0 | 0 | 1
]

```

```
> console.log(matrix.toLogString(0, ' | ', '-- start --', '-- end --'));
-- start --
1 | 0 | 0
0 | 1 | 0
0 | 0 | 1
-- end --

```

#### [](https://www.npmjs.com/package/matrixmath#matrixcopymatrix1)matrix.copy(matrix1)

Copies the data from another matrix into the original matrix.

```
var matrix = new Matrix(2, 2);
var matrix1 = new Matrix(2, 2, false).setData(1, 2, 3, 4);
matrix.copy(matrix1);

```

```
> console.log(matrix.toLogString());
[
 1 2
 3 4
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixclone)matrix.clone()

Returns a new matrix with the same content as the first one.

```
var matrix = new Matrix(2, 2);
var matrix1 = matrix.clone();

```

#### [](https://www.npmjs.com/package/matrixmath#matrixaddmatrix1matrixn)matrix.add(matrix1[,...matrixN])

Adds all the matrices into the original matrix. All matrices must have the same size as the original one. If a matrix is found that is not the same size, it is skipped.

```
var matrix = new Matrix(1, 3).setData(1, 2, 3);
var matrix1 = new Matrix(1, 3).setData(2, 4, 6);
matrix.add(matrix1);

```

```
> console.log(matrix.toLogString());
[
 3 6 9
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixsubtractmatrix1matrixn)matrix.subtract(matrix1[,...matrixN])

Subtracts all the matrices from the original matrix. All matrices must have the same size as the original one. If a matrix is found that is not the same size, it is skipped.

```
var matrix = new Matrix(1, 3).setData(1, 2, 3);
var matrix1 = new Matrix(1, 3).setData(2, 4, 6);
matrix.subtract(matrix1);

```

```
> console.log(matrix.toLogString());
[
 -2 -2 -3
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixmultiplymatrix1matrixn)matrix.multiply(matrix1[,...matrixN])

Multiplies all the matrices into the original matrix. All matrices must either be square and of the same size, or the next matrix in line must have the same number of rows as the number of columns in the previous result.

The arguments can be either matrices or numbers. If a number is seen, a scalar multiplication is made.

```
var matrix = new Matrix(2, 2).setData(1, 2, 3, 4);
var matrix1 = new Matrix(2, 2).setData(2, 4, 6, 8);
matrix.multiply(matrix1);

```

```
> console.log(matrix.toLogString());
[
 14 20
 30 44
]

```

```
var matrix = new Matrix(2, 2).setData(1, 2, 3, 4);
matrix.multiply(3);

```

```
> console.log(matrix.toLogString());
[
 3 6
 9 12
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixdividematrix1matrixn)matrix.divide(matrix1[,...matrixN])

Divides all the matrices from the original matrix. All matrices must be square and of the same size as the original matrix.

```
var matrix = new Matrix(2, 2).setData(1, 0, 0, 1);
var matrix1 = new Matrix(2, 2).setData(2, 0, 0, 2);
matrix.divide(matrix1);

```

```
> console.log(matrix.toLogString());
[
 0.5 0
 0 -0.5
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixpowernumber)matrix.power(number)

Raises the matrix to the the given power.

```
var matrix = new Matrix(2, 2).setData(1, 2, 4, 1);
matrix.power(3);

```

```
> console.log(matrix.toLogString());
[
 25 22
 44 25
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixtranspose)matrix.transpose()

Transposes the matrix.

```
var matrix = new Matrix(2, 2).setData(1, 2, 4, 1);
matrix.transpose();

```

```
> console.log(matrix.toLogString());
[
 1 4
 2 1
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixinvert)matrix.invert()

Inverts the matrix.

```
var matrix = new Matrix(3, 3).setData(
 3, 0, 2,
 2, 0, -2,
 0, 1, 1
);
matrix.invert();

```

```
> console.log(matrix.toLogString());
[
 0.2 0.2 0
 -0.2 0.3 1
 0.2 -0.3 0
]

```

#### [](https://www.npmjs.com/package/matrixmath#matrixgetdeterminant)matrix.getDeterminant()

Gets the determinant of the matrix. The matrix must be square for this to be possible. If it's not square, this will return `null`.

```
var matrix = new Matrix(2, 2).setData(4, 6, 3, 8);
var determinant = matrix.getDeterminant();

```

```
> console.log(determinant);
14

```

#### [](https://www.npmjs.com/package/matrixmath#matrixequalsmatrix1)matrix.equals(matrix1)

Tests if the matrix has the same content as another matrix. Returns `true` if it has, `false` otherwise.

```
var matrix = new Matrix(3, 2).setData(3, 0, 2, 0, 0, 1);
var matrix1 = new Matrix(3, 2).setData(3, 0, 2, 0, 0, 1);
var matrix2 = new Matrix(2, 2).setData(3, 0, 2, 0);

matrix.equals(matrix1); // true
matrix.equals(matrix2); // false

```

#### [](https://www.npmjs.com/package/matrixmath#matrixisidentity)matrix.isIdentity()

Tests if the data of the matrix represents the identity matrix. Returns `true` if it is, `false` otherwise.

```
var matrix = new Matrix(3, 3);
var matrix1 = new Matrix(3, 3, false).setData(1, 0, 0, 0, 1, 0, 0, 0, 1);
var matrix2 = new Matrix(3, 3, false).setData(1, 2, 3, 4, 5, 6, 7, 8, 9);

matrix.isIdentity(); // true
matrix1.isIdentity(); // true
matrix2.isIdentity(); // false

```

`\
`

[](https://www.npmjs.com/package/matrixmath#versioning)
-------------------------------------------------------
{"source": "ewof/sharegpt-instruct-unfiltered-deduped", "kind": "human", "evolved_from": null}