⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lyap_spec.c

📁 时间序列工具
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *   This file is part of TISEAN * *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber * *   TISEAN is 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. * *   TISEAN 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 TISEAN; if not, write to the Free Software *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *//*Author: Rainer Hegger, last modified Dec 4, 2005  *//*Changes:  7/14/05: Changed borders of the sort routine to speed things up  11/25/05: Show also absolute forecast errors  12/04/05: Some more changes in sort  12/20/05: Change in increase neighborhood size loop  12/28/05: Found bug in memory allocation (index)*/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <limits.h>#include <time.h>#include <string.h>#include "routines/tsa.h"#define WID_STR "Estimates the spectrum of Lyapunov exponents using the\n\t\method of Sano and Sawada."#define OUT 10#define BOX 512#define EPSMAX 1.0#define DELAY 1char epsset=0,stdo=1;char INVERSE,*outfile=NULL;char *infile=NULL;char dimset=0;char *COLUMNS=NULL;unsigned long LENGTH=ULONG_MAX,ITERATIONS,exclude=0;unsigned int EMBED=2,DIMENSION=1/*,DELAY=1*/,MINNEIGHBORS=30;unsigned int verbosity=0xff;double EPSSTEP=1.2;double **series,*averr,avneig=0.0,aveps=0.0;double **mat,*vec,*abstand;double epsmin;long imax=BOX-1,count=0;long **box,*list;unsigned long *found;unsigned int alldim,**indexes;void show_options(char *progname){  what_i_do(progname,WID_STR);  fprintf(stderr," Usage: %s [options]\n",progname);  fprintf(stderr," Options:\n");  fprintf(stderr,"Everything not being a valid option will be interpreted"          " as a possible"          " datafile.\nIf no datafile is given stdin is read. Just - also"          " means stdin\n");  fprintf(stderr,"\t-l # of datapoints [default is whole file]\n");  fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n");  fprintf(stderr,"\t-c column to read[default 1]\n");  fprintf(stderr,"\t-m # of components,embedding dimension [default %d,%d]\n",	  DIMENSION,EMBED);  //  fprintf(stderr,"\t-d delay  [default %d]\n",DELAY);  fprintf(stderr,"\t-r epsilon size to start with [default "  "(data interval)/1000]\n");  fprintf(stderr,"\t-f factor to increase epsilon [default: 1.2]\n");  fprintf(stderr,"\t-k # of neighbors to use [default: 30]\n");  fprintf(stderr,"\t-n # of iterations [default: length]\n");  fprintf(stderr,"\t-I invert the time series [default: no]\n");  fprintf(stderr,"\t-o name of output file [default 'datafile'.lyaps]\n");  fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"          "0='only panic messages'\n\t\t"          "1='+ input/output messages'\n");  fprintf(stderr,"\t-h show these options\n");  fprintf(stderr,"\n");  exit(0);}void scan_options(int n,char **argv){  char *out;    if ((out=check_option(argv,n,'l','u')) != NULL)    sscanf(out,"%lu",&LENGTH);  if ((out=check_option(argv,n,'x','u')) != NULL)    sscanf(out,"%lu",&exclude);  if ((out=check_option(argv,n,'c','s')) != NULL)    COLUMNS=out;  /*  if ((out=check_option(argv,n,'d','u')) != NULL)      sscanf(out,"%u",&DELAY);*/  if ((out=check_option(argv,n,'m','2')) != NULL) {    sscanf(out,"%u,%u",&DIMENSION,&EMBED);    dimset=1;  }  if ((out=check_option(argv,n,'n','u')) != NULL)    sscanf(out,"%lu",&ITERATIONS);  if ((out=check_option(argv,n,'r','f')) != NULL) {    epsset=1;    sscanf(out,"%lf",&epsmin);  }  if ((out=check_option(argv,n,'f','f')) != NULL)    sscanf(out,"%lf",&EPSSTEP);  if ((out=check_option(argv,n,'k','u')) != NULL)    sscanf(out,"%u",&MINNEIGHBORS);  if ((out=check_option(argv,n,'V','u')) != NULL)    sscanf(out,"%u",&verbosity);  if ((out=check_option(argv,n,'I','n')) != NULL)    INVERSE=1;  if ((out=check_option(argv,n,'o','o')) != NULL) {    stdo=0;    if (strlen(out) > 0)      outfile=out;  }}double sort(long act,unsigned long* nfound,char *enough){  double maxeps=0.0,dx,dswap,maxdx;  long self=0,i,j,del,hf,iswap,n1;  unsigned long imax=*nfound;  *enough=0;  for (i=0;i<imax;i++) {    hf=found[i];    if (hf != act) {      maxdx=fabs(series[0][act]-series[0][hf]);      for (j=1;j<alldim;j++) {	n1=indexes[0][j];	del=indexes[1][j];	dx=fabs(series[n1][act-del]-series[n1][hf-del]);	if (dx > maxdx) maxdx=dx;      }      abstand[i]=maxdx;    }    else {      self=i;    }  }  if (self != (imax-1)) {    abstand[self]=abstand[imax-1];    found[self]=found[imax-1];  }  for (i=0;i<MINNEIGHBORS;i++) {    for (j=i+1;j<imax-1;j++) {      if (abstand[j]<abstand[i]) {	dswap=abstand[i];	abstand[i]=abstand[j];	abstand[j]=dswap;	iswap=found[i];	found[i]=found[j];	found[j]=iswap;      }    }  }  if (!epsset || (abstand[MINNEIGHBORS-1] >= epsmin)) {    *nfound=MINNEIGHBORS;    *enough=1;    maxeps=abstand[MINNEIGHBORS-1];    return maxeps;  }  for (i=MINNEIGHBORS;i<imax-2;i++) {    for (j=i+1;j<imax-1;j++) {      if (abstand[j]<abstand[i]) {	dswap=abstand[i];	abstand[i]=abstand[j];	abstand[j]=dswap;	iswap=found[i];	found[i]=found[j];	found[j]=iswap;      }    }    if (abstand[i] > epsmin) {      (*nfound)=i+1;      *enough=1;      maxeps=abstand[i];      return maxeps;    }  }  maxeps=abstand[imax-2];  return maxeps;}void make_dynamics(double **dynamics,long act){  long i,hi,j,hj,k,t=act,d;  unsigned long nfound=0;  double **hser,**imat;  double foundeps=0.0,epsilon,hv,hv1;  double new_vec;  char got_enough;  check_alloc(hser=(double**)malloc(sizeof(double*)*DIMENSION));  for (i=0;i<DIMENSION;i++)    hser[i]=series[i]+act;  epsilon=epsmin/EPSSTEP;  do {    epsilon *= EPSSTEP;    if (epsilon > EPSMAX)      epsilon=EPSMAX;    make_multi_box(series,box,list,LENGTH-DELAY,BOX,DIMENSION,EMBED,		   DELAY,epsilon);    nfound=find_multi_neighbors(series,box,list,hser,LENGTH-DELAY,BOX,				DIMENSION,EMBED,DELAY,epsilon,found);    if (nfound > MINNEIGHBORS) {      foundeps=sort(act,&nfound,&got_enough);      if (got_enough)	break;    }  } while (epsilon < EPSMAX);  free(hser);  avneig += nfound;  aveps += foundeps;  if (!epsset)    epsmin=aveps/count;  if (nfound < MINNEIGHBORS) {    fprintf(stderr,"#Not enough neighbors found. Exiting\n");    exit(LYAP_SPEC_NOT_ENOUGH_NEIGHBORS);  }    for (i=0;i<=alldim;i++) {    vec[i]=0.0;    for (j=0;j<=alldim;j++)       mat[i][j]=0.0;  }    for (i=0;i<nfound;i++) {    act=found[i];    mat[0][0] += 1.0;    for (j=0;j<alldim;j++)      mat[0][j+1] += series[indexes[0][j]][act-indexes[1][j]];    for (j=0;j<alldim;j++) {      hv1=series[indexes[0][j]][act-indexes[1][j]];      hj=j+1;      for (k=j;k<alldim;k++)	mat[hj][k+1] += series[indexes[0][k]][act-indexes[1][k]]*hv1;    }  }  for (i=0;i<=alldim;i++)    for (j=i;j<=alldim;j++)      mat[j][i]=(mat[i][j]/=(double)nfound);    imat=invert_matrix(mat,alldim+1);    for (d=0;d<DIMENSION;d++) {    for (i=0;i<=alldim;i++)      vec[i]=0.0;    for (i=0;i<nfound;i++) {      act=found[i];      hv=series[d][act+DELAY];      vec[0] += hv;      for (j=0;j<alldim;j++)	vec[j+1] += hv*series[indexes[0][j]][act-indexes[1][j]];    }    for (i=0;i<=alldim;i++)      vec[i] /= (double)nfound;        new_vec=0.0;    for (i=0;i<=alldim;i++)      new_vec += imat[0][i]*vec[i];    for (i=1;i<=alldim;i++) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -