RWalib C Array Library User Guide > More Example Code
  

More Example Code
 
Polynomial evaluation at multiple points in one or multiple dimensions:
The routine alibPolyEvald below evaluates an n-variate polynomial at m points. It handles double-precision real data, but analogous routines for other data types are obtained simply by changing the 'd' suffix on the RWalib routines, e.g. for double-precision complex data, change the d suffixes to z suffixes. Following the code for alibPolyEvald are examples in 1, 2, and 3 dimensions.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
 
/* evaluate the n-variate polynomial defined by d and p at the m points x;  */
/* p is the coefficient array ordered like an n-dimensional array of dimensions d,    */
/* where p(j1,...,jn) is the coefficient for (x1^j1)*...*(xn^jn);           */
/* array x contains the m evaluation points ordered like an (n,m) matrix;   */
/* returned is pointer to m-element array of polynomial values at points x; */
double *alibPolyEvald( wvlong n, wvlong *d, double *p, wvlong m, double *x )
{
    wvlong k, q, *i, *j[64];
    double *f, *g, *r;
    q = alibProdl(n,d);
    i = (wvlong*)malloc(q*sizeof(wvlong));
    for ( k=0; k<n; k++ ) j[k] = (wvlong*)malloc(q*sizeof(wvlong));
    f = (double*)malloc(m*q*sizeof(double));
    g = (double*)malloc(m*q*sizeof(double));
    r = (double*)malloc(m*sizeof(double));
    alibIndexl( q, 0, 1, i );
    alibIndex1dToNd( n, d, q, i, j );
    alibOuterd( alibPowId, m, q, x, NULL, j[0], f, NULL );
    for ( k=1; k<n; k++ ) {
        alibOuterd( alibPowId, m, q, &x[m*k], NULL, j[k], g, NULL );
        alibMultd( m*q, m*q, f, g, f );
    }
    alibMatMuld( m, q, 1, f, p, r );
    free(i);
    free(f);
    free(g);
    return r;
}
 
/* examples for alibPolyEval in 1, 2, and 3 dimensions */
void main() {
    /* array of dimensions to use for each of the examples */
        wvlong d[3]={4,3,2};
    /* array of polynomial coefficients to use for each example */
        double p[24]={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,2,3,4,5,6,7,8,9};
    /* array of evaluation points to use for each example */
        double x[16]={-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17};
    /* output array to use for each example */
        double *v;
        v = (double*)malloc(4*sizeof(double));
    printf( "\n\n show the coefficients for p = 2 + 3x + 4x^2 + 5x^3" );
        alibinit( NULL, NULL, NULL, NULL );
        alibPrintArrayd( 1, 1, 1, d[0], p, NULL );
    printf( "\n\n show the three evaluation points x" );
        alibPrintArrayd( 1, 1, 1, 3, x, NULL );
    printf( "\n\n evaluate univariate polynomial p at the three points x" );
        v = alibPolyEvald( 1, d, p, 3, x );
        alibPrintArrayd( 1, 1, 1, 3, v, NULL );
    printf( "\n\n show coefficients for p =        2 +  3y +  4y^2   \n" );
    printf( "                             + x   (  5 +  6y +  7y^2 ) \n" );
    printf( "                             + x^2 (  8 +  9y + 10y^2 ) \n" );
    printf( "                             + x^3 ( 11 + 12y + 13y^2 )   " );
        alibPrintArrayd( 1, 1, d[0], d[1], p, NULL );
    printf( "\n\n show the four evaluation points x" );
        alibPrintArrayd( 1, 1, 2, 4, x, NULL );
    printf( "\n\n evaluate bivariate polynomial p at the four points x" );
        v = alibPolyEvald( 2, d, p, 4, x );
        alibPrintArrayd( 1, 1, 1, 4, v, NULL );
    printf( "\n\n show coefficients for trivariate polynomial p" );
        alibPrintArrayd( 1, d[0], d[1], d[2], p, NULL );
    printf( "\n\n show the two evaluation points x" );
        alibPrintArrayd( 1, 1, 3, 2, x, NULL );
    printf( "\n\n evaluate trivariate polynomial p at the two points x" );
        v = alibPolyEvald( 3, d, p, 2, x );
        alibPrintArrayd( 1, 1, 1, 2, v, NULL );
}
 
Output:
 
 show the coefficients for p = 2 + 3x + 4x^2 + 5x^3
 
 
   2.0000000e+00   3.0000000e+00   4.0000000e+00   5.0000000e+00
 
 
 show the three evaluation points x
 
 
  -2.0000000e+00  -3.0000000e+00  -4.0000000e+00
 
 
 evaluate univariate polynomial p at the three points x
 
 
  -2.8000000e+01  -1.0600000e+02  -2.6600000e+02
 
 
 show coefficients for p =        2 +  3y +  4y^2   
                             + x   (  5 +  6y +  7y^2 ) 
                             + x^2 (  8 +  9y + 10y^2 ) 
                             + x^3 ( 11 + 12y + 13y^2 )   
 
 
   2.0000000e+00   3.0000000e+00   4.0000000e+00
   5.0000000e+00   6.0000000e+00   7.0000000e+00
   8.0000000e+00   9.0000000e+00   1.0000000e+01
   1.1000000e+01   1.2000000e+01   1.3000000e+01
 
 
 show the four evaluation points x
 
 
  -2.0000000e+00  -3.0000000e+00  -4.0000000e+00  -5.0000000e+00
  -6.0000000e+00  -7.0000000e+00  -8.0000000e+00  -9.0000000e+00
 
 
 evaluate bivariate polynomial p at the four points x
 
 
  -2.3140000e+03  -1.2054000e+04  -3.9978000e+04  -1.0336600e+05
 
 
 show coefficients for trivariate polynomial p
 
 
   2.0000000e+00   3.0000000e+00
   4.0000000e+00   5.0000000e+00
   6.0000000e+00   7.0000000e+00
 
   8.0000000e+00   9.0000000e+00
   1.0000000e+01   1.1000000e+01
   1.2000000e+01   1.3000000e+01
 
   1.4000000e+01   1.5000000e+01
   1.6000000e+01   1.7000000e+01
   2.0000000e+00   3.0000000e+00
 
   4.0000000e+00   5.0000000e+00
   6.0000000e+00   7.0000000e+00
   8.0000000e+00   9.0000000e+00
 
 
 show the two evaluation points x
 
 
  -2.0000000e+00  -3.0000000e+00
  -4.0000000e+00  -5.0000000e+00
  -6.0000000e+00  -7.0000000e+00
 
 
 evaluate trivariate polynomial p at the two points x
 
 
   6.2600000e+03   3.5844000e+04
 
 
 
 
Table of Equivalents for PV-WAVE, RWalib, and MATLAB
 
PV-WAVE                 RWalib                              MATLAB
 
 
Performance Utilities
 
set_omp                 alibinit, alibset, alibParallel
systime                 alibtime, alibtick                  now
 
 
Printing Utilities
 
pm                      alibPrintArray                      disp, format
print                   alibPrintArray                      disp, format
 
 
Array Initialization
 
*arr                    alibRep                             zeros
replicate               alibRep                             repmat, zeros, ones
*indgen                 alibIndex                           :, linspace
make_array              alibRep, alibIndex                  :, linspace, repmat
linspace                alibIndex                           :, linspace
=                       alibCopy                            =
reverse                 alibFlip, alibFlipDim               flip
 
 
Type Conversion
 
byte                    alibByte                            uint8
fix                     alibShort                           int16
int32                   alibInt                             int32
long                    alibLong                            int64
float                   alibFloat                           single, real
double                  alibDouble                          double, real
complex                 cmplxflt, alibComplex               complex
dcomplex                cmplxdbl, alibComplex               complex
imaginary               alibImaginary                       imag
 
 
Array Indexing
 
get contiguous array    alibGetRange                        get contiguous array
get general array       alibGetSubset
get general set         alibGetTrace
put contiguous array    alibPutRange, alibPutRangeS         put contiguous array
put general array       alibPutSubset, alibPutSubsetS
put general set         alibPutTrace, alibPutTraceS
index_conv              alibIndex1dToNd                     ind2sub
index_conv              alibIndexNdTo1d, alibGetOffsets     sub2ind
 
 
Searches and Intersections
 
wherefirst              alibCount, alibFind                 find
where                   alibCount, alibFind                 find
wherein                 alibIntersectS, alibIntersect       intersect, setdiff
whereinvec              alibIntersect                       intersect, setdiff
 
 
Concatenation and Tiling
 
[]                      alibConcat, alibInterleave          cat, horzcat
[]                      alibConcatR                         cat, vertcat
rebin(,/sample)         alibRepArray                        repmat
expand                  alibRepArray                        repmat
replv                   alibRepArray                        repmat
cprod                   alibCartProd                        ndgrid
 
 
Logical Operations
 
not                     alibNot                             bitcmp
and                     alibAnd                             bitand
or                      alibOr                              bitor
xor                     alibXor                             bitxor
 
 
Relational Operations
 
eq                      alibEq                              ==
ne                      alibNe, alibFirstDiff               ~=
le                      alibLe                              <=
ge                      alibGe                              >=
lt                      alibLt                              <
gt                      alibGt                              >
<                       alibLesser, alibThreshold           min
>                       alibGreater, alibThreshold          max
 
 
Rounding Operations
 
great_int               alibFloor                           floor
small_int               alibCeil                            ceil
nint                    alibRound                           round
 
 
Min and Max
 
min                     alibMinMax, alibMinMaxDim           min
max                     alibMinMax, alibMinMaxDim           max
 
 
Sort and Unique
 
sort                    alibSort                            sort
sortdim                 alibSort                            sort
sortn                   alibSort                            sortrows
unique                  alibUnique                          unique
uniqn                   alibUnique                          unique
 
 
Arithmetic Operations
 
+                       alibAdd                             +
-                       alibNeg, alibSubt                   -
*                       alibMult                            .*
/                       alibDiv                             ./
mod                     alibMod                             mod
conj                    alibConj                            conj
abs                     alibAbs                             abs
total                   alibSum, alibSumDim                 sum
sum                     alibSum, alibSumDim                 sum 
cumsum                  alibCumSum                          cumsum
product                 alibProd                            prod
 
 
Exponentiation and Logarithms
 
^                       alibPowI, alibPow                           .^
expon                   alibPowI, alibPow                           .^
sqrt                    alibSqrt                                    sqrt
exp                     alibExp                                     exp
alog                    alibLog                                     log
alog10                  alibLog10                                   log10
 
 
Broadcast Vector Over Array
 
vecoverarr_*            alibVecOverArr                              bsxfun
 
 
Hyperbolic Functions
 
cosh                    alibCosh                                    cosh
sinh                    alibSinh                                    sinh
tanh                    alibTanh                                    tanh
 
 
Trigonometric Functions
 
cos                     alibCos                                     cos
sin                     alibSin                                     sin
tan                     alibTan                                     tan
acos                    alibAcos                                    acos
asin                    alibAsin                                    asin
atan                    alibAtan, alibAtan2, alibArg                atan, atan2
 
 
Linear Algebra
 
tensor_*                alibOuter                                   
#                       alibMatMul                                  *
spmvm                   alibSpMVM                                   
transpose               alibTranspose                               .', permute
adjoint                 alibAdjoint                                 '
arraytrace              alibTrace
 

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.