Ejemplo n.º 1
0
TEST(AgradRevErrorHandlingMatrix, checkNonzeroSizeMatrix) {
  using stan::agrad::var;
  Eigen::Matrix<var,Eigen::Dynamic,Eigen::Dynamic> y;
  using stan::math::check_nonzero_size;
  var result;
  
  y.resize(3,3);
  EXPECT_TRUE(check_nonzero_size("checkNonzeroSize", "y", y));
  y.resize(2, 3);
  EXPECT_TRUE(check_nonzero_size("checkNonzeroSize", "y", y));

  y.resize(0,0);
  EXPECT_THROW_MSG(check_nonzero_size("checkNonzeroSize", "y", y),
                   std::invalid_argument, "y has size 0");

  std::vector<var> a;
  a.push_back(3.0);
  a.push_back(3.0);
  a.push_back(3.0);
  a.push_back(3.0);


  EXPECT_TRUE(stan::math::check_nonzero_size("checkNonzeroSize",
                                                       "a", a));

  a.resize(2);
  EXPECT_TRUE(stan::math::check_nonzero_size("checkNonzeroSize",
                                                       "a", a));

  a.resize(0);
  EXPECT_THROW_MSG(stan::math::check_nonzero_size("checkNonzeroSize", "a", a),
                   std::invalid_argument,
                   "a has size 0");
}
Ejemplo n.º 2
0
TEST(MathErrorHandlingMatrix, checkNonzeroSizeMatrix) {
  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> y;
  double result;
  
  y.resize(3,3);
  EXPECT_TRUE(stan::math::check_nonzero_size("checkNonzeroSize(%1%)",
                                             y, "y", &result));
  y.resize(2, 3);
  EXPECT_TRUE(stan::math::check_nonzero_size("checkNonzeroSize(%1%)",
                                             y, "y", &result));

  y.resize(0,0);
  EXPECT_THROW(stan::math::check_nonzero_size("checkNonzeroSize(%1%)",y, "y", 
                                              &result), 
               std::domain_error);

  std::vector<double> a;
  a.push_back(3.0);
  a.push_back(3.0);
  a.push_back(3.0);
  a.push_back(3.0);


  EXPECT_TRUE(stan::math::check_nonzero_size("checkNonzeroSize(%1%)",
                                             a, "a", &result));

  a.resize(2);
  EXPECT_TRUE(stan::math::check_nonzero_size("checkNonzeroSize(%1%)",
                                             a, "a", &result));

  a.resize(0);
  EXPECT_THROW(stan::math::check_nonzero_size("checkNonzeroSize(%1%)",a, "a", 
                                              &result), 
               std::domain_error);
}
Ejemplo n.º 3
0
 void
 jacobian(const F& f,
          const Eigen::Matrix<double, Dynamic, 1>& x,
          Eigen::Matrix<double, Dynamic, 1>& fx,
          Eigen::Matrix<double, Dynamic, Dynamic>& J) {
   using Eigen::Matrix;
   using stan::agrad::var;
   start_nested();
   try {
     Matrix<var, Dynamic, 1> x_var(x.size());
     for (int k = 0; k < x.size(); ++k)
       x_var(k) = x(k);
     Matrix<var, Dynamic, 1> fx_var = f(x_var);
     fx.resize(fx_var.size());
     for (int i = 0; i < fx_var.size(); ++i)
       fx(i) = fx_var(i).val();
     J.resize(x.size(), fx_var.size());
     for (int i = 0; i < fx_var.size(); ++i) {
       if (i > 0)
         set_zero_all_adjoints();
       grad(fx_var(i).vi_);
       for (int k = 0; k < x.size(); ++k)
         J(k, i) = x_var(k).adj();
     }
   } catch (const std::exception& e) {
     stan::agrad::recover_memory_nested();
     throw;
   }
   stan::agrad::recover_memory_nested();
 }
Ejemplo n.º 4
0
int main() {

	gen.seed(static_cast<unsigned int>(std::time(0)));

	const int beam_size = 3;
	const int vocab_size = 10;
	const int start_symbol = 0;
	const int end_symbol = 1;
	const int max_decoding_length = 30;
	const int min_decoding_length = 10;
	const int source_length = 10;
	const precision penalty = 3;

	decoder<precision> d(beam_size,vocab_size,start_symbol,end_symbol,
		max_decoding_length,min_decoding_length,penalty,"");

	Eigen::Matrix<precision,Eigen::Dynamic, Eigen::Dynamic> outputDist;
	Eigen::Matrix<precision, 1, Eigen::Dynamic> normalization;
	outputDist.resize(vocab_size,beam_size);
	normalization.resize(1,beam_size);

	init_output_dist(outputDist,normalization);

	d.init_decoder();

	for(int i=0; i<20; i++) {
		init_output_dist(outputDist,normalization);
		d.expand_hypothesis(outputDist);
		d.print_current_hypotheses();
	}
	d.finish_current_hypotheses(outputDist);
	d.print_current_hypotheses();
}
Ejemplo n.º 5
0
 void
 hessian(const F& f,
         const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
         double& fx,
         Eigen::Matrix<double, Eigen::Dynamic, 1>& grad,
         Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& H) {
   H.resize(x.size(), x.size());
   grad.resize(x.size());
   try {
     for (int i = 0; i < x.size(); ++i) {
       start_nested();
       Eigen::Matrix<fvar<var>, Eigen::Dynamic, 1> x_fvar(x.size());
       for (int j = 0; j < x.size(); ++j)
         x_fvar(j) = fvar<var>(x(j), i == j);
       fvar<var> fx_fvar = f(x_fvar);
       grad(i) = fx_fvar.d_.val();
       if (i == 0) fx = fx_fvar.val_.val();
       stan::math::grad(fx_fvar.d_.vi_);
       for (int j = 0; j < x.size(); ++j)
         H(i, j) = x_fvar(j).val_.adj();
       stan::math::recover_memory_nested();
     }
   } catch (const std::exception& e) {
     stan::math::recover_memory_nested();
     throw;
   }
 }
Ejemplo n.º 6
0
IGL_INLINE bool igl::png::texture_from_png(
  const std::string png_file,
  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B,
  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& A
)
{
  int width,height,n;
  unsigned char *data = stbi_load(png_file.c_str(), &width, &height, &n, 4);
  if(data == NULL) {
    return false;
  }

  R.resize(height,width);
  G.resize(height,width);
  B.resize(height,width);
  A.resize(height,width);

  for (unsigned j=0; j<height; ++j) {
    for (unsigned i=0; i<width; ++i) {
      // used to flip with libPNG, but I'm not sure if
      // simply j*width + i wouldn't be better
      // stb_image uses horizontal scanline an starts top-left corner
      R(i,j) = data[4*( (width-1-i) + width * (height-1-j) )];
      G(i,j) = data[4*( (width-1-i) + width * (height-1-j) ) + 1];
      B(i,j) = data[4*( (width-1-i) + width * (height-1-j) ) + 2];
      //A(i,j) = data[4*( (width-1-i) + width * (height-1-j) ) + 3];
    }
  }

  stbi_image_free(data);

  return true;
}
Ejemplo n.º 7
0
IGL_INLINE void igl::mat_min(
  const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & X,
  const int dim,
  Eigen::Matrix<T,Eigen::Dynamic,1> & Y,
  Eigen::Matrix<int,Eigen::Dynamic,1> & I)
{
  assert(dim==1||dim==2);

  // output size
  int n = (dim==1?X.cols():X.rows());
  // resize output
  Y.resize(n);
  I.resize(n);

  // loop over dimension opposite of dim
  for(int j = 0;j<n;j++)
  {
    typename Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>::Index PHONY;
    typename Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>::Index i;
    T m;
    if(dim==1)
    {
      m = X.col(j).minCoeff(&i,&PHONY);
    }else
    {
      m = X.row(j).minCoeff(&PHONY,&i);
    }
    Y(j) = m;
    I(j) = i;
  }
}
Ejemplo n.º 8
0
    Tvector<T> Tmatrix<T>::solve( const Tvector<T>& b, const std::string method ) const
    {
	    assert( ROWS == b.size() ); // Check dimensions are compatible
	    // Convert Tvector to an Eigen matrix
	    Eigen::Matrix<T, -1, 1> B;
	    B.resize(b.size(),1);
	    for (std::size_t i=0; i<b.size(); ++i)
	    {
		    B(i,0) = b.CONTAINER[i];
	    }
	    // Solve the system
	    Eigen::Matrix<T, -1, 1> X;
	    X.resize(b.size(),1);
        assert( method == "LU" || method == "QR" || method == "PartialLU" );
        if ( method == "LU" )
        {
            X = MATRIX.fullPivLu().solve(B);
        }
        if ( method == "PartialLU" )
        {
            X = MATRIX.partialPivLu().solve(B);
        }
        if ( method == "QR" )
        {
            X = MATRIX.fullPivHouseholderQr().solve(B);
        }
	    
	    // Convert back to a Tvector 
	    Tvector<T> x(COLS,0.0);
	    for (std::size_t i=0; i<COLS; ++i)
	    {
		    x.CONTAINER[i] = X(i,0);
	    }	
	    return x;
    }  
Ejemplo n.º 9
0
int main(){

  ifstream myfile;
  myfile.open("../ex1data1.txt");

  vector<double> vX{};
  vector<double> vy{};

  double d1, d2;
  char c;

  if(myfile.is_open()){
    while(myfile>>d1>>c>>d2){
      vX.push_back(d1);
      vy.push_back(d2);
    }
  }

  int m = vy.size();

  cout<<"Running Gradient Descent ...\n";

  Eigen::Matrix<double,Eigen::Dynamic,2> X;
  Eigen::Matrix<double,Eigen::Dynamic,1> y;

  X.resize(m,2);
  y.resize(m,1);

  for(int i = 0; i < m; ++i){
    X(i,0) = 1;
    X(i,1) = vX[i];
    y(i) = vy[i];
  }

  Eigen::Matrix<double, Eigen::Dynamic, 1> theta = Eigen::MatrixXd::Zero(2,1);

  // Some gradient descent settings
  int iterations = 1500;
  double alpha = 0.01;

  //  gradientDescent(X, y, theta, alpha, iterations);
  gradientDescent(X, y, theta, alpha, iterations);

  cout<<"Theta found by gradient descent: "<<endl;
  cout<<theta<<endl<<endl;

  Eigen::Matrix<double, 1, Eigen::Dynamic> predVec;
  predVec.resize(1,theta.rows());
  predVec<<1, 3.5;
  double predict1 = predVec*theta;

  predVec(1) = 7;
  double predict2 = predVec*theta;

  cout<<"For population = 35,000, we predict a profit of "<<predict1*10000<<endl;
  cout<<"For population = 70,000, we predict a profit of "<<predict2*10000<<endl<<endl;
  return 0;
}
Ejemplo n.º 10
0
/**
 * Convert from a depth image to 3D camera space coordinates
 * @param depth_image A width x height array of uint16_t depth values
 * @param vertices A width x height array of Vector3f representing the vertices in the depth image in camera space
 * @param normals A width x height array of Vector3f representing the vertices in the depth image in camera space
 */
void Camera::depth_image_to_vertices_and_normals(const uint16_t * depth_image, const uint32_t width, const uint32_t height, Eigen::Matrix<float, 3, Eigen::Dynamic>& vertices, Eigen::Matrix<float, 3, Eigen::Dynamic>& normals ) const {
    using namespace Eigen;

    // Allocate storage for vertx and norms
    vertices.resize( 3, width * height );
    normals.resize( 3, width * height );

    // Run from bottom right corner so we can create normal sin the same pass
    int32_t src_idx = (width * height) - 1;
    for( int16_t y=height-1; y>=0; y-- ) {
        for( int16_t x=width-1; x >= 0; x-- ) {

            // Vertex and normal for this pixel
            Vector3f vertex;
            Vector3f normal { 0, 0, 0 };

            // If this is a valid depth
            uint16_t depth = depth_image[src_idx];
            if( depth != 0 ) {

                // Back project the point into camera 3D space using D(x,y) * Kinv * (x,y,1)T
                Vector2f cam_point = pixel_to_image_plane( x, y );
                vertex = Vector3f { cam_point.x(), cam_point.y(), 1.0f } * depth;

                // Compute normal as v(y,x+1)-v(y,x) cross v(y+1, x)-v(y, x )
                if( (y < static_cast<int16_t>(height - 1 )) && ( x < static_cast<int16_t>(width - 1 ) ) ) {
                    // We have adjacent vertex to right and below that we can extract
                    // Vector[0] is the element to the right of this one
                    // Vector[width] is the element below
                    Vector3f right_neighbour { vertices(0, src_idx + 1),     vertices(1, src_idx + 1),     vertices(2, src_idx + 1) };
                    Vector3f below_neighbour { vertices(0, src_idx + width), vertices(1, src_idx + width), vertices(2, src_idx + width) };

                    // If they are both not BAD
                    if( ( right_neighbour != BAD_VERTEX) && ( below_neighbour != BAD_VERTEX ) ) {
                        right_neighbour -= vertex;
                        below_neighbour -= vertex;

                        // Compute cross product for normal
                        normal = right_neighbour.cross( below_neighbour ).normalized();
                    }
                }
            } else {
                vertex = BAD_VERTEX;
            }

            // Store
            for( int i=0; i<3; i++ ) {
                vertices(i, src_idx) = vertex[i];
                normals(i, src_idx) = normal[i];
            }

            src_idx--;
        }
    }
}
Ejemplo n.º 11
0
TEST(ErrorHandlingMatrix, checkSquareMatrix) {
  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> y;
  
  y.resize(3,3);
  EXPECT_TRUE(stan::math::check_square("checkSquareMatrix",
                                                 "y", y));

  y.resize(3, 2);
  EXPECT_THROW(stan::math::check_square("checkSquareMatrix", "y", y), 
               std::invalid_argument);
}
Ejemplo n.º 12
0
    Tvector<T> Tsparse_matrix<T>::solve( const Tvector<T>& b ) const
    {
        
        assert( ROWS == b.size() );                                 // Check dimensions are compatible
	    // Convert Tvector to an Eigen matrix
	    Eigen::Matrix<T, -1, 1> B;
	    B.resize(b.size(),1);
	    for (std::size_t i=0; i<b.size(); ++i)
	    {
		    B(i,0) = b.CONTAINER[i];
	    }
        
        // Convert Tsparse_matrix to an Eigen::SparseMatrix
        Eigen::SparseMatrix<T> A(ROWS,COLS);  // Declare Eigen sparse matrix
        std::vector<Eigen::Triplet<T>> triplet_list;                // Declare list of triplets 
        for ( std::size_t i=0; i<ROWS; ++i )
        {
            if ( !S_MATRIX[i].isempty() )                           // Check that the row is not empty 
            {   
                std::vector<std::size_t> index;
                std::vector<T> element;
                index = S_MATRIX[i].index_list();
                element = S_MATRIX[i].element_list();
                std::size_t J = index.size();
                for ( std::size_t j=0; j<J; ++j )
                {
                    triplet_list.push_back( Eigen::Triplet<T>( i, index[j], element[j] ));
                }
            }
        } 
        A.setFromTriplets(triplet_list.begin(), triplet_list.end());
        
        // Setup and solve the system
        Eigen::SparseLU< Eigen::SparseMatrix<T> > solverA;
		//Eigen::BiCGSTAB<Eigen::SparseMatrix<T>> solverA;
		//solverA.analyzePattern(A);
		//solverA.factorize(A);
        solverA.compute(A);

        Eigen::Matrix<T, -1, 1> X;
	    X.resize(b.size(),1);
        X = solverA.solve(B);
        
        // Convert back to a Tvector 
	    Tvector<T> x(COLS,0.0);
	    for (std::size_t i=0; i<COLS; ++i)
	    {
		    x.CONTAINER[i] = X(i,0);
	    }	
	    return x;
    }   
Ejemplo n.º 13
0
TEST(ErrorHandlingMatrix, checkSquareMatrix_nan) {
  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> y;
  double nan = std::numeric_limits<double>::quiet_NaN();

  y.resize(3,3);
  y << nan, nan, nan,nan, nan, nan,nan, nan, nan;
  EXPECT_TRUE(stan::math::check_square("checkSquareMatrix",
                                                 "y", y));

  y.resize(3, 2);
  y << nan, nan, nan,nan, nan, nan;
  EXPECT_THROW(stan::math::check_square("checkSquareMatrix", "y", y), 
               std::invalid_argument);
}
Ejemplo n.º 14
0
TEST(ErrorHandlingMatrix, checkVectorMatrix) {
  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> x;
  
  x.resize(3,3);
  EXPECT_THROW(stan::math::check_vector("checkVector", "x", x),
               std::invalid_argument);
  x.resize(0,0);
  EXPECT_THROW(stan::math::check_vector("checkVector", "x", x),
               std::invalid_argument);

  x.resize(1,5);
  EXPECT_TRUE(stan::math::check_vector("checkVector", "x", x));

  x.resize(5,1);
  EXPECT_TRUE(stan::math::check_vector("checkVector", "x", x));
}
Ejemplo n.º 15
0
 static inline void _fast_vector_copy(Eigen::Matrix<T, Eigen::Dynamic, 1>& v_to, const Eigen::Matrix<T, Eigen::Dynamic, 1>& v_from) {
   int sz = v_from.size();
   v_to.resize(sz);
   if (sz > 0) {
     std::memcpy(&v_to(0), &v_from(0), v_from.size() * sizeof(double));
   }
 }
Ejemplo n.º 16
0
SEXP
vectorToMatVCL(SEXP A, const int nr, const int nc, int device_flag)
{
    Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> Am;
    Am = Rcpp::as<Eigen::Matrix<T, Eigen::Dynamic, 1> >(A);
//    Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> temp = as<Eigen::Matrix<T, Eigen::Dynamic, 1> >(A);
    Am.resize(nr, nc);

//    Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > Amm(&Am(0), nr, nc);

    dynVCLMat<T> *mat = new dynVCLMat<T>(Am, nr, nc, device_flag);
    Rcpp::XPtr<dynVCLMat<T> > pMat(mat);
    return pMat;

//    Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > Amm(&Am(0), nr, nc);
//
//    //use only GPUs:
//    if(device_flag == 0){
//        long id = 0;
//        viennacl::ocl::set_context_device_type(id, viennacl::ocl::gpu_tag());
//    }
//
//    viennacl::matrix<T> *vcl_A = new viennacl::matrix<T>(nr, nc);
//
//    viennacl::copy(Amm, *vcl_A);
//
//    Rcpp::XPtr<viennacl::matrix<T> > pMat(vcl_A);
//    return pMat;
}
Ejemplo n.º 17
0
 void
 hessian_times_vector(const F& f,
                      const Eigen::Matrix<double, Dynamic, 1>& x,
                      const Eigen::Matrix<double, Dynamic, 1>& v,
                      double& fx,
                      Eigen::Matrix<double, Dynamic, 1>& Hv) {
   using stan::math::fvar;
   using stan::math::var;
   using Eigen::Matrix;
   start_nested();
   try {
     Matrix<var, Dynamic, 1> x_var(x.size());
     for (int i = 0; i < x_var.size(); ++i)
       x_var(i) = x(i);
     var fx_var;
     var grad_fx_var_dot_v;
     gradient_dot_vector(f, x_var, v, fx_var, grad_fx_var_dot_v);
     fx = fx_var.val();
     stan::math::grad(grad_fx_var_dot_v.vi_);
     Hv.resize(x.size());
     for (int i = 0; i < x.size(); ++i)
       Hv(i) = x_var(i).adj();
   } catch (const std::exception& e) {
     stan::math::recover_memory_nested();
     throw;
   }
   stan::math::recover_memory_nested();
 }
Ejemplo n.º 18
0
template <typename PointT, typename Scalar> void
pcl::demeanPointCloud (const pcl::PointCloud<PointT> &cloud_in,
                       const std::vector<int> &indices,
                       const Eigen::Matrix<Scalar, 4, 1> &centroid,
                       pcl::PointCloud<PointT> &cloud_out)
{
  cloud_out.header = cloud_in.header;
  cloud_out.is_dense = cloud_in.is_dense;
  if (indices.size () == cloud_in.points.size ())
  {
    cloud_out.width    = cloud_in.width;
    cloud_out.height   = cloud_in.height;
  }
  else
  {
    cloud_out.width    = static_cast<uint32_t> (indices.size ());
    cloud_out.height   = 1;
  }
  cloud_out.resize (indices.size ());

  // Subtract the centroid from cloud_in
  for (size_t i = 0; i < indices.size (); ++i)
  {
    cloud_out[i].x = cloud_in[indices[i]].x - centroid[0];
    cloud_out[i].y = cloud_in[indices[i]].y - centroid[1];
    cloud_out[i].z = cloud_in[indices[i]].z - centroid[2];
  }
}
Ejemplo n.º 19
0
TEST(ErrorHandlingMatrix, checkSquareMatrix_0x0) {
  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> y;
  
  y.resize(0,0);
  EXPECT_TRUE(stan::math::check_square("checkSquareMatrix",
                                                 "y", y));
}
Ejemplo n.º 20
0
      static bool
      read_header(std::istream& in,
                  Eigen::Matrix<std::string, Eigen::Dynamic, 1>& header) {
        std::string line;

        if (in.peek() != 'l')
          return false;

        std::getline(in, line);
        std::stringstream ss(line);

        header.resize(std::count(line.begin(), line.end(), ',') + 1);
        int idx = 0;
        while (ss.good()) {
          std::string token;
          std::getline(ss, token, ',');
          boost::trim(token);

          int pos = token.find('.');
          if (pos > 0) {
            token.replace(pos, 1, "[");
            std::replace(token.begin(), token.end(), '.', ',');
            token += "]";
          }
          header(idx++) = token;
        }
        return true;
      }
Ejemplo n.º 21
0
template <typename PointT, typename Scalar> void
pcl::demeanPointCloud (ConstCloudIterator<PointT> &cloud_iterator,
                       const Eigen::Matrix<Scalar, 4, 1> &centroid,
                       pcl::PointCloud<PointT> &cloud_out,
                       int npts)
{
  // Calculate the number of points if not given
  if (npts == 0)
  {
    while (cloud_iterator.isValid ())
    {
      ++npts;
      ++cloud_iterator;
    }
    cloud_iterator.reset ();
  }

  int i = 0;
  cloud_out.resize (npts);
  // Subtract the centroid from cloud_in
  while (cloud_iterator.isValid ())
  {
    cloud_out[i].x = cloud_iterator->x - centroid[0];
    cloud_out[i].y = cloud_iterator->y - centroid[1];
    cloud_out[i].z = cloud_iterator->z - centroid[2];
    ++i;
    ++cloud_iterator;
  }
  cloud_out.width = cloud_out.size ();
  cloud_out.height = 1;
}
void cv2eigen( const Mat& src,
               Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
{
    dst.resize(src.rows, src.cols);
    if( !(dst.Flags & Eigen::RowMajorBit) )
    {
        Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
             dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
        if( src.type() == _dst.type() )
            transpose(src, _dst);
        else if( src.cols == src.rows )
        {
            src.convertTo(_dst, _dst.type());
            transpose(_dst, _dst);
        }
        else
            Mat(src.t()).convertTo(_dst, _dst.type());
        CV_DbgAssert(_dst.data == (uchar*)dst.data());
    }
    else
    {
        Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
                 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
        src.convertTo(_dst, _dst.type());
        CV_DbgAssert(_dst.data == (uchar*)dst.data());
    }
}
Ejemplo n.º 23
0
template<typename _Tp>  static inline
void cv2eigen( const Mat& src,
               Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
{
    dst.resize(src.rows, src.cols);
    if( !(dst.Flags & Eigen::RowMajorBit) )
    {
        const Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
             dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
        if( src.type() == _dst.type() )
            transpose(src, _dst);
        else if( src.cols == src.rows )
        {
            src.convertTo(_dst, _dst.type());
            transpose(_dst, _dst);
        }
        else
            Mat(src.t()).convertTo(_dst, _dst.type());
    }
    else
    {
        const Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
                 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
        src.convertTo(_dst, _dst.type());
    }
}
Ejemplo n.º 24
0
IGL_INLINE void igl::slice_into(
  const Eigen::PlainObjectBase<DerivedX> & X,
  const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  Eigen::PlainObjectBase<DerivedX> & Y)
{

  int xm = X.rows();
  int xn = X.cols();
  assert(R.size() == xm);
  assert(C.size() == xn);
#ifndef NDEBUG
  int ym = Y.size();
  int yn = Y.size();
  assert(R.minCoeff() >= 0);
  assert(R.maxCoeff() < ym);
  assert(C.minCoeff() >= 0);
  assert(C.maxCoeff() < yn);
#endif

  // Build reindexing maps for columns and rows, -1 means not in map
  Eigen::Matrix<int,Eigen::Dynamic,1> RI;
  RI.resize(xm);
  for(int i = 0;i<xm;i++)
  {
    for(int j = 0;j<xn;j++)
    {
      Y(R(i),C(j)) = X(i,j);
    }
  }
}
Ejemplo n.º 25
0
TEST(MathErrorHandlingMatrix, checkVectorMatrix) {
  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> x;
  double result;
  
  x.resize(3,3);
  EXPECT_THROW(stan::math::check_vector("checkVector(%1%)",x,"x", &result),
               std::domain_error);
  x.resize(0,0);
  EXPECT_THROW(stan::math::check_vector("checkVector(%1%)",x,"x", &result),
               std::domain_error);

  x.resize(1,5);
  EXPECT_TRUE(stan::math::check_vector("checkVector(%1%)",x,"x", &result));

  x.resize(5,1);
  EXPECT_TRUE(stan::math::check_vector("checkVector(%1%)",x,"x", &result));
}
Ejemplo n.º 26
0
Archivo: colon.cpp Proyecto: azer89/BBW
IGL_INLINE void igl::colon(
  const L low, 
  const S step, 
  const H hi, 
  Eigen::Matrix<T,Eigen::Dynamic,1> & I)
{
  if(low < hi)
  {
    if(step < 0)
    {
      I.resize(0);
      //fprintf(stderr,"WARNING: colon() low(%g)<hi(%g) but step(%g)<0\n",
      //  (double)low,
      //  (double)hi,
      //  (double)step);
      //assert(false && "low<hi but step<0");
      return;
    }
  }
  if(low > hi)
  {
    if(step > 0)
    {
      I.resize(0);
      //fprintf(stderr,"Error: colon() low(%g)>hi(%g) but step(%g)>0\n",
      //  (double)low,
      //  (double)hi,
      //  (double)step);
      //assert(false && "low>hi but step<0");
      return;
    }
  }
  // resize output
  int n = floor(double((hi-low)/step))+1;
  I.resize(n);
  int i = 0;
  T v = (T)low;
  while((low<hi && (H)v<=hi) || (low>hi && (H)v>=hi))
  {
    I(i) = v;
    v = v + (T)step;
    i++;
  }
  assert(i==n);
}
Ejemplo n.º 27
0
 static inline void _fast_matrix_copy(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& v_to,
                               const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& v_from) {
   int nr = v_from.rows();
   int nc = v_from.cols();
   v_to.resize(nr, nc);
   if (nr > 0 && nc > 0) {
     std::memcpy(&v_to(0), &v_from(0), v_from.size() * sizeof(double));
   }
 }
Ejemplo n.º 28
0
TEST(ErrorHandlingMatrix, checkSymmetric_nan) {
  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> y;
  double nan = std::numeric_limits<double>::quiet_NaN();

  y.resize(2,2);
  y << 1, nan, 3, 1;
  EXPECT_THROW(stan::math::check_symmetric("checkSymmetric",
                                                     "y", y),
               std::domain_error);
  y << nan, 3, 3, 1;
  EXPECT_TRUE(stan::math::check_symmetric("checkSymmetric",
                                                    "y", y));

  y.resize(1,1);
  y << nan;
  EXPECT_TRUE(stan::math::check_symmetric("checkSymmetric",
                                                    "y", y));
}
Ejemplo n.º 29
0
 inline void assign(Eigen::Matrix<T, R, C>& x,
                    const nil_index_list& /* idxs */,
                    const Eigen::Matrix<U, R, C>& y,
                    const char* name = "ANON",
                    int depth = 0) {
   x.resize(y.rows(), y.cols());
   for (size_t i = 0; i < y.size(); ++i)
     assign(x(i), nil_index_list(), y(i), name, depth + 1);
 }
Ejemplo n.º 30
0
TEST(ErrorHandlingMatrix, checkSymmetric_non_square) {
  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> y;
  
  y.resize(2,3);
  EXPECT_THROW(stan::math::check_symmetric("checkSymmetric",
                                                     "y", y),
               std::invalid_argument);

}