コード例 #1
0
ファイル: TestsEigenHelper.cpp プロジェクト: javang/mlearning
TEST(TestEigenHelper, TestSelectColumns) {
  unsigned int rows = 4;
  unsigned int cls = 5;
  MatrixXi v(rows, cls);
  v << 1, 2, 3, 4, 5,
       6, 7, 8, 9, 10,
       11, 12, 13, 14, 15,
       16, 17, 18, 19, 20;
  std::vector<int> cols = {0,1,4};
  MatrixXi z = select_columns<int, Ints >(v, cols);
  EXPECT_EQ(v.col(0), z.col(0));
  EXPECT_EQ(v.col(1), z.col(1));
  EXPECT_EQ(v.col(4), z.col(2));

  MatrixXd q = v.cast<double>() / 2;
  Ints more_cols = {1,0,2};
  MatrixXd t = select_columns<double, Ints >(q, more_cols);
  for(unsigned int i = 0; i < rows; i++) {
    EXPECT_DOUBLE_EQ(q(i, 1), t(i, 0));
    EXPECT_DOUBLE_EQ(q(i, 0), t(i, 1));
    EXPECT_DOUBLE_EQ(q(i, 2), t(i, 2));
  }

}
コード例 #2
0
// function used internally, which computes lasso fits for subsets containing a
// small number of observations (typically only 3) and returns the indices of
// the respective h observations with the smallest absolute residuals
MatrixXi sparseSubsets(const MatrixXd& x, const VectorXd& y,
                       const double& lambda, const int& h, const MatrixXi& subsets,
                       const bool& normalize, const bool& useIntercept,
                       const double& eps, const bool& useGram) {
    const int nsamp = subsets.cols();
    MatrixXi indices(h, nsamp);
    for(int k = 0; k < nsamp; k++) {
        // compute lasso fit
        double intercept, crit;
        VectorXd coefficients, residuals;
        fastLasso(x, y, lambda, true, subsets.col(k), normalize, useIntercept,
                  eps, useGram, false, intercept, coefficients, residuals, crit);
        // find h observations with smallest absolute residuals
        indices.col(k) = findSmallest(residuals.cwiseAbs(), h);
    }
    return indices;
}
コード例 #3
0
ファイル: parameters.cpp プロジェクト: rforge/clustericat
parameters::parameters(datafile dat, model nv_mod, model ref_mod,parameters ref_param,int compo,int iter){
	const MatrixXi & omega=nv_mod.Get_model(),ref_omega=ref_mod.Get_model(),mat=dat.Get_mat_datafile();
	const int g=omega.rows(),unique=mat.rows();
	m_proba=ref_param.m_proba;
	m_proba_block.resize(g);
	m_param.resize(g);
	for (int k=0;k<g;k++){
		if (k!=compo){
			m_param[k].resize(omega.rowwise().maxCoeff()(k)+1);
			m_param[k]=ref_param.m_param[k];
			m_proba_block[k].resize(unique,omega.rowwise().maxCoeff()(k)+1);
			m_proba_block[k]=ref_param.m_proba_block[k];
			for (int b=0;b<(omega.rowwise().maxCoeff()(k)+1) ;b++){
				if ((omega.row(k).array()==b).any()){
					m_param[k][b]=ref_param.m_param[k][b];
				}
			}
		}else{
			m_param[k].resize(omega.rowwise().maxCoeff()(k)+1);
			m_proba_block[k].resize(unique,omega.rowwise().maxCoeff()(k)+1);
			for (int b=0;b<(omega.rowwise().maxCoeff()(k)+1) ;b++){
				if ((omega.row(k).array()==b).any()){
					if ((((omega.row(k).array()==b)==(ref_omega.row(k).array()==b)).prod())==1){
						m_param[k][b]=ref_param.m_param[k][b];
					}else{
						m_param[k][b]=param_block(k,b,dat,nv_mod,m_proba.col(k).array()/m_proba.rowwise().sum().array(),1);
						if ((omega.row(k).array()==b).count()>1){
							int prem=0;
							while(omega(k,prem)!=b){prem++;}
							if (mat.col(prem).maxCoeff()>5){
									m_param[k][b]=m_param[k][b].Optimise_gamma(k,b,dat,nv_mod,5,m_proba.col(k).array()/m_proba.rowwise().sum().array(),dat.Get_eff_datafile());
							}
						}
					}
				}
			}
		}

	}
	m_propor=uniforme(g);
	Probapost( nv_mod , mat );
	Compte_nbparam(dat,nv_mod);
	Likelihood(dat.Get_eff_datafile());
	Estimation(1,0,iter,dat,nv_mod);
}
コード例 #4
0
int
NMS::nms3x3 (MatrixXd &im_, MatrixXd &om_, vector<int> &id_om_)
{
  //Sizes
  int h_ = im_.rows();
  int w_ = im_.cols();

  lmn_ = 0;
  om_.fill (0); // binary output image
  MatrixXi skip (h_,2);
  skip.fill (0);
  int cur = 0;
  int next = 1;
  int r, tmp;
  
  for (int c=1; c<w_-1; c++)
  {
    r = 1;
    while (r<h_-1)
    {
      if (skip(r,cur))
      {
        // skip current pixel
        r=r+1;
        continue;
      }
      
      if (im_(r,c) <= im_(r+1,c)) // compare to pixel on the left
      {
        r=r+1;
        while ((r<h_-1) && (im_(r,c) <= im_(r+1,c)))
          r=r+1; //rising
        
        if (r==h_-1)
          break; // reach scanline's local maximum
      }
      else // compare to pixel on the right
      {
        if (im_(r,c) <= im_(r-1,c))
        {
          r=r+1;
          continue;
        }
      }
            
      skip(r+1,cur) = 1; // skip next pixel in the scanline
      
      //compare to 3 future then 3 past neighbors
      if (im_(r,c) <= im_(r-1,c+1))
      {
        r=r+1;
        continue;
      }
      skip(r-1,next) = 1; // skip future neighbors only
                
      if (im_(r,c) <= im_(r ,c+1))
      {
        r=r+1;
        continue;
      }
      skip(r ,next) = 1;
      
      if (im_(r,c) <= im_(r+1,c+1))
      {
        r=r+1;
        continue;
      }
      skip(r+1,next) = 1;
      
      if (im_(r,c) <= im_(r-1,c-1))
      {
        r=r+1;
        continue;
      }
      
      if (im_(r,c) <= im_(r ,c-1))
      {
        r=r+1;
        continue;
      }
        
      if (im_(r,c) <= im_(r+1,c-1))
      {
        r=r+1;
        continue;
      }
      // a new local maximum is found
      om_(r,c) = 1;
      id_om_.push_back (r * im_.cols () + c);
      lmn_++;
      r=r+1;
    } 
    
    // swap mask indices
    tmp = cur;
    cur = next;
    next = tmp;

    // reset next scanline mask
    skip.col(next).fill(0);
  }

  return (lmn_);
}
コード例 #5
0
ファイル: selfintersect.cpp プロジェクト: vibe007/gptoolbox
void mexFunction(int nlhs, mxArray *plhs[], 
    int nrhs, const mxArray *prhs[])
{
  // This is useful for debugging whether Matlab is caching the mex binary
  //mexPrintf("%s %s\n",__TIME__,__DATE__);
  igl::matlab::MexStream mout;
  std::streambuf *outbuf = std::cout.rdbuf(&mout);

#else
int main(int argc, char * argv[])
{
#endif
  using namespace std;
  using namespace Eigen;
  using namespace igl;
  using namespace igl::matlab;
  using namespace igl::cgal;

  MatrixXd V;
  MatrixXi F;
  igl::cgal::RemeshSelfIntersectionsParam params;

  string prefix;
  bool use_obj_format = false;
#ifdef MEX
  if(nrhs < 2)
  {
    mexErrMsgTxt("nrhs < 2");
  }
  parse_rhs_double(prhs,V);
  parse_rhs_index(prhs+1,F);
  mexErrMsgTxt(V.cols()==3,"V must be #V by 3");
  mexErrMsgTxt(F.cols()==3,"F must be #F by 3");

  if(nrhs>2)
  {
    int i = 2;
    while(i<nrhs)
    {
      if(!mxIsChar(prhs[i]))
      {
        mexErrMsgTxt("Parameter names should be char strings");
      }
      // Cast to char
      const char * name = mxArrayToString(prhs[i]);
      if(strcmp("DetectOnly",name) == 0)
      {
        validate_arg_scalar(i,nrhs,prhs,name);
        validate_arg_logical(i,nrhs,prhs,name);
        mxLogical * v = (mxLogical *)mxGetData(prhs[++i]);
        params.detect_only = *v;
      }else if(strcmp("FirstOnly",name) == 0)
      {
        validate_arg_scalar(i,nrhs,prhs,name);
        validate_arg_logical(i,nrhs,prhs,name);
        mxLogical * v = (mxLogical *)mxGetData(prhs[++i]);
        params.first_only = *v;
      }else
      {
        mexErrMsgTxt(C_STR("Unsupported parameter: "<<name));
      }
      i++;
    }
  }
#else
  if(argc <= 1)
  {
    cerr<<"Usage:"<<endl<<"  selfintersect [path to .off/.obj mesh] "
      "[0 or 1 for detect only]"<<endl;
    return 1;
  }
  // Apparently CGAL doesn't have a good data structure triangle soup. Their
  // own examples use (V,F):
  // http://www.cgal.org/Manual/latest/doc_html/cgal_manual/AABB_tree/Chapter_main.html#Subsection_64.3.7
  //
  // Load mesh
  string filename(argv[1]);
  if(!read_triangle_mesh(filename,V,F))
  {
    //cout<<REDRUM("Reading "<<filename<<" failed.")<<endl;
    return false;
  }
  cout<<GREENGIN("Read "<<filename<<" successfully.")<<endl;
  {
    // dirname, basename, extension and filename
    string dirname,b,ext;
    pathinfo(filename,dirname,b,ext,prefix);
    prefix = dirname + "/" + prefix;
    transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
    use_obj_format = ext == "obj";
  }
  if(argc>2)
  {
    //http://stackoverflow.com/a/9748431/148668
    char *p;
    long d = strtol(argv[2], &p, 10);
    if (errno != 0 || *p != '\0')
    {
      cerr<<"detect only param should be 0 or 1"<<endl;
    }else
    {
      params.detect_only = d!=0;
    }
  }
#endif
  MatrixXi IF;
  VectorXi J,IM;
  if(F.rows()>0)
  {
    // Check that there aren't any combinatorially or geometrically degenerate triangles
    VectorXd A;
    doublearea(V,F,A);
    if(A.minCoeff()<=0)
    {
#ifdef MEX
      mexErrMsgTxt("Geometrically degenerate face found.");
#else
      cerr<<"Geometrically degenerate face found."<<endl;
      return 1;
#endif
    }
    VectorXi F12,F23,F31;
    F12 = F.col(0)-F.col(1);
    F23 = F.col(1)-F.col(2);
    F31 = F.col(2)-F.col(0);
    if(
      F12.minCoeff() == 0 || 
      F23.minCoeff() == 0 || 
      F31.minCoeff() == 0)
    {
#ifdef MEX
      mexErrMsgTxt("Combinatorially degenerate face found.");
#else
      cerr<<"Geometrically degenerate face found."<<endl;
      return 1;
#endif
    }

    // Now mesh self intersections
    {
      MatrixXd tempV;
      MatrixXi tempF;
      remesh_self_intersections(V,F,params,tempV,tempF,IF,J,IM);
      //cout<<BLUEGIN("Found and meshed "<<IF.rows()<<" pair"<<(IF.rows()==1?"":"s")
      //  <<" of self-intersecting triangles.")<<endl;
      V=tempV;
      F=tempF;
#ifndef MEX
      cout<<"writing pair list to "<<(prefix+"-IF.dmat")<<endl;
      writeDMAT((prefix+"-IF.dmat").c_str(),IF);
      cout<<"writing map to F list to "<<(prefix+"-J.dmat")<<endl;
      writeDMAT((prefix+"-J.dmat").c_str(),J);
      cout<<"writing duplicat index map to "<<(prefix+"-IM.dmat")<<endl;
      writeDMAT((prefix+"-IM.dmat").c_str(),IM);
      if(!params.detect_only)
      {
        if(use_obj_format)
        {
          cout<<"writing mesh to "<<(prefix+"-selfintersect.obj")<<endl;
          writeOBJ(prefix+"-selfintersect.obj",V,F);
        }else
        {
          cout<<"writing mesh to "<<(prefix+"-selfintersect.off")<<endl;
          writeOFF(prefix+"-selfintersect.off",V,F);
        }
      }
#endif
    }

  // Double-check output

#ifdef DEBUG
    // There should be *no* combinatorial duplicates
    {
      MatrixXi tempF;
      unique_simplices(F,tempF);
      if(tempF.rows() < F.rows())
      {
        cout<<REDRUM("Error: selfintersect created "<<
          F.rows()-tempF.rows()<<" combinatorially duplicate faces")<<endl;
      }else
      {
        assert(tempF.rows() == F.rows());
        cout<<GREENGIN("selfintersect created no duplicate faces")<<endl;
      }
      F = tempF;
    }
#endif
  }

#ifdef MEX
  // Prepare left-hand side
  switch(nlhs)
  {
    case 5:
    {
      // Treat indices as reals
      plhs[4] = mxCreateDoubleMatrix(IM.rows(),IM.cols(), mxREAL);
      double * IMp = mxGetPr(plhs[4]);
      VectorXd IMd = (IM.cast<double>().array()+1).matrix();
      copy(&IMd.data()[0],&IMd.data()[0]+IMd.size(),IMp);
      // Fallthrough
    }
    case 4:
    {
      // Treat indices as reals
      plhs[3] = mxCreateDoubleMatrix(J.rows(),J.cols(), mxREAL);
      double * Jp = mxGetPr(plhs[3]);
      VectorXd Jd = (J.cast<double>().array()+1).matrix();
      copy(&Jd.data()[0],&Jd.data()[0]+Jd.size(),Jp);
      // Fallthrough
    }
    case 3:
    {
      // Treat indices as reals
      plhs[2] = mxCreateDoubleMatrix(IF.rows(),IF.cols(), mxREAL);
      double * IFp = mxGetPr(plhs[2]);
      MatrixXd IFd = (IF.cast<double>().array()+1).matrix();
      copy(&IFd.data()[0],&IFd.data()[0]+IFd.size(),IFp);
      // Fallthrough
    }
    case 2:
    {
      // Treat indices as reals
      plhs[1] = mxCreateDoubleMatrix(F.rows(),F.cols(), mxREAL);
      double * Fp = mxGetPr(plhs[1]);
      MatrixXd Fd = (F.cast<double>().array()+1).matrix();
      copy(&Fd.data()[0],&Fd.data()[0]+Fd.size(),Fp);
      // Fallthrough
    }
    case 1:
    {
      plhs[0] = mxCreateDoubleMatrix(V.rows(),V.cols(), mxREAL);
      double * Vp = mxGetPr(plhs[0]);
      copy(&V.data()[0],&V.data()[0]+V.size(),Vp);
      break;
    }
    default:break;
  }

  // Restore the std stream buffer Important!
  std::cout.rdbuf(outbuf);

#else
  return 0;
#endif
}