// inserts the sparse matrix 'ins' into the sparse matrix 'original' in the place given by 'row' and 'col' integers void insertSparseBlock(const Eigen::SparseMatrix<Scalar>& ins, Eigen::SparseMatrix<Scalar>& original, const unsigned int& row, const unsigned int& col) { for (int k=0; k<ins.outerSize(); ++k) for (Eigen::SparseMatrix<Scalar>::InnerIterator iti(ins,k); iti; ++iti) original.coeffRef(iti.row() + row, iti.col() + col) = iti.value(); original.makeCompressed(); }
// Test that column indexes of values from sparse matrix in sparse // format are extracted after makeCompressed(). TEST(SparseStuff, csr_extract_v_sparse_compressed) { stan::math::matrix_d m(2, 3); Eigen::SparseMatrix<double, Eigen::RowMajor> a; m << 2.0, 4.0, 6.0, 0.0, 0.0, 0.0; a = m.sparseView(); a.makeCompressed(); std::vector<int> result = stan::math::csr_extract_v(a); EXPECT_EQ(1, result[0]); EXPECT_EQ(2, result[1]); EXPECT_EQ(3, result[2]); EXPECT_EQ(3, result.size()); }
// Test that values from a dense matrix in sparse format are extracted // after A.makeCompressed(); TEST(SparseStuff, csr_extract_w_dense_compressed) { stan::math::matrix_d m(2, 3); Eigen::SparseMatrix<double, Eigen::RowMajor> a; m << 2.0, 4.0, 6.0, 8.0, 10.0, 12.0; a = m.sparseView(); a.makeCompressed(); stan::math::vector_d result = stan::math::csr_extract_w(a); EXPECT_FLOAT_EQ( 2.0, result(0)); EXPECT_FLOAT_EQ( 4.0, result(1)); EXPECT_FLOAT_EQ( 6.0, result(2)); EXPECT_FLOAT_EQ( 8.0, result(3)); EXPECT_FLOAT_EQ(10.0, result(4)); EXPECT_FLOAT_EQ(12.0, result(5)); }
void place::scanToSparse(const cv::Mat &scan, Eigen::SparseMatrix<double> &sparse) { std::vector<Eigen::Triplet<double>> tripletList; for (int i = 0; i < scan.rows; ++i) { const uchar *src = scan.ptr<uchar>(i); for (int j = 0; j < scan.cols; ++j) { if (src[j] == 255) continue; double confidence = 1.0 - (double)src[j] / 255.0; tripletList.push_back(Eigen::Triplet<double>(i, j, confidence)); } } sparse = Eigen::SparseMatrix<double>(scan.rows, scan.cols); sparse.setFromTriplets(tripletList.begin(), tripletList.end()); sparse.makeCompressed(); sparse.prune(1.0); }
Eigen::SparseMatrix<double> joint2conditional(Eigen::SparseMatrix<double> edgePot)// pa is the second dimension { // second dimension of edgePot is the parent Eigen::SparseMatrix<double> Conditional; Conditional.resize(edgePot.rows(), edgePot.cols()); Eigen::SparseVector<double> Parent_Marginal; Parent_Marginal.resize(edgePot.cols()); for (int id_col = 0; id_col < edgePot.cols(); id_col++) { Eigen::SparseVector<double> tmp_vec = edgePot.block(0, id_col, edgePot.rows(), 1); Parent_Marginal.coeffRef(id_col) = tmp_vec.sum(); if (Parent_Marginal.coeff(id_col)>TOLERANCE) for (int id_row = 0; id_row < edgePot.rows(); id_row++) { Conditional.coeffRef(id_row, id_col) = edgePot.coeff(id_row, id_col) / Parent_Marginal.coeff(id_col); } } Conditional.makeCompressed(); Conditional.prune(TOLERANCE); return Conditional; }
IGL_INLINE void igl::arap_linear_block_spokes( const MatV & V, const MatF & F, const int d, Eigen::SparseMatrix<Scalar> & Kd) { using namespace std; using namespace Eigen; // simplex size (3: triangles, 4: tetrahedra) int simplex_size = F.cols(); // Number of elements int m = F.rows(); // Temporary output Matrix<int,Dynamic,2> edges; Kd.resize(V.rows(), V.rows()); vector<Triplet<Scalar> > Kd_IJV; if(simplex_size == 3) { // triangles Kd.reserve(7*V.rows()); Kd_IJV.reserve(7*V.rows()); edges.resize(3,2); edges << 1,2, 2,0, 0,1; }else if(simplex_size == 4) { // tets Kd.reserve(17*V.rows()); Kd_IJV.reserve(17*V.rows()); edges.resize(6,2); edges << 1,2, 2,0, 0,1, 3,0, 3,1, 3,2; } // gather cotangent weights Matrix<Scalar,Dynamic,Dynamic> C; cotmatrix_entries(V,F,C); // should have weights for each edge assert(C.cols() == edges.rows()); // loop over elements for(int i = 0;i<m;i++) { // loop over edges of element for(int e = 0;e<edges.rows();e++) { int source = F(i,edges(e,0)); int dest = F(i,edges(e,1)); double v = 0.5*C(i,e)*(V(source,d)-V(dest,d)); Kd_IJV.push_back(Triplet<Scalar>(source,dest,v)); Kd_IJV.push_back(Triplet<Scalar>(dest,source,-v)); Kd_IJV.push_back(Triplet<Scalar>(source,source,v)); Kd_IJV.push_back(Triplet<Scalar>(dest,dest,-v)); } } Kd.setFromTriplets(Kd_IJV.begin(),Kd_IJV.end()); Kd.makeCompressed(); }