Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 13 additions & 6 deletions src/type/matrix/utils/matAlgo14xDs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ export const createMatAlgo14xDs = /* #__PURE__ */ factory(name, dependencies, ({

// process data types
if (typeof adt === 'string') {
// datatype
dt = adt
// convert b to the same datatype
b = typed.convert(b, dt)
// callback
cf = typed.find(callback, [dt, dt])
// Take the datatype fast-path only when the scalar converts into the
// matrix's datatype. When it cannot (e.g. a BigNumber scalar and a
// `number` matrix), fall through to the generic callback so the values
// promote as usual instead of throwing on the conversion.
let converted
try {
converted = typed.convert(b, adt)
} catch {}
if (converted !== undefined) {
b = converted
dt = adt
cf = typed.find(callback, [dt, dt])
}
}

// populate cdata, iterate through dimensions
Expand Down
7 changes: 7 additions & 0 deletions test/unit-tests/function/arithmetic/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ describe('add', function () {
assert.deepStrictEqual(a4.size(), [2])
assert.deepStrictEqual(a4.valueOf(), [math.bignumber(8), math.bignumber(10)])
})

it('should promote when a scalar does not fit the matrix datatype (#3612)', function () {
const a = math.matrix([[1, 2]], 'dense', 'number')
const expected = math.matrix([[math.bignumber(4), math.bignumber(5)]])
assert.deepStrictEqual(add(a, math.bignumber(3)), expected)
assert.deepStrictEqual(add(math.bignumber(3), a), expected)
})
})

describe('SparseMatrix', function () {
Expand Down