コード例 #1
0
ファイル: test_svd2.c プロジェクト: 19test/sandbox
void
check_svd (double *M , double * U, double * S, double *V)
{
  double T1 [4] ;
  double T2 [4] ;

  print_matrix("M",M) ;
  print_matrix("U",U) ;
  print_matrix("S",S) ;
  print_matrix("V",V) ;

  transp2(T1, V) ;
  prod2(T2, S, T1) ;
  prod2(T1, U, T2) ;
  print_matrix("USV'",T1) ;

  transp2(T1, U) ;
  prod2(T2, T1, U) ;
  print_matrix("U'U",T2) ;

  transp2(T1, V) ;
  prod2(T2, T1, V) ;
  print_matrix("V'V",T2) ;

  printf("det(M) = %f\n", det2(M)) ;
  printf("det(U) = %f\n", det2(U)) ;
  printf("det(V) = %f\n", det2(V)) ;
  printf("det(S) = %f\n", det2(S)) ;
  printf("\n") ;
}
コード例 #2
0
ファイル: mexProd2nz.c プロジェクト: Emisage/sf-pcd
void mexFunction(int nlhs,  mxArray        *plhs[],
           	 int nrhs,  const mxArray  *prhs[] )
{
   double   *A,    *B,    *P; 
   mwIndex  *irA,  *jcA,  *irB,  *jcB, *irP, *jcP;
   int      *list1, *list2;
   double   *Btmp, *listtmp; 
   int       m1, n1, m2, n2, mlist, nlist, isspA, isspB, k;

/* Check for proper number of arguments */
   if (nrhs < 4){
      mexErrMsgTxt("mexProd2nz: requires at least 4 input arguments."); }
   else if (nlhs>2){ 
      mexErrMsgTxt("mexProd2nz: requires 1 output argument."); }
   
/***********************************************/     

       A = mxGetPr(prhs[1]); 
       isspA = mxIsSparse(prhs[1]);
       m1 = mxGetM(prhs[1]); 
       n1 = mxGetN(prhs[1]); 
       if (isspA) { irA = mxGetIr(prhs[1]);
                    jcA = mxGetJc(prhs[1]); }
       B = mxGetPr(prhs[2]); 
       isspB  = mxIsSparse(prhs[2]);
       m2 = mxGetM(prhs[2]); 
       n2 = mxGetN(prhs[2]); 
       if (isspB) { irB = mxGetIr(prhs[2]);
                    jcB = mxGetJc(prhs[2]); }
       if (m1!=m2) { 
          mexErrMsgTxt("mexProd2nz: A, B are not compatible"); } 
       mlist = mxGetM(prhs[3]); 
       nlist = mxGetN(prhs[3]); 
       if (nlist != 2 ) { 
          mexErrMsgTxt("mexProd2nz: list must have 2 columns");}
       listtmp = mxGetPr(prhs[3]); 
       list1 = mxCalloc(mlist,sizeof(int)); 
       list2 = mxCalloc(mlist,sizeof(int));
       for (k=0; k<mlist; k++) {
          /** subtract 1 to adjust for Matlab index **/
          list1[k] = (int)listtmp[k] -1;
          list2[k] = (int)listtmp[mlist+k] -1;
       }
       plhs[0] = mxCreateSparse(n1,n2,mlist,mxREAL); 
       P=mxGetPr(plhs[0]); irP=mxGetIr(plhs[0]); jcP=mxGetJc(plhs[0]);
       /**********************************************
        * Do the actual computations in a subroutine 
        **********************************************/
        if (!isspB) { 
   	   prod1(n1,m2,n2,A,irA,jcA,isspA,B,P,irP,jcP,list1,list2,mlist); }
        else {
           Btmp = mxCalloc(m2,sizeof(double)); 
   	   prod2(n1,m2,n2,A,irA,jcA,isspA,B,irB,jcB,isspB,\
                 P,irP,jcP,Btmp,list1,list2,mlist);
	}
        mxFree(list1); mxFree(list2); 
        if (isspB) { mxFree(Btmp); }
return;
}		 
コード例 #3
0
ファイル: 10.c プロジェクト: igorsouzacarvalho88/lab1_2-2
int main(){
    float a,b;
    int x;
    printf("Digite qual operaçao deseja realizar\n");
    printf(" 1  Para somar\n");
    printf(" 2 para subtrair\n");
    printf(" 3 para dividir\n");
    printf(" 4 para multiplicar\n");
    scanf("%d",&x);
    printf(" Digite o primeiro numero");
    scanf("%f", &a);
    printf(" Digite o segundo numero");
    scanf("%f", &b);
    if (x=1) { 
             soma2();}else if (x=2) {
                                   dif2();}else if (x=3) {
                                                        div2();} else if (x=4) {
                                                                             prod2();} else printf("Operação nao listada");
    system("pause");
    return 0;
}
コード例 #4
0
ファイル: polyeval.cpp プロジェクト: NaSchkafu/riotng
  Interval LDB::operator ()(const LDB::ipolynomial_type    *poly_ptr,
                            const LDB::additional_var_data *data_ptr  ) const
  {
    unsigned int data_size = data_ptr->size();

    /*
      Abspalten der linearen Terme. Die Konstante wird den hoeheren Termen zugerechnet.
      Gleichzeitig werden die hoeheren Terme eingeschlossen.
    */

    Interval rest(0.0);
    std::vector<Interval> arguments( data_ptr->size() );
    for(unsigned int i = 0; i < data_size; i++)
      if( (*data_ptr)[ i ].operator ->() != 0 )
        arguments[ i ] = ((*data_ptr)[ i ])->domain_ - ((*data_ptr)[ i ])->devel_point_;

    std::vector<Interval> linear_coeffs(data_size, Interval(0.0));
    std::vector<unsigned int> var_codes; var_codes.reserve(data_size); //Codes of Vars with linear term.

    LDB::ipolynomial_type::const_iterator
      curr = poly_ptr->begin(),
      last = poly_ptr->end();

    while( curr != last )
    {
      if( curr->key().expnt_sum() == 0 )      //Konstanter Term.
      {
        rest += curr->value();
      }
      else if( curr->key().expnt_sum() == 1 ) //Linearer Term.
      {
        unsigned int i = curr->key().min_var_code();

        var_codes.push_back(i-1);
        linear_coeffs[i-1] = curr->value();
      }
      else //Nonlinear term.
      {
        Interval prod(1.0);

        //Evaluate the current monomial.
        for(unsigned int i = curr->key().min_var_code(); i <= curr->key().max_var_code(); i++)
        {
          unsigned int e = curr->key().expnt_of_var(i);
          if( e != 0 )
          {
            prod *= power( arguments[i-1], e );
          }
        }
        //Multiply with coefficient.
        prod *= curr->value();

        //Add enclosure to bound interval.
        rest += prod;
      }

      ++curr;
    }

    if( var_codes.size() == 0 ) return rest; //No linear terms in polynomial. So return with enclosure of
    //higher order terms.

    /*
      Lokale Kopien der 'Domains' anlegen, da diese im weiteren Verlauf unter Umstaenden
      veraendert werden.
    */

    std::vector<Interval> domain_of_max(data_size,Interval(0.0));
    std::vector<Interval> domain_of_min(data_size,Interval(0.0));

    for(unsigned int i = 0; i < data_size; i++)
    {
      if( (*data_ptr)[i].operator ->() ) //There is information.
      {
        domain_of_max[i] = domain_of_min[i] = ((*data_ptr)[i])->domain_;
      }
    }

    /*
      Jetzt beginnt der Algorithmus des LDB range bounders.
    */

    Interval idelta(rest.diam());
    for(unsigned int i = 0; i < var_codes.size(); i++)
    {
      unsigned int k = var_codes[i];
      Interval b = linear_coeffs[k];
      idelta += b.diam() * abs( ((*data_ptr)[k])->domain_ - ((*data_ptr)[k])->devel_point_ );
    }
    double delta = idelta.sup();

    bool resizing = false;
    for(unsigned int i = 0; i < var_codes.size(); i++)
    {
      unsigned int k = var_codes[i];

      double   mid_b  = linear_coeffs[k].mid();
      Interval abs_b  = Interval( std::abs(mid_b) );
      Interval domain = ((*data_ptr)[k])->domain_;
      Interval upper  = abs_b * domain.diam();
      if( upper.inf() > delta )
      {
        Interval tmp = delta / abs_b;
        if( mid_b > 0 )
        {
          domain_of_min[k] = domain_of_min[k].inf() + Interval(0,tmp.sup());
          domain_of_max[k] = domain_of_max[k].sup() - Interval(0,tmp.sup());
        }
        else
        {
          domain_of_min[k] = domain_of_min[k].sup() - Interval(0,tmp.sup());
          domain_of_max[k] = domain_of_max[k].inf() + Interval(0,tmp.sup());
        }
        resizing = true;
      }
    }

    for(unsigned int i = 0; i < data_size; i++)
    {
      if( (*data_ptr)[i].operator ->() ) //There is information.
      {
        domain_of_min[i] -= ((*data_ptr)[i])->devel_point_;
        domain_of_max[i] -= ((*data_ptr)[i])->devel_point_;
      }
    }

    if( resizing ) //Die Domains wurden verändert.
    {
      Interval min(0.0),max(0.0);

      curr = poly_ptr->begin();

      while( curr != last ) //Walk trough the polynomial.
      {
        Interval prod1(1.0),prod2(1.0);

        //Evaluate the current monomial.
        for(unsigned int i = (*curr).key().min_var_code(); i <= (*curr).key().max_var_code(); i++)
        {
          unsigned int e = (*curr).key().expnt_of_var(i);
          if( e != 0 )
          {
            prod1 *= power( domain_of_min[ i - 1 ], e );
            prod2 *= power( domain_of_max[ i - 1 ], e );
          }
        }

        //Multiply with coefficient.
        prod1 *= (*curr).value();   // <---- Multiply with double value.
        prod2 *= (*curr).value();   // <---- Multiply with double value.

        //Add enclosure to interval.
        min += prod1;
        max += prod2;

        ++curr;
      }

      return Interval( min.inf(), max.sup() );
    }
    else
    {
      for(unsigned int i = 0; i < var_codes.size(); i++)
      {
        unsigned int k = var_codes[i];
        rest += linear_coeffs[k] * domain_of_min[k];
      }
      return rest;
    }
  }
コード例 #5
0
ファイル: polyeval.cpp プロジェクト: NaSchkafu/riotng
  Interval MeanValueForm::operator ()(const MeanValueForm::ipolynomial_type    *poly_ptr,
                                      const MeanValueForm::additional_var_data *data_ptr  ) const
  {
    Interval bound1(0.0), bound2(0.0);
    std::vector<Interval> bound_of_derivatives( data_ptr->size(), Interval(0.0) );

    std::vector<Interval> arguments( data_ptr->size() ), arguments_mid( data_ptr->size() );
    for(unsigned int i = 0; i < data_ptr->size(); i++)
      if( (*data_ptr)[ i ].operator ->() != 0 )
      {
        arguments    [ i ] = ((*data_ptr)[ i ])->domain_ - ((*data_ptr)[ i ])->devel_point_;
        arguments_mid[ i ] = Interval( arguments[ i ].mid() );
      }

    MeanValueForm::const_ipolynomial_iterator
      curr = poly_ptr->begin(),
      last = poly_ptr->end();

    while( curr != last ) //Walk trough the polynomial.
    {
      Interval prod1(1.0), prod2(1.0);

      //Evaluate the current monomial at the midpoint of the domain.
      for(unsigned int i = (*curr).key().min_var_code(); i <= (*curr).key().max_var_code(); i++)
      {
        unsigned int e = (*curr).key().expnt_of_var(i);
        if( e != 0 )
        {
          prod1 *= power( arguments    [i-1], e );
          prod2 *= power( arguments_mid[i-1], e );
        }
      }

      //Multiply with coefficient.
      prod1 *= (*curr).value();   // <---- Multiply with double value.
      prod2 *= (*curr).value();   // <---- Multiply with double value.

      //Add enclosure to bound interval.
      bound1 += prod1;
      bound2 += prod2;

      //Derivate the current monomial and evaluate it.
      for(unsigned int i = (*curr).key().min_var_code(); i <= (*curr).key().max_var_code(); i++)
      {
        unsigned int e_i = (*curr).key().expnt_of_var(i);
        if( e_i != 0 )
        {
          prod2 = e_i * power( arguments[i-1], e_i-1 ); //Value of derivative.

          unsigned int j;
          for(j = (*curr).key().min_var_code(); j < i; j++)
          {
            unsigned int e_j = (*curr).key().expnt_of_var(j);
            if( e_j != 0 )
            {
              prod2 *= power( arguments[j-1], e_j );
            }
          }
          for(j = i+1; j <= (*curr).key().max_var_code(); j++)
          {
            unsigned int e_j = (*curr).key().expnt_of_var(j);
            if( e_j != 0 )
            {
              prod2 *= power( arguments[j-1], e_j );
            }
          }

          //Multiply with coefficient.
          prod2 *= (*curr).value();   // <---- Multiply with double value.

          //Save enclosure of derivative.
          bound_of_derivatives[ i - 1 ] += prod2;
        }
      }

      ++curr;
    }

    for(unsigned int i = 0; i < bound_of_derivatives.size(); i++)
      if( bound_of_derivatives[i] != Interval(0.0) )
        bound2 += bound_of_derivatives[i] * (((*data_ptr)[i])->domain_ - (((*data_ptr)[i])->domain_).mid() );

    return bound1 & bound2; //Intersection of naive and mean value form evaluation.
  }
コード例 #6
0
void InitSLState( 
    const dTensorBC4& q, const dTensorBC4& aux, SL_state& sl_state )
{

    void ComputeElecField(double t, const dTensor2& node1d,
            const dTensorBC4& qvals, dTensorBC3& Evals);
    void ConvertQ2dToQ1d(const int &mopt, int istart, int iend, 
                     const dTensorBC4& qin, dTensorBC3& qout);
    void IntegrateQ1dMoment1(const dTensorBC4& q2d, dTensorBC3& q1d);

    const int space_order = dogParams.get_space_order();

    const int mx   = q.getsize(1);
    const int my   = q.getsize(2);
    const int meqn = q.getsize(3);
    const int maux = aux.getsize(2);
    const int kmax = q.getsize(4);
    const int mbc  = q.getmbc();
    const int mpoints = space_order*space_order;
    const int kmax1d  = space_order;

    const double tn     = sl_state.tn;

    //////////////////////// Compute Electric Field E(t) ////////////////////
    // save 1d grid points ( this is used for poisson solve )
    dTensorBC3 Enow(mx, meqn, kmax1d, mbc, 1);
    ComputeElecField( tn, *sl_state.node1d, *sl_state.qnew, Enow);

    //////////// Necessary terms for Et = -M1 + g(x,t) /////////////////////
    dTensorBC3 M1(mx, meqn, kmax1d,mbc,1); 
    IntegrateQ1dMoment1(q, M1);  // first moment

    //////////// Necessary terms for Ett = 2*KE_x - rho*E + g_t - psi_u ///////
    dTensorBC3 KE(mx, meqn, kmax1d,mbc,1); 
    void IntegrateQ1dMoment2(const dTensorBC4& q2d, dTensorBC3& q1d);
    IntegrateQ1dMoment2(q, KE);  // 1/2 * \int v^2 * f dv //

    SetBndValues1D( KE );
    SetBndValues1D( Enow );
    SetBndValues1D( M1 );

    // do something to compute KE_x ... //
    dTensorBC3 gradKE(mx,2,kmax1d,mbc,1);
    FiniteDiff( 1, 2, KE, gradKE ); // compute KE_x and KE_xx //

    dTensorBC3 rho(mx,meqn,kmax1d,mbc,1);
    dTensorBC3 prod(mx,meqn,kmax1d,mbc,1);
    void IntegrateQ1d(const int mopt, const dTensorBC4& q2d, dTensorBC3& q1d);
    void MultiplyFunctions(const dTensorBC3& Q1, const dTensorBC3& Q2,
        dTensorBC3& qnew);
    IntegrateQ1d( 1, q, rho );
    MultiplyFunctions( rho, Enow, prod );

    /////////////////////// Save E, Et, Ett, //////////////////////////////////
    for( int i = 1; i <= mx; i++ )
    for( int k = 1; k <= kmax1d; k ++ )
    {
        double E     =  Enow.get  ( i, 1, k );
        double Et    = -M1.get ( i, 1, k );  // + g(x,t)
        double Ett   = -prod.get(i,1,k) + 2.0*gradKE.get(i,1,k); // + others //

        sl_state.aux1d->set(i,2,1, k, E     );
        sl_state.aux1d->set(i,2,2, k, Et    );
        sl_state.aux1d->set(i,2,3, k, Ett   );

    }
    ///////////////////////////////////////////////////////////////////////////

    // terms used for 4th order stuff ... //
    // Ettt = -M3_xx + (2*E_x+rho)*M1 + 3*E*(M1)_x
    dTensorBC3 tmp0(mx,2*meqn,kmax1d,mbc,1);
    dTensorBC3 tmp1(mx,meqn,kmax1d,mbc,1);

    // compute the third moment and its 2nd derivative 
    dTensorBC3 M3    (mx,meqn,kmax1d,mbc,1);
    dTensorBC3 M3_x  (mx,2*meqn,kmax1d,mbc,1);
    void IntegrateQ1dMoment3(const dTensorBC4& q2d, dTensorBC3& q1d);
    IntegrateQ1dMoment3(q, M3 );
    SetBndValues1D( M3 );
    FiniteDiff( 1, 2, M3, M3_x );

    // Compute 2*Ex+rho //
    FiniteDiff(1, 2*meqn, Enow, tmp0);
    for( int i=1; i <= mx; i++ )
    for( int k=1; k <= kmax1d; k++ )
    { tmp1.set(i,1,k, 2.0*tmp0.get(i,1,k) + rho.get(i,1,k) ); }

    // compute (2Ex+rho) * M1
    dTensorBC3 prod1(mx,meqn,kmax1d,mbc,1);
    MultiplyFunctions( tmp1, M1, prod1 );

    // compute M1_x and M1_xx
    dTensorBC3 M1_x  (mx,2*meqn,kmax1d,mbc,1);
    FiniteDiff( 1, 2, M1, M1_x );

    //compute 3*M1_x and E * (3*M1)_x
    dTensorBC3 prod2(mx,meqn,kmax1d,mbc,1);
    for( int i=1; i <= mx; i++ )
    for( int k=1; k <= kmax1d; k++ )
    { tmp1.set(i, 1, k, 3.0*M1_x.get(i, 1, k) ); }
    MultiplyFunctions( Enow, tmp1, prod2 );

    /////////////////////// Save Ettt /////////////////////////////////////////
    for( int i = 1; i <= mx; i++ )
    for( int k = 1; k <= kmax1d; k ++ )
    {
        double Ettt  = -M3_x.get(i,2,k) + prod1.get(i,1,k) + prod2.get(i,1,k);
        sl_state.aux1d->set(i,2,4, k, Ettt );
    }
    ///////////////////////////////////////////////////////////////////////////

    if ( dogParams.get_source_term()>0 )
    {


        double* t_ptr = new double;
        *t_ptr = tn;

        // 2D Electric field, and 1D Electric Field
        dTensorBC4* ExactE;
        ExactE     = new dTensorBC4(mx,1,4,kmax,mbc);

        dTensorBC3* ExactE1d;
        ExactE1d   = new dTensorBC3(mx,4,kmax1d,mbc,1);

        // Extra Source terms needed for the electric field
        dTensorBC4* ExtraE;
        ExtraE     = new dTensorBC4(mx,1,4,kmax,mbc);

        dTensorBC3* ExtraE1d;
        ExtraE1d   = new dTensorBC3(mx,4,kmax1d,mbc,1);


        // Exact, electric field: (TODO - REMOVE THIS!)
//      void ElectricField(const dTensor2& xpts, dTensor2& e, void* data);
//      L2Project_extra(1-mbc, mx+mbc, 1, 1, space_order, -1, ExactE, 
//                      &ElectricField, (void*)t_ptr );
//      ConvertQ2dToQ1d(1, 1, mx, *ExactE, *ExactE1d);

        // Extra Source terms:
        void ExtraSourceWrap(const dTensor2& xpts, 
			     const dTensor2& NOT_USED_1, 
			     const dTensor2& NOT_USED_2,
			     dTensor2& e, 
			     void* data);
        L2Project_extra(1, mx, 1, 1, 20, space_order,
			space_order, space_order,
			&q, &aux, ExtraE, &ExtraSourceWrap, (void*)t_ptr );
        ConvertQ2dToQ1d(1, 1, mx, *ExtraE, *ExtraE1d);
  
        for( int i=1; i <= mx; i++ )
        for( int k=1; k <= kmax1d; k++ )
        {

            // electric fields w/o source term parts added in //
            double Et    = sl_state.aux1d->get(i,2,2,k);
            double Ett   = sl_state.aux1d->get(i,2,3,k);
            double Ettt  = sl_state.aux1d->get(i,2,4,k); 

            // add in missing terms from previously set values //
            sl_state.aux1d->set(i,2,2, k, Et    + ExtraE1d->get(i,2,k) );
            sl_state.aux1d->set(i,2,3, k, Ett   + ExtraE1d->get(i,3,k) );
            sl_state.aux1d->set(i,2,4, k, Ettt  + ExtraE1d->get(i,4,k) );

//          sl_state.aux1d->set(i,2,1, k, 0. );
//          sl_state.aux1d->set(i,2,2, k, 0. );
//          sl_state.aux1d->set(i,2,3, k, 0. );
//          sl_state.aux1d->set(i,2,4, k, 0. );


            // ADD IN EXACT ELECTRIC FIELD HERE:
//          sl_state.aux1d->set(i,2,1, k, ExactE1d->get(i,1,k) );
//          sl_state.aux1d->set(i,2,2, k, ExactE1d->get(i,2,k) );
//          sl_state.aux1d->set(i,2,3, k, ExactE1d->get(i,3,k) );
//          sl_state.aux1d->set(i,2,4, k, ExactE1d->get(i,4,k) );


        }

        delete ExactE;
        delete ExactE1d;
        delete t_ptr;
        delete ExtraE;
        delete ExtraE1d;

    }

    
}