/* probability density function */
double MultivariateFNormalSufficient::density() const
  {
      double d;
      if (N_ == 1)
      {
          d=get_norms()[0]*get_jacobian()
              *exp(-0.5*get_mean_square_residuals()/SQUARE(factor_));
      } else {
          d = get_norms()[0]*get_jacobian()
          *exp(-0.5*(trace_WP()
                     + N_ * get_mean_square_residuals())/SQUARE(factor_));
      }
      LOG( "MVN: density() = " << d << std::endl);
      return d;
  }
  /* probability density function */
double MultivariateFNormalSufficient::density() const
  {
      timer_.start(EVAL);
      double d;
      if (N_ == 1)
      {
          d=get_norms()[0]*get_jacobian()
              *exp(-0.5*get_mean_square_residuals()/IMP::square(factor_));
      } else {
          d = get_norms()[0]*get_jacobian()
          *exp(-0.5*(trace_WP()
                     + N_ * get_mean_square_residuals())/IMP::square(factor_));
      }
      IMP_LOG(TERSE, "MVN: density() = " << d << std::endl);
      timer_.stop(EVAL);
      return d;
  }
    virtual void run(Voxel& voxel, VoxelData& data)
    {
        image::interpolation<image::linear_weighting,3> trilinear_interpolation;
        image::vector<3,double> pos(image::pixel_index<3>(data.voxel_index,des_geo)),Jpos;
        pos[0] *= scale[0];
        pos[1] *= scale[1];
        pos[2] *= scale[2];
        pos += des_offset;
        mni.warp_coordinate(pos,Jpos);
        affine(Jpos.begin());
        if(!trilinear_interpolation.get_location(src_geo,Jpos))
        {
            std::fill(data.odf.begin(),data.odf.end(),0);
            return;
        }
        for (unsigned int i = 0; i < data.space.size(); ++i)
            trilinear_interpolation.estimate(ptr_images[i],data.space[i]);

        if(voxel.half_sphere && b0_index != -1)
            data.space[b0_index] /= 2.0;

        float jacobian[9];
        get_jacobian(pos,jacobian);


        if (voxel.odf_deconvolusion || voxel.odf_decomposition)
            odf_sharpening(voxel,data,jacobian);
        else
        {
            std::vector<float> sinc_ql(data.odf.size()*voxel.q_count);
            for (unsigned int j = 0,index = 0; j < data.odf.size(); ++j)
            {
                image::vector<3,double> dir(voxel.ti.vertices[j]),from;
                math::matrix_product(jacobian,dir.begin(),from.begin(),math::dim<3,3>(),math::dim<3,1>());
                from.normalize();
                if(voxel.r2_weighted)
                    for (unsigned int i = 0; i < voxel.q_count; ++i,++index)
                        sinc_ql[index] = r2_base_function(q_vectors_time[i]*from);
                else
                    for (unsigned int i = 0; i < voxel.q_count; ++i,++index)
                        sinc_ql[index] = boost::math::sinc_pi(q_vectors_time[i]*from);

            }
            math::matrix_vector_product(&*sinc_ql.begin(),&*data.space.begin(),&*data.odf.begin(),
                                        math::dyndim(data.odf.size(),data.space.size()));
        }

        jdet[data.voxel_index] = std::abs(math::matrix_determinant(jacobian,math::dim<3,3>())*voxel_volume_scale);
        std::for_each(data.odf.begin(),data.odf.end(),boost::lambda::_1 *= jdet[data.voxel_index]);
        float accumulated_qa = std::accumulate(data.odf.begin(),data.odf.end(),0.0);
        if (max_accumulated_qa < accumulated_qa)
        {
            max_accumulated_qa = accumulated_qa;
            max_odf = data.odf;
        }
    }
Example #4
0
int RefMap::calc_inv_ref_order()
{
  Quad2D* quad = get_quad_2d();
  int i, o, mo = quad->get_max_order();

  // check first the positivity of the jacobian
  double3* pt = quad->get_points(mo);
  double2x2* m = get_inv_ref_map(mo);
  double* jac = get_jacobian(mo);
  for (i = 0; i < quad->get_num_points(mo); i++)
    if (jac[i] <= 0.0)
      error("Element #%d is concave or badly oriented.", element->id);

  // next, estimate the "exact" value of the typical integral int_grad_u_grad_v
  // (with grad_u == grad_v == (1,1)) using the maximum integration rule
  double exact1 = 0.0;
  double exact2 = 0.0;
  for (i = 0; i < quad->get_num_points(mo); i++, m++)
  {
    exact1 += pt[i][2] * jac[i] * (sqr((*m)[0][0] + (*m)[0][1]) + sqr((*m)[1][0] + (*m)[1][1]));
    exact2 += pt[i][2] / jac[i];
  }
  // find sufficient quadrature degree
  for (o = 0; o < mo; o++)
  {
    pt = quad->get_points(o);
    m = get_inv_ref_map(o);
    jac = get_jacobian(o);
    double result1 = 0.0;
    double result2 = 0.0;
    for (i = 0; i < quad->get_num_points(o); i++, m++)
    {
      result1 += pt[i][2] * jac[i] * (sqr((*m)[0][0] + (*m)[0][1]) + sqr((*m)[1][0] + (*m)[1][1]));
      result2 += pt[i][2] / jac[i] ;
    }
    if ((fabs((exact1 - result1) / exact1) < 1e-8) &&
        (fabs((exact2 - result2) / exact2) < 1e-8)) break;
  }
  if (o >= 10) {
    warn("Element #%d is too distorted (iro ~ %d).", element->id, o);
  }
  return o;
}
/*
 * Function: get_det_jacobian
 *
 * Parameters:
 * @param i         - 
 * @param j         - 
 * @param drzcoeffs - the drizzle coefficients
 * @param width     - 
 * @param height    - 
 *
 * Returns:
 * @return ret - 
 */
double
get_det_jacobian(int i, int j, gsl_matrix * drzcoeffs, int width, int height){

  gsl_matrix * jacob;
  double ret;

  jacob = get_jacobian(i,j,drzcoeffs,width,height);

  ret = gsl_matrix_get(jacob, 0, 0) * gsl_matrix_get(jacob, 1, 1) 
       -gsl_matrix_get(jacob, 0, 1) * gsl_matrix_get(jacob, 1, 0);

  gsl_matrix_free(jacob);

  return ret;
}