📄 utils.c
字号:
/** Sources for utilities. * functions for vectors, window functions, ... * (c) if not stated otherwise: Daniel Potts, Stefan Kunis */#include "utils.h"#include <stdlib.h>/** Actual used CPU time in seconds. * Calls getrusage, limited accuracy */double second(){ static struct rusage temp; double foo, foo1; getrusage(RUSAGE_SELF,&temp); foo = temp.ru_utime.tv_sec; /* seconds */ foo1 = temp.ru_utime.tv_usec; /* uSecs */ return foo + (foo1/1000000.0); /* milliseconds */}/** Computes /f$n\ge N/f$ such that /f$n=2^j,\, j\in\mathhb{N}_0/f$. */int next_power_of_2(int N){ int n,i,logn; int N_is_not_power_of_2=0; n=N; logn=0; while(n!=1) { if(n%2==1) N_is_not_power_of_2=1; n=n/2; logn++; } if(!N_is_not_power_of_2) logn--; for(i=0;i<=logn;i++) n=n*2; return n;} /** Computes integer /f$\prod_{t=0}^{d-1} v_t/f$. */int nfft_prod_int(int *vec, int d){ int t, prod; prod=1; for(t=0; t<d; t++) prod *= vec[t]; return prod;}/** Computes /f$\sum_{t=0}^{d-1} i_t \prod_{t'=t+1}^{d-1} N_{t'}/f$. */int nfft_plain_loop(int *idx,int *N,int d){ int t,sum; sum=idx[0]; for(t=1; t<d; t++) sum=sum*N[t]+idx[t]; return sum;}/** Computes double /f$\prod_{t=0}^{d-1} v_t/f$. */double nfft_prod_real(double *vec,int d){ int t; double prod; prod=1.0; for(t=0; t<d; t++) prod*=vec[t]; return prod;}/** Sinus cardinalis * \f$\frac{sin\left(x\right)}{x}$ */double sinc(double x){ if (fabs(x) < 1e-20) return(1.0); else return((double)(sin(x)/x));} /* sinc */void bspline_help(int k, double x, double *scratch, int j, int ug, int og, int r){ int i; /**< row index of the de Boor scheme */ int index; /**< index in scratch */ double a; /**< alpha of the de Boor scheme */ /* computation of one column */ for(i=og+r-k+1, index=og; index>=ug; i--, index--) { a = ((double)(x - i)) / ((double)(k - j)); scratch[index] = (1 - a) * scratch[index-1] + a * scratch[index]; }} /* bspline_help *//** Computes \f$M_{k,0}\left(x\right)\f$ * scratch is used for de Boor's scheme */double bspline(int k, double x, double *scratch){ double result_value; /**< M_{k,0}\left(x\right) */ int r; /**< \f$x \in {\rm supp}(M_{0,r}) \f$ */ int g1,g2; /**< boundaries */ int j,index,ug,og; /**< indices */ double a; /**< alpha of the de Boor scheme */ result_value=0.0; if(0<x && x<k) { /* using symmetry around k/2 */ if((k-x)<x) x=k-x; r=(int)(ceil(x)-1.0); for(index=0; index<k; index++) scratch[index]=0.0; scratch[k-r-1]=1.0; /* bounds of the algorithm */ g1 = r; g2 = k - 1 - r; ug = g2; /* g1<=g2 holds */ for(j=1, og=g2+1; j<=g1; j++, og++) { a = (x - r + k - 1 - og)/(k - j); scratch[og] = (1 - a) * scratch[og-1]; bspline_help(k,x,scratch,j,ug+1,og-1,r); a = (x - r + k - 1 - ug)/(k - j); scratch[ug] = a * scratch[ug]; } for(og-- ; j<=g2; j++) { bspline_help(k,x,scratch,j,ug+1,og,r); a = (x - r + k - 1 - ug)/(k - j); scratch[ug] = a * scratch[ug]; } for( ; j<k; j++) { ug++; bspline_help(k,x,scratch,j,ug,og,r); } result_value = scratch[k-1]; } return(result_value);} /* bspline *//* mconf.h * * Common include file for math routines * * * * SYNOPSIS: * * #include "mconf.h" * * * * DESCRIPTION: * * This file contains definitions for error codes that are * passed to the common error handling routine mtherr() * (which see). * * The file also includes a conditional assembly definition * for the type of computer arithmetic (IEEE, DEC, Motorola * IEEE, or UNKnown). * * For Digital Equipment PDP-11 and VAX computers, certain * IBM systems, and others that use numbers with a 56-bit * significand, the symbol DEC should be defined. In this * mode, most floating point constants are given as arrays * of octal integers to eliminate decimal to binary conversion * errors that might be introduced by the compiler. * * For little-endian computers, such as IBM PC, that follow the * IEEE Standard for Binary Floating Point Arithmetic (ANSI/IEEE * Std 754-1985), the symbol IBMPC should be defined. These * numbers have 53-bit significands. In this mode, constants * are provided as arrays of hexadecimal 16 bit integers. * * Big-endian IEEE format is denoted MIEEE. On some RISC * systems such as Sun SPARC, double precision constants * must be stored on 8-byte address boundaries. Since integer * arrays may be aligned differently, the MIEEE configuration * may fail on such machines. * * To accommodate other types of computer arithmetic, all * constants are also provided in a normal decimal radix * which one can hope are correctly converted to a suitable * format by the available C language compiler. To invoke * this mode, define the symbol UNK. * * An important difference among these modes is a predefined * set of machine arithmetic constants for each. The numbers * MACHEP (the machine roundoff error), MAXNUM (largest number * represented), and several other parameters are preset by * the configuration symbol. Check the file const.c to * ensure that these values are correct for your computer. * * Configurations NANS, INFINITIES, MINUSZERO, and DENORMAL * may fail on many systems. Verify that they are supposed * to work on your computer. *//*Cephes Math Library Release 2.3: June, 1995Copyright 1984, 1987, 1989, 1995 by Stephen L. Moshier*//* Define if the `long double' type works. */#define HAVE_LONG_DOUBLE 1/* Define as the return type of signal handlers (int or void). */#define RETSIGTYPE void/* Define if you have the ANSI C header files. */#define STDC_HEADERS 1/* Define if your processor stores words with the most significant byte first (like Motorola and SPARC, unlike Intel and VAX). *//* #undef WORDS_BIGENDIAN *//* Define if floating point words are bigendian. *//* #undef FLOAT_WORDS_BIGENDIAN *//* The number of bytes in a int. */#define SIZEOF_INT 4/* Define if you have the <string.h> header file. */#define HAVE_STRING_H 1/* Name of package */#define PACKAGE "cephes"/* Version number of package */#define VERSION "2.7"/* Constant definitions for math error conditions */#define DOMAIN 1 /* argument domain error */#define SING 2 /* argument singularity */#define OVERFLOW 3 /* overflow range error */#define UNDERFLOW 4 /* underflow range error */#define TLOSS 5 /* total loss of precision */#define PLOSS 6 /* partial loss of precision */#define EDOM 33#define ERANGE 34/* Complex numeral. */typedef struct { double r; double i; } cmplx;#ifdef HAVE_LONG_DOUBLE/* Long double complex numeral. */typedef struct { long double r; long double i; } cmplxl;#endif/* Type of computer arithmetic *//* PDP-11, Pro350, VAX: *//* #define DEC 1 *//* Intel IEEE, low order words come first: *//* #define IBMPC 1 *//* Motorola IEEE, high order words come first * (Sun 680x0 workstation): *//* #define MIEEE 1 *//* UNKnown arithmetic, invokes coefficients given in * normal decimal format. Beware of range boundary * problems (MACHEP, MAXLOG, etc. in const.c) and * roundoff problems in pow.c: * (Sun SPARCstation) */#define UNK 1/* If you define UNK, then be sure to set BIGENDIAN properly. */#ifdef FLOAT_WORDS_BIGENDIAN#define BIGENDIAN 1#else#define BIGENDIAN 0#endif/* Define this `volatile' if your compiler thinks * that floating point arithmetic obeys the associative * and distributive laws. It will defeat some optimizations * (but probably not enough of them). * * #define VOLATILE volatile */#define VOLATILE/* For 12-byte long doubles on an i386, pad a 16-bit short 0 * to the end of real constants initialized by integer arrays. * * #define XPD 0, * * Otherwise, the type is 10 bytes long and XPD should be * defined blank (e.g., Microsoft C). * * #define XPD */#define XPD 0,/* Define to support tiny denormal numbers, else undefine. */#define DENORMAL 1/* Define to ask for infinity support, else undefine. */#define INFINITIES 1/* Define to ask for support of numbers that are Not-a-Number, else undefine. This may automatically define INFINITIES in some files. */#define NANS 1/* Define to distinguish between -0.0 and +0.0. */#define MINUSZERO 1/* Define 1 for ANSI C atan2() function See atan.c and clog.c. */#define ANSIC 1/* Get ANSI function prototypes, if you want them. */#if 1/* #ifdef __STDC__ */#define ANSIPROT 1int mtherr ( char *, int );#elseint mtherr();#endif/* Variable for error reporting. See mtherr.c. */extern int merror;/* chbevl.c * * Evaluate Chebyshev series * * * * SYNOPSIS: * * int N; * double x, y, coef[N], chebevl(); * * y = chbevl( x, coef, N ); * * * * DESCRIPTION: * * Evaluates the series * * N-1 * - ' * y = > coef[i] T (x/2) * - i * i=0 * * of Chebyshev polynomials Ti at argument x/2. * * Coefficients are stored in reverse order, i.e. the zero * order term is last in the array. Note N is the number of * coefficients, not the order. * * If coefficients are for the interval a to b, x must * have been transformed to x -> 2(2x - b - a)/(b-a) before * entering the routine. This maps x from (a, b) to (-1, 1), * over which the Chebyshev polynomials are defined. * * If the coefficients are for the inverted interval, in * which (a, b) is mapped to (1/b, 1/a), the transformation * required is x -> 2(2ab/x - b - a)/(b-a). If b is infinity, * this becomes x -> 4a/x - 1. * * * * SPEED: * * Taking advantage of the recurrence properties of the * Chebyshev polynomials, the routine requires one more * addition per loop than evaluating a nested polynomial of * the same degree. * *//* chbevl.c *//*Cephes Math Library Release 2.0: April, 1987Copyright 1985, 1987 by Stephen L. MoshierDirect inquiries to 30 Frost Street, Cambridge, MA 02140*/double chbevl(double x, double array[], int n){ double b0, b1, b2, *p; int i; p = array; b0 = *p++; b1 = 0.0; i = n - 1; do { b2 = b1; b1 = b0; b0 = x * b1 - b2 + *p++; } while( --i ); return( 0.5*(b0-b2) );}/* i0.c * * Modified Bessel function of order zero * * * * SYNOPSIS: * * double x, y, i0(); * * y = i0( x ); * * * * DESCRIPTION: * * Returns modified Bessel function of order zero of the * argument. * * The function is defined as i0(x) = j0( ix ). * * The range is partitioned into the two intervals [0,8] and * (8, infinity). Chebyshev polynomial expansions are employed * in each interval. * * * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * DEC 0,30 6000 8.2e-17 1.9e-17 * IEEE 0,30 30000 5.8e-16 1.4e-16 * *//*Cephes Math Library Release 2.8: June, 2000Copyright 1984, 1987, 2000 by Stephen L. Moshier*//* Chebyshev coefficients for exp(-x) I0(x) * in the interval [0,8]. * * lim(x->0){ exp(-x) I0(x) } = 1. */#ifdef UNKstatic double A[] ={-4.41534164647933937950E-18, 3.33079451882223809783E-17,-2.43127984654795469359E-16, 1.71539128555513303061E-15,-1.16853328779934516808E-14, 7.67618549860493561688E-14,-4.85644678311192946090E-13, 2.95505266312963983461E-12,-1.72682629144155570723E-11, 9.67580903537323691224E-11,-5.18979560163526290666E-10, 2.65982372468238665035E-9,-1.30002500998624804212E-8, 6.04699502254191894932E-8,-2.67079385394061173391E-7, 1.11738753912010371815E-6,-4.41673835845875056359E-6, 1.64484480707288970893E-5,-5.75419501008210370398E-5, 1.88502885095841655729E-4,-5.76375574538582365885E-4, 1.63947561694133579842E-3,-4.32430999505057594430E-3, 1.05464603945949983183E-2,-2.37374148058994688156E-2, 4.93052842396707084878E-2,-9.49010970480476444210E-2, 1.71620901522208775349E-1,-3.04682672343198398683E-1, 6.76795274409476084995E-1};#endif#ifdef DECstatic unsigned short A[] = {0121642,0162671,0004646,0103567,0022431,0115424,0135755,0026104,0123214,0023533,0110365,0156635,0023767,0033304,0117662,0172716,0124522,0100426,0012277,0157531,0025254,0155062,0054461,0030465,0126010,0131143,0013560,0153604,0026517,0170577,0006336,0114437,0127227,0162253,0152243,0052734,0027724,0142766,0061641,0160200,0130416,0123760,0116564,0125262,0031066,0144035,0021246,0054641,0131537,0053664,0060131,0102530,0032201,0155664,0165153,0020652,0132617,0061434,0074423,0176145,0033225,0174444,0136147,0122542,0133624,0031576,0056453,0020470,0034211,0175305,0172321,0041314,0134561,0054462,0147040,0165315,0035105,0124333,0120203,0162532,0135427,0013750,0174257,0055221,0035726,0161654,0050220,0100162,0136215,0131361,0000325,0041110,0036454,0145417,0117357,0017352,0136702,0072367,0104415,0133574,0037111,0172126,0072505,0014544,0137302,0055601,0120550,0033523,0037457,0136543,0136544,0043002,0137633,0177536,0001276,0066150,0040055,0041164,0100655,0010521};#endif#ifdef IBMPCstatic unsigned short A[] = {0xd0ef,0x2134,0x5cb7,0xbc54,0xa589,0x977d,0x3362,0x3c83,0xbbb4,0x721e,0x84eb,0xbcb1,0x5eba,0x93f6,0xe6d8,0x3cde,0xfbeb,0xc297,0x5022,0xbd0a,0x2627,0x4b26,0x9b46,0x3d35,0x1af0,0x62ee,0x164c,0xbd61,0xd324,0xe19b,0xfe2f,0x3d89,0x6abc,0x7a94,0xfc95,0xbdb2,0x3c10,0xcc74,0x98be,0x3dda,0x9556,0x13ae,0xd4fe,0xbe01,0xcb34,0xa454,0xd903,0x3e26,0x30ab,0x8c0b,0xeaf6,0xbe4b,0x6435,0x9d4d,0x3b76,0x3e70,0x7f8d,0x8f22,0xec63,0xbe91,0xf4ac,0x978c,0xbf24,0x3eb2,0x6427,0xcba5,0x866f,0xbed2,0x2859,0xbe9a,0x3f58,0x3ef1,0x1d5a,0x59c4,0x2b26,0xbf0e,0x7cab,0x7410,0xb51b,0x3f28,0xeb52,0x1f15,0xe2fd,0xbf42,0x100e,0x8a12,0xdc75,0x3f5a,0xa849,0x201a,0xb65e,0xbf71,0xe3dd,0xf3dd,0x9961,0x3f85,0xb6f0,0xf121,0x4e9e,0xbf98,0xa32d,0xcea8,0x3e8a,0x3fa9,0x06ea,0x342d,0x4b70,0xbfb8,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -