// MPS_frac_cal.cc // Author: Mario J. Bencomo // last modified: 11/10/16 #include "MPS_frac_cal.hh" //#define VERBOSE_MJB #define MY_MAX(x,y) ((x)>(y))?(x):(y) #define MY_MIN(x,y) -(MY_MAX(-(x),-(y))) #define MINUS_TO_POW(p) ((p)%2)?(-1):(1) namespace TSOpt{ //----------------------------------------------------------------------// int nchoosek(int n, int k){ //----------------------------------------------------------------------// try{ if(n<0||k<0){ RVLException e; e << "n and/or k is/are negative!\n" << "k="<< k <<", n="<< n <<"\n"; throw e; } if( (k>n) && (n!=0)){ RVLException e; e << "k is greater than n!\n" << "k=" <30 ){ RVLException e; e << "n is greater that 30!\n"; throw e; } if( (k==0) || (k==n)) return 1; return nchoosek(n-1, k-1)+nchoosek(n-1,k); } catch(RVLException &e){ e << " ERROR from nchoosek()\n"; throw e; } } //----------------------------------------------------------------------// int factorial(int n){ //----------------------------------------------------------------------// try{ if(n<0){ RVLException e; e << "n is negative!\n"; throw e; } if(n>30){ RVLException e; e << "n is greater than 30!\n"; throw e; } int result = 1; for (int i=1; i<=n; i++) result = result * i; return result; } catch(RVLException &e){ e << " ERROR from factorial()\n"; throw e; } } //----------------------------------------------------------------------// void frac_deriv( float q, float scal, int N, const float *x, float *y, float dt, bool adj ){ //----------------------------------------------------------------------// try{ if( q<0 || N<=0 ){ RVLException e; e << "Derivative order (q="< const order, vector const scalars, int N_src, string in_file, string out_file, bool adj ){ //----------------------------------------------------------------------// try{ #ifdef VERBOSE_MJB cerr << "*************************************************\n" << " Inside MPS_frac_cal_kern\n" << "*************************************************\n\n" << "in file = " << in_file << "\n" << "out file = " << out_file << "\n" << "adj flag = " << adj << "\n" << "order:"; for(int i=0; i=0){ frac_deriv( q, scalars[i_b], NS, x_arr+offset, y_arr+offset, dt, adj ); } else{ q *= -1; frac_integ( q, scalars[i_b], NS, x_arr+offset, y_arr+offset, dt, adj ); } }//MPS basis loop }//sources loop //writing out fp_y= iwave_const_fopen(out_file.c_str(),"r+",NULL,stderr); for( int i=0; i MPS_basis = mps_sp.get_MPS_basis(); //loop over MPS basis for(int i=0; i const &x, Vector const &y ) const{ //----------------------------------------------------------------------// try{ AssignParams ap_x(*pars,in_key,stderr); x.eval(ap_x); AssignParams ap_y(*pars,out_key,stderr); y.eval(ap_y); } catch(RVLException &e){ e << "ERROR from MPS_frac_cal::param_set\n"; throw e; } } //----------------------------------------------------------------------// ostream & MPS_frac_cal::write(ostream & str) const { //----------------------------------------------------------------------// str<<"MPS_frac_cal object\n"; str<<"order:"; for(int i=0; i &x, Vector &y ) const{ //----------------------------------------------------------------------// try{ SpaceTest(this->get_MPS_Domain(),x,"MPS_frac_cal::apply (dom)"); SpaceTest(this->get_MPS_Range(),y,"MPS_frac_cal::apply (rng)"); //setting filenames in param param_set(x,y); // zero output y.zero(); //extracting file names string x_file = valparse(*pars,in_key); string y_file = valparse(*pars,out_key); //diffint kernel #ifdef IWAVE_USE_MPI MPS_frac_cal_kern_MPI( orders, scalars, mps_sp.get_s_pos().size(), x_file, y_file, false ); #else MPS_frac_cal_kern( orders, scalars, mps_sp.get_s_pos().size(), x_file, y_file, false ); #endif } catch (RVLException & e) { e<<"ERROR from MPS_frac_cal::apply\n"; throw e; } } //----------------------------------------------------------------------// void MPS_frac_cal::applyAdj( const Vector &y, Vector &x ) const { //----------------------------------------------------------------------// try{ SpaceTest(this->get_MPS_Domain(),y,"MPS_conv::applyAdj (rng)"); SpaceTest(this->get_MPS_Range(),x,"MPS_conv::applyAdj (dom)"); //setting filenames in param param_set(y,x); // zero output x.zero(); //extracting file names string y_file = valparse(*pars,in_key); string x_file = valparse(*pars,out_key); //diffint adj kernel #ifdef IWAVE_USE_MPI MPS_frac_cal_kern_MPI( orders, scalars, mps_sp.get_s_pos().size(), y_file, x_file, true ); #else MPS_frac_cal_kern( orders, scalars, mps_sp.get_s_pos().size(), y_file, x_file, true ); #endif } catch (RVLException & e) { e<<"ERROR from MPS_frac_cal::applyAdj\n"; throw e; } } }//end TSOpt