## Modelica_LinearSystems2.WorkInProgress.Math.Complex.Matrices

### Package Content

Name | Description
--- | ---
conditionNumber | Calculate the condition number norm(A)*norm(inv(A))
norm | Returns the norm of a matrix
inv | Inverse of a comlex matrix (try to avoid, use function solve(..) instead)

## Modelica_LinearSystems2.WorkInProgress.Math.Complex.Matrices.conditionNumber

Calculate the condition number norm(A)*norm(inv(A))

### Information

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

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Complex | A[:, :] |  | Input matrix
Real | p | 2 | Type of p-norm (only allowed: 1, 2 or Modelica.Constants.inf)

### Outputs

Type | Name | Description
--- | --- | ---
Real | result | p-norm of matrix A

### Modelica definition

    encapsulated function conditionNumber
      "Calculate the condition number norm(A)*norm(inv(A))"
      extends Modelica.Icons.Function;
      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2;
      import Modelica;

      input Complex A[:,:] "Input matrix";
      input Real p(min=1) = 2
        "Type of p-norm (only allowed: 1, 2 or Modelica.Constants.inf)";
      output Real result=0.0 "p-norm of matrix A";
    protected
      Real eps=1e-20;
      Real s[size(A, 1)] "singular values";

    algorithm
      if p == 2 then
        s := Modelica_LinearSystems2.WorkInProgress.Math.Matrices.C_singularValues(
                                                                    A);
        if min(s) < eps then
          result := -1e100;
        else
          result := max(s)/min(s);
        end if;
      else
        result := Modelica_LinearSystems2.WorkInProgress.Math.Complex.Matrices.norm(
                                        A, p)*Modelica_LinearSystems2.WorkInProgress.Math.Complex.Matrices.norm(
                                                                    Modelica_LinearSystems2.WorkInProgress.Math.Complex.Matrices.inv(
                                                                                         A), p);
      end if;
    end conditionNumber;

## Modelica_LinearSystems2.WorkInProgress.Math.Complex.Matrices.norm

Returns the norm of a matrix

### Information

#### Syntax

>     Matrices.norm(A);
>     Matrices.norm(A, p=2);

#### Description

The function call "`Matrices.norm(A)`" returns the 2-norm of matrix A, i.e., the largest singular value of A.
The function call "`Matrices.norm(A, p)`" returns the p-norm of matrix A. The only allowed values for p are

- "p=1": the largest column sum of A
- "p=2": the largest singular value of A
- "p=Modelica.Constants.inf": the largest row sum of A

Note, for any matrices A1, A2 the following inequality holds:

>     Matrices.norm(A1+A2,p) ≤ Matrices.norm(A1,p) + Matrices.norm(A2,p)

Note, for any matrix A and vector v the following inequality holds:

>     Vectors.norm(A*v,p) ≤ Matrices.norm(A,p)*Vectors.norm(A,p)

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

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Complex | A[:, :] |  | Input matrix
Real | p | 2 | Type of p-norm (only allowed: 1, 2 or Modelica.Constants.inf)

### Outputs

Type | Name | Description
--- | --- | ---
Real | result | p-norm of matrix A

### Modelica definition

    function norm "Returns the norm of a matrix"
      extends Modelica.Icons.Function;
      import Modelica_LinearSystems2;
      import Modelica_LinearSystems2.Math;
      import Modelica_LinearSystems2.Math.Complex;

      input Complex A[:, :] "Input matrix";
      input Real p(min=1) = 2
        "Type of p-norm (only allowed: 1, 2 or Modelica.Constants.inf)";
      output Real result=0.0 "p-norm of matrix A";

    algorithm
      if p == 1 then
        // column sum norm
        for i in 1:size(A, 2) loop
          result := max(result, sum(Complex.'abs'(A[:, i])));

        end for;
      elseif p == 2 then
        // largest singular value
        result := max(Modelica_LinearSystems2.WorkInProgress.Math.Matrices.C_singularValues(
                                                     A));
      elseif p==3 then
        // Frobenius norm
        result := Complex.Internal.frobeniusNorm(A);
      elseif p == Modelica.Constants.inf then
        // row sum norm
        for i in 1:size(A, 1) loop
          result := max(result, sum(Complex.'abs'(A[i, :])));
        end for;
      else
        assert(false, "Optional argument \"p\" of function \"norm\" must be
    1, 2 or Modelica.Constants.inf");
      end if;
    end norm;

## Modelica_LinearSystems2.WorkInProgress.Math.Complex.Matrices.inv

Inverse of a comlex matrix (try to avoid, use function solve(..) instead)

### Information

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

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Complex | A[:, size(A, 1)] |  |

### Outputs

Type | Name | Description
--- | --- | ---
Complex | invA[size(A, 1), size(A, 2)] | Inverse of matrix A

### Modelica definition

    function inv
      "Inverse of a comlex matrix (try to avoid, use function solve(..) instead)"
      extends Modelica.Icons.Function;

      import Modelica_LinearSystems2.Math.Complex;
      import Modelica_LinearSystems2.Math.Matrices;

      input Complex A[:,size(A, 1)];
      output Complex invA[size(A, 1),size(A, 2)] "Inverse of matrix A";
    protected
      Integer info;
      Integer pivots[size(A, 1)] "Pivot vector";
      Complex LU[size(A, 1),size(A, 2)] "LU factors of A";
    algorithm
      (LU,pivots,info) := Modelica_LinearSystems2.WorkInProgress.Math.LAPACK.zgetrf(
                                                 A);

      assert(info == 0,
                    "Calculating an inverse matrix with function
    \"Matrices.inv\" is not possible, since matrix A is singular.");

      (invA,info) := Modelica_LinearSystems2.WorkInProgress.Math.LAPACK.zgetri(
                                            LU, pivots);

    end inv;