void itoa( char arr[], int size, int val ) { int i, max, dig, subval, loc; for (i=0; i<size; i++ ) { arr[i] = '\0'; } for ( i=1; i<=2; i++ ) { if (( val / myexp(i) ) == 0 ) { max = i-1; break; } } subval = 0; loc = 0; for ( i=max; i>=0; i-- ) { dig = 48 + ((val-subval) / myexp(i)); subval = (dig-48) * myexp(max); arr[loc] = dig; loc++; } return; Exit(0); }
static char* test_more() { mu_assert(in_delta(myexp(0.0), expect_myexp(0.0))); mu_assert(in_delta(myexp(15.0), expect_myexp(15.0))); mu_assert(in_delta(myexp(1.1), expect_myexp(1.1))); mu_assert(in_delta(myexp(3.3), expect_myexp(3.3))); mu_assert(in_delta(myexp(6.6), expect_myexp(6.6))); return 0; }
static char* test_example() { mu_assert(in_delta(myexp(1.0), 2.71828)); mu_assert(in_delta(myexp(2.0), 7.38906)); mu_assert(in_delta(myexp(3.0), 20.08554)); mu_assert(in_delta(myexp(4.0), 54.59815)); mu_assert(in_delta(myexp(5.0), 148.41316)); mu_assert(in_delta(myexp(6.0), 403.42879)); mu_assert(in_delta(myexp(7.0), 1096.63316)); mu_assert(in_delta(myexp(8.0), 2980.95799)); mu_assert(in_delta(myexp(9.0), 8103.08393)); mu_assert(in_delta(myexp(10.0), 22026.46579)); return 0; }
///////////////////////////////////////////////////////////////////////////// // // // // // MATH // // // // // ///////////////////////////////////////////////////////////////////////////// double log_add (double x, double y) { if (x==LOG_ZERO)return y; else if (y==LOG_ZERO)return x; else if (x>=y)return x+mylog(1+exp(y-x)); else return y+mylog (1+myexp(x-y)); }
void main(void) { double x; printf(" x myexp(x) exp(x)\n"); for (x = 0; x <= 40; x = x + 10){ printf("%5.1f%14.6g%14.6g\n", x, myexp(x), exp(x)); } }
int main ( int argc, char ** argv ) { for ( long double i = -20; i <= 20; i += 5 ) { double mexp = ( double ) myexp ( i ); long double check = ( long double ) exp ( i ); long double er = ( long double ) err ( mexp, check ); printf ( "%+0.9Le %0.9e %0.9Le %0.15Le\n", i, mexp, check, er ); } return 1; }
int display_model (Hmm *H)//for debug only { FILE *fp; UNPACK_HMM(H); int a, b; for (a=0; a<ns; a++) { for (b=0; b<ns; b++) { fprintf (stdout, "ST::%d -- ST::%d --> %.5f\n", a, b, myexp(M[a][b])); } for (b=ns; b<(ns+ne); b++) { fprintf (stdout, "ST::%d -- EM::%d --> %.5f\n", a, b-ns, myexp(M[a][b])); } } return 1; }
int main() { float x = 1.0; float y1 =0.0, y2 = 0.0; for (int i = 1; i <= 30; x+=.5, i++) { y1 = myexp(x); y2 = expl(x); float rel_error = abs((y2 - y1) / y2); printf("%+0.9Le %0.9e %0.9Le %0.15Le\n", x, y1, y2, rel_error); } return 0; }
real do_lmfit(int ndata, real c1[], real sig[], real dt, real x0[], real begintimefit, real endtimefit, const output_env_t oenv, gmx_bool bVerbose, int eFitFn, real fitparms[], int fix) { FILE *fp; char buf[32]; int i, j, nparm, nfitpnts; real integral, ttt; real *parm, *dparm; real *x, *y, *dy; real ftol = 1e-4; nparm = nfp_ffn[eFitFn]; if (debug) { fprintf(debug, "There are %d points to fit %d vars!\n", ndata, nparm); fprintf(debug, "Fit to function %d from %g through %g, dt=%g\n", eFitFn, begintimefit, endtimefit, dt); } snew(x, ndata); snew(y, ndata); snew(dy, ndata); j = 0; for (i = 0; i < ndata; i++) { ttt = x0 ? x0[i] : dt*i; if (ttt >= begintimefit && ttt <= endtimefit) { x[j] = ttt; y[j] = c1[i]; /* mrqmin does not like sig to be zero */ if (sig[i] < 1.0e-7) { dy[j] = 1.0e-7; } else { dy[j] = sig[i]; } if (debug) { fprintf(debug, "j= %d, i= %d, x= %g, y= %g, dy= %g\n", j, i, x[j], y[j], dy[j]); } j++; } } nfitpnts = j; integral = 0; if (nfitpnts < nparm) { fprintf(stderr, "Not enough data points for fitting!\n"); } else { snew(parm, nparm); snew(dparm, nparm); if (fitparms) { for (i = 0; (i < nparm); i++) { parm[i] = fitparms[i]; } } if (!lmfit_exp(nfitpnts, x, y, dy, ftol, parm, dparm, bVerbose, eFitFn, fix)) { fprintf(stderr, "Fit failed!\n"); } else if (nparm <= 3) { /* Compute the integral from begintimefit */ if (nparm == 3) { integral = (parm[0]*myexp(begintimefit, parm[1], parm[0]) + parm[2]*myexp(begintimefit, 1-parm[1], parm[2])); } else if (nparm == 2) { integral = parm[0]*myexp(begintimefit, parm[1], parm[0]); } else if (nparm == 1) { integral = parm[0]*myexp(begintimefit, 1, parm[0]); } else { gmx_fatal(FARGS, "nparm = %d in file %s, line %d", nparm, __FILE__, __LINE__); } /* Generate THE output */ if (bVerbose) { fprintf(stderr, "FIT: # points used in fit is: %d\n", nfitpnts); fprintf(stderr, "FIT: %21s%21s%21s\n", "parm0 ", "parm1 (ps) ", "parm2 (ps) "); fprintf(stderr, "FIT: ------------------------------------------------------------\n"); fprintf(stderr, "FIT: %8.3g +/- %8.3g%9.4g +/- %8.3g%8.3g +/- %8.3g\n", parm[0], dparm[0], parm[1], dparm[1], parm[2], dparm[2]); fprintf(stderr, "FIT: Integral (calc with fitted function) from %g ps to inf. is: %g\n", begintimefit, integral); sprintf(buf, "test%d.xvg", nfitpnts); fp = xvgropen(buf, "C(t) + Fit to C(t)", "Time (ps)", "C(t)", oenv); fprintf(fp, "# parm0 = %g, parm1 = %g, parm2 = %g\n", parm[0], parm[1], parm[2]); for (j = 0; (j < nfitpnts); j++) { ttt = x0 ? x0[j] : dt*j; fprintf(fp, "%10.5e %10.5e %10.5e\n", ttt, c1[j], fit_function(eFitFn, parm, ttt)); } xvgrclose(fp); } } if (fitparms) { for (i = 0; (i < nparm); i++) { fitparms[i] = parm[i]; } } sfree(parm); sfree(dparm); } sfree(x); sfree(y); sfree(dy); return integral; }
real do_lmfit(int ndata, real c1[], real sig[], real dt, real *x0, real begintimefit, real endtimefit, const gmx_output_env_t *oenv, gmx_bool bVerbose, int eFitFn, double fitparms[], int fix, const char *fn_fitted) { FILE *fp; int i, j, nfitpnts; double integral, ttt; double *x, *y, *dy; if (0 != fix) { fprintf(stderr, "Using fixed parameters in curve fitting is temporarily not working.\n"); } if (debug) { fprintf(debug, "There are %d points to fit %d vars!\n", ndata, effnNparams(eFitFn)); fprintf(debug, "Fit to function %d from %g through %g, dt=%g\n", eFitFn, begintimefit, endtimefit, dt); } snew(x, ndata); snew(y, ndata); snew(dy, ndata); j = 0; for (i = 0; i < ndata; i++) { ttt = x0 ? x0[i] : dt*i; if (ttt >= begintimefit && ttt <= endtimefit) { x[j] = ttt; y[j] = c1[i]; if (NULL == sig) { // No weighting if all values are divided by 1. dy[j] = 1; } else { dy[j] = std::max(1.0e-7, (double)sig[i]); } if (debug) { fprintf(debug, "j= %d, i= %d, x= %g, y= %g, dy=%g, ttt=%g\n", j, i, x[j], y[j], dy[j], ttt); } j++; } } nfitpnts = j; integral = 0; if (nfitpnts < effnNparams(eFitFn)) { fprintf(stderr, "Not enough (%d) data points for fitting, dt = %g!\n", nfitpnts, dt); } else { gmx_bool bSuccess; if (bVerbose) { print_chi2_params(stdout, eFitFn, fitparms, "initial", nfitpnts, x, y); } initiate_fit_params(eFitFn, fitparms); bSuccess = lmfit_exp(nfitpnts, x, y, dy, fitparms, bVerbose, eFitFn, fix); extract_fit_params(eFitFn, fitparms); if (!bSuccess) { fprintf(stderr, "Fit failed!\n"); } else { if (bVerbose) { print_chi2_params(stdout, eFitFn, fitparms, "final", nfitpnts, x, y); } switch (eFitFn) { case effnEXP1: integral = fitparms[0]*myexp(begintimefit, 1, fitparms[0]); break; case effnEXP2: integral = fitparms[0]*myexp(begintimefit, fitparms[1], fitparms[0]); break; case effnEXPEXP: integral = (fitparms[0]*myexp(begintimefit, fitparms[1], fitparms[0]) + fitparms[2]*myexp(begintimefit, 1-fitparms[1], fitparms[2])); break; case effnEXP5: case effnEXP7: case effnEXP9: integral = 0; for (i = 0; (i < (effnNparams(eFitFn)-1)/2); i++) { integral += fitparms[2*i]*myexp(begintimefit, fitparms[2*i+1], fitparms[2*i]); } break; default: /* Do numerical integration */ integral = 0; for (i = 0; (i < nfitpnts-1); i++) { double y0 = lmcurves[eFitFn](x[i], fitparms); double y1 = lmcurves[eFitFn](x[i+1], fitparms); integral += (x[i+1]-x[i])*(y1+y0)*0.5; } break; } if (bVerbose) { printf("FIT: Integral of fitted function: %g\n", integral); if ((effnEXP5 == eFitFn) || (effnEXP7 == eFitFn) || (effnEXP9 == eFitFn)) { printf("FIT: Note that the constant term is not taken into account when computing integral.\n"); } } /* Generate debug output */ if (NULL != fn_fitted) { fp = xvgropen(fn_fitted, "Data + Fit", "Time (ps)", "Data (t)", oenv); for (i = 0; (i < effnNparams(eFitFn)); i++) { fprintf(fp, "# fitparms[%d] = %g\n", i, fitparms[i]); } for (j = 0; (j < nfitpnts); j++) { real ttt = x0 ? x0[j] : dt*j; fprintf(fp, "%10.5e %10.5e %10.5e\n", x[j], y[j], (lmcurves[eFitFn])(ttt, fitparms)); } xvgrclose(fp); } } } sfree(x); sfree(y); sfree(dy); return integral; }
static void do_curses(struct varnish_stats *VSL_stats, int delay, const char *fields) { struct varnish_stats copy; struct varnish_stats seen; intmax_t ju; struct timeval tv; double tt, lt, hit, miss, ratio, up; double a1, a2, a3; unsigned n1, n2, n3; time_t rt; int ch, line; memset(©, 0, sizeof copy); memset(&seen, 0, sizeof seen); a1 = a2 = a3 = 0.0; n1 = n2 = n3 = 0; initscr(); raw(); noecho(); nonl(); intrflush(stdscr, FALSE); curs_set(0); erase(); lt = 0; while (1) { gettimeofday(&tv, NULL); tt = tv.tv_usec * 1e-6 + tv.tv_sec; lt = tt - lt; rt = tv.tv_sec - VSL_stats->start_time; up = rt; mvprintw(0, 0, "%*s", COLS - 1, VSL_Name()); mvprintw(0, 0, "%d+%02d:%02d:%02d", rt / 86400, (rt % 86400) / 3600, (rt % 3600) / 60, rt % 60); hit = VSL_stats->cache_hit - copy.cache_hit; miss = VSL_stats->cache_miss - copy.cache_miss; hit /= lt; miss /= lt; if (hit + miss != 0) { ratio = hit / (hit + miss); myexp(&a1, ratio, &n1, 10); myexp(&a2, ratio, &n2, 100); myexp(&a3, ratio, &n3, 1000); } mvprintw(1, 0, "Hitrate ratio: %8u %8u %8u", n1, n2, n3); mvprintw(2, 0, "Hitrate avg: %8.4f %8.4f %8.4f", a1, a2, a3); line = 3; #define MAC_STAT(n, t, l, f, d) \ if ((fields == NULL || show_field( #n, fields )) && line < LINES) { \ ju = VSL_stats->n; \ if (ju == 0 && !seen.n) { \ } else if (f == 'a') { \ seen.n = 1; \ line++; \ mvprintw(line, 0, "%12ju %12.2f %12.2f %s\n", \ ju, (ju - (intmax_t)copy.n)/lt, ju / up, d); \ copy.n = ju; \ } else { \ seen.n = 1; \ line++; \ mvprintw(line, 0, "%12ju %12s %12s %s\n", \ ju, ". ", ". ", d); \ } \ } #include "stat_field.h" #undef MAC_STAT lt = tt; refresh(); timeout(delay * 1000); switch ((ch = getch())) { case ERR: break; #ifdef KEY_RESIZE case KEY_RESIZE: erase(); break; #endif case '\014': /* Ctrl-L */ case '\024': /* Ctrl-T */ redrawwin(stdscr); refresh(); break; case '\003': /* Ctrl-C */ raise(SIGINT); break; case '\032': /* Ctrl-Z */ raise(SIGTSTP); break; case '\021': /* Ctrl-Q */ case 'Q': case 'q': endwin(); exit(0); case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': delay = 1 << (ch - '0'); break; default: beep(); break; } } }
double baum_welch(Hmm*H) { int k,b, l, i; int pseudocount=0; UNPACK_HMM(H); if (isnan(H->P=backward(H)))return LOG_ZERO; if (isnan(H->P=forward (H)))return LOG_ZERO; for(k=0; k<ns; k++) for (l=0; l<(ns+ne); l++) NM[k][l]=LOG_ZERO; for (k=0; k<ns; k++) { int ll=0; while ((l=PST[k][ll++])!=-1) { for (i=1; i<L; i++) { double fo, ba, tr, em; int symbol=S[i+1]; if (M[l][symbol]==LOG_ZERO){continue;} fo=F[i][k]; ba=B[i+1][l]; tr=M[k][l]; em=M[l][symbol]; NM[k][l]=log_add(NM[k][l], log_multiply4(fo,tr,em,ba)); } NM[k][l]=log_divide (NM[k][l],H->P); } } //update emissions for (k=0; k<ns; k++) { for (b=ns; b<(ns+ne);b++) { for (i=1; i<=L; i++) { if (S[i]==b) { NM[k][b]=log_add(NM[k][b], log_multiply2(F[i][k],B[i][k])); } } NM[k][b]=log_divide(NM[k][b],H->P); } } //modelL2count+psudocounts for (k=0; k<ns; k++) for (l=0; l<(ns+ne); l++) NM[k][l]=myexp(NM[k][l])+pseudocount; //update transitions for (k=0; k<ns; k++) { double t=0,f=0; for (l=0; l<ns; l++) { if (CM[k][l]==LOG_ZERO)t+=NM[k][l]; else f+=CM[k][l]; } t/=(f==1)?1:(1-f); for (l=0; l<ns; l++) { if (CM[k][l]==LOG_ZERO)NM[k][l]/=(t<DELTA)?1:t; else NM[k][l]=CM[k][l]; } } //update emissions for (k=0; k<ns; k++) { double t=0,f=0; for (l=ns; l<(ns+ne); l++) { if (CM[k][l]==LOG_ZERO)t+=NM[k][l]; else f+=CM[k][l]; } t/=(f==1)?1:(1-f); for (l=ns; l<(ns+ne); l++) { if (CM[k][l]==LOG_ZERO)NM[k][l]/=(t<DELTA)?1:t; else NM[k][l]=CM[k][l]; } } //moldel2modelL for (k=0; k<ns; k++) { for (l=0; l<(ns+ne); l++) { M[k][l]=mylog(NM[k][l]); } } return H->P; }