vectorNorm¶
Computes various norms of a vector or the difference of two vectors.
Synopsis¶
vectorNorm (x)
Required Arguments¶
- float
x
(Input) - Input vector for which the norm is to be computed
Return Value¶
The requested norm of the input vector. If the norm cannot be computed, NaN is returned.
Optional Arguments¶
oneNorm
- Return the 1-norm.
infNorm
(Output)Compute the infinity norm,
\[\|x\|_{\infty} = \max_{0 \leq i < n} \left|x_i\right|\]The index at which the vector has its maximum absolute value is also returned.
secondVector
(Input)- Return the norm of x-y.
Description¶
By default, vectorNorm
computes the Euclidean norm
\[\left(\sum_{i=0}^{n-1} x_i^2\right)^{\tfrac{1}{2}}\]
If the option oneNorm
is selected, the 1-norm
\[\sum_{i=0}^{n-1} \left|x_i\right|\]
is returned. If the option infNorm
is selected, the infinity norm
\[\max ∣x_i∣\]
is returned. In the case of the infinity norm, the program also returns the
index of the element with maximum modulus. If secondVector
is selected,
then the norm of x −y is computed.
Examples¶
Example 1¶
In this example, the Euclidean norm of an input vector is computed.
from __future__ import print_function
from pyimsl.math.vectorNorm import vectorNorm
x = [1., 3., -2., 4.]
norm = vectorNorm(x)
print("Euclidian norm of x = ", norm)
Output¶
Euclidian norm of x = 5.477225575051661
Example 2¶
This example computes \(\max|x_i-y_i|\) and prints the norm and index.
from __future__ import print_function
from pyimsl.math.vectorNorm import vectorNorm
x = [1., 3., -2., 4.]
y = [4., 2., -1., -5.]
index = []
norm = vectorNorm(x, secondVector=y, infNorm=index)
print("Infinity norm of x-y =", norm, end=' ')
print("at location", index[0])
Output¶
Infinity norm of x-y = 9.0 at location 3