Skip to content

Latest commit

 

History

History
191 lines (119 loc) · 4.09 KB

File metadata and controls

191 lines (119 loc) · 4.09 KB

polevl

Evaluate a polynomial using Horner's rule.

Usage

var polevl = require( '@stdlib/math/base/special/polevl' );

polevl( x, coef, N )

Evaluates a polynomial using Horner's rule.

The function accepts the following arguments:

  • x: number - input value.
  • coef: Collection<number> - polynomial coefficients.
  • N: number - degree of the polynomial.
var coef = [ 1.0, 2.0, 3.0 ]; // Represents 1 + 2x + 3x^2
var v = polevl( 2.0, coef, 2 );
// returns 17.0  ( 1 + 2 * ( 2 ) + 3 * ( 2^2 ) = 1 + 4 + 12 = 17 )

v = polevl( 0.0, [ 5.0, -2.0 ], 1 );
// returns 5.0 ( 5 - 2*0 = 5 )

v = polevl( 1.0, [ 1.0, 1.0, 1.0, 1.0 ], 3 );
// returns 4.0 ( 1 + 1 + 1 + 1 = 4 )

v = polevl( NaN, coef, 2 );
// returns NaN

Examples

var polevl = require( '@stdlib/math/base/special/polevl' );

var coef = [ 1.0, 2.0, 3.0, 4.0 ]; // 1 + 2x + 3x^2 + 4x^3
var x = 2.0;
var v = polevl( x, coef, 3 );
console.log( 'polevl(%d, [ %s ], %d) = %d', x, coef.join( ', ' ), 3, v );
// => 'polevl(2, [ 1, 2, 3, 4 ], 3) = 49'

C APIs

Usage

#include "stdlib/math/base/special/polevl.h"

stdlib_base_polevl( x, coef, N )

Evaluates a polynomial using Horner's rule.

#include <stdio.h> // For printf
#include "stdlib/math/base/special/polevl.h"

double coef[] = { 1.0, 2.0, 3.0 }; // Represents 1 + 2x + 3x^2
double v = stdlib_base_polevl( 2.0, coef, 2 );
// returns 17.0

printf( "polevl( %lf, [ %lf, %lf, %lf ], %d ) = %lf\n", 2.0, coef[0], coef[1], coef[2], 2, v );
// => polevl( 2.000000, [ 1.000000, 2.000000, 3.000000 ], 2 ) = 17.000000

The function accepts the following arguments:

  • x: [in] double input value.
  • coef: [in] const double* polynomial coefficients.
  • N: [in] int degree of the polynomial.
double stdlib_base_polevl( const double x, const double coef[], const int N );

Examples

#include "stdlib/math/base/special/polevl.h"
#include <stdio.h> // For printf

int main( void ) {
    double x[] = { 1.0, 2.0, 3.0 };
    double coef[] = { 1.0, 2.0, 3.0, 4.0 }; // 1 + 2x + 3x^2 + 4x^3
    double v;
    int i;

    for ( i = 0; i < 3; i++ ) {
        v = stdlib_base_polevl( x[ i ], coef, 3 );
        printf( "polevl(%lf, [ %lf, %lf, %lf, %lf ], %d) = %lf\n", x[ i ], coef[0], coef[1], coef[2], coef[3], 3, v );
    }
}