Skip to content

Commit df8d098

Browse files
authored
fix: add missing type definition for function diff (#3520). Thanks @dodokw.
1 parent 2e07821 commit df8d098

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

test/typescript-tests/testTypes.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,24 @@ Chaining examples
809809
.dotPow(2)
810810
).toMatchTypeOf<MathJsChain<Matrix>>()
811811

812+
// diff
813+
expectTypeOf(math.chain([1, 2, 3]).diff()).toMatchTypeOf<
814+
MathJsChain<MathArray>
815+
>()
816+
expectTypeOf(math.chain([1, 2, 3]).diff(0)).toMatchTypeOf<
817+
MathJsChain<MathArray>
818+
>()
819+
expectTypeOf(
820+
math
821+
.chain(
822+
math.matrix([
823+
[1, 2],
824+
[3, 4]
825+
])
826+
)
827+
.diff(1)
828+
).toMatchTypeOf<MathJsChain<Matrix>>()
829+
812830
// exp
813831
expectTypeOf(math.chain(1).exp()).toMatchTypeOf<MathJsChain<MathType>>()
814832
// @ts-expect-error ... verify exp does not run on arrays.
@@ -2381,6 +2399,44 @@ Function round examples
23812399
assert.throws(() => math.round([3.21, 3.82], [1, 2]), TypeError)
23822400
}
23832401

2402+
/*
2403+
Function diff examples
2404+
*/
2405+
{
2406+
const math = create(all, {})
2407+
2408+
// Array input
2409+
assert.deepStrictEqual(math.diff([1, 2, 4, 7, 0]), [1, 2, 3, -7])
2410+
assert.deepStrictEqual(math.diff([1, 2, 4, 7, 0], 0), [1, 2, 3, -7])
2411+
2412+
// Matrix input
2413+
assert.deepStrictEqual(
2414+
math.diff(math.matrix([1, 2, 4, 7, 0])),
2415+
math.matrix([1, 2, 3, -7])
2416+
)
2417+
assert.deepStrictEqual(
2418+
math.diff(math.matrix([1, 2, 4, 7, 0]), 0),
2419+
math.matrix([1, 2, 3, -7])
2420+
)
2421+
2422+
// with bignumber
2423+
assert.deepStrictEqual(
2424+
math.diff(
2425+
[
2426+
[1, 2],
2427+
[3, 4]
2428+
],
2429+
math.bignumber(1)
2430+
),
2431+
[[1], [1]]
2432+
)
2433+
2434+
// type checks
2435+
expectTypeOf(math.diff([1, 2, 3])).toMatchTypeOf<MathArray>()
2436+
expectTypeOf(math.diff([1, 2, 3], 0)).toMatchTypeOf<MathArray>()
2437+
expectTypeOf(math.diff(math.matrix([1, 2, 3]))).toMatchTypeOf<Matrix>()
2438+
expectTypeOf(math.diff(math.matrix([1, 2, 3]), 0)).toMatchTypeOf<Matrix>()
2439+
}
23842440
/*
23852441
Clone examples
23862442
*/

types/index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,14 @@ export interface MathJsInstance extends MathJsFactory {
19041904
*/
19051905
det(x: MathCollection): number
19061906

1907+
/**
1908+
* Calculate the difference between adjacent elements of a matrix or array.
1909+
* @param x A matrix or array
1910+
* @param dim The dimension to apply the difference on.
1911+
* @returns A matrix or array containing the differences
1912+
*/
1913+
diff<T extends MathCollection>(x: T, dim?: number | BigNumber): T
1914+
19071915
/**
19081916
* Create a diagonal matrix or retrieve the diagonal of a matrix. When x
19091917
* is a vector, a matrix with vector x on the diagonal will be returned.
@@ -5806,6 +5814,15 @@ export interface MathJsChain<TValue> {
58065814
y: MathCollection
58075815
): MathJsChain<MathCollection>
58085816

5817+
/**
5818+
* Calculate the difference between adjacent elements of the chained matrix or array.
5819+
* @param dim The dimension to apply the difference on.
5820+
*/
5821+
diff<T extends MathCollection>(
5822+
this: MathJsChain<T>,
5823+
dim?: number | BigNumber
5824+
): MathJsChain<T>
5825+
58095826
/**
58105827
* Calculate the determinant of a matrix.
58115828
*/

0 commit comments

Comments
 (0)