Skip to content

Commit 7a617b1

Browse files
authored
fix: support multiplication of arrays with units (#3456)
1 parent 123d4aa commit 7a617b1

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

src/function/complex/conj.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ export const createConj = /* #__PURE__ */ factory(name, dependencies, ({ typed }
2525
*
2626
* re, im, arg, abs
2727
*
28-
* @param {number | BigNumber | Complex | Array | Matrix} x
28+
* @param {number | BigNumber | Complex | Array | Matrix | Unit} x
2929
* A complex number or array with complex numbers
30-
* @return {number | BigNumber | Complex | Array | Matrix}
30+
* @return {number | BigNumber | Complex | Array | Matrix | Unit}
3131
* The complex conjugate of x
3232
*/
3333
return typed(name, {
3434
'number | BigNumber | Fraction': x => x,
3535
Complex: x => x.conjugate(),
36+
Unit: typed.referToSelf(self => x => new x.constructor(self(x.toNumeric()), x.formatUnits())),
3637
'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
3738
})
3839
})

test/unit-tests/function/arithmetic/multiply.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,19 @@ describe('multiply', function () {
318318
approxDeepEqual(multiply(matrix(a), matrix(b)), 32)
319319
})
320320

321+
it('should multiply vectors with units correctly (dot product)', function () {
322+
const a = [1, 2, 3]
323+
const b = [unit('4cm'), unit('5cm'), unit('6cm')]
324+
325+
approxDeepEqual(multiply(a, b), unit('32cm'))
326+
approxDeepEqual(multiply(matrix(a), matrix(b)), unit('32cm'))
327+
328+
const e = [unit('1cm'), unit('2cm'), unit('3cm')]
329+
330+
assert.strictEqual(multiply(e, b).format(5), '32 cm^2')
331+
assert.strictEqual(multiply(matrix(e), matrix(b)).format(5), '32 cm^2')
332+
})
333+
321334
it('should conjugate the first argument in dot product', function () {
322335
const a = [complex(1, 2), complex(3, 4)]
323336
const b = [complex(5, 6), complex(7, 8)]

test/unit-tests/function/complex/conj.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ describe('conj', function () {
2626
assert.strictEqual(conj(math.complex('2')).toString(), '2')
2727
assert.strictEqual(conj(math.complex('-4i')).toString(), '4i')
2828
assert.strictEqual(conj(math.i).toString(), '-i')
29+
assert.strictEqual(conj(math.unit('5cm')).toString(), '5 cm')
30+
})
31+
32+
it('should calculate the conjugate of a units with complex, Fraction and BigNumber values', function () {
33+
assert.strictEqual(conj(math.unit(math.complex('300+250i'), 'ohm')).toString(), '(300 - 250i) ohm')
34+
assert.strictEqual(conj(math.unit(math.bignumber('0.3'), 'm/s')).toString(), '0.3 m / s')
35+
assert.strictEqual(conj(math.unit(math.fraction('0.(285714)'), 'fahrenheit')).toString(), '2/7 fahrenheit')
36+
assert.strictEqual(conj(math.unit(math.fraction(0.125), 'hp')).toString(), '1/8 hp')
2937
})
3038

3139
it('should calculate the conjugate for each element in a matrix', function () {
@@ -37,7 +45,6 @@ describe('conj', function () {
3745

3846
it('should throw an error when called with an unsupported type of argument', function () {
3947
assert.throws(function () { conj(new Date()) }, /TypeError: Unexpected type of argument/)
40-
assert.throws(function () { conj(math.unit('5cm')) }, /TypeError: Unexpected type of argument/)
4148
})
4249

4350
it('should throw an error in case of invalid number of arguments', function () {

test/unit-tests/function/matrix/dot.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const dot = math.dot
55
const matrix = math.matrix
66
const sparse = math.sparse
77
const complex = math.complex
8+
const unit = math.unit
9+
const bignumber = math.bignumber
10+
const fraction = math.fraction
811

912
describe('dot', function () {
1013
it('should calculate dot product for two 1-dim arrays', function () {
@@ -57,6 +60,40 @@ describe('dot', function () {
5760
assert.strictEqual(dot(matrix([[7], [3]]), matrix([2, 4])), 26)
5861
})
5962

63+
it('should calculate dot product for two 1-dim unit arrays', function () {
64+
assert.strictEqual(dot([unit('2m'), unit('4m'), unit('1m')], [2, 2, 3]).toString(), '15 m')
65+
assert.strictEqual(dot([7, 3], [unit('2m'), unit('4m')]).toString(), '26 m')
66+
assert.strictEqual(dot([unit('2m'), unit('4m'), unit('1m')], [unit('2m'), unit('2m'), unit('3m')]).toString(), '15 m^2')
67+
})
68+
69+
it('should calculate dot product for two column unit arrays', function () {
70+
assert.strictEqual(dot([[unit('2g')], [unit('4g')], [unit('1g')]], [[2], [2], [3]]).toString(), '15 g')
71+
assert.strictEqual(dot([[unit('7g')], [unit('3g')]], [[2], [4]]).toString(), '26 g')
72+
})
73+
74+
it('should calculate dot product for two 1-dim unit vectors', function () {
75+
assert.strictEqual(dot(matrix([2, 4, 1]), matrix([unit('2g'), unit('2g'), unit('3g')])).toString(), '15 g')
76+
assert.strictEqual(dot(matrix([unit('7m'), unit('3m')]), matrix([unit('2m'), unit('4m')])).toString(), '26 m^2')
77+
})
78+
79+
it('should calculate dot product for two column unit vectors', function () {
80+
assert.strictEqual(dot(matrix([[unit('2m')], [unit('4m')], [unit('1m')]]), matrix([[unit('2m')], [unit('2m')], [unit('3m')]])).toString(), '15 m^2')
81+
assert.strictEqual(dot(matrix([[7], [3]]), matrix([[unit('2g')], [unit('4g')]])).toString(), '26 g')
82+
})
83+
84+
it('should calculate dot product for two 1-dim unit with complex value vectors', function () {
85+
assert.deepEqual(dot(matrix([unit(complex(2, 3), 'm'), unit(complex(4, 5), 'm')]), matrix([unit(complex(1, 1), 'm'), unit(complex(2, 4), 'm')])).toNumeric(), complex(33, 5))
86+
assert.strictEqual(dot(matrix([unit(complex(2, 3), 'm'), unit(complex(4, 5), 'm')]), matrix([unit(complex(1, 1), 'm'), unit(complex(2, 4), 'm')])).toString(), '(33 + 5i) m^2')
87+
})
88+
89+
it('should calculate dot product for two 1-dim unit with BigNumber value vectors', function () {
90+
assert.strictEqual(dot(matrix([[unit(bignumber(7), 'g')], [unit(bignumber(3), 'g')]]), matrix([[unit(bignumber(2), 'g')], [unit(bignumber(4), 'g')]])).toString(), '26 g^2')
91+
})
92+
93+
it('should calculate dot product for two 1-dim unit with Fraction value vectors', function () {
94+
assert.strictEqual(dot(matrix([[unit(fraction(0.4), 'm')], [unit(fraction('0.5'), 'm')]]), matrix([[unit(fraction(1, 4), 'm')], [unit(fraction('3/4'), 'm')]])).toString(), '19/40 m^2')
95+
})
96+
6097
it('should calculate dot product for sparse vectors', function () {
6198
assert.strictEqual(dot(sparse([0, 0, 2, 4, 4, 1]), sparse([1, 0, 2, 2, 0, 3])), 15)
6299
assert.strictEqual(dot(sparse([7, 1, 2, 3]), sparse([2, 0, 0, 4])), 26)

0 commit comments

Comments
 (0)