📄 int_misc.c
字号:
/*..........................................................................*//* *//* L a s t W a v e K e r n e l 3 . 0 *//* *//* Copyright (C) 1998-2002 Emmanuel Bacry. *//* email : lastwave@cmap.polytechnique.fr *//* *//*..........................................................................*//* *//* This program is a free software, you can redistribute it and/or *//* modify it under the terms of the GNU General Public License as *//* published by the Free Software Foundation; either version 2 of the *//* License, or (at your option) any later version *//* *//* This program is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *//* GNU General Public License for more details. *//* *//* You should have received a copy of the GNU General Public License *//* along with this program (in a file named COPYRIGHT); *//* if not, write to the Free Software Foundation, Inc., *//* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* *//*..........................................................................*//****************************************************************************//* *//* misc.c miscellaneous Functions *//* *//****************************************************************************/#include "lastwave.h"#include <time.h>#include "xx_system.h"#include "signals.h"/* * Quick Sort * * Sorts the array of double 'x' which is of size 'n' * * WARNING : 'x' starts at index 1 (up to 'n' included) */void QuickSort(double *x,int n){ int l,j,ir,i; double xx; l = ( n>>1)+1; ir = n; for (;;) { if(l>1) xx = x[--l]; else { xx = x[ir]; x[ir] = x[1]; if( --ir ==1) { x[1]=xx; return; } } i=l; j=l<<1; while( j<= ir) { if( j<ir && x[j]< x[j+1]) ++j; if(xx < x[j]) { x[i] = x[j]; j+=(i=j); } else j=ir+1; } x[i] = xx; }}/* * Command to call a unix command */ void C_SystemUnix(char **argv){ char *cmd; cmd = List2Str(argv," "); TempStr(cmd); system("stty iexten"); system("stty ixon"); system("stty ixoff"); system("stty echonl"); system("stty -noflsh"); system("stty echo"); system("stty icanon"); system(cmd); system("stty -icanon min 1"); system("stty -echo"); system("stty -iexten"); system("stty -ixon"); system("stty -ixoff"); system("stty -echonl"); system("stty noflsh"); }/* MyRandom Uniform number */ # define MBIG 1000000000# define MSEED 161803398# define MZ 0# define FAC (1.0/MBIG)static int iff=0;static long int idum = -1;void RandInit(long int idum1){ iff = 0; idum = idum1;}void C_RandInit(char **argv){ int i; ParseArgv(argv,tINT_,-1,&i,0); RandInit(i);}LWFLOAT Urand(void){ static int inext,inextp; static long ma[56]; register long mj,mk; register int i,ii,k; if ( idum <0 || iff == 0){ iff = 1; if (idum < 0) idum = -time(NULL); mj = MSEED - ( idum < 0 ? -idum : idum); mj %= MBIG; ma[55] = mj; mk = 1; for ( i=1; i<=54; i++){ ii = ( 21*i) % 55; ma[ii] = mk; mk = mj - mk; if ( mk < MZ ) mk += MBIG; mj = ma[ii]; } for ( k=1; k<=4; k++) for (i=1; i<=55; i++){ ma[i] -= ma[1 + (i+30) % 55]; if ( ma[i] < MZ ) ma[i] += MBIG; } inext = 0; inextp = 31; idum = 1; } if( ++inext == 56 )inext=1; if( ++inextp == 56 )inextp=1; mj = ma[inext] - ma[inextp]; if( mj < MZ) mj += MBIG; ma[inext] = mj; return mj*FAC;}/* MyRandom Gaussian number */ LWFLOAT Grand(LWFLOAT sigma){ static int iset=0; static LWFLOAT gset; LWFLOAT fac,rsq,v1,v2; if (iset == 0) { do { v1 = 2.0*Urand()-1.0; v2 = 2.0*Urand()-1.0; rsq=v1*v1+v2*v2; } while (rsq >= 1.0 || rsq == 0.0); fac=sqrt(-2.0*log(rsq)/rsq); gset = v1*fac; iset=1; return(v2*fac*sigma); } else { iset = 0; return(sigma*gset); }}LWFLOAT Prand(LWFLOAT xm){ static LWFLOAT sq,alxm,g,oldm=(-1.0); LWFLOAT em,t,y; long int intem,idum=1; if (xm< 12.0) { if (xm != oldm){ oldm = xm; g = exp(-xm); } em = -1; t=1.0; do { ++em; t *= Urand(); } while(t>g); } else { if (xm != oldm){ oldm = xm; sq = sqrt(2.0*xm); alxm=log(xm); g=xm*alxm-gammln(xm+1.0); } do { do { y = tan(3.141592654*Urand()); em = sq*y+xm; } while(em <0.0); intem = em; em = intem; t = 0.9*(1.0+y*y)*exp(em*alxm-gammln(em+1.0)-g); }while(Urand() > t); } return(em);}void C_PRand(char **argv) { int size; LWFLOAT mean; SIGNAL sig; int i; argv = ParseArgv(argv,tINT,&size,tFLOAT,&mean,0); sig = NewSignal(); SizeSignal(sig,size,YSIG); for (i = 0;i<size;i++) sig->Y[i] = Prand(mean); SetResultValue((VALUE) sig); DeleteSignal(sig);} /************************************************** * * Functions that deal with time * **************************************************//* * Get the number of seconds since 1 January 1970 00h00 */LWFLOAT MyTime(void) { long sec,microsec; XXGetTime(&sec,µsec); return(sec+microsec*1.e-6);}/* * Wait function */void Wait(LWFLOAT delay) { LWFLOAT t; if (delay <= 0) return; t = MyTime()+delay; while(MyTime() < t) {// ProcessNextEvent(YES,t-MyTime()); } }void C_Wait(char **argv){ LWFLOAT delay; argv = ParseArgv(argv,tFLOAT,&delay,0); Wait(delay);}// Get current date and timevoid GetCurrentDate(int *d,int *m, int *y, int *h, int *mn, int *s, int flagGMT){ time_t now = time(NULL); struct tm *tm; if (flagGMT) tm = gmtime(&now); else tm = localtime(&now); *y = tm->tm_year+1900; *d = tm->tm_mday; *m = tm->tm_mon+1; *h = tm->tm_hour; *mn = tm->tm_min; *s = tm->tm_sec; }// Corresponding commandvoid C_Time(char **argv){ extern int YYYYMMDD(int yy, int mm, int dd); LISTV lv; int d,m,y,h,mn,s; char *action,opt; LWFLOAT ymd,hms,sec; char flagDate,flagListv,flagGMT; argv = ParseArgv(argv,tWORD,&action,-1); // time sec if (!strcmp(action,"sec")) { NoMoreArgs(argv); sec = MyTime(); SetResultFloat(sec); return; } // time 2sec if (!strcmp(action,"2sec")) { argv = ParseArgv(argv,tLISTV,&lv,0); if (lv->length != 2) ErrorUsage(); NoMoreArgs(argv); ymd = GetListvNthFloat(lv,0); if (ymd != (int) ymd) ErrorUsage(); hms = (int) GetListvNthFloat(lv,1); if (hms != (int) hms) ErrorUsage(); sec = (Date2Index((int) ymd)-Date2Index(19700101))*24*3600; h = (int) (hms/10000); hms -= h*10000; mn = (int) (hms/100); hms -= mn*100; sec += hms + mn*60+h*3600; SetResultFloat(sec); return; } // time sec2 if (!strcmp(action,"sec2")) { argv = ParseArgv(argv,tFLOAT,&sec,0); if (sec != (int) sec) ErrorUsage();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -