/* From REF_:[DBSSHR]COMPUTEPOLY.FOR integer*4 function ComputePoly(X, COEFF, CNT, F, DF) C C++ C C FACILTY: C **MEMBER**=SLCLIBS:DBSSHRLIB C C ABSTRACT: C Routine to calculate the polynomial function f(x) and C its derivative df/dx for a given set of coefficients. C C AUTHOR: C Michael Huffer, SLAC (415) 854-3300 (x2829) C Abstracted from orignal routine in "DBPOLY" by N. Phinney. C C CREATION DATE: C June 20, 1987, C C REVISION HISTORY: C 10-Jan-2007, Ed Miller C Convert from subroutine to function and return signalled C arithmetic errors as function value. C December 7, 1995, Greg Sherwin (GREGS): C To prevent unnecessary operand faults, and to simplify the C algorithm, the guts of this procedure were replaced with a reverse- C order algorithm from "Numerical Recipies in FORTRAN". C C ARGUMENTS: C X - A pointer to a real longword which contains the C point at which the function is to be evaluated. C COEFF - A pointer to a longword real array which contains the C polynomial coefficants. C CNT - A pointer to a longword which contains the number of C coefficents in the coefficant array. C F - A pointer to a real longword which will contain the C value of the function at "X". C DF - A pointer to a real longword which will contain the C value of the derivative (df/dx) at "X". C C RETURNS: C If success, 1 C Else status converted (by UTL_SIG_TO_RET handler) from a signalled C error -- typically floating arithmetic errors. C C-- C IMPLICIT NONE C C Routines arguments... C REAL*4 X ! Function evaluation point. REAL*4 COEFF(*) ! Coefficant array. INTEGER*4 CNT ! Number of elements in array. REAL*4 F ! Returned function. REAL*4 DF ! Return derivative. C C Externals C EXTERNAL UTL_SIG_TO_RET ! convert signalled arith errs to funct return C C Local storage... C INTEGER*4 J ! Array Index. C C Signalled arithmetic errors will be converted to COMPUTEPOLY return status. C computepoly = 1 call lib$establish (UTL_SIG_TO_RET) C C Initialize C F = COEFF(CNT) DF = 0 C C Sum the terms in the polynomial and derivative. C DO J = CNT-1, 1, -1 DF = DF * X + F F = F * X + COEFF(J) ENDDO C RETURN END */ #include "computePoly.h" void computePoly(float X, float COEFF[], short CNT, float * F, float * DF) { int j; float f; float df; f = COEFF[CNT-1]; df = 0; for (j=CNT-2; j>=0; j=j-1) { df = df * X + f; f = f * X + COEFF[j]; } *F = f; *DF = df; }