View Raw SPL
/*****************************************************************************
* *
* POLYVAL.SPL Copyright (C) 1998-2003 DSP Development Corporation *
* All Rights Reserved *
* *
* Author: Matthew Tom *
* *
* Synopsis: Calculates p(x) for a given polynomial p and value x. *
* *
* Revisions: 3 May 1998 MAT Creation *
* 21 Aug 1998 MAT Help Menu Entry *
* 4 Sep 2003 RRR refactored with polygraph *
* *
*****************************************************************************/
#include
#if @HELP_POLYVAL
POLYVAL
Purpose: Calculates P(x) for a given polynomial P and value x.
Syntax: POLYVAL(p, x, form)
p - A series, the coefficients of the polynomial.
x - A scalar or series. The values where the polynomial will
be evaluated.
form - Optional. An integer, the form of the polynomial
coefficients, 0: ascending powers, 1: decreasing powers.
Defaults to 1 (highest degree to lowest).
Returns: A scalar or series, the result of P(x).
Example:
polyval({1, 2, -1}, 2)
returns 7 the value of P(x) = x^2 + 2x - 1 at x = 2.
Example:
polyval({1, 2, -1}, 0..3)
returns the series {-1, 2, 7, 14}.
Remarks:
POLYVAL is similar to POLYGRAPH except the default form is
in decreasing powers (highest degree to lowest). Unlike
POLYGRAPH, POLYVAL returns a scalar for a scalar input.
See Also:
Polyfit
Polygraph
#endif
/* evaluate polynomial P(x) */
polyval(p, x, form)
{
local scalarval = FALSE;
if (argc < 3)
{
if (argc < 2)
{
error("polyval - polynomial coefficents and x range required");
}
form = 1;
}
/* check for scalar or series */
if (isscalar(x))
{
scalarval = TRUE;
x = castseries(x);
}
if (isscalar(p))
{
p = castseries(p);
}
/* degenerate cases */
if (length(x) == 0)
{
return({});
}
if (length(p) == 0)
{
return(zeros(length(x), 1));
}
/* evaluate polynomial, return values only, (no x) */
y = polygraph(p, x, 0, form);
/* cast to scalar if necessary */
if (scalarval)
{
y = y[1];
}
return(y);
}