From b10e3169c68292d8f19c55e17ba1db6571ab66c3 Mon Sep 17 00:00:00 2001 From: Orel Ben Neriah <77707952+orelbn@users.noreply.github.com> Date: Wed, 6 Nov 2024 23:44:03 -0800 Subject: [PATCH] fix: #3098 updates MathArray types to be n-dimensional (#3306) --- test/typescript-tests/testTypes.ts | 68 +++++++++++++++++++++++++++--- types/index.d.ts | 9 ++-- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index acce2c029e..6dc2834eb4 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -857,7 +857,6 @@ Chaining examples assert.throws( () => - // @ts-expect-error ... gcd() supports only 1d matrices! math.gcd([ [ [1, 5], @@ -1462,8 +1461,8 @@ Math types examples: Type results after multiplying 'MathTypes' with matrices [5, 6, 7, 8] ] - const cde: MathArray = [1] - const def: MathArray = [2] + const efg: MathArray = [1, 2, 3, 4, 5] + const fgh: MathArray = [2, 3, 4, 5, 6] const Mbcd = math.matrix(bcd) const Mabc = math.matrix(abc) @@ -1477,9 +1476,11 @@ Math types examples: Type results after multiplying 'MathTypes' with matrices const _r2 = math.multiply(a, b) // 1D JS Array - const _r12 = math.multiply(cde, def) // equal 2 - const r3 = math.multiply(abc, bcd) - const _r31 = r3[1] // By default least promised valid syntax + const r3 = math.multiply(abc, bcd) // 1D * 2D => Array + const r3a = math.multiply(efg, fgh) // 1D * 1D => Scalar + + const _r31 = r3[1] + assert.strictEqual(typeof r3a, 'number') // 2D JS Array const r12 = math.multiply(bcd, bcd) @@ -1489,7 +1490,8 @@ Math types examples: Type results after multiplying 'MathTypes' with matrices if (multiDimensional(r12)) { const _r1211 = r12[1][1] } - const _r121 = r12[1] // Valid syntax + + const _r121 = r12[1] // Matrix: matrix * vector const r7 = math.multiply(Mabc, bcd) @@ -2808,3 +2810,55 @@ Match types of exact positional arguments. expectTypeOf(e).toMatchTypeOf() assert.deepStrictEqual(e, [1]) } + +/** + * N-dimensional array examples + */ +{ + const math = create(all, {}) + + const array1 = [1, 2, 3] + const array2 = [ + [1, 2], + [3, 4] + ] + const array3 = [ + [ + [1, 2], + [3, 4] + ], + [ + [5, 6], + [7, 8] + ] + ] + const array4 = [ + [[[1, 2]], [[3, 4]]], + [[[5, 6]], [[7, 8]]], + [[[9, 10]], [[11, 12]]] + ] + + const mixArray3 = [ + [ + [1, math.unit(2, 'cm'), math.bignumber(1), math.complex(1, 2)], + [3, math.unit(4, 'cm'), math.bignumber(2), math.complex(3, 4)] + ], + [ + [5, math.unit(6, 'cm'), math.bignumber(3), math.complex(5, 6)], + [7, math.unit(8, 'cm'), math.bignumber(4), math.complex(7, 8)] + ] + ] + + const unitArray3 = [ + [[math.unit(1, 'cm'), math.unit(2, 'cm')]], + [[math.unit(3, 'cm'), math.unit(4, 'cm')]] + ] + + expectTypeOf(array1).toMatchTypeOf() + expectTypeOf(array2).toMatchTypeOf() + expectTypeOf(array3).toMatchTypeOf() + expectTypeOf(array4).toMatchTypeOf() + + expectTypeOf(mixArray3).toMatchTypeOf>() + expectTypeOf(unitArray3).toMatchTypeOf>() +} diff --git a/types/index.d.ts b/types/index.d.ts index d5774e0615..96ed06e4ca 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -13,7 +13,7 @@ export type NoLiteralType = T extends number export type MathNumericType = number | BigNumber | bigint | Fraction | Complex export type MathScalarType = MathNumericType | Unit export type MathGeneric = T -export type MathArray = T[] | T[][] +export type MathArray = T[] | Array> export type MathCollection = MathArray | Matrix export type MathType = MathScalarType | MathCollection export type MathExpression = string | string[] | MathCollection @@ -1393,9 +1393,10 @@ export interface MathJsInstance extends MathJsFactory { multiply(x: T, y: MathType): Matrix multiply(x: MathType, y: T): Matrix - multiply(x: T, y: T[]): T - multiply(x: T[], y: T): T - multiply>(x: T, y: T): T + multiply(x: T, y: T[]): T + multiply(x: T[], y: T): T + multiply(x: T[], y: T[]): T + multiply(x: T, y: T): MathScalarType multiply(x: Unit, y: Unit): Unit multiply(x: number, y: number): number multiply(x: MathType, y: MathType): MathType