## FCSys.Utilities.Polynomial

Polynomial functions

### Information

Extends from Modelica.Icons.Package (Icon for standard packages).

### Package Content

Name | Description
--- | ---
F | ∫f()·dx evaluated at x with zero integration constant
dF | Derivative of F()
f | Polynomial expressed in form: f = ((… + a-1-n)/x + a-n)/x + a1-n + x·(a2-n + x·(a3-n + …))
df | Derivative of f()
d2f | Derivative of df()

## FCSys.Utilities.Polynomial.F

∫f()·dx evaluated at x with zero integration constant

### Information

By definition, the partial derivative of this function with respect to x (with a constant) is f(). The complete derivative, however, is dF().

Extends from Modelica.Icons.Function (Icon for functions).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | x |  | Argument
Real | a[:] |  | Coefficients
Integer | n | 0 | Power associated with the first term (before integral)

### Outputs

Type | Name | Description
--- | --- | ---
Real | F | Integral

### Modelica definition

    function F
      "∫f()·dx evaluated at x with zero integration constant"
      annotation(derivative=FCSys.Utilities.Polynomial.dF);

      extends Modelica.Icons.Function;
      input Real x "Argument";
      input Real a[:] "Coefficients";
      input Integer n=0 "Power associated with the first term (before integral)";
      output Real F "Integral";

    algorithm
      F := f(
        x,
        a .* {if n + i == 0 then log(x) else 1/(n + i) for i in 1:size(a, 1)},
        n + 1);
    end F;

## FCSys.Utilities.Polynomial.dF

Derivative of F()

### Information

Extends from Modelica.Icons.Function (Icon for functions).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | x |  | Argument
Real | a[:] |  | Coefficients
Integer | n | 0 | Power associated with the first term (before integral)
Real | dx |  | Derivative of argument
Real | da[size(a, 1)] | zeros(size(a, 1)) | Derivatives of coefficients

### Outputs

Type | Name | Description
--- | --- | ---
Real | dF | Derivative

### Modelica definition

    function dF
      "Derivative of F()"
      extends Modelica.Icons.Function;

      input Real x "Argument";
      input Real a[:] "Coefficients";
      input Integer n=0 "Power associated with the first term (before integral)";
      input Real dx "Derivative of argument";
      input Real da[size(a, 1)]=zeros(size(a, 1)) "Derivatives of coefficients";
      output Real dF "Derivative";

    algorithm
      dF := f(
        x,
        a,
        n)*dx + f(
        x,
        da .* {if n + i == 0 then log(x) else 1/(n + i) for i in 1:size(a, 1)},
        n + 1);
    end dF;

## FCSys.Utilities.Polynomial.f

Polynomial expressed in form: f = ((… + a-1-n)/x + a-n)/x + a1-n + x·(a2-n + x·(a3-n + …))

### Information

For high-order polynomials, this is more computationally efficient than the form Σai xn + i - 1.

Note that the order of the polynomial is `n + size(a, 1) - 1` (not `n`).

The derivative of this function is df().

Extends from Modelica.Icons.Function (Icon for functions).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | x |  | Argument
Real | a[:] |  | Coefficients
Integer | n | 0 | Power of the first term

### Outputs

Type | Name | Description
--- | --- | ---
Real | f | Result

### Modelica definition

    function f
      "Polynomial expressed in form: f = ((… + a-1-n)/x + a-n)/x + a1-n + x·(a2-n + x·(a3-n + …))"
      annotation(derivative=FCSys.Utilities.Polynomial.df);
      extends Modelica.Icons.Function;
      input Real x "Argument";
      input Real a[:] "Coefficients";
      input Integer n=0 "Power of the first term";
      output Real f "Result";

    protected

        "Polynomial expressed in form: y = x*(a + x*(a2 + …))"
        input Real x "Argument";
        input Real a[:] "Coefficients";
        output Real y "Result";

      algorithm
        y := if size(a, 1) > 0 then x*(a[1] + (if size(a, 1) > 1 then x*(a[2] + (
          if size(a, 1) > 2 then x*(a[3] + (if size(a, 1) > 3 then x*(a[4] + (if
          size(a, 1) > 4 then x*(a[5] + (if size(a, 1) > 5 then x*(a[6] + (if size(
          a, 1) > 6 then x*(a[7] + (if size(a, 1) > 7 then x*(a[8] + (if size(a, 1) >
          8 then x*(a[9] + (if size(a, 1) > 9 then x*(a[10] + (if size(a, 1) > 10
           then positivePoly(x, a[11:end]) else 0)) else 0)) else 0)) else 0))
           else 0)) else 0)) else 0)) else 0)) else 0)) else 0)) else 0;
        // Note:  Dymola 7.4 does seem to not inline the recursive calls beyond
        // depth 1; therefore, the function is "unrolled" up to the 10th order.
        // Also, in Dymola 7.4, if this function is called from a stack of (nested)
        // functions, it seems to reduce the depth allowed for the nested
        // parentheses.  The implementation here ("unrolled" only up to the 10th
        // order) allows poly() to be called from within one other function within
        // a model.

      end positivePoly;

    algorithm
      f := (if n < 0 then (if n + size(a, 1) < 0 then x^(n + size(a, 1)) else 1)*
        positivePoly(1/x, a[min(size(a, 1), -n):-1:1]) else 0) + (if n <= 0 and n >
        -size(a, 1) then a[1 - n] else 0) + (if n + size(a, 1) > 1 then (if n > 1
         then x^(n - 1) else 1)*positivePoly(x, a[1 + max(0, 1 - n):size(a, 1)])
         else 0);
      // Here, Dymola 2014 won't allow indexing via a[1 + max(0, 1 - n):end], so
      // a[1 + max(0, 1 - n):size(a, 1)] is necessary.
    end f;

## FCSys.Utilities.Polynomial.df

Derivative of f()

### Information

The derivative of this function is d2f().

Extends from Modelica.Icons.Function (Icon for functions).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | x |  | Argument
Real | a[:] |  | Coefficients
Integer | n | 0 | Power associated with the first term (before derivative)
Real | dx |  | Derivative of argument
Real | da[size(a, 1)] | zeros(size(a, 1)) | Derivatives of coefficients

### Outputs

Type | Name | Description
--- | --- | ---
Real | df | Derivative

### Modelica definition

    function df
      "Derivative of f()"
      annotation(derivative=FCSys.Utilities.Polynomial.d2f);
      extends Modelica.Icons.Function;
      input Real x "Argument";
      input Real a[:] "Coefficients";
      input Integer n=0 "Power associated with the first term (before derivative)";
      input Real dx "Derivative of argument";
      input Real da[size(a, 1)]=zeros(size(a, 1)) "Derivatives of coefficients";
      output Real df "Derivative";

    algorithm
      df := f(
        x,
        a={(n + i - 1)*a[i] for i in 1:size(a, 1)},
        n=n - 1)*dx + f(
        x,
        da,
        n);
    end df;

## FCSys.Utilities.Polynomial.d2f

Derivative of df()

### Information

Extends from Modelica.Icons.Function (Icon for functions).

### Inputs

Type | Name | Default | Description
--- | --- | --- | ---
Real | x |  | Argument
Real | a[:] |  | Coefficients
Integer | n | 0 | Power associated with the first term (before derivative)
Real | dx |  | Derivative of argument
Real | da[size(a, 1)] | zeros(size(a, 1)) | Derivatives of coefficients
Real | d2x |  | Second derivative of argument
Real | d2a[size(a, 1)] | zeros(size(a, 1)) | Second derivatives of coefficients

### Outputs

Type | Name | Description
--- | --- | ---
Real | d2f | Second derivative

### Modelica definition

    function d2f
      "Derivative of df()"
      extends Modelica.Icons.Function;
      input Real x "Argument";
      input Real a[:] "Coefficients";
      input Integer n=0 "Power associated with the first term (before derivative)";
      input Real dx "Derivative of argument";
      input Real da[size(a, 1)]=zeros(size(a, 1)) "Derivatives of coefficients";
      input Real d2x "Second derivative of argument";
      input Real d2a[size(a, 1)]=zeros(size(a, 1))
        "Second derivatives of coefficients";
      output Real d2f "Second derivative";

    algorithm
      d2f := sum(f(
        x,
        {a[i]*(n + i - 1)*(n + i - 2)*dx^2,(n + i - 1)*(2*da[i]*dx + a[i]*d2x),d2a[
          i]},
        n + i - 3) for i in 1:size(a, 1));
    end d2f;