View Raw SPL
/*****************************************************************************
*                                                                            *
*   POLYINT.SPL    Copyright (C) 2010 DSP Development Corporation            *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Performs polynomial integration                             *
*                                                                            *
*   Revisions:   18 Jun 2010  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_POLYINT

    POLYINT

    Purpose: Performs polynomial integration

    Syntax:  POLYINT(p, k, form)

                 p - A series, the coefficients of the polynomial.

                 k - A scalar, the integration constant, defaults to 0.0.

              form - Optional. An integer, the form of the polynomial
                     coefficients
                         
                      0: ascending powers
                      1: decreasing powers (default, highest degree to lowest).

    Returns: A series.

    Example:
             polyint({1, 2, 3})

             returns the series {0.333, 1, 3, 0} representing the polynomial
             1/3 x^3 + x^2 + 3x as the integration of x^2 + 2x + 3.

    Example:
             polyint({1, 2, 3}, 10)

             returns the series {0.333, 1, 3, 10} representing the polynomial
             1/3 x^3 + x^2 + 3x + 10 as the integration of x^2 + 2x + 3 with
             an integration constant of 10.

    Example:
             polyint({1, 2, 3}, 10, 0)

             returns the series {10, 1, 1, 1} representing the polynomial
             10 + x + x^2 + x^3 as the integration of 1 + 2x^2 + 3x^3 with an
             integration constant of 10.

    Example:
             polyder(polyint({1, 2, 3}))

             returns the series {1, 2, 3} indicating that polyder is an
             inverse function of polyint.

    Remarks:
             If the input is a matrix, POLYINT considers each column
             to represent the polynomial coefficients with the result
             having the same number of columns as the input.

    See Also:
             Poly
             Polyder
             Polyfit
             Polygraph
             Polyval
             Roots
#endif


/* polynomial integration */
ITERATE polyint (p, k, form)
{
        if (argc < 3)
        {
                if (argc < 2)
                {
                        if (argc < 1) error("polyint - input series required");
                        
                        k = 0;
                }
                
                form = 1;
        }

        if (not(isscalar(k)))
        {
                error("polyint - integration constant must be a scalar");
        }

        /* ensure series */
        if (not(isarray(p)))
        {
                p = {p};
        }

        if (not(form))
        {
                /* ascending powers */
                p =  {k, rev(p) / (length(p)..-1..1)};
        }
        else
        {
                /* polynomial integration */
                p =  {p / (length(p)..-1..1), k};
        }

        return(p);
}