|
| 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 | +}) |
0 commit comments