예제 #1
0
real IntegraleDeterministico::gauss()
{
    resetIntegral();

    real omega1 = ((real)128.)/225;
    real omega23 = ((322+13*sqrt((real)70))/900);
    real omega45 = ((322-13*sqrt((real)70))/900);

    real xi23 = ( ((real)1) /3)*sqrt(5-2*sqrt( (real)(10./7) ));
    real xi45 = ( ((real)1) /3)*sqrt(5+2*sqrt( (real)(10./7) ));

    for (int i = 0; i < intervalli(); i++) {

        real c = (x_i(i+1)+x_i(i))/2.;
        real m = (x_i(i+1)-x_i(i))/2.;

        // http://mathworld.wolfram.com/Legendre-GaussQuadrature.html

        add( m*omega1*f_test(c) ); // root = 0

        add( m*omega23*f_test(c - m*xi23) );
        add( m*omega23*f_test(c + m*xi23) );

        add( m*omega45*f_test(c - m*xi45) );
        add( m*omega45*f_test(c + m*xi45) );
    }

    return getIntegral();
}
예제 #2
0
real IntegraleDeterministico::simpson()
{
    resetIntegral();

    for (int i = 0; i < intervalli()-1; i+=2) {
        add( h()/3 * (f_test(x_i(i)) + 4*f_test(x_i(i+1)) + f_test(x_i(i+2)) ) );
    }

    return getIntegral();
}
예제 #3
0
real IntegraleDeterministico::trapezi()
{
    resetIntegral();

    for (int i = 0; i < intervalli(); i++) {
        add( h()/2 * (f_test(x_i(i)) + f_test(x_i(i+1)) ) );
    }

    return getIntegral();
}
예제 #4
0
void CParam::CalculateInitProbA(CData &Data) {
  // initial value of Y_out_compact and z_out
  int count_out = 0;  int count_in = 0;
  ColumnVector z_out_large(toolarge_nout);
  Matrix Y_out_compact_large(toolarge_nout,n_var_independent);
  while ( (count_in < Data.n_sample) && (count_out < toolarge_nout) ) {
    int k = rdiscrete_fn(pi);
    ColumnVector mu_k = Mu.column(k);             // Note that mu_k is Mu.column(k)
    ColumnVector y_compact_i = rMVN_fn( mu_k,LSIGMA[k-1] );
    ColumnVector x_compact_i = exp_ColumnVector(y_compact_i);

    ColumnVector x_i(Data.n_var) ;
    Data.UpdateFullVector(x_compact_i, x_i);
    Data.update_full_x_for_balance_edit(x_i);

    if (Data.PassEdits(x_i)) {
      count_in++;
    } else {
      count_out++;
      Y_out_compact_large.row(count_out) = y_compact_i.t();
      z_out_large(count_out)=k;
    }
  }
  Matrix Y_out_compact = Y_out_compact_large.rows(1,count_out) ;    // cut extra space
  ColumnVector z_out = z_out_large.rows(1,count_out) ;    // cut extra space
  // calculate n_z and Sum_groupX
  Matrix Y_aug_compact = Y_in_compact & Y_out_compact;
  ColumnVector z_aug = z_in & z_out;
  int n_out = z_out.nrows();
  Prob_A =  (1.0 * Data.n_sample / (Data.n_sample+n_out));
}
예제 #5
0
TEST(generateExpression, algebra_solver) {
  static const bool user_facing = true;
  std::stringstream msgs;

  stan::lang::algebra_solver so;  // null ctor should work and not raise error
  std::string system_function_name = "bronzino";
  stan::lang::variable y("y_var_name");
  y.set_type(stan::lang::vector_type());
  stan::lang::variable theta("theta_var_name");
  theta.set_type(stan::lang::vector_type());
  stan::lang::variable x_r("x_r_r_var_name");
  x_r.set_type(stan::lang::bare_array_type(stan::lang::double_type()));
  stan::lang::variable x_i("x_i_var_name");
  x_i.set_type(stan::lang::bare_array_type(stan::lang::int_type()));
  stan::lang::algebra_solver so2(system_function_name, y, theta, x_r, x_i);
  stan::lang::expression e1 = so2;

  generate_expression(e1, user_facing, msgs);
  EXPECT_EQ(msgs.str(),
            "algebra_solver(bronzino_functor__(), y_var_name, "
            "theta_var_name, x_r_r_var_name, x_i_var_name, pstream__)");
}
예제 #6
0
파일: Schur.cpp 프로젝트: marioyc/INF560
void SolveSystem (
        Vector<double, int>& x_local,
        const MatrixDense<double, int>& K_local,
        const Vector<double, int>& b_local,
        const int numb_node_i,
        const int* list_node_i,
        const int* l2i,
        int numb_node_p,
        const int* list_node_p,
        const int* l2p,
        const int numb_global_node,
        const int numb_l2g,
        const int* l2g,
        const MPI_Comm& mpi_comm ) {
  // -- number of processors
  int numb_procs;
  // -- process number (process rank)
  int proc_numb;

  // -- get number of processes
  MPI_Comm_size( mpi_comm, &numb_procs );
  // -- get current process rank
  MPI_Comm_rank( mpi_comm, &proc_numb );


  MatrixDense<double,int> Kii;
  MatrixDense<double,int> Kip;
  MatrixDense<double,int> Kpi;
  MatrixDense<double,int> Kpp;

  Schur::SplitMatrixToBlock(Kii, Kip, Kpi, Kpp, K_local,
                            list_node_i, numb_node_i,
                            list_node_p, numb_node_p);

  // Check transpose
  //CheckTranspose(Kip, Kpi);

  // Reconstruct K
  //std::string gmatrix_filename = "../output/cube-125_2/cube-125_g_2.csv";
  //ReconstructK(K_local, l2g, numb_global_node, gmatrix_filename, mpi_comm);

  // LU factorization of Kii
  MatrixDense<double, int> Kii_lu;
  Factor::LU(Kii_lu, Kii);

  MatrixDense<double, int> Uii_inv,Lii_inv;
  Lii_inv.Allocate(numb_node_i, numb_node_i);
  Uii_inv.Allocate(numb_node_i, numb_node_i);

  Vector<double, int> x,rhs;
  rhs.Allocate(numb_node_i);

  for(int i = 0;i < numb_node_i;++i)
    rhs(i) = 0;

  // invert Lii and Uii
  for(int i = 0;i < numb_node_i;++i){
    if(i > 0) rhs(i - 1) = 0;
    rhs(i) = 1;
    DirectSolver::Forward(x, Kii_lu, rhs);

    for(int j = 0;j < numb_node_i;++j)
      Lii_inv(j,i) = x(j);

    DirectSolver::Backward(x,Kii_lu, rhs);

    for(int j = 0;j < numb_node_i;++j)
      Uii_inv(j,i) = x(j);
  }

  // calculate S_local
  MatrixDense<double, int> Lpi,Uip,prod;
  Kpi.MatrixMatrixProduct(Lpi, Uii_inv);
  Lii_inv.MatrixMatrixProduct(Uip, Kip);
  Lpi.MatrixMatrixProduct(prod, Uip);

  MatrixDense<double, int> S_local;
  Kpp.MatrixMatrixSubstraction(S_local, prod);

  // merge all numb_node_p in one list
  int length_list_p[numb_procs];

  MPI_Allgather(&numb_node_p, 1, MPI_INT, length_list_p, 1, MPI_INT, mpi_comm);

  /*std::stringstream out_length_list;

  for(int i = 0;i < numb_procs;++i){
    out_length_list << length_list_p[i] << " ";
  }
  out_length_list << "\n";*/

  int all_list_global_p[numb_procs][numb_global_node];

  for(int i = 0;i < numb_procs;++i){
    if(proc_numb == i){
      for(int j = 0;j < numb_node_p;++j){
        all_list_global_p[i][j] = l2g[ list_node_p[j] ];
      }
    }

    MPI_Bcast(all_list_global_p[i], length_list_p[i], MPI_INT, i, mpi_comm);
  }

  /*for(int i = 0;i < numb_procs;++i){
    for(int j = 0;j < length_list_p[i];++j){
      out_length_list << all_list_global_p[i][j] << " ";
    }

    out_length_list << "\n";
  }

  iomrg::printf("%s\n", out_length_list.str().c_str());*/

  // create array pos_S : from global position to position in S
  int pos_S[numb_global_node];

  for(int i = 0;i < numb_global_node;++i){
    pos_S[i] = -1;
  }

  std::vector<int> S_indices;

  for(int i = 0;i < numb_procs;++i){
    for(int j = 0;j < length_list_p[i];++j){
      S_indices.push_back(all_list_global_p[i][j]);
    }
  }

  std::sort(S_indices.begin(), S_indices.end());
  S_indices.erase(std::unique(S_indices.begin(), S_indices.end()), S_indices.end());
  int size_S = S_indices.size();

  for(int i = 0;i < size_S;++i){
    pos_S[ S_indices[i] ] = i;
  }

  // Assemble S
  MatrixDense<double, int> S;
  S.Allocate(size_S, size_S);
  S.Initialize(0);

  double aux_coef[size_S];

  iomrg::printf("size_S = %d\n",size_S);

  for(int i = 0;i < numb_procs;++i){
    int size_local_S = length_list_p[i];

    for(int j = 0;j < size_local_S;++j){
      if(proc_numb == i){
        for(int k = 0;k < size_local_S;++k){
          aux_coef[k] = S_local(j,k);
        }
      }

      MPI_Bcast(aux_coef, size_local_S, MPI_DOUBLE, i, mpi_comm);

      int r = pos_S[ all_list_global_p[i][j] ];

      for(int k = 0;k < size_local_S;++k){
        S(r, pos_S[ all_list_global_p[i][k] ]) += aux_coef[k];
      }
    }
  }

  // Separate b_local
  Vector<double, int> b_i,b_p;
  b_i.Allocate(numb_node_i);
  b_p.Allocate(numb_node_p);

  for(int i = 0;i < numb_l2g;++i){
    if(l2p[i] == -1){
      b_i( l2i[i] ) = b_local(i);
    }else{
      b_p( l2p[i] ) = b_local(i);
    }
  }

  // Calculate y_p_local
  Vector<double, int> z;
  DirectSolver::Forward(z, Kii_lu, b_i);

  Vector<double, int> aux_prod;
  Lpi.MatrixVectorProduct(aux_prod, z);
  Vector<double, int> y_p_local;
  y_p_local.Allocate(numb_node_p);

  for(int i = 0;i < numb_node_p;++i){
    y_p_local(i) = b_p(i) - aux_prod(i);
  }

  // Calculate y_p
  Vector<double, int> y_p(size_S);

  for(int i = 0;i < size_S;++i){
    aux_coef[i] = 0;
  }

  for(int i = 0;i < numb_node_p;++i){
    aux_coef[ pos_S[ l2g[ list_node_p[i] ] ] ] += y_p_local(i);
  }

  MPI_Allreduce(aux_coef, y_p.GetCoef(), size_S, MPI_DOUBLE,  MPI_SUM,mpi_comm);

  // Calculate x_p
  Vector<double, int> x_p;
  DirectSolver::SolveLU(x_p, S, y_p);

  // Calculate y_i
  Vector<double, int> aux_prod_2;
  aux_prod_2.Allocate(numb_node_i);
  aux_prod_2.Assign(0, numb_node_i - 1, 0);

  for(int i = 0;i < numb_node_i;++i){
    for(int j = 0;j < numb_node_p;++j){
      int id_S = pos_S[ l2g[ list_node_p[j] ] ];
      aux_prod_2(i) += Uip(i,j) * x_p(id_S);
    }
  }

  Vector<double, int> y_i;
  y_i.Allocate(numb_node_i);

  for(int i = 0;i < numb_node_i;++i){
    y_i(i) = z(i) - aux_prod_2(i);
  }

  // Calculate x_i
  Vector<double, int> x_i;
  DirectSolver::Backward(x_i, Kii_lu, y_i);

  // Assemble x_local
  for(int i = 0;i < numb_node_i;++i){
    x_local(list_node_i[i]) = x_i(i);
  }

  for(int i = 0;i < numb_node_p;++i){
    x_local(list_node_p[i]) = x_p(i);
  }
}