diff --git a/README.md b/README.md index 28110558a0..55a8b1008a 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ A common case is to implement a new function. This involves the following steps: - Write documentation on the function in the source code comment of `myNewFunction.js`. This documentation is used to auto generate documentation on the website. - Write embedded documentation for the new function in `./src/expression/embeddedDocs/function/arithmetic/myNewFunction.js`. Add the new documentation to the index file `./src/expression/embeddedDocs/embeddedDocs.js`. - Write unit tests for the function in `./test/unit-tests/function/arithmetic/myNewFunction.test.js`. -- Write the necessary TypeScript definitions for the new function in `./types/index.d.ts`, and write tests for it in `./test/typescript-tests/testTypes.ts`. This is described in [./types/EXPLANATION.md](./types/EXPLANATION.md). +- Write the necessary TypeScript definitions for the new function in `./types/index.d.ts`, and write tests for it in `./test/typescript-tests/testTypes.ts`. This is described in [./types/EXPLANATION.md](./types/EXPLANATION.md) -- make sure to read that page, as Typescript definitions must be added in _multiple_ places in the code. - Ensure the code style is ok by running `npm run lint` (run `npm run format` to fix the code style automatically). diff --git a/src/expression/embeddedDocs/embeddedDocs.js b/src/expression/embeddedDocs/embeddedDocs.js index 0696f3c679..9c6d124161 100644 --- a/src/expression/embeddedDocs/embeddedDocs.js +++ b/src/expression/embeddedDocs/embeddedDocs.js @@ -238,6 +238,8 @@ import { hasNumericValueDocs } from './function/utils/hasNumericValue.js' import { hexDocs } from './function/utils/hex.js' import { isIntegerDocs } from './function/utils/isInteger.js' import { isNaNDocs } from './function/utils/isNaN.js' +import { isBoundedDocs } from './function/utils/isBounded.js' +import { isFiniteDocs } from './function/utils/isFinite.js' import { isNegativeDocs } from './function/utils/isNegative.js' import { isNumericDocs } from './function/utils/isNumeric.js' import { isPositiveDocs } from './function/utils/isPositive.js' @@ -598,6 +600,8 @@ export const embeddedDocs = { oct: octDocs, hex: hexDocs, isNaN: isNaNDocs, + isBounded: isBoundedDocs, + isFinite: isFiniteDocs, isInteger: isIntegerDocs, isNegative: isNegativeDocs, isNumeric: isNumericDocs, diff --git a/src/expression/embeddedDocs/function/utils/isBounded.js b/src/expression/embeddedDocs/function/utils/isBounded.js new file mode 100644 index 0000000000..1c983e02b5 --- /dev/null +++ b/src/expression/embeddedDocs/function/utils/isBounded.js @@ -0,0 +1,14 @@ +export const isBoundedDocs = { + name: 'isBounded', + category: 'Utils', + syntax: [ + 'isBounded(x)' + ], + description: 'Test whether a value or its entries are bounded.', + examples: [ + 'isBounded(Infinity)', + 'isBounded(bigint(3))', + 'isBounded([3, -Infinity, -3])' + ], + seealso: ['isFinite', 'isNumeric', 'isNaN', 'isNegative', 'isPositive'] +} diff --git a/src/expression/embeddedDocs/function/utils/isFinite.js b/src/expression/embeddedDocs/function/utils/isFinite.js new file mode 100644 index 0000000000..97d6b0471d --- /dev/null +++ b/src/expression/embeddedDocs/function/utils/isFinite.js @@ -0,0 +1,14 @@ +export const isFiniteDocs = { + name: 'isFinite', + category: 'Utils', + syntax: [ + 'isFinite(x)' + ], + description: 'Test whether a value is finite, elementwise on collections.', + examples: [ + 'isFinite(Infinity)', + 'isFinite(bigint(3))', + 'isFinite([3, -Infinity, -3])' + ], + seealso: ['isBounded', 'isNumeric', 'isNaN', 'isNegative', 'isPositive'] +} diff --git a/src/expression/node/ConstantNode.js b/src/expression/node/ConstantNode.js index 7ab4443a22..e227f96c20 100644 --- a/src/expression/node/ConstantNode.js +++ b/src/expression/node/ConstantNode.js @@ -5,10 +5,10 @@ import { factory } from '../../utils/factory.js' const name = 'ConstantNode' const dependencies = [ - 'Node' + 'Node', 'isBounded' ] -export const createConstantNode = /* #__PURE__ */ factory(name, dependencies, ({ Node }) => { +export const createConstantNode = /* #__PURE__ */ factory(name, dependencies, ({ Node, isBounded }) => { class ConstantNode extends Node { /** * A ConstantNode holds a constant value like a number or string. @@ -149,8 +149,7 @@ export const createConstantNode = /* #__PURE__ */ factory(name, dependencies, ({ case 'number': case 'BigNumber': { - const finite = type === 'BigNumber' ? this.value.isFinite() : isFinite(this.value) - if (!finite) { + if (!isBounded(this.value)) { return (this.value.valueOf() < 0) ? '-\\infty' : '\\infty' diff --git a/src/factoriesAny.js b/src/factoriesAny.js index 4b7d70bf49..89b49af029 100644 --- a/src/factoriesAny.js +++ b/src/factoriesAny.js @@ -14,6 +14,8 @@ export { createHasNumericValue } from './function/utils/hasNumericValue.js' export { createIsPositive } from './function/utils/isPositive.js' export { createIsZero } from './function/utils/isZero.js' export { createIsNaN } from './function/utils/isNaN.js' +export { createIsBounded } from './function/utils/isBounded.js' +export { createIsFinite } from './function/utils/isFinite.js' export { createTypeOf } from './function/utils/typeOf.js' export { createEqualScalar } from './function/relational/equalScalar.js' export { createSparseMatrixClass } from './type/matrix/SparseMatrix.js' diff --git a/src/factoriesNumber.js b/src/factoriesNumber.js index 21b4834270..0047dc3624 100644 --- a/src/factoriesNumber.js +++ b/src/factoriesNumber.js @@ -322,6 +322,8 @@ export { createHasNumericValue } from './function/utils/hasNumericValue.js' export const createIsPositive = /* #__PURE__ */ createNumberFactory('isPositive', isPositiveNumber) export const createIsZero = /* #__PURE__ */ createNumberFactory('isZero', isZeroNumber) export const createIsNaN = /* #__PURE__ */ createNumberFactory('isNaN', isNaNNumber) +export { createIsBounded } from './function/utils/isBounded.js' +export { createIsFinite } from './function/utils/isFinite.js' export { createTypeOf } from './function/utils/typeOf.js' export { createIsPrime } from './function/utils/isPrime.js' export { createNumeric } from './function/utils/numeric.js' diff --git a/src/function/algebra/simplifyConstant.js b/src/function/algebra/simplifyConstant.js index 06232c9716..f7a2417809 100644 --- a/src/function/algebra/simplifyConstant.js +++ b/src/function/algebra/simplifyConstant.js @@ -10,6 +10,7 @@ const dependencies = [ 'config', 'mathWithTransform', 'matrix', + 'isBounded', '?fraction', '?bignumber', 'AccessorNode', @@ -27,6 +28,7 @@ export const createSimplifyConstant = /* #__PURE__ */ factory(name, dependencies config, mathWithTransform, matrix, + isBounded, fraction, bignumber, AccessorNode, @@ -141,7 +143,7 @@ export const createSimplifyConstant = /* #__PURE__ */ factory(name, dependencies // and when both numerator and denominator are small enough function _exactFraction (n, options) { const exactFractions = (options && options.exactFractions !== false) - if (exactFractions && isFinite(n) && fraction) { + if (exactFractions && isBounded(n) && fraction) { const f = fraction(n) const fractionsLimit = (options && typeof options.fractionsLimit === 'number') ? options.fractionsLimit diff --git a/src/function/arithmetic/nthRoots.js b/src/function/arithmetic/nthRoots.js index ca0c3c12d5..42abfef79b 100644 --- a/src/function/arithmetic/nthRoots.js +++ b/src/function/arithmetic/nthRoots.js @@ -63,7 +63,11 @@ export const createNthRoots = /* #__PURE__ */ factory(name, dependencies, ({ typ * Calculate the nth roots of a value. * An nth root of a positive real number A, * is a positive real solution of the equation "x^root = A". - * This function returns an array of complex values. + * This function returns an array of Complex values. + * Note that currently the precision of Complex numbers are limited + * to the precision of a 64-bit IEEE floating point, so even if the input + * is a BigNumber with greater precision, rounding to 64 bits will occur + * in computing the nth roots. * * Syntax: * diff --git a/src/function/special/zeta.js b/src/function/special/zeta.js index 675a9080d3..303026601d 100644 --- a/src/function/special/zeta.js +++ b/src/function/special/zeta.js @@ -1,9 +1,9 @@ import { factory } from '../../utils/factory.js' const name = 'zeta' -const dependencies = ['typed', 'config', 'multiply', 'pow', 'divide', 'factorial', 'equal', 'smallerEq', 'isNegative', 'gamma', 'sin', 'subtract', 'add', '?Complex', '?BigNumber', 'pi'] +const dependencies = ['typed', 'config', 'multiply', 'pow', 'divide', 'factorial', 'equal', 'smallerEq', 'isBounded', 'isNegative', 'gamma', 'sin', 'subtract', 'add', '?Complex', '?BigNumber', 'pi'] -export const createZeta = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, multiply, pow, divide, factorial, equal, smallerEq, isNegative, gamma, sin, subtract, add, Complex, BigNumber, pi }) => { +export const createZeta = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, multiply, pow, divide, factorial, equal, smallerEq, isBounded, isNegative, gamma, sin, subtract, add, Complex, BigNumber, pi }) => { /** * Compute the Riemann Zeta function of a value using an infinite series for * all of the complex plane using Riemann's Functional equation. @@ -57,7 +57,7 @@ export const createZeta = /* #__PURE__ */ factory(name, dependencies, ({ typed, if (equal(s, 1)) { return createValue(NaN) } - if (!isFinite(s)) { + if (!isBounded(s)) { return isNegative(s) ? createValue(NaN) : createValue(1) } diff --git a/src/function/utils/isBounded.js b/src/function/utils/isBounded.js new file mode 100644 index 0000000000..67c31ae387 --- /dev/null +++ b/src/function/utils/isBounded.js @@ -0,0 +1,49 @@ +import { factory } from '../../utils/factory.js' + +const name = 'isBounded' +const dependencies = ['typed'] + +export const createIsBounded = /* #__PURE__ */ factory(name, dependencies, ({ + typed +}) => { + /** + * Test whether a value is bounded. For scalars, this test is equivalent + * to the isFinite finiteness test. On the other hand, a Matrix or Array + * is defined to be bounded if every entry is finite. + * + * Syntax: + * + * math.isBounded(x) + * + * Examples: + * + * math.isBounded(0) // returns true + * math.isBounded(NaN) // returns false + * math.isBounded(math.bignumber(Infinity)) // returns false + * math.isBounded(math.fraction(1,3)) // returns true + * math.isBounded(math.complex('2 - 4i')) // returns true + * math.isBounded(-10000000000000000n) // returns true + * math.isBounded(undefined) // returns false + * math.isBounded(null) // returns false + * math.isBounded([0.001, -3n, 0]) // returns true + * math.isBounded([2, -Infinity, -3]) // returns false + * + * See also: + * + * isFinite, isNumeric, isPositive, isNegative, isNaN + * + * @param {number | BigNumber | bigint | Complex | Fraction | Unit | Array | Matrix} x Value to be tested + * @return {boolean} Returns true when `x` is bounded. + */ + return typed(name, { + number: n => Number.isFinite(n), + 'BigNumber | Complex': x => x.isFinite(), + 'bigint | Fraction': () => true, + 'null | undefined': () => false, + Unit: typed.referToSelf(self => x => self(x.value)), + 'Array | Matrix': typed.referToSelf(self => A => { + if (!Array.isArray(A)) A = A.valueOf() + return A.every(entry => self(entry)) + }) + }) +}) diff --git a/src/function/utils/isFinite.js b/src/function/utils/isFinite.js new file mode 100644 index 0000000000..57dd2b10f0 --- /dev/null +++ b/src/function/utils/isFinite.js @@ -0,0 +1,43 @@ +import { factory } from '../../utils/factory.js' + +const name = 'isFinite' +const dependencies = ['typed', 'isBounded', 'map'] + +export const createIsFinite = /* #__PURE__ */ factory(name, dependencies, ({ + typed, isBounded, map +}) => { + /** + * Test whether a value is finite. + * + * Operates elementwise on Array and Matrix values. To test if all entries + * of an Array or Matrix are finite, use isBounded. + * + * Syntax: + * + * math.isFinite(x) + * + * Examples: + * + * math.isFinite(0) // returns true + * math.isFinite(NaN) // returns false + * math.isFinite(math.bignumber(Infinity)) // returns false + * math.isFinite(math.fraction(1,3)) // returns true + * math.isFinite(math.complex('2 - 4i')) // returns true + * math.isFinite(-10000000000000000n) // returns true + * math.isFinite(undefined) // returns false + * math.isFinite(null) // returns false + * math.isFinite([0.001, -3n, 0]) // Array [true, true, true] + * math.isFinite([2, -Infinity, -3]) // Array [true, false, true] + * + * See also: + * + * isBounded isNumeric, isPositive, isNegative, isNaN + * + * @param {number | BigNumber | bigint | Complex | Fraction | Unit | Array | Matrix} x Value to be tested + * @return {boolean | Array | Matrix} + */ + return typed(name, { + 'Array | Matrix': A => map(A, isBounded), + any: x => isBounded(x) + }) +}) diff --git a/src/json/replacer.js b/src/json/replacer.js index e085d82243..13568c42e2 100644 --- a/src/json/replacer.js +++ b/src/json/replacer.js @@ -18,7 +18,7 @@ export const createReplacer = /* #__PURE__ */ factory(name, dependencies, () => */ return function replacer (key, value) { // the numeric values Infinitiy, -Infinity, and NaN cannot be serialized to JSON - if (typeof value === 'number' && (!isFinite(value) || isNaN(value))) { + if (typeof value === 'number' && (!Number.isFinite(value) || isNaN(value))) { return { mathjs: 'number', value: String(value) diff --git a/src/plain/number/probability.js b/src/plain/number/probability.js index 4dd1731641..fb15bd3f35 100644 --- a/src/plain/number/probability.js +++ b/src/plain/number/probability.js @@ -8,7 +8,7 @@ export function gammaNumber (n) { if (isInteger(n)) { if (n <= 0) { - return isFinite(n) ? Infinity : NaN + return Number.isFinite(n) ? Infinity : NaN } if (n > 171) { @@ -91,7 +91,7 @@ export const lgammaSeries = [ export function lgammaNumber (n) { if (n < 0) return NaN if (n === 0) return Infinity - if (!isFinite(n)) return n + if (!Number.isFinite(n)) return n if (n < 0.5) { // Use Euler's reflection formula: diff --git a/src/plain/number/trigonometry.js b/src/plain/number/trigonometry.js index 03bf6e04c7..f67deabe11 100644 --- a/src/plain/number/trigonometry.js +++ b/src/plain/number/trigonometry.js @@ -19,7 +19,7 @@ export function acotNumber (x) { acotNumber.signature = n1 export function acothNumber (x) { - return isFinite(x) + return Number.isFinite(x) ? (Math.log((x + 1) / x) + Math.log(x / (x - 1))) / 2 : 0 } diff --git a/src/type/fraction/function/fraction.js b/src/type/fraction/function/fraction.js index e97e155a5d..e1cf0dbf4b 100644 --- a/src/type/fraction/function/fraction.js +++ b/src/type/fraction/function/fraction.js @@ -44,7 +44,7 @@ export const createFraction = /* #__PURE__ */ factory(name, dependencies, ({ typ */ return typed('fraction', { number: function (x) { - if (!isFinite(x) || isNaN(x)) { + if (!Number.isFinite(x) || isNaN(x)) { throw new Error(x + ' cannot be represented as a fraction') } diff --git a/src/utils/number.js b/src/utils/number.js index eda0bd8515..78dd910a40 100644 --- a/src/utils/number.js +++ b/src/utils/number.js @@ -14,7 +14,7 @@ export function isInteger (value) { return true } - return isFinite(value) + return Number.isFinite(value) ? (value === Math.round(value)) : false } @@ -111,7 +111,7 @@ export const cbrt = Math.cbrt || function cbrt (x) { x = -x } - if (isFinite(x)) { + if (Number.isFinite(x)) { result = Math.exp(Math.log(x) / 3) // from https://en.wikipedia.org/wiki/Cube_root#Numerical_methods result = (x / (result * result) + (2 * result)) / 3 @@ -396,7 +396,7 @@ export function splitNumber (value) { * @param {number} [precision] Optional number of significant figures to return. */ export function toEngineering (value, precision) { - if (isNaN(value) || !isFinite(value)) { + if (isNaN(value) || !Number.isFinite(value)) { return String(value) } @@ -451,7 +451,7 @@ export function toEngineering (value, precision) { * decimal point. null by default. */ export function toFixed (value, precision) { - if (isNaN(value) || !isFinite(value)) { + if (isNaN(value) || !Number.isFinite(value)) { return String(value) } @@ -490,7 +490,7 @@ export function toFixed (value, precision) { * is used. */ export function toExponential (value, precision) { - if (isNaN(value) || !isFinite(value)) { + if (isNaN(value) || !Number.isFinite(Number(value))) { return String(value) } @@ -522,7 +522,7 @@ export function toExponential (value, precision) { * @return {string} */ export function toPrecision (value, precision, options) { - if (isNaN(value) || !isFinite(value)) { + if (isNaN(value) || !Number.isFinite(value)) { return String(value) } @@ -670,7 +670,7 @@ export function nearlyEqual (a, b, relTol = 1e-8, absTol = 0) { return false } - if (!isFinite(a) || !isFinite(b)) { + if (!Number.isFinite(a) || !Number.isFinite(b)) { return a === b } diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index 1797125087..fa8db01a0d 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -79,6 +79,8 @@ Basic usage examples math.round(100.123, 3) const _res = math.atan2(3, -3) / math.pi math.log(10000, 10) + math.nthRoot(16, 5) + math.nthRoots(1, 3) math.sqrt(-4) math.pow(m2by2, 2) @@ -974,11 +976,29 @@ Chaining examples .log10() ).toMatchTypeOf>() + // nthRoot(s) + expectTypeOf(math.chain(81).nthRoot(4)).toMatchTypeOf< + MathJsChain + >() + expectTypeOf(math.chain(1).nthRoots(5)).toMatchTypeOf< + MathJsChain> + >() + expectTypeOf(math.chain([1, 2]).count()).toMatchTypeOf>() expectTypeOf(math.chain('mathjs').count()).toMatchTypeOf< MathJsChain >() expectTypeOf(math.chain([1, 2]).sum()).toMatchTypeOf>() + + expectTypeOf(math.chain(7).isBounded()).toMatchTypeOf>() + expectTypeOf(math.chain(8).isFinite()).toMatchTypeOf>() + expectTypeOf(math.chain([1, Infinity]).isBounded()).toMatchTypeOf< + MathJsChain + >() + expectTypeOf(math.chain([1, Infinity]).isFinite()).toMatchTypeOf< + MathJsChain + >() + // TODO complete the rest of these... } @@ -2680,6 +2700,12 @@ Factory Test ]) assert.strictEqual(math.hasNumericValue(math.fraction(4)), true) assert.strictEqual(math.hasNumericValue(math.complex('2-4i')), false) + assert.strictEqual(math.isBounded(NaN), false) + assert.deepStrictEqual(math.isFinite([2, math.fraction(-3, 4), Infinity]), [ + true, + true, + false + ]) } /** diff --git a/test/unit-tests/function/probability/factorial.test.js b/test/unit-tests/function/probability/factorial.test.js index ff44d7ffbd..dde6bf970c 100644 --- a/test/unit-tests/function/probability/factorial.test.js +++ b/test/unit-tests/function/probability/factorial.test.js @@ -11,8 +11,8 @@ describe('factorial', function () { assert.strictEqual(factorial(3), 6) assert.strictEqual(factorial(4), 24) assert.strictEqual(factorial(5), 120) - assert.ok(!isFinite(factorial(Number.MAX_VALUE))) // shouldn't stall - assert.ok(!isFinite(factorial(Infinity))) + assert.ok(!Number.isFinite(factorial(Number.MAX_VALUE))) // shouldn't stall + assert.ok(!Number.isFinite(factorial(Infinity))) }) it('should calculate the factorial of a bignumber', function () { diff --git a/test/unit-tests/function/probability/lgamma.test.js b/test/unit-tests/function/probability/lgamma.test.js index 557264d714..e799e9030f 100644 --- a/test/unit-tests/function/probability/lgamma.test.js +++ b/test/unit-tests/function/probability/lgamma.test.js @@ -207,8 +207,8 @@ describe('lgamma', function () { assert.ok(isNaN(lgamma(NaN))) assert.ok(isNaN(lgamma(-NaN))) - assert.ok(!isFinite(lgamma(Infinity))) - assert.ok(!isFinite(lgamma(-Infinity))) + assert.ok(!Number.isFinite(lgamma(Infinity))) + assert.ok(!Number.isFinite(lgamma(-Infinity))) }) it('should throw an error if called with a big number', function () { diff --git a/test/unit-tests/function/utils/isBounded.test.js b/test/unit-tests/function/utils/isBounded.test.js new file mode 100644 index 0000000000..b20d8bc608 --- /dev/null +++ b/test/unit-tests/function/utils/isBounded.test.js @@ -0,0 +1,32 @@ +import assert from 'assert' +import math from '../../../../src/defaultInstance.js' + +const isBounded = math.isBounded + +describe('isBounded', function () { + it('should check scalars for boundednesss', function () { + assert(isBounded(0)) + assert(isBounded(math.bignumber('0'))) + assert(isBounded(math.fraction(0))) + assert(isBounded(math.evaluate('0 + 0i'))) + assert(isBounded(0n)) + assert(isBounded(math.unit('0 kB'))) + + assert.strictEqual(isBounded(null), false) + assert.strictEqual(isBounded(undefined), false) + assert.strictEqual(isBounded(Infinity), false) + assert.strictEqual(isBounded(math.bignumber(NaN)), false) + assert.strictEqual(isBounded(math.unit(-Infinity, 'm')), false) + assert.strictEqual(isBounded('Infinity'), false) + }) + + it('should check every element of an Array/Matrix is bounded', function () { + assert(isBounded([1n, 1, math.complex(1, 1)])) + assert(isBounded(math.identity(3))) + + assert.strictEqual(isBounded([0, 0, NaN, 0]), false) + const I = math.identity(4) + I.set([2, 2], Infinity) + assert.strictEqual(isBounded(I), false) + }) +}) diff --git a/test/unit-tests/function/utils/isFinite.test.js b/test/unit-tests/function/utils/isFinite.test.js new file mode 100644 index 0000000000..e0cf8c0ab9 --- /dev/null +++ b/test/unit-tests/function/utils/isFinite.test.js @@ -0,0 +1,38 @@ +import assert from 'assert' +import math from '../../../../src/defaultInstance.js' + +const isFinite = math.isFinite + +describe('isFinite', function () { + it('should check scalars for finiteness', function () { + assert(isFinite(0)) + assert(isFinite(math.bignumber('0'))) + assert(isFinite(math.fraction(0))) + assert(isFinite(math.evaluate('0 + 0i'))) + assert(isFinite(0n)) + assert(isFinite(math.unit('0 kB'))) + + assert.strictEqual(isFinite(null), false) + assert.strictEqual(isFinite(undefined), false) + assert.strictEqual(isFinite(Infinity), false) + assert.strictEqual(isFinite(math.bignumber(NaN)), false) + assert.strictEqual(isFinite(math.unit(-Infinity, 'm')), false) + assert.strictEqual(isFinite('Infinity'), false) + }) + + it('should test finiteness of an Array/Matrix elementwise', function () { + assert.deepStrictEqual( + isFinite([1n, 1, math.complex(1, 1)]), [true, true, true]) + assert.deepStrictEqual( + isFinite(math.identity(3)), + math.matrix( + [[true, true, true], [true, true, true], [true, true, true]])) + + assert.deepStrictEqual( + isFinite([0, 0, NaN, 0]), [true, true, false, true]) + const I = math.identity(2) + I.set([1, 1], Infinity) + assert.deepStrictEqual( + isFinite(I), math.matrix([[true, true], [true, false]])) + }) +}) diff --git a/types/index.d.ts b/types/index.d.ts index 2049487bae..4c4dd60d75 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -523,6 +523,58 @@ export interface MathJsInstance extends MathJsFactory { SQRT2: number tau: number + // Physical constants + atomicMass: Unit + avogadro: Unit + bohrMagneton: Unit + bohrRadius: Unit + boltzmann: Unit + classicalElectronRadius: Unit + conductanceQuantum: Unit + coulomb: Unit + deuteronMass: Unit + efimovFactor: Unit + electricConstant: Unit + electronMass: Unit + elementaryCharge: Unit + faraday: Unit + fermiCoupling: Unit + fineStructure: Unit + firstRadiation: Unit + gasConstant: Unit + gravitationConstant: Unit + gravity: Unit + hartreeEnergy: Unit + inverseConductanceQuantum: Unit + klitzing: Unit + loschmidt: Unit + magneticConstant: Unit + magneticFluxQuantum: Unit + molarMass: Unit + molarMassC12: Unit + molarPlanckConstant: Unit + molarVolume: Unit + neutronMass: Unit + nuclearMagneton: Unit + planckCharge: Unit + planckConstant: Unit + planckLength: Unit + planckMass: Unit + planckTemperature: Unit + planckTime: Unit + protonMass: Unit + quantumOfCirculation: Unit + reducedPlanckConstant: Unit + rydberg: Unit + sackurTetrode: Unit + secondRadiation: Unit + speedOfLight: Unit + stefanBoltzmann: Unit + thomsonCrossSection: Unit + vacuumImpedance: Unit + weakMixingAngle: Unit + wienDisplacement: Unit + // Class-like constructors Node: NodeCtor AccessorNode: AccessorNodeCtor @@ -1492,9 +1544,18 @@ export interface MathJsInstance extends MathJsFactory { * @return The nth root of a */ nthRoot( - a: number | BigNumber | MathCollection | Complex, + a: number | BigNumber | Complex, root?: number | BigNumber - ): number | Complex | MathCollection + ): number | Complex + nthRoot(M: MathCollection, root?: number | BigNumber): MathCollection + + /** + * Calculate all nth roots of a value. + * @param a Value for which to calculate the nth roots + * @param n Which roots. Default value: 2. + * @return An array of Complex numbers giving the n nth roots of a + */ + nthRoots(a: number | BigNumber | Complex, n?: number): Array /** * Calculates the power of x to y, x ^ y. Matrix exponentiation is @@ -2088,6 +2149,13 @@ export interface MathJsInstance extends MathJsFactory { callback: (value: any, index: number[], matrix: T) => void ): void + /** + * Return the (name of the) data type of the elements of matrix, or 'mixed'. + * @param m the matrix + * @returns A string specifying the data type of the elements of m + */ + getMatrixDataType(m: MathCollection): string + /** * Calculate the inverse of a square matrix. * @param x Matrix to be inverted @@ -3632,6 +3700,21 @@ export interface MathJsInstance extends MathJsFactory { // eslint-disable-next-line @typescript-eslint/no-explicit-any hasNumericValue(x: any): boolean | boolean[] + /** + * Test whether a value is bounded + * @param x Value to be tested + * @returns Boolean true when x represents a bounded mathematical entity + */ + isBounded(x: MathType): boolean + + /** + * Test whether a value is finite, elementwise on collections + * @param x Value to be tested + * @returns Boolean | MathCollection + */ + isFinite(x: MathScalarType): boolean + isFinite(A: MathCollection): MathCollection + /** * Test whether a value is an integer number. The function supports * number, BigNumber, and Fraction. The function is evaluated @@ -3768,141 +3851,340 @@ export interface MathJsFactory { } export const { + // system dependencies all, - typedDependencies, ResultSetDependencies, + FibonacciHeapDependencies, + SpaDependencies, BigNumberDependencies, ComplexDependencies, FractionDependencies, RangeDependencies, - MatrixDependencies, DenseMatrixDependencies, - cloneDependencies, - isIntegerDependencies, - isNegativeDependencies, - isNumericDependencies, - hasNumericValueDependencies, - isPositiveDependencies, - isZeroDependencies, - isNaNDependencies, - typeOfDependencies, - typeofDependencies, - equalScalarDependencies, + ImmutableDenseMatrixDependencies, SparseMatrixDependencies, - numberDependencies, - stringDependencies, - booleanDependencies, + IndexDependencies, + ParserDependencies, + HelpDependencies, + ChainDependencies, + reviverDependencies, + versionDependencies, + + // core function dependencies + typedDependencies, + // create and factory do not have dependencies + + // constant dependencies + eDependencies, + piDependencies, + iDependencies, + InfinityDependencies, + LN2Dependencies, + LN10Dependencies, + LOG2EDependencies, + LOG10EDependencies, + phiDependencies, + SQRT1_2Dependencies, + SQRT2Dependencies, + tauDependencies, + + // physical constant dependencies + atomicMassDependencies, + avogadroDependencies, + bohrMagnetonDependencies, + bohrRadiusDependencies, + boltzmannDependencies, + classicalElectronRadiusDependencies, + conductanceQuantumDependencies, + coulombDependencies, + deuteronMassDependencies, + efimovFactorDependencies, + electricConstantDependencies, + electronMassDependencies, + elementaryChargeDependencies, + faradayDependencies, + fermiCouplingDependencies, + fineStructureDependencies, + firstRadiationDependencies, + gasConstantDependencies, + gravitationConstantDependencies, + gravityDependencies, + hartreeEnergyDependencies, + inverseConductanceQuantumDependencies, + klitzingDependencies, + loschmidtDependencies, + magneticConstantDependencies, + magneticFluxQuantumDependencies, + molarMassDependencies, + molarMassC12Dependencies, + molarPlanckConstantDependencies, + molarVolumeDependencies, + neutronMassDependencies, + nuclearMagnetonDependencies, + planckChargeDependencies, + planckConstantDependencies, + planckLengthDependencies, + planckMassDependencies, + planckTemperatureDependencies, + planckTimeDependencies, + protonMassDependencies, + quantumOfCirculationDependencies, + reducedPlanckConstantDependencies, + rydbergDependencies, + sackurTetrodeDependencies, + secondRadiationDependencies, + speedOfLightDependencies, + stefanBoltzmannDependencies, + thomsonCrossSectionDependencies, + vacuumImpedanceDependencies, + weakMixingAngleDependencies, + wienDisplacementDependencies, + + // constructor dependencies + NodeDependencies, + AccessorNodeDependencies, + ArrayNodeDependencies, + AssignmentNodeDependencies, + BlockNodeDependencies, + ConditionalNodeDependencies, + ConstantNodeDependencies, + FunctionAssignmentNodeDependencies, + FunctionNodeDependencies, + IndexNodeDependencies, + ObjectNodeDependencies, + OperatorNodeDependencies, + ParenthesisNodeDependencies, + RangeNodeDependencies, + RelationalNodeDependencies, + SymbolNodeDependencies, + MatrixDependencies, + UnitDependencies, + + // construction function dependencies bignumberDependencies, + bigintDependencies, + booleanDependencies, + chainDependencies, complexDependencies, + createUnitDependencies, fractionDependencies, + indexDependencies, matrixDependencies, + numberDependencies, + numericDependencies, + sparseDependencies, splitUnitDependencies, - unaryMinusDependencies, - unaryPlusDependencies, + stringDependencies, + unitDependencies, + + // expression function dependencies + compileDependencies, + evaluateDependencies, + helpDependencies, + parseDependencies, + parserDependencies, + + // algebra dependencies + derivativeDependencies, + lsolveDependencies, + lupDependencies, + lusolveDependencies, + polynomialRootDependencies, + qrDependencies, + rationalizeDependencies, + simplifyDependencies, + simplifyConstantDependencies, + simplifyCoreDependencies, + symbolicEqualDependencies, + leafCountDependencies, + resolveDependencies, + sluDependencies, + usolveDependencies, + + // arithmetic function dependencies absDependencies, - mapSlicesDependencies, + addDependencies, addScalarDependencies, cbrtDependencies, ceilDependencies, cubeDependencies, + divideDependencies, + divideScalarDependencies, + dotDivideDependencies, + dotMultiplyDependencies, + dotPowDependencies, expDependencies, expm1Dependencies, fixDependencies, floorDependencies, gcdDependencies, + hypotDependencies, lcmDependencies, + logDependencies, log10Dependencies, + log1pDependencies, log2Dependencies, + matrixFromRowsDependencies, + matrixFromColumnsDependencies, + matrixFromFunctionDependencies, modDependencies, - multiplyScalarDependencies, multiplyDependencies, + multiplyScalarDependencies, + normDependencies, nthRootDependencies, + nthRootsDependencies, + powDependencies, + roundDependencies, signDependencies, sqrtDependencies, squareDependencies, subtractDependencies, + unaryMinusDependencies, + unaryPlusDependencies, xgcdDependencies, - dotMultiplyDependencies, + + // bitwise dependencies bitAndDependencies, bitNotDependencies, bitOrDependencies, bitXorDependencies, + leftShiftDependencies, + rightArithShiftDependencies, + rightLogShiftDependencies, + + // combinatorics dependencies + bellNumbersDependencies, + catalanDependencies, + compositionDependencies, + stirlingS2Dependencies, + + // complex dependencies argDependencies, conjDependencies, imDependencies, reDependencies, + + // geometry dependencies + distanceDependencies, + intersectDependencies, + + // logical dependencies + andDependencies, notDependencies, orDependencies, xorDependencies, + + // matrix function dependencies + mapSlicesDependencies, + // array is deprecated concatDependencies, - columnDependencies, crossDependencies, + ctransposeDependencies, + detDependencies, + diffDependencies, diagDependencies, - eyeDependencies, + dotDependencies, + eigsDependencies, + expmDependencies, + sylvesterDependencies, + schurDependencies, + lyapDependencies, + identityDependencies, filterDependencies, flattenDependencies, forEachDependencies, getMatrixDataTypeDependencies, - identityDependencies, + invDependencies, kronDependencies, mapDependencies, onesDependencies, + partitionSelectDependencies, + pinvDependencies, rangeDependencies, reshapeDependencies, resizeDependencies, + rotationMatrixDependencies, rowDependencies, + columnDependencies, + rotateDependencies, sizeDependencies, + sortDependencies, + sqrtmDependencies, squeezeDependencies, subsetDependencies, + traceDependencies, transposeDependencies, - ctransposeDependencies, zerosDependencies, - erfDependencies, - modeDependencies, - prodDependencies, - formatDependencies, - printDependencies, - toDependencies, - toBestDependencies, - isPrimeDependencies, - numericDependencies, - divideScalarDependencies, - powDependencies, - roundDependencies, - logDependencies, - log1pDependencies, - nthRootsDependencies, - dotPowDependencies, - dotDivideDependencies, - lsolveDependencies, - usolveDependencies, - leftShiftDependencies, - rightArithShiftDependencies, - rightLogShiftDependencies, - andDependencies, + fftDependencies, + ifftDependencies, + + // probability dependencies + combinationsDependencies, + factorialDependencies, + gammaDependencies, + kldivergenceDependencies, + lgammaDependencies, + multinomialDependencies, + permutationsDependencies, + pickRandomDependencies, + randomDependencies, + randomIntDependencies, + + // relational function dependencies compareDependencies, compareNaturalDependencies, compareTextDependencies, + deepEqualDependencies, equalDependencies, + equalScalarDependencies, equalTextDependencies, - smallerDependencies, - smallerEqDependencies, largerDependencies, largerEqDependencies, - deepEqualDependencies, + smallerDependencies, + smallerEqDependencies, unequalDependencies, - partitionSelectDependencies, - sortDependencies, + + // set function dependencies + setCartesianDependencies, + setDifferenceDependencies, + setDistinctDependencies, + setIntersectDependencies, + setIsSubsetDependencies, + setMultiplicityDependencies, + setPowersetDependencies, + setSizeDependencies, + setSymDifferenceDependencies, + setUnionDependencies, + + // signal function dependencies + zpk2tfDependencies, + freqzDependencies, + + // special function dependencies + erfDependencies, + zetaDependencies, + + // statistics function dependencies + madDependencies, maxDependencies, + meanDependencies, + medianDependencies, minDependencies, - ImmutableDenseMatrixDependencies, - IndexDependencies, - FibonacciHeapDependencies, - SpaDependencies, - UnitDependencies, - unitDependencies, - sparseDependencies, - createUnitDependencies, + modeDependencies, + prodDependencies, + quantileSeqDependencies, + stdDependencies, + sumDependencies, + countDependencies, + cumsumDependencies, + varianceDependencies, + corrDependencies, + + // string function dependencies + formatDependencies, + printDependencies, + + // trigonometry function dependencies acosDependencies, acoshDependencies, acotDependencies, @@ -3928,159 +4210,69 @@ export const { sinhDependencies, tanDependencies, tanhDependencies, - setCartesianDependencies, - setDifferenceDependencies, - setDistinctDependencies, - setIntersectDependencies, - setIsSubsetDependencies, - setMultiplicityDependencies, - setPowersetDependencies, - setSizeDependencies, - setSymDifferenceDependencies, - setUnionDependencies, - zpk2tfDependencies, - freqzDependencies, - addDependencies, - hypotDependencies, - normDependencies, - dotDependencies, - traceDependencies, - indexDependencies, - NodeDependencies, - AccessorNodeDependencies, - ArrayNodeDependencies, - AssignmentNodeDependencies, - BlockNodeDependencies, - ConditionalNodeDependencies, - ConstantNodeDependencies, - FunctionAssignmentNodeDependencies, - IndexNodeDependencies, - ObjectNodeDependencies, - OperatorNodeDependencies, - ParenthesisNodeDependencies, - RangeNodeDependencies, - RelationalNodeDependencies, - SymbolNodeDependencies, - FunctionNodeDependencies, - parseDependencies, - compileDependencies, - evaluateDependencies, - evalDependencies, - ParserDependencies, - parserDependencies, - lupDependencies, - qrDependencies, - sluDependencies, - lusolveDependencies, - HelpDependencies, - ChainDependencies, - helpDependencies, - chainDependencies, - detDependencies, - invDependencies, - expmDependencies, - sqrtmDependencies, - sylvesterDependencies, - schurDependencies, - lyapDependencies, - divideDependencies, - distanceDependencies, - intersectDependencies, - sumDependencies, - meanDependencies, - medianDependencies, - madDependencies, - varianceDependencies, - varDependencies, - quantileSeqDependencies, - stdDependencies, - combinationsDependencies, - gammaDependencies, - factorialDependencies, - kldivergenceDependencies, - multinomialDependencies, - permutationsDependencies, - pickRandomDependencies, - randomDependencies, - randomIntDependencies, - stirlingS2Dependencies, - bellNumbersDependencies, - catalanDependencies, - compositionDependencies, - simplifyDependencies, - derivativeDependencies, - rationalizeDependencies, - reviverDependencies, - eDependencies, - EDependencies, - falseDependencies, - iDependencies, - InfinityDependencies, - LN10Dependencies, - LN2Dependencies, - LOG10EDependencies, - LOG2EDependencies, - NaNDependencies, - nullDependencies, - phiDependencies, - piDependencies, - PIDependencies, - SQRT1_2Dependencies, - SQRT2Dependencies, - tauDependencies, - trueDependencies, - versionDependencies, - atomicMassDependencies, - avogadroDependencies, - bohrMagnetonDependencies, - bohrRadiusDependencies, - boltzmannDependencies, - classicalElectronRadiusDependencies, - conductanceQuantumDependencies, - coulombDependencies, - deuteronMassDependencies, - efimovFactorDependencies, - eigsDependencies, - electricConstantDependencies, - electronMassDependencies, - elementaryChargeDependencies, - faradayDependencies, - fermiCouplingDependencies, - fineStructureDependencies, - firstRadiationDependencies, - gasConstantDependencies, - gravitationConstantDependencies, - gravityDependencies, - hartreeEnergyDependencies, - inverseConductanceQuantumDependencies, - klitzingDependencies, - loschmidtDependencies, - magneticConstantDependencies, - magneticFluxQuantumDependencies, - molarMassDependencies, - molarMassC12Dependencies, - molarPlanckConstantDependencies, - molarVolumeDependencies, - neutronMassDependencies, - nuclearMagnetonDependencies, - planckChargeDependencies, - planckConstantDependencies, - planckLengthDependencies, - planckMassDependencies, - planckTemperatureDependencies, - planckTimeDependencies, - protonMassDependencies, - quantumOfCirculationDependencies, - reducedPlanckConstantDependencies, - rydbergDependencies, - sackurTetrodeDependencies, - secondRadiationDependencies, - speedOfLightDependencies, - stefanBoltzmannDependencies, - thomsonCrossSectionDependencies, - vacuumImpedanceDependencies, - weakMixingAngleDependencies, - wienDisplacementDependencies, + + // unit function dependencies + toDependencies, + toBestDependencies, + + // util function dependencies + isNumberDependencies, + isBigNumberDependencies, + isBigIntDependencies, + isComplexDependencies, + isFractionDependencies, + isUnitDependencies, + isStringDependencies, + isArrayDependencies, + isMatrixDependencies, + isCollectionDependencies, + isDenseMatrixDependencies, + isSparseMatrixDependencies, + isRangeDependencies, + isIndexDependencies, + isBooleanDependencies, + isResultSetDependencies, + isHelpDependencies, + isFunctionDependencies, + isDateDependencies, + isRegExpDependencies, + isObjectDependencies, + isMapDependencies, + isPartitionedMapDependencies, + isObjectWrappingMapDependencies, + isNullDependencies, + isUndefinedDependencies, + isAccessorNodeDependencies, + isArrayNodeDependencies, + isAssignmentNodeDependencies, + isBlockNodeDependencies, + isConditionalNodeDependencies, + isConstantNodeDependencies, + isFunctionAssignmentNodeDependencies, + isFunctionNodeDependencies, + isIndexNodeDependencies, + isNodeDependencies, + isObjectNodeDependencies, + isOperatorNodeDependencies, + isParenthesisNodeDependencies, + isRangeNodeDependencies, + isRelationalNodeDependencies, + isSymbolNodeDependencies, + isChainDependencies, + cloneDependencies, + hasNumericValueDependencies, + isBoundedDependencies, + isFiniteDependencies, + isIntegerDependencies, + isNaNDependencies, + isNegativeDependencies, + isNumericDependencies, + isPositiveDependencies, + isPrimeDependencies, + isZeroDependencies, + typeOfDependencies, + + // Transform dependencies mapSlicesTransformDependencies, columnTransformDependencies, filterTransformDependencies, @@ -5452,9 +5644,22 @@ export interface MathJsChain { * @param root The root. Default value: 2. */ nthRoot( - this: MathJsChain, + this: MathJsChain, + root?: number | BigNumber + ): MathJsChain + nthRoot( + this: MathCollection, root?: number | BigNumber - ): MathJsChain + ): MathJsChain + + /** + * Calculate all nth roots of a value. + * @param n Which root to take. Default value: 2. + */ + nthRoots( + this: MathJsChain, + n?: number + ): MathJsChain> /** * Calculates the power of x to y, x ^ y. Matrix exponentiation is @@ -5953,6 +6158,11 @@ export interface MathJsChain { callback: (value: any, index: number[], matrix: T) => void ): void + /** + * Get the data type in a collection + */ + getMatrixDataType(this: MathJsChain): MathJsChain + /** * Calculate the inverse of a square matrix. */ @@ -7087,6 +7297,17 @@ export interface MathJsChain { this: MathJsChain ): MathJsChain + /** + * Test whether a value is bounded, works on entire collection at once + */ + isBounded(this: MathJsChain): MathJsChain + + /** + * Test whether a value is finite, works elementwise on collections + */ + isFinite(this: MathJsChain): MathJsChain + isFinite(this: MathJsChain): MathJsChain + /** * Test whether a value is negative: smaller than zero. The function * supports types number, BigNumber, Fraction, and Unit. The function is @@ -7184,6 +7405,58 @@ export const { SQRT2, tau, + // Physical constants + atomicMass, + avogadro, + bohrMagneton, + bohrRadius, + boltzmann, + classicalElectronRadius, + conductanceQuantum, + coulomb, + deuteronMass, + efimovFactor, + electricConstant, + electronMass, + elementaryCharge, + faraday, + fermiCoupling, + fineStructure, + firstRadiation, + gasConstant, + gravitationConstant, + gravity, + hartreeEnergy, + inverseConductanceQuantum, + klitzing, + loschmidt, + magneticConstant, + magneticFluxQuantum, + molarMass, + molarMassC12, + molarPlanckConstant, + molarVolume, + neutronMass, + nuclearMagneton, + planckCharge, + planckConstant, + planckLength, + planckMass, + planckTemperature, + planckTime, + protonMass, + quantumOfCirculation, + reducedPlanckConstant, + rydberg, + sackurTetrode, + secondRadiation, + speedOfLight, + stefanBoltzmann, + thomsonCrossSection, + vacuumImpedance, + weakMixingAngle, + wienDisplacement, + // Class-like constructors Node, AccessorNode, @@ -7210,7 +7483,9 @@ export const { reviver, replacer, + // Construction functions bignumber, + bigint, boolean, chain, complex, @@ -7219,10 +7494,13 @@ export const { index, matrix, number, + numeric, sparse, splitUnit, string, unit, + + // Expression functions compile, evaluate, help, @@ -7251,9 +7529,6 @@ export const { add, cbrt, ceil, - fix, - floor, - round, cube, divide, dotDivide, @@ -7261,6 +7536,8 @@ export const { dotPow, exp, expm1, + fix, + floor, gcd, hypot, lcm, @@ -7268,11 +7545,16 @@ export const { log10, log1p, log2, + matrixFromRows, + matrixFromColumns, + matrixFromFunction, mod, multiply, norm, nthRoot, + nthRoots, pow, + round, sign, sqrt, square, @@ -7319,6 +7601,7 @@ export const { cross, ctranspose, det, + diff, diag, dot, eigs, @@ -7330,6 +7613,7 @@ export const { filter, flatten, forEach, + getMatrixDataType, inv, kron, map, @@ -7391,9 +7675,11 @@ export const { setSymDifference, setUnion, - // special functions + // signal functions zpk2tf, freqz, + + // special functions erf, zeta, @@ -7451,6 +7737,7 @@ export const { // util functions isNumber, isBigNumber, + isBigInt, isComplex, isFraction, isUnit, @@ -7469,6 +7756,9 @@ export const { isDate, isRegExp, isObject, + isMap, + isPartitionedMap, + isObjectWrappingMap, isNull, isUndefined, isAccessorNode, @@ -7490,6 +7780,8 @@ export const { isChain, clone, hasNumericValue, + isBounded, + isFinite, isInteger, isNaN, isNegative,