配置 #
Math.js 包含许多配置选项。这些选项可以应用于已创建的 mathjs 实例,并在之后进行更改。
import { create, all } from 'mathjs'
// create a mathjs instance with configuration
const config = {
relTol: 1e-12,
absTol: 1e-15,
matrix: 'Matrix',
number: 'number',
numberFallback: 'number',
precision: 64,
predictable: false,
randomSeed: null,
legacySubset: false
}
const math = create(all, config)
// read the applied configuration
console.log(math.config())
// change the configuration
math.config({
number: 'BigNumber'
})
以下配置选项可用:
-
relTol。用于测试两个比较值之间相等性的最小相对差值。此值用于所有关系函数。默认值为1e-12。 -
absTol。用于测试两个比较值之间相等性的最小绝对差值。此值用于所有关系函数。默认值为1e-15。 -
matrix。函数输出的默认矩阵类型。可用值包括:'Matrix'(默认)或'Array'。在可能的情况下,函数的矩阵输出类型由函数输入决定:数组作为输入将返回数组,Matrix 作为输入将返回 Matrix。在没有矩阵作为输入的情况下,输出类型由选项matrix决定。在混合矩阵输入的情况下,将始终返回一个矩阵。 -
number。用于将字符串解析为数值或在内部创建新数值的类型。对于大多数函数,输出类型由输入决定:数字作为输入将返回数字作为输出,BigNumber 作为输入返回 BigNumber 作为输出。但例如函数
math.evaluate('2+3')、math.parse('2+3')、math.range('1:10')和math.unit('5cm')使用number配置设置。请注意,
math.sqrt(4)始终返回数字2,无论number配置如何,因为数字类型可以从输入值确定。可用值包括:
'number'(默认)、'BigNumber'、'bigint'或'Fraction'。BigNumbers 具有比 JavaScript 的默认数字更高的精度,bigint 可以表示大整数,而Fractions以分子和分母的形式存储值。 -
numberFallback。当number配置为例如'bigint'值,并且某个值无法表示为bigint,例如在math.evaluate('2.3')中,该值将按照numberFallback配置的类型进行解析。可用值:'number'(默认)或'BigNumber'。 -
precision。BigNumbers 的最大有效数字位数。此设置仅适用于 BigNumbers,不适用于数字。默认值为64。 -
predictable。函数的可预测输出类型。当为 true 时,输出类型仅取决于输入类型。当为 false(默认)时,输出类型可能因输入值而异。例如,当 predictable 为 false 时,math.sqrt(-4)返回complex('2i'),当 predictable 为 true 时,返回NaN。在以编程方式处理计算结果时可能需要可预测的输出,但对于评估动态方程的用户来说可能不方便。 -
randomSeed。将此选项设置为伪随机数生成器的种子,使其具有确定性。每次设置此选项时,都会使用提供的种子重置伪随机数生成器。例如,将其设置为'a'将导致math.random()在每次设置选项后首次调用时返回0.43449421599986604。设置为null以使用随机种子初始化伪随机数生成器。默认值为null。 -
legacySubset。当设置为true时,subset函数的行为与 math.js 的早期版本相同:检索索引大小只有一个元素的子集时,将返回该值本身。当设置为false(默认)时,如果索引维度是标量,subset将消除结果中的维度;如果索引维度是范围、矩阵或数组,则保留维度。此选项有助于保持与依赖先前行为的旧代码的兼容性。
Examples #
本节展示了一些配置示例。
node.js #
import { create, all } from 'mathjs'
const config = {
matrix: 'Array' // Choose 'Matrix' (default) or 'Array'
}
const math = create(all, config)
// range will output an Array
math.range(0, 4) // Array [0, 1, 2, 3]
// change the configuration from Arrays to Matrices
math.config({
matrix: 'Matrix' // Choose 'Matrix' (default) or 'Array'
})
// range will output a Matrix
math.range(0, 4) // Matrix [0, 1, 2, 3]
// create an instance of math.js with BigNumber configuration
const bigmath = create(all, {
number: 'BigNumber', // Choose 'number' (default), 'BigNumber', or 'Fraction'
precision: 32 // 64 by default, only applicable for BigNumbers
})
// parser will parse numbers as BigNumber now:
bigmath.evaluate('1 / 3') // BigNumber, 0.33333333333333333333333333333333
浏览器 #
<!DOCTYPE HTML>
<html>
<head>
<script src="math.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
// the default instance of math.js is available as 'math'
// range will output a Matrix
math.range(0, 4) // Matrix [0, 1, 2, 3]
// change the configuration of math from Matrices to Arrays
math.config({
matrix: 'Array' // Choose 'Matrix' (default) or 'Array'
})
// range will output an Array
math.range(0, 4) // Array [0, 1, 2, 3]
// create a new instance of math.js with bignumber configuration
const bigmath = math.create({
number: 'BigNumber', // Choose 'number' (default), 'BigNumber', or 'Fraction'
precision: 32 // 64 by default, only applicable for BigNumbers
})
// parser will parse numbers as BigNumber now:
bigmath.evaluate('1 / 3') // BigNumber, 0.33333333333333333333333333333333
</script>
</body>
</html>