Skip to content

Commit 9074912

Browse files
committed
Implement config.legacySubset
1 parent c5051f5 commit 9074912

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,6 @@ Christian Stussak <stussak@mfo.de>
265265
aitee <1228639+aitee@users.noreply.github.com>
266266
Delaney Sylvans <delaneysylvans@gmail.com>
267267
Rani D. <73716554+ranidam@users.noreply.github.com>
268+
Don McCurdy <dm@donmccurdy.com>
268269

269270
# Generated by tools/update-authors.js

src/core/config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@ export const DEFAULT_CONFIG = {
2828

2929
// random seed for seeded pseudo random number generation
3030
// null = randomly seed
31-
randomSeed: null
31+
randomSeed: null,
32+
33+
// legacy behavior for matrix subset, when false, the subset function
34+
// return a matrix or array with the same size of the index (except for the scalar),
35+
// when false returns a matrix or array with a size depending
36+
// on the type of index
37+
legacySubset: false
3238
}

src/core/function/config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export function configFactory (config, emit) {
6161
delete optionsFix.epsilon
6262
return _config(optionsFix)
6363
}
64+
65+
if (options.legacySubset === true) {
66+
// this if is only for backwards compatibility, it can be removed in the future.
67+
console.warn('Warning: The configuration option "legacySubset" is for compatibility only and might be deprecated in the future.')
68+
}
6469
const prev = clone(config)
6570

6671
// validate some of the options

src/function/algebra/sylvester.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const dependencies = [
1616
'subtract',
1717
'identity',
1818
'lusolve',
19-
'abs'
19+
'abs',
20+
'config'
2021
]
2122

2223
export const createSylvester = /* #__PURE__ */ factory(name, dependencies, (
@@ -35,7 +36,8 @@ export const createSylvester = /* #__PURE__ */ factory(name, dependencies, (
3536
subtract,
3637
identity,
3738
lusolve,
38-
abs
39+
abs,
40+
config
3941
}
4042
) => {
4143
/**

src/type/matrix/DenseMatrix.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import { optimizeCallback } from '../../utils/optimizeCallback.js'
1010

1111
const name = 'DenseMatrix'
1212
const dependencies = [
13-
'Matrix'
13+
'Matrix',
14+
'config'
1415
]
1516

16-
export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies, ({ Matrix }) => {
17+
export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies, ({ Matrix, config }) => {
1718
/**
1819
* Dense Matrix implementation. A regular, dense matrix, supporting multi-dimensional matrices. This is the default matrix type.
1920
* @class DenseMatrix
@@ -218,7 +219,9 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
218219
throw new TypeError('Invalid index')
219220
}
220221

221-
const isScalar = index.isScalar()
222+
const isScalar = config.legacySubset
223+
? index.size().every(idx => idx === 1)
224+
: index.isScalar()
222225
if (isScalar) {
223226
// return a scalar
224227
return matrix.get(index.min())
@@ -243,7 +246,7 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
243246
returnMatrix._size = submatrix.size
244247
returnMatrix._datatype = matrix._datatype
245248
returnMatrix._data = submatrix.data
246-
return returnMatrix
249+
return config.legacySubset ? returnMatrix.reshape(index.size()) : returnMatrix
247250
}
248251
}
249252

@@ -262,27 +265,27 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
262265
return { data: getSubmatrixRecursive(data), size: size.filter(x => x !== null) }
263266

264267
function getSubmatrixRecursive (data, depth = 0) {
265-
const ranges = index.dimension(depth)
266-
function _mapIndex (range, callback) {
268+
const dims = index.dimension(depth)
269+
function _mapIndex (dim, callback) {
267270
// applies a callback for when the index is a Number or a Matrix
268-
if (isNumber(range)) return callback(range)
269-
else return range.map(callback).valueOf()
271+
if (isNumber(dim)) return callback(dim)
272+
else return dim.map(callback).valueOf()
270273
}
271274

272-
if (isNumber(ranges)) {
275+
if (isNumber(dims)) {
273276
size[depth] = null
274277
} else {
275-
size[depth] = ranges.size()[0]
278+
size[depth] = dims.size()[0]
276279
}
277280
if (depth < maxDepth) {
278-
return _mapIndex(ranges, rangeIndex => {
279-
validateIndex(rangeIndex, data.length)
280-
return getSubmatrixRecursive(data[rangeIndex], depth + 1)
281+
return _mapIndex(dims, dimIndex => {
282+
validateIndex(dimIndex, data.length)
283+
return getSubmatrixRecursive(data[dimIndex], depth + 1)
281284
})
282285
} else {
283-
return _mapIndex(ranges, rangeIndex => {
284-
validateIndex(rangeIndex, data.length)
285-
return data[rangeIndex]
286+
return _mapIndex(dims, dimIndex => {
287+
validateIndex(dimIndex, data.length)
288+
return data[dimIndex]
286289
})
287290
}
288291
}

test/unit-tests/core/config.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,22 @@ describe('config', function () {
3737
// Restore console.warn
3838
warnStub.restore()
3939
})
40+
41+
it('should work with config legacySubset during deprecation', function () {
42+
const math2 = math.create()
43+
// Add a spy to temporarily disable console.warn
44+
const warnStub = sinon.stub(console, 'warn')
45+
46+
// Set legacySubset to throw a warning
47+
assert.doesNotThrow(function () { math2.config({ legacySubset: true }) })
48+
49+
// Check if legacySubset is set
50+
assert.strictEqual(math2.config().legacySubset, true)
51+
52+
// Check if console.warn was called
53+
assert.strictEqual(warnStub.callCount, 1)
54+
55+
// Restore console.warn
56+
warnStub.restore()
57+
})
4058
})

0 commit comments

Comments
 (0)