From oleg@nrcbsa.bio.nrc.ca Thu May 23 08:51:21 1991 Return-Path: Received: from nrcnet0.nrc.ca by CS.UTK.EDU with SMTP (5.61++/2.5.1s-UTK) id AA25974; Thu, 23 May 91 08:51:05 -0400 Message-Id: <9105231250.AA28661@ nrcbsa.bio.nrc.ca> Date: Thu, 23 May 91 08:50:38 EDT From: Dr. Oleg Keselyov To: dongarra@cs.utk.edu Subject: serv.shar Status: RO #--------------------------------CUT HERE------------------------------------- #! /bin/sh # # This is a shell archive. Save this into a file, edit it # and delete all lines above this comment. Then give this # file to sh by executing the command 'sh file'. The files # will be extracted into the current directory owned by # you with default permissions. # This arcive contains h-files and a set of service functions that provide # a friendly environment, in particular, for Numerical # programming in C. These files are needed to compile the # programs in packages # brent.shar vector.shar fft.shar # Verification programs and data are included as well. # Contents of the arcive # # H-file needed for compilation. They are special files not to be confused # with the general purpose files, say, /usr/include/math.h # They are 'included' as "math.h" rather than . # math.h Contains machine-specific parameters (EPSILON) # as well as declaration of all the my functions. # assert.h Declaration of assure and assert macros # stdio.h Constants and functions used by the test drivers # # Set of the service math and general purpose functions # servio.c Only _error() function is used in this package # servmath.c Especially powi functions # epsilon.c Obtain the machine epsilon # # vservmath.c Validate service mathematical functions # vservmath.dat # epsilon.dat echo 'x - math.h' sed 's/^X//' << '________This_Is_The_END________' > math.h X/* Revision history X * EPSILON, SQRT_EPSILON added 28-apr-1990 X * zeroin function added 4-may-1990 X * fmin, fmax, X * iabs, imin, imax, X * powi functions added 8-may-1990 X * nrandom added 10-may-1990 X * fminbr added 11-may-1990 X * alis added 18-jun-1990 X * PI added 18-jun-1990 X * minvhol added 10-sep-1990 X * hjmin added 15-oct-1990 X */ X X#pragma once X X#include /* Include the standard math.h file */ X X#if CC$gfloat X# define HUGE_VAL 8.988465674311578540726371186585e+307 X#else X# define EPSILON 2.22045e-16 X# define SQRT_EPSILON 1.49012e-08 X# define PI 3.14159265358979300 X#endif X /* Service mathematical functions */ Xint iabs(int); /* Absolute value of a integer */ Xint imax(int,int); /* Maximum of two integers */ Xint imin(int,int); /* Minimum of two integers */ X Xdouble fmax(double,double); /* Maximum of two doubles */ Xdouble fmin(double,double); /* Minimum of two doubles */ X Xdouble powi(double r,int exp); /* r up exp */ X Xdouble nrandom(void); /* Random number distributed as N(0,1) */ X X /* Obtain a zero of function f */ X /* over the range [ax,bx] with the */ X /* accuracy tol. tol may be specified as 0.0*/ Xdouble zeroin(double ax, double bx, double (*f)(), double tol); X X /* Find a minimum of function f */ X /* over the range [a,b] with the */ X /* accuracy tol > 0. */ X /* Returns an approx. to the min location*/ Xdouble fminbr(double a, double b, double (*f)(), double tol); X X#ifdef V$elem_type X /* Aitken-Lagrange interpolation to the */ X /* point x over the table with even mesh*/ X /* x[i] = x0 + s*(i-Lwb(y)), */ X /* i = Lwb(y)..Upb(y) */ Xextern double alis(double q, double x0, double s, VECTOR y); Xextern double alin(double q, VECTOR x, VECTOR y); X X /* Inverse a positive definite symmetric*/ X /* matrix, return condition number */ Xextern double minvhol(float *A,const int N); X X /* Find the local minimum of a given */ X /* function by the Hook-Jeevse method */ Xdouble hjmin( /* Return the function value at min */ X VECTOR b, /* On input - initial guess to min loc*/ X /* On output - loc for the min found */ X const VECTOR h0, /* Initial values for the steps along */ X /* each directions */ X double (*f)(const VECTOR x) /* Procedure to compute a function */ X /* value at the specified point */ X ); X#endif ________This_Is_The_END________ if test `wc -l < math.h` -ne 71; then echo 'shar: math.h was damaged during transit (should have had 71 lines)' fi echo 'x - assert.h' sed 's/^X//' << '________This_Is_The_END________' > assert.h X/* ASSERT - V3.0 */ X X#include /* Include the standard assert */ Xextern void _error( const char * message,... ); /* Print a message and abort*/ X# define assure(expr,message) \ X if (expr) ; \ X else _error(message); ________This_Is_The_END________ if test `wc -l < assert.h` -ne 7; then echo 'shar: assert.h was damaged during transit (should have had 7 lines)' fi echo 'x - stdio.h' sed 's/^X//' << '________This_Is_The_END________' > stdio.h X/* X ************************************************************************ X * X * Standard input/output X * Service functions X */ X X#include X X /* Strings of symbols */ X /* They may be used as a delimiting lines*/ Xextern const char _Minuses []; Xextern const char _Asteriscs []; Xextern const char _Equals []; X X X /* Safe file open function */ X /* Null pointer is never returned, so */ X /* the user may be safe */ XFILE * fopenc( X const char * name, /* File name */ X const char * mode /* Open mode */ X ); X X X /* Print an error message at stderr and */ X /* abort */ Xvoid _error( X const char * message, /* Message to be printed */ X ... /* Additional args to printf */ X ); X X /* Print a message at stderr */ Xvoid message( X const char * text, /* Message to be printed */ X ... /* Additional args to printf */ X ); X X /* PASCAL-like reading functions */ Xvoid nl( /* Skip all characters until */ X const FILE *fp /* the end-of-line */ X ); X X /* Format read of the input stream */ Xvoid readf( X const FILE * fp, /* File pointer */ X const char * fstring, /* Format string as in scanf */ X ...); /* Other args - ptr to vars to */ X /* be read */ X X X /* Same as above excepting i/o pointer */ X /* being moved to the beginning of the */ X /* next record */ Xvoid readln( X const FILE * fp, /* File pointer */ X const char * fstring, /* Format string as in scanf */ X ...); /* Other args - ptr to vars to */ X /* be read */ X ________This_Is_The_END________ if test `wc -l < stdio.h` -ne 60; then echo 'shar: stdio.h was damaged during transit (should have had 60 lines)' fi echo 'x - servio.c' sed 's/^X//' << '________This_Is_The_END________' > servio.c X/* X ************************************************************************ X * Service C functions X * concerning the input/output facilities X */ X X#include "stdio.h" X#include "assert.h" X#include X#include X#include X X/* X *----------------------------------------------------------------------- X * Some global constant pertaining to input/output X */ X Xconst char _Minuses [] = "\ X-------------------------------------------------------------------------------"; X Xconst char _Asteriscs [] = "\ X*******************************************************************************"; X Xconst char _Equals [] = "\ X==============================================================================="; X X X X/* X *------------------------------------------------------------------------ X * Safe file open function X * Synopsis X * FILE *fp = fopenc(const char * file_name, const char * mode) X * X * Opens the file specified like 'fopen' and returns ptr to the FILE X * structure corresponding to the file. X * On open failure an error message is typed and program is aborted. X * Null pointer is never returned, so the user may be safe using fp in X * his own. X */ X XFILE * fopenc(name,mode) Xconst char * name; /* File name */ Xconst char * mode; /* Open mode */ X{ X register FILE * fp; X if( (fp = fopen(name,mode)) == NULL ) X { X fprintf(stderr,"\n\nCan't open a file '%s'\n",name); X perror(""); X exit(2); X } X return fp; X} X X X/* X *------------------------------------------------------------------------ X * Open the level II file X * with checking of successfullness X * and explicit specified file name extension X */ X XFILE * fopencex(name,ext,mode) Xconst char * name; /* File name */ Xconst char * ext; /* File name extension */ Xconst char * mode; /* Open mode */ X{ X char fname[65]; /* The full file name */ X register int i; X X /* Copy the name of file X strncpy(fname,name,sizeof(fname)); X for(i=0; i 0 && len < 25*80*2 ); X p = malloc(len+1); /* Plus null char at end of str */ X assert( fread(p,len,1,fp) == 1 ); /* Read string symbols */ X p[len] = '\0'; /* Append the null char */ X return p; X} X X/* X *------------------------------------------------------------------------ X * Print an error message at stderr and abort X * Synopsis X * void _error(const char * message,... ); X * Message may contain format control sequences %x. Additional X * arguments for them a passed next to the message. X */ X X Xvoid _error( X const char * message, X ... X) X{ X va_list args; X va_start(args,message); /* Init 'args' to the beginning of */ X /* the variable length list of args*/ X fprintf(stderr,"\n_error:\n"); X vfprintf(stderr,message,args); X fputs("\n",stderr); X abort(); X} X X X/* X *------------------------------------------------------------------------ X * Print a message at stderr X * Synopsis X * void message(const char * text,... ); X * Message may contain format control sequences %x. Additional X * arguments for them a passed next to the message. X */ X Xvoid message( X const char * text, X ... X) X{ X va_list args; X va_start(args,text); /* Init 'args' to the beginning of */ X /* the variable length list of args*/ X vfprintf(stderr,text,args); X} ________This_Is_The_END________ if test `wc -l < servio.c` -ne 258; then echo 'shar: servio.c was damaged during transit (should have had 258 lines)' fi echo 'x - servmath.c' sed 's/^X//' << '________This_Is_The_END________' > servmath.c X/* X ************************************************************************ X * Service C functions X * concerning the mathematical computations X */ X X#include "math.h" X Xdouble fmin(x1,x2) /* Minimum of two doubles */ Xconst double x1,x2; X{ X if( x1 > x2 ) X return x2; X else X return x1; X} X X Xdouble fmax(x1,x2) /* Maximum of two doubles */ Xconst double x1,x2; X{ X if( x1 > x2 ) X return x1; X else X return x2; X} X Xint iabs(x) /* Absolute value of a integer */ Xint x; X{ X if( x < 0 ) X x = -x; X return x; X} X Xint imin(x1,x2) /* Minimum of two integers */ Xconst int x1,x2; X{ X if( x1 > x2 ) X return x2; X else X return x1; X} X X Xint imax(x1,x2) /* Maximum of two integers */ Xconst int x1,x2; X{ X if( x1 > x2 ) X return x1; X else X return x2; X} X X Xdouble powi(x,e) /* x up integer power */ Xconst double x; Xconst int e; X{ X register int i; X register double r; X X switch(e) X { X case 0: X return 1.0; X X case 2: X return x*x; X X case 3: X return x*x*x; X X case -1: X return 1/x; X X case -2: X return 1/x/x; X } X X { X register double xn = x; X register int en = e; X if( e < 0 ) X { X xn = 1/x; X en = -e; X } X X for(r=xn,i=1; 2*i<=en; i*=2) X r *= r; X for(; i X Xdouble nrandom() /* Random number NORMAL distributed as N(0,1) */ X{ X register int i; X register double accum = 0; X X for(i=0; i<12; i++) X accum += rand(); X X return accum/(double)RAND_MAX - 6.; X} ________This_Is_The_END________ if test `wc -l < servmath.c` -ne 112; then echo 'shar: servmath.c was damaged during transit (should have had 112 lines)' fi echo 'x - vservmath.c' sed 's/^X//' << '________This_Is_The_END________' > vservmath.c X/* Verify SERVMATH functions */ X X#include "math.h" X Xvoid timin(n1,n2) /* Test imin function */ Xint n1,n2; X{ printf("\nimin(%d,%d)=%d",n1,n2,imin(n1,n2)); } X Xvoid timax(n1,n2) /* Test imax function */ Xint n1,n2; X{ printf("\nimax(%d,%d)=%d",n1,n2,imax(n1,n2)); } X Xvoid tfmin(n1,n2) /* Test fmin function */ Xdouble n1,n2; X{ printf("\nfmin(%g,%g)=%g",n1,n2,fmin(n1,n2)); } X Xvoid tfmax(n1,n2) /* Test fmax function */ Xdouble n1,n2; X{ printf("\nfmax(%g,%g)=%g",n1,n2,fmax(n1,n2)); } X X Xvoid tiabs(n1) /* Test iabs function */ Xint n1; X{ printf("\niabs(%d)=%d",n1,iabs(n1)); } X X Xvoid tnrandom() /* Test nrandom() function */ X{ /* Making use of the fact */ X register int i; /* Chi2(n-1) = SUM[ nrandom() ], i=1,n */ X register int trial_counter; X X printf("\n\nTest the normal random number generator\n\n"); X printf("No. of freedoms\t\tChi^2\n"); X X for(trial_counter=1; trial_counter < 25; trial_counter++ ) X { X double chi2 = 0; X for(i=0; i vservmath.dat X X X Test SERVMATH functions X X Xiabs(0)=0 Xiabs(2)=2 Xiabs(-2)=2 Ximin(1,0)=0 Ximin(0,32767)=0 Ximin(-32767,0)=-32767 Ximin(4,55)=4 Ximin(-6,10)=-6 Ximax(1,0)=1 Ximax(0,32767)=32767 Ximax(-32767,0)=0 Ximax(4,55)=55 Ximax(-6,10)=10 Xfmin(1,0)=0 Xfmin(0,0)=0 Xfmin(-Infinity,0)=-Infinity Xfmin(4,5.5e+21)=4 Xfmin(-6e-05,-0.004)=-0.004 Xfmax(1,-0.004)=1 Xfmax(1,-0.004)=1 Xfmax(-Infinity,-0.004)=-0.004 Xfmax(4,5.5e+21)=5.5e+21 Xfmax(-6e-05,-0.004)=-6e-05 X XTest the normal random number generator X XNo. of freedoms Chi^2 X0 1.75892 X1 6.18685 X2 0.0880468 X3 6.95459 X4 2.3511 X5 9.1004 X6 15.0925 X7 12.8436 X8 8.80412 X9 10.6615 X10 9.68159 X11 12.4396 X12 25.9775 X13 13.9597 X14 13.9225 X15 12.5468 X16 33.985 X17 8.75921 X18 22.0895 X19 20.0303 X20 15.2557 X21 19.0718 X22 13.7847 X23 26.6989 X XEnd of test nrandom() X X Xpowi(2,-7)=0.0078125 Xpowi(2,-6)=0.015625 Xpowi(2,-5)=0.03125 Xpowi(2,-4)=0.0625 Xpowi(2,-3)=0.125 Xpowi(2,-2)=0.25 Xpowi(2,-1)=0.5 Xpowi(2,0)=1 Xpowi(2,1)=2 Xpowi(2,2)=4 Xpowi(2,3)=8 Xpowi(2,4)=16 Xpowi(2,5)=32 Xpowi(2,6)=64 Xpowi(2,7)=128 Xpowi(2,8)=256 Xpowi(2,9)=512 Xpowi(2,10)=1024 ________This_Is_The_END________ if test `wc -l < vservmath.dat` -ne 78; then echo 'shar: vservmath.dat was damaged during transit (should have had 78 lines)' fi echo 'x - epsilon.c' sed 's/^X//' << '________This_Is_The_END________' > epsilon.c X/* X * Obtain the machine EPSILON X * i.e. the smallest positive number which, been added to 1., yields X * the result other than 1. X */ X#include X Xmain() X{ X double eps; X eps = 1.0; X while( eps + 1.0 != 1.0 ) X eps /= 2.0; X eps *= 2.0; X X printf("FPU epsilon is\t %g\nsqrt(eps) is\t %g\n", X eps, sqrt(eps) ); X} ________This_Is_The_END________ if test `wc -l < epsilon.c` -ne 18; then echo 'shar: epsilon.c was damaged during transit (should have had 18 lines)' fi echo 'x - epsilon.dat' sed 's/^X//' << '________This_Is_The_END________' > epsilon.dat XFPU epsilon is 2.22045e-16 Xsqrt(eps) is 1.49012e-08 ________This_Is_The_END________ if test `wc -l < epsilon.dat` -ne 2; then echo 'shar: epsilon.dat was damaged during transit (should have had 2 lines)' fi .