Example #1
0
Polinomio* Polinomio::operator*(Polinomio *p) {
    Polinomio *mult = new Polinomio(this->getGrado()+p->getGrado());
    mult->setCoeficiente(this->getGrado()+p->getGrado(),0);
    for(int i=0; i<=this->getGrado(); i++) {
        for(int j=0; j<=p->getGrado(); j++) {
            mult->setCoeficiente(i+j,(*mult)[i+j] + ((*this)[i] * (*p)[j]) );
        }
    }
    return mult;
}
Example #2
0
void RadialInterpolatePoints( const AcGePoint3dArray& datas, AcGePoint3dArray& pts )
{
    MQ<double>          rbf; // RBF as kernel we can use : MQ, IMQ, GAU, TPS, POT
    Polinomio<double>   pol;
    double              c;
    Matrix<double>      A;
    Vector<double>      x, y, f, b, lambda;
    Vector<double>      x_new, y_new, f_new;
    int                 n, m;

    //define the number of nodes
    n = 10;

    //define the shape parameter for MQ kernel
    c = 3.0;

    //make the 2d data, see 2d-comun.hpp
    make_data( datas, x, y, f );
    n = x.GetSize();

    //configure the associate polynomial
    // pol.make( data_dimension, degree_pol)
    pol.make( 2 , rbf.get_degree_pol() );

    //get the number of elements in the polynomial base
    m = pol.get_M();

    //make aditional data for:  Ax=b
    b.Reallocate( n + m );
    b = 0.0;

    //fill the expanded vector b = [f 0]
    for( int i = 0;  i < n;  i++ )
        b( i ) = f( i );

    //fill the Gramm matrix
    fill_gram( rbf, pol, c, x, y, A );

    //solve the linear system by LU factorization
    lambda = gauss( A, b );

    make_data( pts, x_new, y_new, f_new );

    //interpolate the data
    f_new = interpolate( rbf, pol, c, lambda, x, y, x_new, y_new );

    write_back_data( f_new, pts );
}
Example #3
0
bool operator==(const Polinomio& m, const Polinomio& s) {
	//se ho due polinomi nulli
	if ( (m.Nullo()) && (s.Nullo()) ){
		return true;
	}
	//se solamente uno dei polinomi e' nullo
	else if ( (m.Nullo()) || (s.Nullo()) ) {
		return false;
	}
	else {
		return m.first->coefficiente == s.first->coefficiente  
				&& m.first->esponente == s.first->esponente 
				&& Polinomio(m.first->next) == Polinomio(s.first->next);
				//invocazione ricorsiva su (m,s)->next;
	}
}
Example #4
0
Polinomio operator/(const Polinomio& m, const Polinomio& s) {
	Polinomio aux;

	//se (almeno) uno dei polinomi e' il polinomio nullo, ritorno 0
	if (m.Nullo() || s.Nullo()) {
		return aux;
	}
	//else

	if (m.first->esponente >= s.first->esponente) {
		aux = Polinomio(m.first->coefficiente/s.first->coefficiente,
									m.first->esponente - s.first->esponente);
		if (aux.first->esponente!=0) {
			aux.first->next = (Polinomio((m - (aux*s)).first->next) / s).first;
		}
	}

	return aux;
}
int main(int argc,char *argv[]){
  char workspace[]="workspace.txt";
  Pol_Directory the_directory;
  the_directory.load_data(workspace);
  static char buf[TAMDBUF],varname[TAMDBUF],coefici[TAMDBUF];
  int fd,r,s1,numerador,denominador;
  string coeficients,ms,ms1,string_num,string_den,newpol,coeffs;
  string string_op1,string_op2;
  String_Tokenizer ST0;
  vector<string> pcoef;
  Rac *RacPt;
  Polinomio *Pol;
  while(getcmd(buf,sizeof(buf))>=0){
    echo_(buf);
    if(cmd_is_exit(buf)){
      break;
    }
    if(r=hay_equal_in_buf(buf)){
      if(first_and_last_are(buf,'[',']')){
          get_var_name(buf,varname);
          ms=string(varname);/* ms contiene un nombre de variable polinomio */
          get_coefici(buf,coefici,r);
          coeficients=string(coefici);
          ST0=String_Tokenizer(coeficients,",");
          while(ST0.has_more_tokens()){
            pcoef.push_back(ST0.next_token());
          }
          
          RacPt=new Rac[pcoef.size()];
          for(int i=0;i<pcoef.size();i++){
            if((fd=pcoef[i].find("/"))!=std::string::npos){
              string_num=pcoef[i].substr(0,fd);
              string_den=pcoef[i].substr(fd+1);
              numerador=atoi(&(string_num[0]));
              denominador=atoi(&(string_den[0]));
            }else{
              string_num=pcoef[i];
              numerador=atoi(&(string_num[0]));
              denominador=1;
            }
            *(RacPt+i)=Rac(numerador,denominador);
          }/* end for() */
          Pol=new Polinomio(pcoef.size()-1,RacPt);
          newpol=Pol->string_show();
          the_directory.add_or_change_entry(ms,newpol);
          pcoef.clear();
      }else{
      /* En buf hay un '=' y tambi\'en hay un '+', \'o un '-', \'o un '*' */
      /* La variable del lado izquierdo del signo de = */
        get_var_name(buf,varname);
        ms=string(varname);
        /*obtener cadena despu\'es del = */
        ms1=get_string_after_equal(buf);//cout<<"="<<ms1<<endl;
        /*identificar las variables operandos*/
        if(is_there_a_char_in_buf(buf,'+')){
          if((fd=ms1.find("+"))!=std::string::npos){
            string_op1=ms1.substr(0,fd);
            string_op2=ms1.substr(fd+1);
            fd=string_op1.find_first_not_of(' ');
            string_op1=string_op1.substr(fd);
            if((fd=string_op1.find(' '))!=std::string::npos){
              string_op1=string_op1.substr(0,fd);
            }
            fd=string_op2.find_first_not_of(' ');
            string_op2=string_op2.substr(fd);
            if((fd=string_op2.find(' '))!=std::string::npos){
              string_op2=string_op2.substr(0,fd);
            }
          }
          do_suma(the_directory,string_op1,string_op2,ms);
        }
        if(is_there_a_char_in_buf(buf,'-')){
          if((fd=ms1.find("-"))!=std::string::npos){
            string_op1=ms1.substr(0,fd);
            string_op2=ms1.substr(fd+1);
            fd=string_op1.find_first_not_of(' ');
            string_op1=string_op1.substr(fd);
            if((fd=string_op1.find(' '))!=std::string::npos){
              string_op1=string_op1.substr(0,fd);
            }
            fd=string_op2.find_first_not_of(' ');
            string_op2=string_op2.substr(fd);
            if((fd=string_op2.find(' '))!=std::string::npos){
              string_op2=string_op2.substr(0,fd);
            }
          }
          do_resta(the_directory,string_op1,string_op2,ms);
        }
        if(is_there_a_char_in_buf(buf,'*')){
          if((fd=ms1.find("*"))!=std::string::npos){
            string_op1=ms1.substr(0,fd);
            string_op2=ms1.substr(fd+1);
            fd=string_op1.find_first_not_of(' ');
            string_op1=string_op1.substr(fd);
            if((fd=string_op1.find(' '))!=std::string::npos){
              string_op1=string_op1.substr(0,fd);
            }
            fd=string_op2.find_first_not_of(' ');
            string_op2=string_op2.substr(fd);
            if((fd=string_op2.find(' '))!=std::string::npos){
              string_op2=string_op2.substr(0,fd);
            }
          }
          do_multiplicacion(the_directory,string_op1,string_op2,ms);
        }
      }
    }/* if(r=hay_equal_in_buf(buf)) */
    if(!(r=hay_equal_in_buf(buf))){
      if(get_var_name1(buf,varname)){
        ms=string(varname);
        coeffs=the_directory.lookup_entry(ms);
        cout<<ms<<" = "<<coeffs<<endl;
      }else{
        /*En caso de que un buf sin '=' contenga +,-, o * no se hace algo*/
        /*Nothing to be done here*/
      }
    }
  }//end while(getcmd(buf, sizeof(buf)) >= 0)
  printf("Exiting now...\n");
  do_save(the_directory); 
  system("PAUSE");
  return 0;
}//end main()
Example #6
0
//---------------------------------------------------------
int main(int argc, char **argv)
{
   TPS<double>         rbf;
   Polinomio<double>   pol;
   Matrix<double>      A,B;
   Vector<double>      x,y,f;
   Vector<double>      lambda,b;
   Vector<double>      xnew,ynew,fnew; 
   double              c=0.01;
   int                 n,ni,m;
 
//make the data in the square [0,1] x [0,1]   
   make_data(0,1,0,1, 21, 21, x, y, ni, n);

//stablish the exponent in: r^beta log(r)
   rbf.set_beta(4);
   
//configure the associate polynomial
// pol.make( data_dimension, degree_pol)
   pol.make(2 , rbf.get_degree_pol());
   
//show the rbf and pol info
   cout<<rbf;
   cout<<pol;
   
//show the number of nodes   
   cout<<endl;
   cout<<"total nodes    N  = "<<n<<endl;
   cout<<"interior nodes ni = "<<ni<<endl;
   cout<<"boundary nodes nf = "<<n-ni<<endl;
   cout<<endl;
   

//get the number of elements in the polynomial base
   m = pol.get_M();
    
//resize the matrices to store the partial derivatives
   A.Resize(n+m,n+m);  A = 0.0;
   B.Resize(n+m,n+m);  B = 0.0;
   
//Recall that this problem has the general form
//   (Uxx+Uyy)   (Pxx+Pyy)   =  f     interior nodes  0..ni 
//     U           P_b       =  g     boundary nodes  ni..n 
//   P^transpose   0         =  0     momentun conditions in P  
//
// P is the polynomial wit size n x m
// P_b is the polynomial working in the boundary nodes, size   nf x m
// Pxx+Pyy  has size  ni x m   
   
//make the matriz derivatives   
   fill_matrix(   "dxx"     , rbf , pol , c , x , y, 0 ,  ni ,  A);
   fill_matrix(   "dyy"     , rbf , pol , c , x , y, 0 ,  ni ,  B);
      
   A = A + B;   //  A <-  Uxx + Uyy
    
//Add the submatriz for the boundary nodes:   U , P_b           boundary nodes  ni..n        
   fill_matrix(   "normal"  , rbf , pol , c , x , y, ni,   n ,  A);
   
//Add the submatriz P^transpose at the end:    P^transpose   
   fill_matrix( "pol_trans" , rbf , pol , c , x , y, n ,  n+m,  A);
     
//resize the vector to store the right_size of the PDE      
   b.Resize(n+m); b = 0.0;
    
//fill with   f  
   for(int i=0;i<ni;i++)
      b(i) = right_side(x(i), y(i));
      
//fill with the boundary condition      
   for(int i=ni;i<n;i++)
      b(i) = boundary_condition(x(i),y(i));
      
//solve the linear system of equations 
   lambda =  gauss(A,b);
    
//make the new data grid    
   int ni2,n2;
   make_data(0,1,0,1, 41, 41, xnew, ynew, ni2, n2);
    
//interpolate on this new data grid (xnew,ynew)     
   fnew = interpolate(rbf,pol,c,lambda,x,y,xnew,ynew);
     
//determine the maximum error
   double e=0.0;
   for(int i=0;i<ni2;i++)
   {
     e = max(e, fabs(fnew(i) - sin(2*M_PI*xnew(i))*cos(2*M_PI*ynew(i)))  );
   }
     
//show the error     
   cout<<endl;
   cout<<"The maximum error: e_max = "<<e<<endl<<endl;
   
//store the interpolated data   
   save_gnu_data("data",xnew,ynew,fnew);
     
  return 0;
}