Sindbad~EG File Manager
/* polynomial arithmetic */
/*
/* array-representation polynomial manipulation for Mathpert
M. Beeson
Original date 3.19.91
1.29.98 last modified
*/
#include <assert.h>
#include <math.h>
#define POLYVAL_DLL
#include "globals.h"
#include "polynoms.h"
#include "deval.h"
#include "evalpoly.h"
MEXPORT_POLYVAL term evalpoly(POLYnomial c, term a)
/* return p(a). It is not presumed that a is seminumerical although
in applications it often is. The coefficients are simplified using
polyval. */
{ int j;
term temp;
unsigned short n = ARITY(c) - 1; /* degree of c */
term p = ARG(n,c);
for(j=n-1;j>=0;j--)
{ polyval(sum(product(p,a),ARG(j,c)),&temp);
p = temp;
}
return p;
}
/*___________________________________________________________________________*/
MEXPORT_POLYVAL int devalpoly(POLYnomial c, double a, double *ans)
/* Put p(a) in *ans, returning 0 for success.
Return 1 if deval fails. Watch out for overflow.
*/
{ int j;
double p,z;
unsigned short n = ARITY(c) - 1; /* degree of c */
int exponent,exponent2;
static int maxexp;
deval(ARG(n,c),&p);
if(p == BADVAL)
return 1;
for(j=n-1;j>=0;j--)
{ deval(ARG(j,c),&z);
frexp(p,&exponent);
frexp(a,&exponent2);
if(maxexp == 0)
frexp(BADVAL,&maxexp);
if(fabs(exponent + exponent2) > maxexp)
return 1;
p = p*a+z;
if(fabs(p) > BADVAL)
return 1;
}
*ans = p;
return 0;
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists