Skip to content

Commit

Permalink
Fix gcd type (#2922)
Browse files Browse the repository at this point in the history
* refactor gcd type

* add tests

* just refactor gcd...

* little change on test

* update gcd chain
  • Loading branch information
brunoSnoww committed Mar 27, 2023
1 parent 7950d9c commit 079300f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
71 changes: 50 additions & 21 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,27 +764,56 @@ Chaining examples
expectTypeOf(math.chain([1, 2]).gcd(3, 4)).toMatchTypeOf<
MathJsChain<number>
>()
// TODO make gcd() work in the following cases
// expectTypeOf(math.chain([1, 2]).gcd()).toMatchTypeOf<MathJsChain<number>>()
// expectTypeOf(math.chain([[1], [2]]).gcd()).toMatchTypeOf<
// MathJsChain<MathArray>
// >()
// expectTypeOf(
// math.chain([math.bignumber(1), math.bignumber(1)]).gcd()
// ).toMatchTypeOf<MathJsChain<BigNumber>>()
// expectTypeOf(
// math.chain([math.complex(1, 2), math.complex(1, 2)]).gcd()
// ).toMatchTypeOf<MathJsChain<Complex>>()
// expectTypeOf(
// math
// .chain(
// math.matrix([
// [1, 2],
// [3, 4],
// ])
// )
// .expm1()
// ).toMatchTypeOf<MathJsChain<Matrix>>()
expectTypeOf(math.chain([1, 2]).gcd()).toMatchTypeOf<MathJsChain<number>>()
expectTypeOf(
math.chain([math.bignumber(1), math.bignumber(1)]).gcd()
).toMatchTypeOf<MathJsChain<BigNumber>>()
expectTypeOf(
math.chain([math.bignumber(1), math.bignumber(1)]).gcd()
).toMatchTypeOf<MathJsChain<BigNumber>>()
expectTypeOf(
math.gcd(math.bignumber(1), math.bignumber(1))
).toMatchTypeOf<BigNumber>()
expectTypeOf(
math.gcd([
math.matrix([
[1, 2],
[3, 4],
]),
math.matrix([
[1, 2],
[3, 4],
]),
])
).toMatchTypeOf<Matrix>()
expectTypeOf(
math.gcd(
[
[1, 2],
[3, 4],
],
[
[1, 2],
[3, 4],
]
)
).toMatchTypeOf<MathArray>()

assert.throws(
() =>
// @ts-expect-error ... gcd() supports only 1d matrices!
math.gcd([
[
[1, 5],
[10, 49],
],
[
[1, 5],
[5, 7],
],
]),
Error
)

// hypot
expectTypeOf(math.chain([1, 2]).hypot()).toMatchTypeOf<MathJsChain<number>>()
Expand Down
6 changes: 3 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,9 +1186,10 @@ declare namespace math {
* @param args Two or more integer numbers
* @returns The greatest common divisor
*/
gcd<T extends (number | BigNumber | Fraction | MathCollection)[]>(
gcd<T extends number | BigNumber | Fraction | MathCollection>(
...args: T[]
): T
gcd<T extends number | BigNumber | Fraction | Matrix>(args: T[]): T

/**
* Calculate the hypotenusa of a list with values. The hypotenusa is
Expand Down Expand Up @@ -4676,11 +4677,10 @@ declare namespace math {
* Calculate the greatest common divisor for two or more values or
* arrays. For matrices, the function is evaluated element wise.
*/
gcd<T extends number | BigNumber | MathCollection>(
gcd<T extends number | BigNumber | Fraction | Matrix>(
this: MathJsChain<T[]>,
...args: T[]
): MathJsChain<T>
gcd(this: MathJsChain<Complex[]>, ...args: Fraction[]): MathJsChain<Complex>

/**
* Calculate the hypotenusa of a list with values. The hypotenusa is
Expand Down

0 comments on commit 079300f

Please sign in to comment.