2 * This file is part of TISEAN
4 * Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
6 * TISEAN is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * TISEAN is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with TISEAN; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /*Author: Rainer Hegger. Last modified: Sep 3, 1999*/
26 #include "routines/tsa.h"
28 #define WID_STR "Estimates the maximal Lyapunov exponent using the Kantz\n\t\
32 const unsigned int ibox=BOX-1;
34 unsigned long length=ULONG_MAX;
35 unsigned long exclude=0;
36 unsigned long reference=ULONG_MAX;
37 unsigned int maxdim=2;
38 unsigned int mindim=2;
40 unsigned int column=1;
41 unsigned int epscount=5;
42 unsigned int maxiter=50;
43 unsigned int window=0;
44 unsigned int verbosity=0xff;
45 double epsmin=1.e-3,epsmax=1.e-2;
46 char eps0set=0,eps1set=0;
50 double *series,**lyap;
51 long box[BOX][BOX],*liste,**lfound,*found,**count;
54 void show_options(char *progname)
56 what_i_do(progname,WID_STR);
58 fprintf(stderr," Usage: %s [options]\n",progname);
59 fprintf(stderr," Options:\n");
60 fprintf(stderr,"Everything not being a valid option will be "
61 "interpreted as a possible datafile.\nIf no datafile "
62 "is given stdin is read. Just - also means stdin\n");
63 fprintf(stderr,"\t-l # of data [default: whole file]\n");
64 fprintf(stderr,"\t-x # of lines to be ignored [default: 0]\n");
65 fprintf(stderr,"\t-c column to read [default: 1]\n");
66 fprintf(stderr,"\t-M maxdim [default: 2]\n");
67 fprintf(stderr,"\t-m mindim [default: 2]\n");
68 fprintf(stderr,"\t-d delay [default: 1]\n");
69 fprintf(stderr,"\t-r mineps [default: (data interval)/1000]\n");
70 fprintf(stderr,"\t-R maxeps [default: (data interval)/100]\n");
71 fprintf(stderr,"\t-# # of eps [default: 5]\n");
72 fprintf(stderr,"\t-n # of reference points [default: # of data]\n");
73 fprintf(stderr,"\t-s # of iterations [default: 50]\n");
74 fprintf(stderr,"\t-t time window [default: 0]\n");
75 fprintf(stderr,"\t-o outfile [default: 'datafile'.lyap]\n");
76 fprintf(stderr,"\t-V verbosity level [default: 3]\n\t\t"
77 "0='only panic messages'\n\t\t"
78 "1='+ input/output messages'\n\t\t"
79 "2='+ plus statistics'\n");
80 fprintf(stderr,"\t-h show these options\n");
84 void scan_options(int n,char **str)
88 if ((out=check_option(str,n,'l','u')) != NULL)
89 sscanf(out,"%lu",&length);
90 if ((out=check_option(str,n,'x','u')) != NULL)
91 sscanf(out,"%lu",&exclude);
92 if ((out=check_option(str,n,'c','u')) != NULL)
93 sscanf(out,"%u",&column);
94 if ((out=check_option(str,n,'M','u')) != NULL)
95 sscanf(out,"%u",&maxdim);
96 if ((out=check_option(str,n,'m','u')) != NULL)
97 sscanf(out,"%u",&mindim);
98 if ((out=check_option(str,n,'d','u')) != NULL)
99 sscanf(out,"%u",&delay);
100 if ((out=check_option(str,n,'r','f')) != NULL) {
102 sscanf(out,"%lf",&epsmin);
104 if ((out=check_option(str,n,'R','f')) != NULL) {
106 sscanf(out,"%lf",&epsmax);
108 if ((out=check_option(str,n,'#','u')) != NULL)
109 sscanf(out,"%u",&epscount);
110 if ((out=check_option(str,n,'n','u')) != NULL)
111 sscanf(out,"%lu",&reference);
112 if ((out=check_option(str,n,'s','u')) != NULL)
113 sscanf(out,"%u",&maxiter);
114 if ((out=check_option(str,n,'t','u')) != NULL)
115 sscanf(out,"%u",&window);
116 if ((out=check_option(str,n,'V','u')) != NULL)
117 sscanf(out,"%u",&verbosity);
118 if ((out=check_option(str,n,'o','o')) != NULL)
123 void put_in_boxes(double eps)
127 static unsigned long blength;
129 blength=length-(maxdim-1)*delay-maxiter;
135 for (i=0;i<blength;i++) {
136 j=(long)(series[i]/eps)&ibox;
137 k=(long)(series[i+delay]/eps)&ibox;
143 void lfind_neighbors(long act,double eps)
145 unsigned int hi,k,k1;
146 long i,j,i1,i2,j1,element;
148 double dx,eps2=sqr(eps);
150 lwindow=(long)window;
151 for (hi=0;hi<maxdim-1;hi++)
153 i=(long)(series[act]/eps)&ibox;
154 j=(long)(series[act+delay]/eps)&ibox;
155 for (i1=i-1;i1<=i+1;i1++) {
157 for (j1=j-1;j1<=j+1;j1++) {
158 element=box[i2][j1&ibox];
159 while (element != -1) {
160 if ((element < (act-lwindow)) || (element > (act+lwindow))) {
161 dx=sqr(series[act]-series[element]);
163 for (k=1;k<maxdim;k++) {
165 dx += sqr(series[act+k1]-series[element+k1]);
168 lfound[k1][found[k1]]=element;
176 element=liste[element];
182 void iterate_points(long act)
186 unsigned int i,j,l,l1;
187 long k,element,**lcount;
189 check_alloc(lfactor=(double**)malloc(sizeof(double*)*(maxdim-1)));
190 check_alloc(lcount=(long**)malloc(sizeof(long*)*(maxdim-1)));
191 for (i=0;i<maxdim-1;i++) {
192 check_alloc(lfactor[i]=(double*)malloc(sizeof(double)*(maxiter+1)));
193 check_alloc(lcount[i]=(long*)malloc(sizeof(long)*(maxiter+1)));
195 check_alloc(dx=(double*)malloc(sizeof(double)*(maxiter+1)));
197 for (i=0;i<=maxiter;i++)
198 for (j=0;j<maxdim-1;j++) {
203 for (j=mindim-2;j<maxdim-1;j++) {
204 for (k=0;k<found[j];k++) {
205 element=lfound[j][k];
206 for (i=0;i<=maxiter;i++)
207 dx[i]=sqr(series[act+i]-series[element+i]);
208 for (l=1;l<j+2;l++) {
210 for (i=0;i<=maxiter;i++)
211 dx[i] += sqr(series[act+i+l1]-series[element+l1+i]);
213 for (i=0;i<=maxiter;i++)
216 lfactor[j][i] += dx[i];
220 for (i=mindim-2;i<maxdim-1;i++)
221 for (j=0;j<=maxiter;j++)
224 lyap[i][j] += log(lfactor[i][j]/lcount[i][j])/2.0;
227 for (i=0;i<maxdim-1;i++){
236 int main(int argc,char **argv)
244 if (scan_help(argc,argv))
245 show_options(argv[0]);
247 scan_options(argc,argv);
248 #ifndef OMIT_WHAT_I_DO
249 if (verbosity&VER_INPUT)
250 what_i_do(argv[0],WID_STR);
253 infile=search_datafile(argc,argv,&column,verbosity);
257 if (outfile == NULL) {
259 check_alloc(outfile=(char*)calloc(strlen(infile)+6,1));
260 sprintf(outfile,"%s.lyap",infile);
263 check_alloc(outfile=(char*)calloc(11,1));
264 sprintf(outfile,"stdin.lyap");
267 test_outfile(outfile);
269 series=get_series(infile,&length,exclude,column,verbosity);
270 rescale_data(series,length,&min,&max);
277 if (epsmin >= epsmax) {
282 if (reference > (length-maxiter-(maxdim-1)*delay))
283 reference=length-maxiter-(maxdim-1)*delay;
284 if ((maxiter+(maxdim-1)*delay) >= length) {
285 fprintf(stderr,"Too few points to handle these parameters!\n");
286 exit(LYAP_K__MAXITER_TOO_LARGE);
296 check_alloc(liste=(long*)malloc(sizeof(long)*(length)));
297 check_alloc(found=(long*)malloc(sizeof(long)*(maxdim-1)));
298 check_alloc(lfound=(long**)malloc(sizeof(long*)*(maxdim-1)));
299 for (i=0;i<maxdim-1;i++)
300 check_alloc(lfound[i]=(long*)malloc(sizeof(long)*(length)));
301 check_alloc(count=(long**)malloc(sizeof(long*)*(maxdim-1)));
302 for (i=0;i<maxdim-1;i++)
303 check_alloc(count[i]=(long*)malloc(sizeof(long)*(maxiter+1)));
304 check_alloc(lyap=(double**)malloc(sizeof(double*)*(maxdim-1)));
305 for (i=0;i<maxdim-1;i++)
306 check_alloc(lyap[i]=(double*)malloc(sizeof(double)*(maxiter+1)));
311 eps_fak=pow(epsmax/epsmin,1.0/(double)(epscount-1));
313 fout=fopen(outfile,"w");
314 if (verbosity&VER_INPUT)
315 fprintf(stderr,"Opened %s for writing\n",outfile);
316 for (l=0;l<epscount;l++) {
317 epsilon=epsmin*pow(eps_fak,(double)l);
318 for (i=0;i<maxdim-1;i++)
319 for (j=0;j<=maxiter;j++) {
323 put_in_boxes(epsilon);
324 for (i=0;i<reference;i++) {
325 lfind_neighbors(i,epsilon);
328 if (verbosity&VER_USR1)
329 fprintf(stderr,"epsilon= %e\n",epsilon*max);
330 for (i=mindim-2;i<maxdim-1;i++) {
331 fprintf(fout,"#epsilon= %e dim= %d\n",epsilon*max,i+2);
332 for (j=0;j<=maxiter;j++)
334 fprintf(fout,"%d %e %ld\n",j,lyap[i][j]/count[i][j],count[i][j]);