Skip to content

Commit b7213f0

Browse files
committed
Added migration section
1 parent 0e55c7c commit b7213f0

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

docs/datatypes/matrices.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,12 @@ in the matrix, and if not, a subset of the matrix will be returned.
270270

271271
A subset can be defined using an `Index`. An `Index` contains a single value
272272
or a set of values for each dimension of a matrix. An `Index` can be
273-
created using the function `index`. When getting a single value from a matrix,
274-
`subset` will return the value itself instead of a matrix containing just this
275-
value.
273+
created using the function `index`. The way `subset` returns results depends on how you specify indices for each dimension:
276274

277-
**New behavior:**
278-
When using `subset`, indexing a dimension with a scalar removes that dimension from the result, while indexing with an array (even if it has only one element) preserves that dimension. In other words, scalar indices eliminate dimensions, and array indices retain them. To restore the previous behavior, where retrieving an index with an Array or Matrix with size 1 or a single value always returns the value itself regardless of index type, set the configuration option `{legacySubset: true}` using the `config` function.
275+
- If you use a scalar (single number) as an index for a dimension, that dimension is removed from the result.
276+
- If you use an array, matrix or range (even with just one element) as an index, that dimension is preserved in the result.
277+
278+
This means that scalar indices eliminate dimensions, while array, matrix or range indices retain them. See the section [Migrate to v15](#migrate-to-v15) for more details and examples of this behavior.
279279

280280
For example:
281281

@@ -333,6 +333,38 @@ c.subset(math.index(1, [0, 1]), [2, 3]) // Matrix, [[0, 1], [2, 3]]
333333
e.resize([2, 3], 0) // Matrix, [[0, 0, 0], [0, 0, 0]]
334334
e.subset(math.index(1, 2), 5) // Matrix, [[0, 0, 0], [0, 0, 5]]
335335
```
336+
## Migrate to v15
337+
338+
With the release of math.js v15, the behavior of `subset` when indexing matrices and arrays has changed. If your code relies on the previous behavior (where indexing with an array or matrix of size 1 would always return the value itself), you may need to update your code or enable legacy mode.
339+
340+
### How to migrate
341+
342+
- **Option 1: Enable legacy behavior**
343+
If you want your code to work as before without changes, enable legacy mode by adding:
344+
```js
345+
math.config({ legacySubset: true })
346+
```
347+
This restores the old behavior for `subset`.
348+
349+
- **Option 2: Update your code**
350+
Update your code to use scalar indices when you want to eliminate dimensions, or use array indices to preserve dimensions.
351+
352+
### Migration examples
353+
354+
```js
355+
const m = math.matrix([[10, 11, 12], [20, 21, 22]])
356+
```
357+
358+
| Code example | v15+ (default) result | v14- and v15+ (legacy mode) result | How to get old result in v15+. |
359+
|------------------------------------|-------------------------------|------------------------------------|----------------------------------|
360+
| `m.subset(math.index(1, [2]))` | `[22]` | `22` | Use `m.subset(math.index(1, 2))` |
361+
| `m.subset(math.index([1], 2))` | `[22]` | `22` | Use `m.subset(math.index(1, 2))` |
362+
| `m.subset(math.index([1], [2]))` | `[[22]]` | `22` | Use `m.subset(math.index(1, 2))` |
363+
| `m.subset(math.index(1, 2))` | `22` | `22` | No change needed |
364+
365+
> **Tip:**
366+
> If you want to always get a scalar value, use scalar indices.
367+
> If you want to preserve dimensions, use array, matrix or range indices.
336368
337369
## Getting and setting a value in a matrix
338370

0 commit comments

Comments
 (0)