Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions docs/datatypes/matrices.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ dense and sparse matrices.
Math.js supports two types of matrices:

- `Array`, a regular JavaScript array. A multidimensional array can be created
by nesting arrays.
by nesting arrays. The following terminology will be used to define different kinds of nested arrays.
- **Recangular array**: All elements of an array are arrays of the same size. Like `[[1, 2], [3, 4]]`
- **Jagged arrays**: All elements of an array are arrays but not of the same size. Like `[[1, 2], [3, 4, 5]]`
- **Heterogeneous arrays**: If not all the elements inside an array are arrays. Like `[[1, 2], 3]`.
- `Matrix`, a matrix implementation by math.js. A `Matrix` is an object wrapped
around a regular JavaScript `Array`, providing utility functions for easy
matrix manipulation such as `subset`, `size`, `resize`, `clone`, and more.
matrix manipulation such as `subset`, `size`, `resize`, `clone`, and more. Nested arrays must be rectangular to be converted to a `Matrix`.

In most cases, the type of matrix output from functions is determined by the
function input: An `Array` as input will return an `Array`, a `Matrix` as input
Expand Down Expand Up @@ -224,6 +227,27 @@ If you have a matrix where the first dimension means `x` and the second
means `y`, this will look confusing since `x` is printed as _column_
(vertically) and `y` as _row_ (horizontally).

## Non-rectangular arrays

By nesting arrays it is possible to have arrays that are not rectangular, for example.
```js
[[1, 2], [3, 4]] // rectangular of size [2, 2]
[[1, 2], [3, 4, 5]] // jagged
[[1, 2], 3] // heterogeneous
```

Jagged and heterogeneous arrays can't be converted to a matrix, but many operations are available for them.
```js
const A = [[1, 2], 3]
math.add(A, 1)
math.map(A, a => a+1)
math.forEach(A, a => console.log(a))
```
Some matrix functions expect a rectangular array and might provide unexpected results with non rectangular arrays, for example.
```js
math.size([[1, 2], [3]]) // [2, 2]
```
The process of validation for rectangularity is expensive and is mandatory to create a `Matrix`, thus there might be a performance benefit of not converting an `Array` to a `Matrix`.

## Resizing

Expand Down
8 changes: 3 additions & 5 deletions src/function/arithmetic/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ const dependencies = [
'matrix',
'addScalar',
'equalScalar',
'DenseMatrix',
'SparseMatrix',
'concat'
'DenseMatrix'
]

export const createAdd = /* #__PURE__ */ factory(
name,
dependencies,
({ typed, matrix, addScalar, equalScalar, DenseMatrix, SparseMatrix, concat }) => {
({ typed, matrix, addScalar, equalScalar, DenseMatrix }) => {
const matAlgo01xDSid = createMatAlgo01xDSid({ typed })
const matAlgo04xSidSid = createMatAlgo04xSidSid({ typed, equalScalar })
const matAlgo10xSids = createMatAlgo10xSids({ typed, DenseMatrix })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })
/**
* Add two or more values, `x + y`.
* For matrices, the function is evaluated element wise.
Expand Down
5 changes: 2 additions & 3 deletions src/function/arithmetic/dotDivide.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ const dependencies = [
'equalScalar',
'divideScalar',
'DenseMatrix',
'concat',
'SparseMatrix'
]

export const createDotDivide = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, divideScalar, DenseMatrix, concat, SparseMatrix }) => {
export const createDotDivide = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, divideScalar, DenseMatrix, SparseMatrix }) => {
const matAlgo02xDS0 = createMatAlgo02xDS0({ typed, equalScalar })
const matAlgo03xDSf = createMatAlgo03xDSf({ typed })
const matAlgo07xSSf = createMatAlgo07xSSf({ typed, SparseMatrix })
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
const matAlgo12xSfs = createMatAlgo12xSfs({ typed, DenseMatrix })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Divide two matrices element wise. The function accepts both matrices and
Expand Down
7 changes: 3 additions & 4 deletions src/function/arithmetic/dotMultiply.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ const dependencies = [
'typed',
'matrix',
'equalScalar',
'multiplyScalar',
'concat'
'multiplyScalar'
]

export const createDotMultiply = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, multiplyScalar, concat }) => {
export const createDotMultiply = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, multiplyScalar }) => {
const matAlgo02xDS0 = createMatAlgo02xDS0({ typed, equalScalar })
const matAlgo09xS0Sf = createMatAlgo09xS0Sf({ typed, equalScalar })
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Multiply two matrices element wise. The function accepts both matrices and
Expand Down
5 changes: 2 additions & 3 deletions src/function/arithmetic/dotPow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ const dependencies = [
'matrix',
'pow',
'DenseMatrix',
'concat',
'SparseMatrix'
]

export const createDotPow = /* #__PURE__ */ factory(name, dependencies, ({ typed, equalScalar, matrix, pow, DenseMatrix, concat, SparseMatrix }) => {
export const createDotPow = /* #__PURE__ */ factory(name, dependencies, ({ typed, equalScalar, matrix, pow, DenseMatrix, SparseMatrix }) => {
const matAlgo03xDSf = createMatAlgo03xDSf({ typed })
const matAlgo07xSSf = createMatAlgo07xSSf({ typed, SparseMatrix })
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
const matAlgo12xSfs = createMatAlgo12xSfs({ typed, DenseMatrix })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

const powScalarSignatures = {}
for (const signature in pow.signatures) {
Expand Down
2 changes: 1 addition & 1 deletion src/function/arithmetic/gcd.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const createGcd = /* #__PURE__ */ factory(name, dependencies, ({ typed, m
const matAlgo01xDSid = createMatAlgo01xDSid({ typed })
const matAlgo04xSidSid = createMatAlgo04xSidSid({ typed, equalScalar })
const matAlgo10xSids = createMatAlgo10xSids({ typed, DenseMatrix })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Calculate the greatest common divisor for two or more values or arrays.
Expand Down
7 changes: 3 additions & 4 deletions src/function/arithmetic/lcm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ const name = 'lcm'
const dependencies = [
'typed',
'matrix',
'equalScalar',
'concat'
'equalScalar'
]

export const createLcm = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, concat }) => {
export const createLcm = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar }) => {
const matAlgo02xDS0 = createMatAlgo02xDS0({ typed, equalScalar })
const matAlgo06xS0S0 = createMatAlgo06xS0S0({ typed, equalScalar })
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

const lcmTypes = 'number | BigNumber | Fraction | Matrix | Array'
const lcmManySignature = {}
Expand Down
7 changes: 3 additions & 4 deletions src/function/arithmetic/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ const dependencies = [
'matrix',
'equalScalar',
'zeros',
'DenseMatrix',
'concat'
'DenseMatrix'
]

export const createMod = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round, matrix, equalScalar, zeros, DenseMatrix, concat }) => {
export const createMod = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round, matrix, equalScalar, zeros, DenseMatrix }) => {
const floor = createFloor({ typed, config, round, matrix, equalScalar, zeros, DenseMatrix })
const matAlgo02xDS0 = createMatAlgo02xDS0({ typed, equalScalar })
const matAlgo03xDSf = createMatAlgo03xDSf({ typed })
const matAlgo05xSfSf = createMatAlgo05xSfSf({ typed, equalScalar })
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
const matAlgo12xSfs = createMatAlgo12xSfs({ typed, DenseMatrix })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Calculates the modulus, the remainder of an integer division.
Expand Down
7 changes: 3 additions & 4 deletions src/function/arithmetic/nthRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ const dependencies = [
'typed',
'matrix',
'equalScalar',
'BigNumber',
'concat'
'BigNumber'
]

export const createNthRoot = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, BigNumber, concat }) => {
export const createNthRoot = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, BigNumber }) => {
const matAlgo01xDSid = createMatAlgo01xDSid({ typed })
const matAlgo02xDS0 = createMatAlgo02xDS0({ typed, equalScalar })
const matAlgo06xS0S0 = createMatAlgo06xS0S0({ typed, equalScalar })
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Calculate the nth root of a value.
Expand Down
8 changes: 3 additions & 5 deletions src/function/arithmetic/subtract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@ const dependencies = [
'matrix',
'equalScalar',
'subtractScalar',
'unaryMinus',
'DenseMatrix',
'concat'
'DenseMatrix'
]

export const createSubtract = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, subtractScalar, unaryMinus, DenseMatrix, concat }) => {
export const createSubtract = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, subtractScalar, DenseMatrix }) => {
// TODO: split function subtract in two: subtract and subtractScalar

const matAlgo01xDSid = createMatAlgo01xDSid({ typed })
const matAlgo03xDSf = createMatAlgo03xDSf({ typed })
const matAlgo05xSfSf = createMatAlgo05xSfSf({ typed, equalScalar })
const matAlgo10xSids = createMatAlgo10xSids({ typed, DenseMatrix })
const matAlgo12xSfs = createMatAlgo12xSfs({ typed, DenseMatrix })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Subtract two values, `x - y`.
Expand Down
7 changes: 3 additions & 4 deletions src/function/bitwise/bitAnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ const name = 'bitAnd'
const dependencies = [
'typed',
'matrix',
'equalScalar',
'concat'
'equalScalar'
]

export const createBitAnd = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, concat }) => {
export const createBitAnd = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar }) => {
const matAlgo02xDS0 = createMatAlgo02xDS0({ typed, equalScalar })
const matAlgo06xS0S0 = createMatAlgo06xS0S0({ typed, equalScalar })
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Bitwise AND two values, `x & y`.
Expand Down
7 changes: 3 additions & 4 deletions src/function/bitwise/bitOr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ const dependencies = [
'typed',
'matrix',
'equalScalar',
'DenseMatrix',
'concat'
'DenseMatrix'
]

export const createBitOr = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, DenseMatrix, concat }) => {
export const createBitOr = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, DenseMatrix }) => {
const matAlgo01xDSid = createMatAlgo01xDSid({ typed })
const matAlgo04xSidSid = createMatAlgo04xSidSid({ typed, equalScalar })
const matAlgo10xSids = createMatAlgo10xSids({ typed, DenseMatrix })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Bitwise OR two values, `x | y`.
Expand Down
5 changes: 2 additions & 3 deletions src/function/bitwise/bitXor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ const dependencies = [
'typed',
'matrix',
'DenseMatrix',
'concat',
'SparseMatrix'
]

export const createBitXor = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, DenseMatrix, concat, SparseMatrix }) => {
export const createBitXor = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, DenseMatrix, SparseMatrix }) => {
const matAlgo03xDSf = createMatAlgo03xDSf({ typed })
const matAlgo07xSSf = createMatAlgo07xSSf({ typed, SparseMatrix })
const matAlgo12xSfs = createMatAlgo12xSfs({ typed, DenseMatrix })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Bitwise XOR two values, `x ^ y`.
Expand Down
28 changes: 21 additions & 7 deletions src/function/bitwise/leftShift.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { createMatAlgo02xDS0 } from '../../type/matrix/utils/matAlgo02xDS0.js'
import { createMatAlgo11xS0s } from '../../type/matrix/utils/matAlgo11xS0s.js'
import { createMatAlgo14xDs } from '../../type/matrix/utils/matAlgo14xDs.js'
import { createMatAlgo15xAs } from '../../type/matrix/utils/matAlgo15xAs.js'
import { createMatAlgo01xDSid } from '../../type/matrix/utils/matAlgo01xDSid.js'
import { createMatAlgo10xSids } from '../../type/matrix/utils/matAlgo10xSids.js'
import { createMatAlgo08xS0Sid } from '../../type/matrix/utils/matAlgo08xS0Sid.js'
import { factory } from '../../utils/factory.js'
import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js'
import { createUseMatrixForArrayScalar } from './useMatrixForArrayScalar.js'
import { leftShiftNumber } from '../../plain/number/index.js'
import { leftShiftBigNumber } from '../../utils/bignumber/bitwise.js'
import { deepMap, clone } from '../../utils/array.js'

const name = 'leftShift'
const dependencies = [
'typed',
'matrix',
'equalScalar',
'zeros',
'DenseMatrix',
'concat'
'DenseMatrix'
]

export const createLeftShift = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, zeros, DenseMatrix, concat }) => {
export const createLeftShift = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, equalScalar, zeros, DenseMatrix }) => {
const matAlgo01xDSid = createMatAlgo01xDSid({ typed })
const matAlgo02xDS0 = createMatAlgo02xDS0({ typed, equalScalar })
const matAlgo08xS0Sid = createMatAlgo08xS0Sid({ typed, equalScalar })
const matAlgo10xSids = createMatAlgo10xSids({ typed, DenseMatrix })
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
const matAlgo14xDs = createMatAlgo14xDs({ typed })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
const useMatrixForArrayScalar = createUseMatrixForArrayScalar({ typed, matrix })
const matAlgo15xAs = createMatAlgo15xAs()
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix })

/**
* Bitwise left logical shift of a value x by y number of bits, `x << y`.
Expand Down Expand Up @@ -78,6 +78,14 @@ export const createLeftShift = /* #__PURE__ */ factory(name, dependencies, ({ ty
return matAlgo14xDs(x, y, self, false)
}),

'Array, number | BigNumber': typed.referToSelf(self => (x, y) => {
// check scalar
if (equalScalar(y, 0)) {
return clone(x)
}
return matAlgo15xAs(x, y, self, false)
}),

'number | BigNumber, SparseMatrix': typed.referToSelf(self => (x, y) => {
// check scalar
if (equalScalar(x, 0)) {
Expand All @@ -92,9 +100,15 @@ export const createLeftShift = /* #__PURE__ */ factory(name, dependencies, ({ ty
return zeros(y.size(), y.storage())
}
return matAlgo14xDs(y, x, self, true)
}),
'number | BigNumber, Array': typed.referToSelf(self => (x, y) => {
// check scalar
if (equalScalar(x, 0)) {
return deepMap(y, () => x)
}
return matAlgo15xAs(y, x, self, true)
})
},
useMatrixForArrayScalar,
matrixAlgorithmSuite({
SS: matAlgo08xS0Sid,
DS: matAlgo01xDSid,
Expand Down
Loading