## Modelica_LinearSystems2.Math.Vectors

Functions operating on vectors

### Information

#### Library content

This library provides functions operating on vectors:

Function | Description
--- | ---
isEqual(v1, v2) | Determines whether two vectors have the same size and elements
norm(v,p) | p-norm of vector v
length(v) | Length of vector v (= norm(v,2), but inlined and therefore usable in symbolic manipulations)
normalize(v) | Return normalized vector such that length = 1 and prevent zero-division for zero vector
reverse(v) | Reverse vector elements
sort(v) | Sort elements of vector in ascending or descending order

#### See also

Matrices

Extends from Modelica.Icons.Library (Icon for library).

### Package Content

Name | Description
--- | ---
find | find element in vector
householderReflexion | reflect vector a on a plane with orthogonal vector u
householderReflexion_e1 | reflect vector a to the unity vector e_1={1,0,...,0}
householderReflexion_en | reflect vector a to the unity vector e_n={0, ..., 0, 1}
householderVector | calculate a normalized householder vector for the reflexion of vector a onto vector b
length | Return length of a vector
printVector | print vector
Internal |

## Modelica_LinearSystems2.Math.Vectors.find

find element in vector

### Information

Extends from Modelica.Icons.Function (Icon for a function).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Integer | s |  | Search for s
Integer | v[:] |  | Vector

### Outputs

Type | Name | Description
--- | --- | ---
Integer | result | v[result] = s (first occurrence of s); result=0, if not found

### Modelica definition

    encapsulated function find "find element in vector"
      import Modelica;
      import Modelica_LinearSystems2.Math;
      extends Modelica.Icons.Function;
      input Integer s "Search for s";
      input Integer v[:] "Vector";
      output Integer result
        "v[result] = s (first occurrence of s); result=0, if not found";
    protected
      Integer i;
    algorithm
      result :=0;
      i :=1;
      while i <= size(v,1) loop
         if v[i] == s then
            result :=i;
            i :=size(v, 1) + 1;
         else
            i :=i + 1;
         end if;
      end while;
    end find;

## Modelica_LinearSystems2.Math.Vectors.householderReflexion

reflect vector a on a plane with orthogonal vector u

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | a[:] |  |
Real | u[size(a, 1)] |  | householder vector

### Outputs

Type | Name | Description
--- | --- | ---
Real | ra[size(u, 1)] | reflexion of a

### Modelica definition

    function householderReflexion
      "reflect vector a on a plane with orthogonal vector u"
      import Modelica_LinearSystems2.Math.Vectors;

      input Real a[:];
      input Real u[size(a, 1)] "householder vector";
      output Real ra[size(u, 1)] "reflexion of a";

    protected
      Real norm_a=Modelica.Math.Vectors.length(a);
      Real h=2*u*a;

    algorithm
      ra := a - h*u;

    // this function is mainly used in the fromStateSpace transformations.
    // In this context the calculation of invariant zeros is very susceptible with subject to
    // elements of the output vector are zero or not. Therefore, values close to zero are set to zero.
      for i in 1:size(ra, 1) loop
        ra[i] := if abs(ra[i]) >= norm_a*1e-12 then ra[i] else 0;
      end for;

    end householderReflexion;

## Modelica_LinearSystems2.Math.Vectors.householderReflexion_e1

reflect vector a to the unity vector e_1={1,0,...,0}

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | a[:] |  |
Real | u[size(a, 1)] |  | householder vector

### Outputs

Type | Name | Description
--- | --- | ---
Real | ra[size(u, 1)] | reflexion of a

### Modelica definition

    function householderReflexion_e1
      "reflect vector a to the unity vector e_1={1,0,...,0}"
      import Modelica_LinearSystems2.Math.Vectors;

      input Real a[:];
      input Real u[size(a, 1)] "householder vector";
      output Real ra[size(u, 1)] "reflexion of a";

    protected
      Integer n=size(u, 1);

    algorithm
      ra := cat(
        1,
        {a[1] - 2*u[1]*(u*a)},
        fill(0, n - 1));

    end householderReflexion_e1;

## Modelica_LinearSystems2.Math.Vectors.householderReflexion_en

reflect vector a to the unity vector e_n={0, ..., 0, 1}

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | a[:] |  |
Real | u[size(a, 1)] |  | householder vector

### Outputs

Type | Name | Description
--- | --- | ---
Real | ra[size(u, 1)] | reflexion of a

### Modelica definition

    function householderReflexion_en
      "reflect vector a to the unity vector e_n={0, ..., 0, 1}"
      import Modelica_LinearSystems2.Math.Vectors;

      input Real a[:];
      input Real u[size(a, 1)] "householder vector";
      output Real ra[size(u, 1)] "reflexion of a";

    protected
      Integer n=size(u, 1);

    algorithm
      ra := cat(
        1,
        fill(0, n - 1),
        {a[n] - 2*u[n]*(u*a)});

    end householderReflexion_en;

## Modelica_LinearSystems2.Math.Vectors.householderVector

calculate a normalized householder vector for the reflexion of vector a onto vector b

### Information

#### Syntax

>     Vectors.householderVector(a, b);

#### Description

The function call "`householderVector(a, b)`" returns vector u, which is the normalized Householder vector for a Householder reflexion with matrix Q

Q = I - 2*u*u'

with

Q*a = c*b

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | a[:] |  |
Real | b[size(a, 1)] |  |

### Outputs

Type | Name | Description
--- | --- | ---
Real | u[size(a, 1)] |

### Modelica definition

    function householderVector
      "calculate a normalized householder vector for the reflexion of vector a onto vector b "

      import Modelica_LinearSystems2.Math.Vectors.length;

      input Real a[:];
      input Real b[size(a, 1)];
      output Real u[size(a, 1)];
    protected
      Real la=length(a);
      Real alpha=if length(a + la*b) > 0 then la else -la;

    algorithm
      assert(length(b) > 0,
        "vector b in function housholderVector is zero vector, but al least one element should be different to zero ");
      assert(length(a) > 0,
        "vector a in function housholderVector is zero vector, but al least one element should be different to zero");
      u := (a + alpha*b)/length(a + alpha*b);

    end householderVector;

## Modelica_LinearSystems2.Math.Vectors.length

Return length of a vector

### Information

#### Syntax

>     Vectors.length(v);

#### Description

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(..).

#### Example

>       v = {2, -4, -2, -1};
>       length(v);  // = 5

#### See also

Vectors.norm

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | v[:] |  | Vector

### Outputs

Type | Name | Description
--- | --- | ---
Real | result | Length of vector v

### Modelica definition

    function length "Return length of a vector"

      input Real v[:] "Vector";
      output Real result "Length of vector v";

    algorithm
      result := sqrt(v*v);
    end length;

## Modelica_LinearSystems2.Math.Vectors.printVector

print vector

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | v[:] |  |
Integer | significantDigits | 6 | Number of significant digits that are shown
String | name | "v" | Independent variable name used for printing

### Outputs

Type | Name | Description
--- | --- | ---
String | s |

### Modelica definition

    function printVector "print vector"
      import Modelica_LinearSystems2.StateSpace;
      import Modelica.Utilities.Strings;

      input Real v[:];
      input Integer significantDigits=6
        "Number of significant digits that are shown";
      input String name="v" "Independent variable name used for printing";
      output String s="";
    protected
      String blanks=Strings.repeat(significantDigits);
      String space=Strings.repeat(8);
      String space2=Strings.repeat(3);
      Integer r=size(v, 1);

    algorithm
      if r == 0 then
        s := name + " = []";
      else
        s := "\n" + name + " = \n";
        for i in 1:r loop
          s := s + space;

          if v[i] >= 0 then
            s := s + " ";
          end if;
          s := s + String(v[i], significantDigits=significantDigits) +
            Strings.repeat(significantDigits + 8 - Strings.length(String(abs(v[i]))));

          s := s + "\n";
        end for;

      end if;
    end printVector;