函数 norm #
计算数字、向量或矩阵的范数。
第二个参数 p 是可选的。如果未提供,则默认为 2。
Syntax #
math.norm(x)
math.norm(x, p)
Parameters #
| Parameter | Type | Description |
|---|---|---|
x |
number | BigNumber | Complex | Array | Matrix | 要计算范数的值 |
p |
number | BigNumber | string | 向量空间。支持的数字包括 Infinity 和 -Infinity。支持的字符串包括:‘inf’、‘-inf’ 和 ‘fro’(Frobenius 范数)。默认值:2。 |
Returns #
| Type | Description |
|---|---|
| number | BigNumber | p-范数 |
Throws #
Type | Description —- | ———–
Examples #
math.abs(-3.5) // returns 3.5
math.norm(-3.5) // returns 3.5
math.norm(math.complex(3, -4)) // returns 5
math.norm([1, 2, -3], Infinity) // returns 3
math.norm([1, 2, -3], -Infinity) // returns 1
math.norm([3, 4], 2) // returns 5
math.norm([[1, 2], [3, 4]], 1) // returns 6
math.norm([[1, 2], [3, 4]], 'inf') // returns 7
math.norm([[1, 2], [3, 4]], 'fro') // returns 5.477225575051661