Skip to content

Commit c9c7d4d

Browse files
committed
feat: add zero() to generate additive identity
1 parent 34b6614 commit c9c7d4d

File tree

12 files changed

+174
-3
lines changed

12 files changed

+174
-3
lines changed

src/expression/embeddedDocs/embeddedDocs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import { roundDocs } from './function/arithmetic/round.js'
8282
import { signDocs } from './function/arithmetic/sign.js'
8383
import { sqrtDocs } from './function/arithmetic/sqrt.js'
8484
import { sqrtmDocs } from './function/arithmetic/sqrtm.js'
85+
import { zeroDocs } from './function/arithmetic/zero.js'
8586
import { sylvesterDocs } from './function/algebra/sylvester.js'
8687
import { schurDocs } from './function/algebra/schur.js'
8788
import { lyapDocs } from './function/algebra/lyap.js'
@@ -412,6 +413,7 @@ export const embeddedDocs = {
412413
unaryPlus: unaryPlusDocs,
413414
xgcd: xgcdDocs,
414415
invmod: invmodDocs,
416+
zero: zeroDocs,
415417

416418
// functions - bitwise
417419
bitAnd: bitAndDocs,

src/expression/embeddedDocs/function/arithmetic/one.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export const oneDocs = {
44
syntax: ['one(x)'],
55
description: 'returns the multiplicative identity of the same type as x',
66
examples: ['one(2/3)', 'one([[1, -1], [-1, 2]])'],
7-
seealso: ['typeOf', 'numeric']
7+
seealso: ['zero', 'typeOf', 'numeric']
88
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const zeroDocs = {
2+
name: 'zero',
3+
category: 'arithmetic',
4+
syntax: ['zero(x)'],
5+
description: 'returns the additive identity of the same type as x',
6+
examples: ['zero(2/3)', 'zero([[1, -1, 1], [-1, 2, -1]])'],
7+
seealso: ['one', 'typeOf', 'numeric']
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const oneDocs = {
2+
name: 'one',
3+
category: 'arithmetic',
4+
syntax: ['one(x)'],
5+
description: 'returns the multiplicative identity of the same type as x',
6+
examples: ['one(2/3)', 'one([[1, -1], [-1, 2]])'],
7+
seealso: ['typeOf', 'numeric']
8+
}

src/factoriesAny.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export { createNthRoots } from './function/arithmetic/nthRoots.js'
123123
export { createDotPow } from './function/arithmetic/dotPow.js'
124124
export { createDotDivide } from './function/arithmetic/dotDivide.js'
125125
export { createScalarDivide } from './function/arithmetic/scalarDivide.js'
126+
export { createZero } from './function/arithmetic/zero.js'
126127
export { createLsolve } from './function/algebra/solver/lsolve.js'
127128
export { createUsolve } from './function/algebra/solver/usolve.js'
128129
export { createLsolveAll } from './function/algebra/solver/lsolveAll.js'

src/factoriesNumber.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export const createAdd = /* #__PURE__ */ createNumberFactory('add', addNumber)
135135
export { createHypot } from './function/arithmetic/hypot.js'
136136
export const createNorm = /* #__PURE__ */ createNumberFactory('norm', normNumber)
137137
export const createDivide = /* #__PURE__ */ createNumberFactory('divide', divideNumber)
138+
export { createZeroNumber } from './function/arithmetic/zero.js'
138139

139140
// bitwise
140141
export const createBitAnd = /* #__PURE__ */ createNumberFactory('bitAnd', bitAndNumber)

src/function/arithmetic/one.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export const createOne = /* #__PURE__ */ factory(
4848
/**
4949
* Return the multiplicative identity of the same type as the argument.
5050
*
51-
*
5251
* Syntax:
5352
*
5453
* math.one(x)
@@ -62,7 +61,7 @@ export const createOne = /* #__PURE__ */ factory(
6261
* math.one([[2, 3], [4, 5]]) // [[1, 0], [0,1]]
6362
*
6463
* See also:
65-
* typeOf, numeric
64+
* typeOf, numeric, zero
6665
*
6766
* @param {MathType} x Any entity mathjs understands
6867
* @return {MathType} Multiplicative identity of same type as x

src/function/arithmetic/zero.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { factory } from '../../utils/factory.js'
2+
3+
const name = 'zero'
4+
const dependencies = [
5+
'typed', '?BigNumber', '?Complex', '?Fraction', '?unit', 'size', 'zeros'
6+
]
7+
8+
export const createZeroNumber = /* #__PURE__ */ factory(
9+
name, ['typed'], ({ typed }) => {
10+
return typed(name, { number: () => 0 })
11+
})
12+
13+
export const createZero = /* #__PURE__ */ factory(name, dependencies, ({
14+
typed, BigNumber, Complex, Fraction, unit, size, zeros
15+
}) => {
16+
/**
17+
* Return the additive identity of the same type as the argument.
18+
*
19+
* Syntax:
20+
*
21+
* math.zero(x)
22+
*
23+
* Examples:
24+
*
25+
* math.zero(1.618) // returns 0
26+
* math.zero(math.bignumber(222)) // BigNumber 0
27+
* math.zero(math.fraction(1, 3)) // Fraction 0
28+
* math.zero(math.evaluate('0 + 2i')) // Complex 0+0i
29+
* math.zero([[2, 3, 4], [4, 5, 6]]) // [[0, 0, 0], [0, 0, 0]]
30+
*
31+
* See also:
32+
* typeOf, numeric, one
33+
*
34+
* @param {MathType} x Any entity mathjs understands
35+
* @return {MathType} Additive identity of same type as x
36+
*/
37+
return typed(name, {
38+
number: () => 0,
39+
bigint: () => 0n,
40+
BigNumber: () => new BigNumber(0),
41+
Complex: () => new Complex(0),
42+
Fraction: () => new Fraction(0),
43+
boolean: () => false,
44+
Unit: typed.referToSelf(self => u => {
45+
if (u.value === undefined || u.value === null) return unit(0)
46+
return unit(self(u.value))
47+
}),
48+
Array: A => zeros(size(A)).valueOf(),
49+
Matrix: M => zeros(size(M))
50+
})
51+
})

src/function/arithmetic/zero.js~

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { factory } from '../../utils/factory.js'
2+
3+
const name = 'zero'
4+
const dependencies = [
5+
'typed', '?BigNumber', '?Complex', '?Fraction', '?unit', 'size', 'zeros'
6+
]
7+
8+
export const createZeroNumber = /* #__PURE__ */ factory(
9+
name, ['typed'], ({ typed }) => {
10+
return typed(name, { number: () => 0 })
11+
})
12+
13+
export const createZero = /* #__PURE__ */ factory(name, dependencies, ({
14+
typed, BigNumber, Complex, Fraction, unit, size, zeros
15+
}) => {
16+
/**
17+
* Return the additive identity of the same type as the argument.
18+
*
19+
* Syntax:
20+
*
21+
* math.zero(x)
22+
*
23+
* Examples:
24+
*
25+
* math.zero(1.618) // returns 0
26+
* math.zero(math.bignumber(222)) // BigNumber 0
27+
* math.zero(math.fraction(1, 3)) // Fraction 0
28+
* math.zero(math.evaluate('0 + 2i')) // Complex 0+0i
29+
* math.zero([[2, 3, 4], [4, 5, 6]]) // [[0, 0, 0], [0, 0, 0]]
30+
*
31+
* See also:
32+
* typeOf, numeric, one
33+
*
34+
* @param {MathType} x Any entity mathjs understands
35+
* @return {MathType} Additive identity of same type as x
36+
*/
37+
return typed(name, {
38+
number: () => 0,
39+
bigint: () => 0n,
40+
BigNumber: () => new BigNumber(0),
41+
Complex: () => new Complex(0),
42+
Fraction: () => new Fraction(0),
43+
boolean: () => false,
44+
Unit: typed.referToSelf(self => u => {
45+
if (u.value === undefined || u.value === null) return unit(0)
46+
return unit(self(u.value))
47+
})
48+
Array: A => zeros(size(A)).valueOf(),
49+
Matrix: M => zeros(size(M))
50+
})
51+
})

test/typescript-tests/testTypes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ Basic usage examples
8888
if (math.one(3.5) === 2) {
8989
console.error('This should not happen')
9090
}
91+
// @ts-expect-error: since zero(bigint) returns 0n, this comparison fails
92+
if (math.zero(-23n) === 1n) {
93+
console.error('Nor should this happen')
94+
}
9195
const angle = 0.2
9296
math.add(math.pow(math.sin(angle), 2), math.pow(math.cos(angle), 2))
9397
math.add(2, 3, 4)
@@ -1909,6 +1913,7 @@ Units examples
19091913
math.divide(math.unit('1 m'), math.unit('1 s'))
19101914
math.pow(math.unit('12 in'), 3)
19111915
math.one(math.unit('5m'))
1916+
math.zero(math.unit('22 m/secs^2'))
19121917

19131918
// units can be converted to a specific type, or to a number
19141919
b.to('cm')

0 commit comments

Comments
 (0)