Skip to content

Commit cdceb79

Browse files
committed
Added documentation
1 parent 8e58d2b commit cdceb79

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

docs/datatypes/matrices.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ dense and sparse matrices.
1212
Math.js supports two types of matrices:
1313

1414
- `Array`, a regular JavaScript array. A multidimensional array can be created
15-
by nesting arrays.
15+
by nesting arrays. If the elements of an array are of the same size, it is a
16+
rectangular array. If the elements are all arrays but not of the same size,
17+
it is a jagged array and if not all elements are arrays then it's an
18+
heterogeneous array.
1619
- `Matrix`, a matrix implementation by math.js. A `Matrix` is an object wrapped
1720
around a regular JavaScript `Array`, providing utility functions for easy
1821
matrix manipulation such as `subset`, `size`, `resize`, `clone`, and more.
@@ -224,6 +227,27 @@ If you have a matrix where the first dimension means `x` and the second
224227
means `y`, this will look confusing since `x` is printed as _column_
225228
(vertically) and `y` as _row_ (horizontally).
226229

230+
## Not rectangular arrays
231+
232+
By nesting arrays it is possible to have arrays that are not rectangular, for example.
233+
```js
234+
[[1, 2], [3, 4]] // rectangular of size [2, 2]
235+
[[1, 2], [3]] // jagged
236+
[[1, 2], 3] // heterogeneous
237+
```
238+
239+
These types of arrays can't be converted to a matrix, but many operations are available for them.
240+
```js
241+
const A = [[1, 2], 3]
242+
math.add(A, 1)
243+
math.map(A, a => a+1)
244+
math.forEach(A, a => console.log(a))
245+
```
246+
Many matrix functions expect a rectangular array and might provide unexpected results with non rectangular arrays, for example.
247+
```js
248+
math.size([[1, 2], [3]]) // [2, 2]
249+
```
250+
The process of validation for rectangularity is expensive and this provides a fast way of working with nested arrays of many kinds.
227251

228252
## Resizing
229253

0 commit comments

Comments
 (0)