mathjs 14.5.3 → 14.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HISTORY.md +21 -1
- package/lib/browser/math.js +1 -1
- package/lib/browser/math.js.LICENSE.txt +2 -2
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/core/function/config.js +0 -1
- package/lib/cjs/entry/dependenciesAny/dependenciesToBest.generated.js +17 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesUnitClass.generated.js +2 -0
- package/lib/cjs/entry/dependenciesAny.generated.js +7 -0
- package/lib/cjs/entry/impureFunctionsAny.generated.js +1 -0
- package/lib/cjs/entry/pureFunctionsAny.generated.js +7 -3
- package/lib/cjs/expression/Parser.js +35 -1
- package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +2 -0
- package/lib/cjs/expression/embeddedDocs/function/units/toBest.js +14 -0
- package/lib/cjs/expression/node/FunctionAssignmentNode.js +3 -1
- package/lib/cjs/expression/parse.js +1 -1
- package/lib/cjs/factoriesAny.js +7 -0
- package/lib/cjs/function/arithmetic/sign.js +1 -1
- package/lib/cjs/function/matrix/range.js +7 -5
- package/lib/cjs/function/unit/toBest.js +53 -0
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/type/matrix/DenseMatrix.js +21 -17
- package/lib/cjs/type/string.js +4 -4
- package/lib/cjs/type/unit/Unit.js +106 -19
- package/lib/cjs/version.js +1 -1
- package/lib/esm/core/function/config.js +0 -1
- package/lib/esm/entry/dependenciesAny/dependenciesToBest.generated.js +10 -0
- package/lib/esm/entry/dependenciesAny/dependenciesUnitClass.generated.js +2 -0
- package/lib/esm/entry/dependenciesAny.generated.js +1 -0
- package/lib/esm/entry/impureFunctionsAny.generated.js +2 -1
- package/lib/esm/entry/pureFunctionsAny.generated.js +6 -2
- package/lib/esm/expression/Parser.js +35 -1
- package/lib/esm/expression/embeddedDocs/embeddedDocs.js +2 -0
- package/lib/esm/expression/embeddedDocs/function/units/toBest.js +8 -0
- package/lib/esm/expression/node/FunctionAssignmentNode.js +3 -1
- package/lib/esm/expression/parse.js +1 -1
- package/lib/esm/factoriesAny.js +1 -0
- package/lib/esm/function/arithmetic/sign.js +1 -1
- package/lib/esm/function/matrix/range.js +7 -5
- package/lib/esm/function/unit/toBest.js +47 -0
- package/lib/esm/type/matrix/DenseMatrix.js +21 -17
- package/lib/esm/type/string.js +4 -4
- package/lib/esm/type/unit/Unit.js +106 -19
- package/lib/esm/version.js +1 -1
- package/package.json +15 -16
- package/types/index.d.ts +50 -1
- package/types/tslint.json +0 -6
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.toBestDependencies = void 0;
|
7
|
+
var _dependenciesTypedGenerated = require("./dependenciesTyped.generated.js");
|
8
|
+
var _factoriesAny = require("../../factoriesAny.js");
|
9
|
+
/**
|
10
|
+
* THIS FILE IS AUTO-GENERATED
|
11
|
+
* DON'T MAKE CHANGES HERE
|
12
|
+
*/
|
13
|
+
|
14
|
+
const toBestDependencies = exports.toBestDependencies = {
|
15
|
+
typedDependencies: _dependenciesTypedGenerated.typedDependencies,
|
16
|
+
createToBest: _factoriesAny.createToBest
|
17
|
+
};
|
@@ -19,6 +19,7 @@ var _dependenciesNumberGenerated = require("./dependenciesNumber.generated.js");
|
|
19
19
|
var _dependenciesPowGenerated = require("./dependenciesPow.generated.js");
|
20
20
|
var _dependenciesRoundGenerated = require("./dependenciesRound.generated.js");
|
21
21
|
var _dependenciesSubtractScalarGenerated = require("./dependenciesSubtractScalar.generated.js");
|
22
|
+
var _dependenciesToBestGenerated = require("./dependenciesToBest.generated.js");
|
22
23
|
var _factoriesAny = require("../../factoriesAny.js");
|
23
24
|
/**
|
24
25
|
* THIS FILE IS AUTO-GENERATED
|
@@ -41,5 +42,6 @@ const UnitDependencies = exports.UnitDependencies = {
|
|
41
42
|
powDependencies: _dependenciesPowGenerated.powDependencies,
|
42
43
|
roundDependencies: _dependenciesRoundGenerated.roundDependencies,
|
43
44
|
subtractScalarDependencies: _dependenciesSubtractScalarGenerated.subtractScalarDependencies,
|
45
|
+
toBestDependencies: _dependenciesToBestGenerated.toBestDependencies,
|
44
46
|
createUnitClass: _factoriesAny.createUnitClass
|
45
47
|
};
|
@@ -2049,6 +2049,12 @@ Object.defineProperty(exports, "thomsonCrossSectionDependencies", {
|
|
2049
2049
|
return _dependenciesThomsonCrossSectionGenerated.thomsonCrossSectionDependencies;
|
2050
2050
|
}
|
2051
2051
|
});
|
2052
|
+
Object.defineProperty(exports, "toBestDependencies", {
|
2053
|
+
enumerable: true,
|
2054
|
+
get: function () {
|
2055
|
+
return _dependenciesToBestGenerated.toBestDependencies;
|
2056
|
+
}
|
2057
|
+
});
|
2052
2058
|
Object.defineProperty(exports, "toDependencies", {
|
2053
2059
|
enumerable: true,
|
2054
2060
|
get: function () {
|
@@ -2525,6 +2531,7 @@ var _dependenciesTanhGenerated = require("./dependenciesAny/dependenciesTanh.gen
|
|
2525
2531
|
var _dependenciesTauGenerated = require("./dependenciesAny/dependenciesTau.generated.js");
|
2526
2532
|
var _dependenciesThomsonCrossSectionGenerated = require("./dependenciesAny/dependenciesThomsonCrossSection.generated.js");
|
2527
2533
|
var _dependenciesToGenerated = require("./dependenciesAny/dependenciesTo.generated.js");
|
2534
|
+
var _dependenciesToBestGenerated = require("./dependenciesAny/dependenciesToBest.generated.js");
|
2528
2535
|
var _dependenciesTraceGenerated = require("./dependenciesAny/dependenciesTrace.generated.js");
|
2529
2536
|
var _dependenciesTransposeGenerated = require("./dependenciesAny/dependenciesTranspose.generated.js");
|
2530
2537
|
var _dependenciesTrueGenerated = require("./dependenciesAny/dependenciesTrue.generated.js");
|
@@ -336,6 +336,7 @@ const help = exports.help = (0, _factoriesAny.createHelp)({
|
|
336
336
|
string: _pureFunctionsAnyGenerated.string,
|
337
337
|
subtractScalar: _pureFunctionsAnyGenerated.subtractScalar,
|
338
338
|
tan: _pureFunctionsAnyGenerated.tan,
|
339
|
+
toBest: _pureFunctionsAnyGenerated.toBest,
|
339
340
|
typeOf: _pureFunctionsAnyGenerated.typeOf,
|
340
341
|
acosh: _pureFunctionsAnyGenerated.acosh,
|
341
342
|
acsch: _pureFunctionsAnyGenerated.acsch,
|
@@ -5,8 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.dotPow = exports.dotMultiply = exports.dotDivide = exports.dot = exports.divideScalar = exports.divide = exports.distance = exports.diff = exports.diag = exports.deuteronMass = exports.det = exports.deepEqual = exports.cumsum = exports.cube = exports.ctranspose = exports.csch = exports.csc = exports.cross = exports.createUnit = exports.count = exports.coulombConstant = exports.coulomb = exports.coth = exports.cot = exports.cosh = exports.cos = exports.corr = exports.conj = exports.conductanceQuantum = exports.concat = exports.composition = exports.complex = exports.compareText = exports.compareNatural = exports.compare = exports.combinationsWithRep = exports.combinations = exports.column = exports.clone = exports.classicalElectronRadius = exports.ceil = exports.cbrt = exports.catalan = exports.boolean = exports.boltzmann = exports.bohrRadius = exports.bohrMagneton = exports.bitXor = exports.bitOr = exports.bitNot = exports.bitAnd = exports.bin = exports.bignumber = exports.bigint = exports.bellNumbers = exports.avogadro = exports.atomicMass = exports.atanh = exports.atan2 = exports.atan = exports.asinh = exports.asin = exports.asech = exports.asec = exports.arg = exports.apply = exports.and = exports.addScalar = exports.add = exports.acsch = exports.acsc = exports.acoth = exports.acot = exports.acosh = exports.acos = exports.abs = exports._true = exports._null = exports._false = exports._NaN = exports._Infinity = exports.Unit = exports.SparseMatrix = exports.Spa = exports.SQRT2 = exports.SQRT1_2 = exports.ResultSet = exports.Range = exports.Matrix = exports.LOG2E = exports.LOG10E = exports.LN2 = exports.LN10 = exports.Index = exports.ImmutableDenseMatrix = exports.Fraction = exports.FibonacciHeap = exports.DenseMatrix = exports.Complex = exports.BigNumber = void 0;
|
7
7
|
exports.number = exports.nuclearMagneton = exports.nthRoots = exports.nthRoot = exports.not = exports.norm = exports.neutronMass = exports.multiplyScalar = exports.multiply = exports.multinomial = exports.molarVolume = exports.molarPlanckConstant = exports.molarMassC12 = exports.molarMass = exports.mode = exports.mod = exports.min = exports.median = exports.mean = exports.max = exports.matrixFromRows = exports.matrixFromFunction = exports.matrixFromColumns = exports.matrix = exports.mapSlices = exports.map = exports.magneticFluxQuantum = exports.magneticConstant = exports.mad = exports.lyap = exports.lusolve = exports.lup = exports.lsolveAll = exports.lsolve = exports.loschmidt = exports.log2 = exports.log1p = exports.log10 = exports.log = exports.lgamma = exports.leftShift = exports.lcm = exports.largerEq = exports.larger = exports.kron = exports.klitzing = exports.kldivergence = exports.isZero = exports.isPrime = exports.isPositive = exports.isNumeric = exports.isNegative = exports.isNaN = exports.isInteger = exports.invmod = exports.inverseConductanceQuantum = exports.inv = exports.intersect = exports.index = exports.im = exports.ifft = exports.identity = exports.i = exports.hypot = exports.hex = exports.hasNumericValue = exports.hartreeEnergy = exports.gravity = exports.gravitationConstant = exports.getMatrixDataType = exports.gcd = exports.gasConstant = exports.gamma = exports.freqz = exports.fraction = exports.format = exports.forEach = exports.floor = exports.flatten = exports.fix = exports.firstRadiation = exports.fineStructure = exports.filter = exports.fft = exports.fermiCoupling = exports.faraday = exports.factorial = exports.expm1 = exports.expm = exports.exp = exports.erf = exports.equalText = exports.equalScalar = exports.equal = exports.elementaryCharge = exports.electronMass = exports.electricConstant = exports.eigs = exports.efimovFactor = exports.e = void 0;
|
8
|
-
exports.
|
9
|
-
exports.zpk2tf = exports.zeta = exports.zeros = exports.xor = void 0;
|
8
|
+
exports.wienDisplacement = exports.weakMixingAngle = exports.version = exports.variance = exports.vacuumImpedance = exports.usolveAll = exports.usolve = exports.unit = exports.unequal = exports.unaryPlus = exports.unaryMinus = exports.typed = exports.typeOf = exports.transpose = exports.trace = exports.toBest = exports.to = exports.thomsonCrossSection = exports.tau = exports.tanh = exports.tan = exports.sylvester = exports.sum = exports.subtractScalar = exports.subtract = exports.subset = exports.string = exports.stirlingS2 = exports.stefanBoltzmann = exports.std = exports.squeeze = exports.square = exports.sqrtm = exports.sqrt = exports.splitUnit = exports.speedOfLight = exports.sparse = exports.sort = exports.solveODE = exports.smallerEq = exports.smaller = exports.slu = exports.size = exports.sinh = exports.sin = exports.sign = exports.setUnion = exports.setSymDifference = exports.setSize = exports.setPowerset = exports.setMultiplicity = exports.setIsSubset = exports.setIntersect = exports.setDistinct = exports.setDifference = exports.setCartesian = exports.secondRadiation = exports.sech = exports.sec = exports.schur = exports.sackurTetrode = exports.rydberg = exports.row = exports.round = exports.rotationMatrix = exports.rotate = exports.rightLogShift = exports.rightArithShift = exports.resize = exports.reshape = exports.replacer = exports.reducedPlanckConstant = exports.re = exports.range = exports.randomInt = exports.random = exports.quantumOfCirculation = exports.quantileSeq = exports.qr = exports.protonMass = exports.prod = exports.print = exports.pow = exports.polynomialRoot = exports.planckTime = exports.planckTemperature = exports.planckMass = exports.planckLength = exports.planckConstant = exports.planckCharge = exports.pinv = exports.pickRandom = exports.pi = exports.phi = exports.permutations = exports.partitionSelect = exports.or = exports.ones = exports.oct = exports.numeric = void 0;
|
9
|
+
exports.zpk2tf = exports.zeta = exports.zeros = exports.xor = exports.xgcd = void 0;
|
10
10
|
var _configReadonly = require("./configReadonly.js");
|
11
11
|
var _factoriesAny = require("../factoriesAny.js");
|
12
12
|
/**
|
@@ -308,6 +308,9 @@ const subtractScalar = exports.subtractScalar = /* #__PURE__ */(0, _factoriesAny
|
|
308
308
|
const tan = exports.tan = /* #__PURE__ */(0, _factoriesAny.createTan)({
|
309
309
|
typed
|
310
310
|
});
|
311
|
+
const toBest = exports.toBest = /* #__PURE__ */(0, _factoriesAny.createToBest)({
|
312
|
+
typed
|
313
|
+
});
|
311
314
|
const typeOf = exports.typeOf = /* #__PURE__ */(0, _factoriesAny.createTypeOf)({
|
312
315
|
typed
|
313
316
|
});
|
@@ -1185,7 +1188,8 @@ const Unit = exports.Unit = /* #__PURE__ */(0, _factoriesAny.createUnitClass)({
|
|
1185
1188
|
number,
|
1186
1189
|
pow,
|
1187
1190
|
round,
|
1188
|
-
subtractScalar
|
1191
|
+
subtractScalar,
|
1192
|
+
toBest
|
1189
1193
|
});
|
1190
1194
|
const vacuumImpedance = exports.vacuumImpedance = /* #__PURE__ */(0, _factoriesAny.createVacuumImpedance)({
|
1191
1195
|
BigNumber,
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.createParserClass = void 0;
|
7
7
|
var _factory = require("../utils/factory.js");
|
8
|
+
var _is = require("../utils/is.js");
|
8
9
|
var _map = require("../utils/map.js");
|
9
10
|
const name = 'Parser';
|
10
11
|
const dependencies = ['evaluate', 'parse'];
|
@@ -160,7 +161,40 @@ const createParserClass = exports.createParserClass = /* #__PURE__ */(0, _factor
|
|
160
161
|
Parser.prototype.clear = function () {
|
161
162
|
this.scope.clear();
|
162
163
|
};
|
164
|
+
Parser.prototype.toJSON = function () {
|
165
|
+
const json = {
|
166
|
+
mathjs: 'Parser',
|
167
|
+
variables: {},
|
168
|
+
functions: {}
|
169
|
+
};
|
170
|
+
for (const [name, value] of this.scope) {
|
171
|
+
if ((0, _is.isFunction)(value)) {
|
172
|
+
if (!isExpressionFunction(value)) {
|
173
|
+
throw new Error(`Cannot serialize external function ${name}`);
|
174
|
+
}
|
175
|
+
json.functions[name] = `${value.syntax} = ${value.expr}`;
|
176
|
+
} else {
|
177
|
+
json.variables[name] = value;
|
178
|
+
}
|
179
|
+
}
|
180
|
+
return json;
|
181
|
+
};
|
182
|
+
Parser.fromJSON = function (json) {
|
183
|
+
const parser = new Parser();
|
184
|
+
Object.entries(json.variables).forEach(_ref2 => {
|
185
|
+
let [name, value] = _ref2;
|
186
|
+
return parser.set(name, value);
|
187
|
+
});
|
188
|
+
Object.entries(json.functions).forEach(_ref3 => {
|
189
|
+
let [_name, fn] = _ref3;
|
190
|
+
return parser.evaluate(fn);
|
191
|
+
});
|
192
|
+
return parser;
|
193
|
+
};
|
163
194
|
return Parser;
|
164
195
|
}, {
|
165
196
|
isClass: true
|
166
|
-
});
|
197
|
+
});
|
198
|
+
function isExpressionFunction(value) {
|
199
|
+
return typeof value === 'function' && typeof value.syntax === 'string' && typeof value.expr === 'string';
|
200
|
+
}
|
@@ -235,6 +235,7 @@ var _sinh = require("./function/trigonometry/sinh.js");
|
|
235
235
|
var _tan = require("./function/trigonometry/tan.js");
|
236
236
|
var _tanh = require("./function/trigonometry/tanh.js");
|
237
237
|
var _to = require("./function/units/to.js");
|
238
|
+
var _toBest = require("./function/units/toBest.js");
|
238
239
|
var _bin = require("./function/utils/bin.js");
|
239
240
|
var _clone = require("./function/utils/clone.js");
|
240
241
|
var _format = require("./function/utils/format.js");
|
@@ -718,6 +719,7 @@ const embeddedDocs = exports.embeddedDocs = {
|
|
718
719
|
tanh: _tanh.tanhDocs,
|
719
720
|
// functions - units
|
720
721
|
to: _to.toDocs,
|
722
|
+
toBest: _toBest.toBestDocs,
|
721
723
|
// functions - utils
|
722
724
|
clone: _clone.cloneDocs,
|
723
725
|
format: _format.formatDocs,
|
@@ -0,0 +1,14 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.toBestDocs = void 0;
|
7
|
+
const toBestDocs = exports.toBestDocs = {
|
8
|
+
name: 'toBest',
|
9
|
+
category: 'Units',
|
10
|
+
syntax: ['toBest(x)', 'toBest(x, unitList)', 'toBest(x, unitList, options)'],
|
11
|
+
description: 'Converts to the most appropriate display unit.',
|
12
|
+
examples: ['toBest(unit(5000, "m"))', 'toBest(unit(3500000, "W"))', 'toBest(unit(0.000000123, "A"))', 'toBest(unit(10, "m"), "cm")', 'toBest(unit(10, "m"), "mm,km", {offset: 1.5})'],
|
13
|
+
seealso: []
|
14
|
+
};
|
@@ -105,7 +105,8 @@ const createFunctionAssignmentNode = exports.createFunctionAssignmentNode = /* #
|
|
105
105
|
});
|
106
106
|
|
107
107
|
// compile the function expression with the child args
|
108
|
-
const
|
108
|
+
const expr = this.expr;
|
109
|
+
const evalExpr = expr._compile(math, childArgNames);
|
109
110
|
const name = this.name;
|
110
111
|
const params = this.params;
|
111
112
|
const signature = (0, _array.join)(this.types, ',');
|
@@ -121,6 +122,7 @@ const createFunctionAssignmentNode = exports.createFunctionAssignmentNode = /* #
|
|
121
122
|
};
|
122
123
|
const fn = typed(name, signatures);
|
123
124
|
fn.syntax = syntax;
|
125
|
+
fn.expr = expr.toString();
|
124
126
|
scope.set(name, fn);
|
125
127
|
return fn;
|
126
128
|
};
|
@@ -54,7 +54,7 @@ const createParse = exports.createParse = /* #__PURE__ */(0, _factory.factory)(n
|
|
54
54
|
* node1.compile().evaluate() // 5
|
55
55
|
*
|
56
56
|
* let scope = {a:3, b:4}
|
57
|
-
* const node2 = math.parse('a * b')
|
57
|
+
* const node2 = math.parse('a * b')
|
58
58
|
* node2.evaluate(scope) // 12
|
59
59
|
* const code2 = node2.compile()
|
60
60
|
* code2.evaluate(scope) // 12
|
package/lib/cjs/factoriesAny.js
CHANGED
@@ -2031,6 +2031,12 @@ Object.defineProperty(exports, "createTo", {
|
|
2031
2031
|
return _to.createTo;
|
2032
2032
|
}
|
2033
2033
|
});
|
2034
|
+
Object.defineProperty(exports, "createToBest", {
|
2035
|
+
enumerable: true,
|
2036
|
+
get: function () {
|
2037
|
+
return _toBest.createToBest;
|
2038
|
+
}
|
2039
|
+
});
|
2034
2040
|
Object.defineProperty(exports, "createTrace", {
|
2035
2041
|
enumerable: true,
|
2036
2042
|
get: function () {
|
@@ -2290,6 +2296,7 @@ var _oct = require("./function/string/oct.js");
|
|
2290
2296
|
var _hex = require("./function/string/hex.js");
|
2291
2297
|
var _print = require("./function/string/print.js");
|
2292
2298
|
var _to = require("./function/unit/to.js");
|
2299
|
+
var _toBest = require("./function/unit/toBest.js");
|
2293
2300
|
var _isPrime = require("./function/utils/isPrime.js");
|
2294
2301
|
var _numeric = require("./function/utils/numeric.js");
|
2295
2302
|
var _divideScalar = require("./function/arithmetic/divideScalar.js");
|
@@ -58,7 +58,7 @@ const createSign = exports.createSign = /* #__PURE__ */(0, _factory.factory)(nam
|
|
58
58
|
return x > 0n ? 1n : x < 0n ? -1n : 0n;
|
59
59
|
},
|
60
60
|
Fraction: function (x) {
|
61
|
-
return new Fraction(x.s);
|
61
|
+
return x.n === 0n ? new Fraction(0) : new Fraction(x.s);
|
62
62
|
},
|
63
63
|
// deep map collection, skip zeros since sign(0) = 0
|
64
64
|
'Array | Matrix': typed.referToSelf(self => x => (0, _collection.deepMap)(x, self, true)),
|
@@ -22,7 +22,7 @@ const createRange = exports.createRange = /* #__PURE__ */(0, _factory.factory)(n
|
|
22
22
|
isPositive
|
23
23
|
} = _ref;
|
24
24
|
/**
|
25
|
-
* Create
|
25
|
+
* Create a matrix or array containing a range of values.
|
26
26
|
* By default, the range end is excluded. This can be customized by providing
|
27
27
|
* an extra parameter `includeEnd`.
|
28
28
|
*
|
@@ -50,10 +50,12 @@ const createRange = exports.createRange = /* #__PURE__ */(0, _factory.factory)(n
|
|
50
50
|
* - `includeEnd: boolean`
|
51
51
|
* Option to specify whether to include the end or not. False by default.
|
52
52
|
*
|
53
|
-
*
|
54
|
-
*
|
55
|
-
*
|
56
|
-
*
|
53
|
+
* The function returns a `DenseMatrix` when the library is configured with
|
54
|
+
* `config = { matrix: 'Matrix' }, and returns an Array otherwise.
|
55
|
+
* Note that the type of the returned values is taken from the type of the
|
56
|
+
* provided start/end value. If only one of these is a built-in `number` type,
|
57
|
+
* it will be promoted to the type of the other endpoint. However, in the case
|
58
|
+
* of Unit values, both endpoints must have compatible units, and the return
|
57
59
|
* value will have compatible units as well.
|
58
60
|
*
|
59
61
|
* Examples:
|
@@ -0,0 +1,53 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.createToBest = void 0;
|
7
|
+
var _factory = require("../../utils/factory.js");
|
8
|
+
const name = 'toBest';
|
9
|
+
const dependencies = ['typed'];
|
10
|
+
const createToBest = exports.createToBest = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
11
|
+
let {
|
12
|
+
typed
|
13
|
+
} = _ref;
|
14
|
+
/**
|
15
|
+
* Converts a unit to the most appropriate display unit.
|
16
|
+
* When no preferred units are provided, the function automatically find the best prefix.
|
17
|
+
* When preferred units are provided, it converts to
|
18
|
+
* the unit that gives a value closest to 1.
|
19
|
+
*
|
20
|
+
* Syntax:
|
21
|
+
*
|
22
|
+
* math.toBest(unit)
|
23
|
+
* math.toBest(unit, unitList)
|
24
|
+
* math.toBest(unit, unitList, options)
|
25
|
+
*
|
26
|
+
* Where:
|
27
|
+
* - `unitList` is an optional array of preferred target units as string or Unit.
|
28
|
+
* - `options` is an optional object with options, formed as follows:
|
29
|
+
* - `offset`: number | BigNumber
|
30
|
+
*
|
31
|
+
* Examples:
|
32
|
+
*
|
33
|
+
* math.unit(0.05, 'm').toBest(['cm', 'mm']) // returns Unit 5 cm
|
34
|
+
* math.unit(2 / 3, 'cm').toBest() // returns Unit 0.6666666666666666 cm
|
35
|
+
* math.unit(10, 'm').toBest(['mm', 'km'], { offset: 1.5 }) // returns Unit 10000 mm
|
36
|
+
*
|
37
|
+
* See also:
|
38
|
+
*
|
39
|
+
* unit, to, format
|
40
|
+
*
|
41
|
+
* @param {Unit} x The unit to be converted
|
42
|
+
* @param {Array<string>} [unitList=[]] Optional array of preferred target units
|
43
|
+
* @param {Object} [options] Optional options object
|
44
|
+
* @return {Unit} Value converted to the best matching unit
|
45
|
+
*/
|
46
|
+
return typed(name, {
|
47
|
+
Unit: x => x.toBest(),
|
48
|
+
'Unit, string': (x, unitList) => x.toBest(unitList.split(',')),
|
49
|
+
'Unit, string, Object': (x, unitList, options) => x.toBest(unitList.split(','), options),
|
50
|
+
'Unit, Array': (x, unitList) => x.toBest(unitList),
|
51
|
+
'Unit, Array, Object': (x, unitList, options) => x.toBest(unitList, options)
|
52
|
+
});
|
53
|
+
});
|
package/lib/cjs/header.js
CHANGED
@@ -6,8 +6,8 @@
|
|
6
6
|
* It features real and complex numbers, units, matrices, a large set of
|
7
7
|
* mathematical functions, and a flexible expression parser.
|
8
8
|
*
|
9
|
-
* @version 14.
|
10
|
-
* @date 2025-
|
9
|
+
* @version 14.7.0
|
10
|
+
* @date 2025-09-05
|
11
11
|
*
|
12
12
|
* @license
|
13
13
|
* Copyright (C) 2013-2025 Jos de Jong <wjosdejong@gmail.com>
|
@@ -679,24 +679,28 @@ const createDenseMatrixClass = exports.createDenseMatrixClass = /* #__PURE__ */(
|
|
679
679
|
}
|
680
680
|
return;
|
681
681
|
}
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
index[depth] = i;
|
692
|
-
yield {
|
693
|
-
value: value[i],
|
694
|
-
index: index.slice()
|
695
|
-
};
|
696
|
-
}
|
682
|
+
|
683
|
+
// Multi-dimensional matrix: iterate over all elements
|
684
|
+
const index = Array(maxDepth + 1).fill(0);
|
685
|
+
const totalElements = this._size.reduce((a, b) => a * b, 1);
|
686
|
+
for (let count = 0; count < totalElements; count++) {
|
687
|
+
// Traverse to the current element using indices
|
688
|
+
let current = this._data;
|
689
|
+
for (let d = 0; d < maxDepth; d++) {
|
690
|
+
current = current[index[d]];
|
697
691
|
}
|
698
|
-
|
699
|
-
|
692
|
+
yield {
|
693
|
+
value: current[index[maxDepth]],
|
694
|
+
index: index.slice()
|
695
|
+
};
|
696
|
+
|
697
|
+
// Increment indices for next element
|
698
|
+
for (let d = maxDepth; d >= 0; d--) {
|
699
|
+
index[d]++;
|
700
|
+
if (index[d] < this._size[d]) break;
|
701
|
+
index[d] = 0;
|
702
|
+
}
|
703
|
+
}
|
700
704
|
};
|
701
705
|
|
702
706
|
/**
|
package/lib/cjs/type/string.js
CHANGED
@@ -23,13 +23,13 @@ const createString = exports.createString = /* #__PURE__ */(0, _factory.factory)
|
|
23
23
|
*
|
24
24
|
* Examples:
|
25
25
|
*
|
26
|
-
* math.string(4.2)
|
27
|
-
* math.string(math.complex(3, 2) // returns string '3 + 2i'
|
26
|
+
* math.string(4.2) // returns string '4.2'
|
27
|
+
* math.string(math.complex(3, 2)) // returns string '3 + 2i'
|
28
28
|
*
|
29
29
|
* const u = math.unit(5, 'km')
|
30
|
-
* math.string(u.to('m'))
|
30
|
+
* math.string(u.to('m')) // returns string '5000 m'
|
31
31
|
*
|
32
|
-
* math.string([true, false])
|
32
|
+
* math.string([true, false]) // returns ['true', 'false']
|
33
33
|
*
|
34
34
|
* See also:
|
35
35
|
*
|
@@ -13,7 +13,7 @@ var _string = require("../../utils/string.js");
|
|
13
13
|
var _object = require("../../utils/object.js");
|
14
14
|
var _constants = require("../../utils/bignumber/constants.js");
|
15
15
|
const name = 'Unit';
|
16
|
-
const dependencies = ['?on', 'config', 'addScalar', 'subtractScalar', 'multiplyScalar', 'divideScalar', 'pow', 'abs', 'fix', 'round', 'equal', 'isNumeric', 'format', 'number', 'Complex', 'BigNumber', 'Fraction'];
|
16
|
+
const dependencies = ['?on', 'config', 'addScalar', 'subtractScalar', 'multiplyScalar', 'divideScalar', 'pow', 'abs', 'fix', 'round', 'equal', 'isNumeric', 'format', 'toBest', 'number', 'Complex', 'BigNumber', 'Fraction'];
|
17
17
|
const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
|
18
18
|
let {
|
19
19
|
on,
|
@@ -29,6 +29,7 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
|
|
29
29
|
equal,
|
30
30
|
isNumeric,
|
31
31
|
format,
|
32
|
+
toBest,
|
32
33
|
number,
|
33
34
|
Complex,
|
34
35
|
BigNumber,
|
@@ -1048,6 +1049,54 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
|
|
1048
1049
|
return str;
|
1049
1050
|
};
|
1050
1051
|
|
1052
|
+
/**
|
1053
|
+
* Get a unit, with optional formatting options.
|
1054
|
+
* @memberof Unit
|
1055
|
+
* @param {string[] | Unit[]} [units] Array of units strings or valueLess Unit objects in wich choose the best one
|
1056
|
+
* @param {Object} [options] Options for parsing the unit. See parseUnit for details.
|
1057
|
+
*
|
1058
|
+
* @return {Unit} Returns a new Unit with the given value and unit.
|
1059
|
+
*/
|
1060
|
+
Unit.prototype.toBest = function () {
|
1061
|
+
let unitList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
1062
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
1063
|
+
if (unitList && !Array.isArray(unitList)) {
|
1064
|
+
throw new Error('Invalid unit type. Expected string or Unit.');
|
1065
|
+
}
|
1066
|
+
const startPrefixes = this.units[0].unit.prefixes;
|
1067
|
+
if (unitList && unitList.length > 0) {
|
1068
|
+
const unitObjects = unitList.map(u => {
|
1069
|
+
let unit = null;
|
1070
|
+
if (typeof u === 'string') {
|
1071
|
+
unit = Unit.parse(u);
|
1072
|
+
if (!unit) {
|
1073
|
+
throw new Error('Invalid unit type. Expected compatible string or Unit.');
|
1074
|
+
}
|
1075
|
+
} else if (!(0, _is.isUnit)(u)) {
|
1076
|
+
throw new Error('Invalid unit type. Expected compatible string or Unit.');
|
1077
|
+
}
|
1078
|
+
if (unit === null) {
|
1079
|
+
unit = u.clone();
|
1080
|
+
}
|
1081
|
+
try {
|
1082
|
+
this.to(unit.formatUnits());
|
1083
|
+
return unit;
|
1084
|
+
} catch (e) {
|
1085
|
+
throw new Error('Invalid unit type. Expected compatible string or Unit.');
|
1086
|
+
}
|
1087
|
+
});
|
1088
|
+
const prefixes = unitObjects.map(el => el.units[0].prefix);
|
1089
|
+
this.units[0].unit.prefixes = prefixes.reduce((acc, prefix) => {
|
1090
|
+
acc[prefix.name] = prefix;
|
1091
|
+
return acc;
|
1092
|
+
}, {});
|
1093
|
+
this.units[0].prefix = prefixes[0];
|
1094
|
+
}
|
1095
|
+
const result = formatBest(this, options).simp;
|
1096
|
+
this.units[0].unit.prefixes = startPrefixes;
|
1097
|
+
result.fixPrefix = true;
|
1098
|
+
return result;
|
1099
|
+
};
|
1051
1100
|
/**
|
1052
1101
|
* Get a string representation of the Unit, with optional formatting options.
|
1053
1102
|
* @memberof Unit
|
@@ -1058,11 +1107,54 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
|
|
1058
1107
|
* @return {string}
|
1059
1108
|
*/
|
1060
1109
|
Unit.prototype.format = function (options) {
|
1110
|
+
const {
|
1111
|
+
simp,
|
1112
|
+
valueStr,
|
1113
|
+
unitStr
|
1114
|
+
} = formatBest(this, options);
|
1115
|
+
let str = valueStr;
|
1116
|
+
if (simp.value && (0, _is.isComplex)(simp.value)) {
|
1117
|
+
str = '(' + str + ')'; // Surround complex values with ( ) to enable better parsing
|
1118
|
+
}
|
1119
|
+
if (unitStr.length > 0 && str.length > 0) {
|
1120
|
+
str += ' ';
|
1121
|
+
}
|
1122
|
+
str += unitStr;
|
1123
|
+
return str;
|
1124
|
+
};
|
1125
|
+
|
1126
|
+
/**
|
1127
|
+
* Helper function to normalize a unit for conversion and formatting
|
1128
|
+
* @param {Unit} unit The unit to be normalized
|
1129
|
+
* @return {Object} Object with normalized unit and value
|
1130
|
+
* @private
|
1131
|
+
*/
|
1132
|
+
function formatBest(unit) {
|
1133
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
1061
1134
|
// Simplfy the unit list, unless it is valueless or was created directly in the
|
1062
1135
|
// constructor or as the result of to or toSI
|
1063
|
-
const simp =
|
1136
|
+
const simp = unit.skipAutomaticSimplification || unit.value === null ? unit.clone() : unit.simplify();
|
1064
1137
|
|
1065
1138
|
// Apply some custom logic for handling VA and VAR. The goal is to express the value of the unit as a real value, if possible. Otherwise, use a real-valued unit instead of a complex-valued one.
|
1139
|
+
handleVAandVARUnits(simp);
|
1140
|
+
// Now apply the best prefix
|
1141
|
+
// Units must have only one unit and not have the fixPrefix flag set
|
1142
|
+
applyBestPrefixIfNeeded(simp, options.offset);
|
1143
|
+
const value = simp._denormalize(simp.value);
|
1144
|
+
const valueStr = simp.value !== null ? format(value, options || {}) : '';
|
1145
|
+
const unitStr = simp.formatUnits();
|
1146
|
+
return {
|
1147
|
+
simp,
|
1148
|
+
valueStr,
|
1149
|
+
unitStr
|
1150
|
+
};
|
1151
|
+
}
|
1152
|
+
|
1153
|
+
/**
|
1154
|
+
* Helper to handle VA and VAR units
|
1155
|
+
* @param {Unit} simp The unit to be normalized
|
1156
|
+
*/
|
1157
|
+
function handleVAandVARUnits(simp) {
|
1066
1158
|
let isImaginary = false;
|
1067
1159
|
if (typeof simp.value !== 'undefined' && simp.value !== null && (0, _is.isComplex)(simp.value)) {
|
1068
1160
|
// TODO: Make this better, for example, use relative magnitude of re and im rather than absolute
|
@@ -1079,37 +1171,32 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
|
|
1079
1171
|
}
|
1080
1172
|
}
|
1081
1173
|
}
|
1174
|
+
}
|
1082
1175
|
|
1083
|
-
|
1084
|
-
|
1176
|
+
/**
|
1177
|
+
* Helper to apply the best prefix if needed
|
1178
|
+
* @param {Unit} simp The unit to be normalized
|
1179
|
+
*/
|
1180
|
+
function applyBestPrefixIfNeeded(simp, offset) {
|
1085
1181
|
if (simp.units.length === 1 && !simp.fixPrefix) {
|
1086
1182
|
// Units must have integer powers, otherwise the prefix will change the
|
1087
1183
|
// outputted value by not-an-integer-power-of-ten
|
1088
1184
|
if (Math.abs(simp.units[0].power - Math.round(simp.units[0].power)) < 1e-14) {
|
1089
1185
|
// Apply the best prefix
|
1090
|
-
simp.units[0].prefix = simp._bestPrefix();
|
1186
|
+
simp.units[0].prefix = simp._bestPrefix(offset);
|
1091
1187
|
}
|
1092
1188
|
}
|
1093
|
-
|
1094
|
-
let str = simp.value !== null ? format(value, options || {}) : '';
|
1095
|
-
const unitStr = simp.formatUnits();
|
1096
|
-
if (simp.value && (0, _is.isComplex)(simp.value)) {
|
1097
|
-
str = '(' + str + ')'; // Surround complex values with ( ) to enable better parsing
|
1098
|
-
}
|
1099
|
-
if (unitStr.length > 0 && str.length > 0) {
|
1100
|
-
str += ' ';
|
1101
|
-
}
|
1102
|
-
str += unitStr;
|
1103
|
-
return str;
|
1104
|
-
};
|
1189
|
+
}
|
1105
1190
|
|
1106
1191
|
/**
|
1107
1192
|
* Calculate the best prefix using current value.
|
1108
1193
|
* @memberof Unit
|
1109
1194
|
* @returns {Object} prefix
|
1195
|
+
* @param {number} [offset] Optional offset for the best prefix calculation (default 1.2)
|
1110
1196
|
* @private
|
1111
1197
|
*/
|
1112
1198
|
Unit.prototype._bestPrefix = function () {
|
1199
|
+
let offset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1.2;
|
1113
1200
|
if (this.units.length !== 1) {
|
1114
1201
|
throw new Error('Can only compute the best prefix for single units with integer powers, like kg, s^2, N^-1, and so forth!');
|
1115
1202
|
}
|
@@ -1132,7 +1219,7 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
|
|
1132
1219
|
return bestPrefix;
|
1133
1220
|
}
|
1134
1221
|
const power = this.units[0].power;
|
1135
|
-
let bestDiff = Math.log(absValue / Math.pow(bestPrefix.value * absUnitValue, power)) / Math.LN10 -
|
1222
|
+
let bestDiff = Math.log(absValue / Math.pow(bestPrefix.value * absUnitValue, power)) / Math.LN10 - offset;
|
1136
1223
|
if (bestDiff > -2.200001 && bestDiff < 1.800001) return bestPrefix; // Allow the original prefix
|
1137
1224
|
bestDiff = Math.abs(bestDiff);
|
1138
1225
|
const prefixes = this.units[0].unit.prefixes;
|
@@ -1140,7 +1227,7 @@ const createUnitClass = exports.createUnitClass = /* #__PURE__ */(0, _factory.fa
|
|
1140
1227
|
if ((0, _object.hasOwnProperty)(prefixes, p)) {
|
1141
1228
|
const prefix = prefixes[p];
|
1142
1229
|
if (prefix.scientific) {
|
1143
|
-
const diff = Math.abs(Math.log(absValue / Math.pow(prefix.value * absUnitValue, power)) / Math.LN10 -
|
1230
|
+
const diff = Math.abs(Math.log(absValue / Math.pow(prefix.value * absUnitValue, power)) / Math.LN10 - offset);
|
1144
1231
|
if (diff < bestDiff || diff === bestDiff && prefix.name.length < bestPrefix.name.length) {
|
1145
1232
|
// choose the prefix with the smallest diff, or if equal, choose the one
|
1146
1233
|
// with the shortest name (can happen with SHORTLONG for example)
|
package/lib/cjs/version.js
CHANGED
@@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.version = void 0;
|
7
|
-
const version = exports.version = '14.
|
7
|
+
const version = exports.version = '14.7.0';
|
8
8
|
// Note: This file is automatically generated when building math.js.
|
9
9
|
// Changes made in this file will be overwritten.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
/**
|
2
|
+
* THIS FILE IS AUTO-GENERATED
|
3
|
+
* DON'T MAKE CHANGES HERE
|
4
|
+
*/
|
5
|
+
import { typedDependencies } from './dependenciesTyped.generated.js';
|
6
|
+
import { createToBest } from '../../factoriesAny.js';
|
7
|
+
export var toBestDependencies = {
|
8
|
+
typedDependencies,
|
9
|
+
createToBest
|
10
|
+
};
|