void testSubVector() { VectorXi v = VectorXi::LinSpaced(8,0,7); std::cout << "original vector:" << std::endl; std::cout << v.transpose() << std::endl << std::endl; Matrix<VectorXi::Index,Dynamic,1> row(4); row << 2,3,5,0; SubMatrix<VectorXi, RowPermutation> pv(v,row); std::cout << "subVector:" << std::endl; std::cout << pv.transpose() << std::endl << std::endl; assert(pv[0]==2); assert(pv[3]==0); std::cout << "*****Read access******" << std::endl; std::cout << "coef 2 is " << pv[2] << std::endl; assert(pv[2]==5); std::cout << "*****Operation******" << std::endl; std::cout << "subVector:" << std::endl; VectorXi res = pv.transpose() + Vector4i::Ones().transpose(); std::cout << res << std::endl << std::endl; assert( res[1] == 4 ); Matrix<VectorXi::Index,Dynamic,1> row2(4); row2 << 7,6,1,4; SubMatrix<VectorXi, RowPermutation> pv2(v); pv2.setRowIndices(row2); pv2 = pv; std::cout << pv2.transpose() << std::endl; std::cout << v.transpose() << std::endl; pv.popRowFront(); std::cout << pv.transpose() << std::endl; pv.pushRowFront(6); std::cout << pv.transpose() << std::endl; assert( pv[0] == 3 ); assert( pv[1] == 3 ); }
SparseMatrix<double,0,int> kronecker(SparseMatrix<double,0,int>& A,SparseMatrix<double,0,int>& B) { int Br = B.rows(); int Bc = B.cols(); int Ar = A.rows(); int Ac = A.cols(); SparseMatrix<double,0,int> AB; AB.resize(Ar*Br,Ac*Bc); VectorXi nnzA = VectorXi::Zero(Ac); for (int kA=0; kA < A.outerSize(); ++kA){ for (SparseMatrix<double,0,int>::InnerIterator itA(A,kA); itA; ++itA){ nnzA(itA.col())++; } } VectorXi nnzB = VectorXi::Zero(Bc); for (int kB=0; kB < B.outerSize(); ++kB){ for (SparseMatrix<double,0,int>::InnerIterator itB(B,kB); itB; ++itB){ nnzB(itB.col())++; } } Matrix<int,Dynamic,Dynamic,ColMajor> nnzAB = nnzB * nnzA.transpose(); AB.reserve(VectorXi::Map(nnzAB.data(), nnzAB.size())); for (int kA=0; kA < A.outerSize(); ++kA) { for (int kB=0; kB < B.outerSize(); ++kB) { for (SparseMatrix<double,0,int>::InnerIterator itA(A,kA); itA; ++itA) { for (SparseMatrix<double,0,int>::InnerIterator itB(B,kB); itB; ++itB) { int i = itA.row() * Br + itB.row(); int j = itA.col() * Bc + itB.col(); AB.insert(i,j) = itA.value() * itB.value(); } } } } return AB; }
int main() { VectorXd z(10); z << 7, 4, 2, 8, 1, 3, 6, 0, 9, 5; cout<<"z = ["<< z.transpose() <<"]\n"<<endl; VectorXi p = find( z.array() > 6 ); cout<<"p = z>6 = ["<< p.transpose() << "]\n"<<endl; VectorXd y = pick(z, p); cout<<"z(p) = ["<< y.transpose() <<"]\n"<<endl; z = remove(z, p); cout<<"z(p) = []; z = ["<< z.transpose() <<"]\n"<<endl; VectorXd a(10); a << 10, 20, 30, 40, 50, 60, 70, 80, 90, 100; cout<<"a = ["<< a.transpose() <<"]\n"<<endl; VectorXi ind(3); ind << 0, 4, 5; cout<<"ind = ["<< ind.transpose() <<"]\n"<<endl; cout<<"a(ind) = []; a = ["<< remove(a, ind).transpose() <<"]\n"<<endl; MatrixXd M(3, 3); M << 11, 12, 13, 21, 22, 23, 31, 32, 33; cout<<"\n M = \n"<< M <<endl; VectorXi c(2); c<< 0, 2; cout<<"M(:, [1, 3]) = \n"<< pick_col(M, c) <<endl; cout<<"M([1, 3], :) = \n"<< pick_row(M, c) <<endl; cout<<"\n a = ["<< a.transpose() <<"]\n"<<endl; cout<<"Crash test:"<<endl; try { ind(0) = -1; ind(1) = 0; ind(2) = 3; cout<<"\n ind = ["<< ind.transpose() <<"]"<<endl; cout<<"a(ind) = [];"<<endl; VectorXd b = remove(a, ind); } catch (const std::exception& e) { cout<<"Caught expection: \""<< e.what() <<"\"."<<endl; } try { ind(0) = 3; ind(1) = 2; ind(2) = 1; cout<<"\n ind = ["<< ind.transpose() <<"]"<<endl; cout<<"a(ind) = [];"<<endl; VectorXd b = remove(a, ind); } catch (const std::exception& e) { cout<<"Caught expection: \""<< e.what() <<"\"."<<endl; } set_ind(a, ind, static_cast<VectorXd>(M.row(0))); cout<<"\nind = ["<< ind.transpose() <<"]"<<endl; cout<<"a(ind) = M(1,:); a = ["<< a.transpose() <<"]\n"<<endl; return 0; }