Library of functions operating on complex vectors
This library provides functions operating on vectors of Complex numbers.
Extends from Modelica.Icons.Package (Icon for standard packages).
Name | Description |
---|---|
norm | Returns the p-norm of a complex vector |
length | Return length of a complex vector |
normalize | Return normalized complex vector such that length = 1 and prevent zero-division for zero vector |
reverse | Reverse vector elements (e.g., v[1] becomes last element) |
sort | Sort elements of complex vector |
Returns the p-norm of a complex vector
Vectors.norm(v); Vectors.norm(v,p=2); // 1 ≤ p ≤ ∞
The function call "Vectors.norm(v)
" returns the
Euclidean norm "sqrt(v*v)
" of vector v.
With the optional
second argument "p", any other p-norm can be computed:
Besides the Euclidean norm (p=2), also the 1-norm and the infinity-norm are sometimes used:
1-norm | = sum(abs(v)) | norm(v,1) |
2-norm | = sqrt(v*v) | norm(v) or norm(v,2) |
infinity-norm | = max(abs(v)) | norm(v,Modelica.Constants.inf) |
Note, for any vector norm the following inequality holds:
norm(v1+v2,p) ≤ norm(v1,p) + norm(v2,p)
v = {2, -4, -2, -1}; norm(v,1); // = 9 norm(v,2); // = 5 norm(v); // = 5 norm(v,10.5); // = 4.00052597412635 norm(v,Modelica.Constants.inf); // = 4
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
v[:] | Vector |
p | Type of p-norm (often used: 1, 2, or Modelica.Constants.inf) |
Name | Description |
---|---|
result | p-norm of vector v |
Return length of a complex vector
Vectors.length(v);
The function call "Vectors.length(v)
" returns the
Euclidean length "sqrt(v*v)
" of vector v.
The function call is equivalent to Vectors.norm(v). The advantage of
length(v) over norm(v)"is that function length(..) is implemented
in one statement and therefore the function is usually automatically
inlined. Further symbolic processing is therefore possible, which is
not the case with function norm(..).
v = {2, -4, -2, -1}; length(v); // = 5
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
v[:] | Vector |
Name | Description |
---|---|
result | Length of vector v |
Return normalized complex vector such that length = 1 and prevent zero-division for zero vector
Vectors.normalize(v); Vectors.normalize(v,eps=100*Modelica.Constants.eps);
The function call "Vectors.normalize(v)
" returns the
unit vector "v/length(v)
" of vector v.
If length(v) is close to zero (more precisely, if length(v) < eps),
v is returned in order to avoid
a division by zero. For many applications this is useful, because
often the unit vector e = v/length(v) is used to compute
a vector x*e, where the scalar x is in the order of length(v),
i.e., x*e is small, when length(v) is small and then
it is fine to replace e by v to avoid a division by zero.
Since the function is implemented in one statement, it is usually inlined and therefore symbolic processing is possible.
normalize({1,2,3}); // = {0.267, 0.534, 0.802} normalize({0,0,0}); // = {0,0,0}
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
v[:] | Vector |
eps | if |v| < eps then result = v |
Name | Description |
---|---|
result[size(v, 1)] | Input vector v normalized to length=1 |
Reverse vector elements (e.g., v[1] becomes last element)
Vectors.reverse(v);
Vectors.reverse(v)
" returns the complex vector elements in reverse order.
reverse({1,2,3,4}); // = {4,3,2,1}
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
v[:] | Vector |
Name | Description |
---|---|
result[size(v, 1)] | Elements of vector v in reversed order |
Sort elements of complex vector
sorted_v = Vectors.sort(v); (sorted_v, indices) = Vectors.sort(v, ascending=true);
Function sort(..) sorts a Real vector v in ascending order and returns the result in sorted_v. If the optional argument "ascending" is false, the vector is sorted in descending order. In the optional second output argument the indices of the sorted vector with respect to the original vector are given, such that sorted_v = v[indices].
(v2, i2) := Vectors.sort({-1, 8, 3, 6, 2}); -> v2 = {-1, 2, 3, 6, 8} i2 = {1, 5, 3, 4, 2}
Extends from Modelica.Icons.Function (Icon for functions).
Name | Description |
---|---|
v[:] | Vector to be sorted |
ascending | = true if ascending order, otherwise descending order |
sortFrequency | = true, if sorting is first for imaginary then for real value; = false, if sorting is for absolute value |
Name | Description |
---|---|
sorted_v[size(v, 1)] | Sorted vector |
indices[size(v, 1)] | sorted_v = v[indices] |