eigen

Creating an Eigen matrix from an array with row-major order

微笑、不失礼 提交于 2019-12-09 13:05:42
问题 I have an array of doubles, and I want to create a 4-by-4 matrix using the Eigen library. I also want to specify that the data is stored in row-major order. How can I do this? I have tried the following, but it does not compile: double data[16]; Eigen::Matrix4d M = Eigen::Map<Eigen::Matrix4d>(data, 4, 4, Eigen::RowMajor); 回答1: You need to pass a row-major matrix type to Map, e.g.: Map<Matrix<double,4,4,RowMajor> > M(data); then you can use M as an Eigen matrix, and the values of data will be

How can I calculate inverse of sparse matrix in Eigen library

爷,独闯天下 提交于 2019-12-09 12:33:40
问题 I have a question about Eigen library in C++. Actually, I want to calculate inverse matrix of sparse matrix. When I used Dense matrix in Eigen, I can use .inverse() operation to calculate inverse of dense matrix. But in Sparse matrix, I cannot find inverse operation anywhere. Does anyone who know to calculate inverse of sparse matrix? help me. 回答1: You cannot do it directly, but you can always calculate it, using one of the sparse solvers. The idea is to solve A*X=I , where I is the identity

MATLAB find() / Numpy nonzero idioms for Eigen

孤街醉人 提交于 2019-12-09 04:57:14
问题 Chances are this is a very stupid question but I spent a pretty absurd amount of time looking for it on the documentation, to no avail. in MATLAB, the find() function gives me an array with the indices of nonzero elements. Numpy's np.nonzero function does something similar. How do I do this in the C++ Eigen library? I have a Boolean array of typedef <bool, 10, 1> foobar = MatrixA < MatrixB; so far. Thanks! 回答1: It is reasonable to expect Eigen to have a find() function. Unfortunately, Eigen

Fastest way to calculate minimum euclidean distance between two matrices containing high dimensional vectors

谁都会走 提交于 2019-12-09 04:53:35
问题 I started a similar question on another thread, but then I was focusing on how to use OpenCV. Having failed to achieve what I originally wanted, I will ask here exactly what I want. I have two matrices. Matrix a is 2782x128 and Matrix b is 4000x128, both unsigned char values. The values are stored in a single array. For each vector in a, I need the index of the vector in b with the closest euclidean distance. Ok, now my code to achieve this: #include <windows.h> #include <stdlib.h> #include

Eigen::Ref<> as a member variable

不想你离开。 提交于 2019-12-09 01:42:33
问题 I need a class to have an Eigen::Ref variable as a static member which would be initialized through an init static method. Something like this: class CostFunction { public: static Eigen::Ref<Eigen::VectorXd> data; static void init(const Eigen::Ref<Eigen::VectorXd>& d) { data = d; } CostFunction() {} }; int main() { Eigen::VectorXd data = Eigen::VectorXd::Random(30); CostFunction cf; cf.init(data); return 0; } This doesn't compile. I get an error which looks like this: /var/tmp/doNotRemove

Eigen library: return a matrix block in a function as lvalue

ぐ巨炮叔叔 提交于 2019-12-08 21:52:43
问题 I am trying to return a block of a matrix as an lvalue of a function. Let's say my function looks like this: Block<Derived> getBlock(MatrixXd & m, int i, int j, int row, int column) { return m.block(i,j,row,column); } As it turns out, it seems that C++ compiler understands that block() operator gives only temporary value and so returning it as an lvalue is prohibited by the compiler. However, in Eigen documentation there is some example that we can use Eigen as an lvalue (http://eigen

Multiplying Transform and Matrix types in Eigen

余生长醉 提交于 2019-12-08 17:54:28
问题 To me this should just work , so the fact it does not, almost certainly means I am the one in the wrong. Even though in principle a Transform< double, 3, Affine > is the same as a Matrix< double, 4, 4 >, they cannot be used together sensibly: Affine3d rotMat( AngleAxisd( 45.0, ( Vector3d() << 0.0, 1.0, 0.0 ).finished() ) ); Matrix4d m; m << 1.0, 0.0, 0.0, 6.0, 0.0, 1.0, 0.0, 6.0, 0.0, 0.0, 1.0, 6.0, 0.0, 0.0, 0.0, 1.0; m = m * rotMat; Results in a 'no match for operator=' error on the last

Eigen dynamic matrix initialization

天大地大妈咪最大 提交于 2019-12-08 17:36:12
问题 I am having some trouble figuring out how to set the rows and columns of a MatrixXd at runtime in Eigen. Can anyone point me to some documentation or give some pointer on how to do this? Thanks. 回答1: You can define a MatrixXd size at runtime by using the method resize(nrow, ncol) . You can read more about resizing a dynamic matrix in this link and its API definition here. 来源: https://stackoverflow.com/questions/19821144/eigen-dynamic-matrix-initialization

Overriding system defaults for C++ compilation flags from R

房东的猫 提交于 2019-12-08 17:07:46
问题 I'm using RcppEigen to write some C++ functions for my R code, and I'd like to optimize their compilation as much as possible. When I've used Eigen in the past, I've gotten a significant boost from -O3 and -fopenmp. Following Dirk's advice, I edited ~/.R/Makevars so that my Eigen code would be compiled with these flags: CPPFLAGS=-O3 -fopenmp This works--when I check what's happening during compilation (ps ax | grep cpp) I see: 27097 pts/6 R+ 0:00 /usr/libexec/gcc/x86_64-redhat-linux/4.4.7

sort eigen vectorXf in ascending order

拟墨画扇 提交于 2019-12-08 17:02:38
问题 I'm trying to sort an Eigen VectorXf x in ascending order. This sorts it in descending order: std::sort(x.data(),x.data()+x.size()); and this doesn't work: bool myfunction (int i,int j) { return (i<j); } std::sort(x.data(),x.data()+x.size(),myfunction); any ideas? 回答1: Preface Since the original question turned out to be a misunderstanding, and the code in it is already the proper answer, I decided to write up and post a bit about using std::sort in general. std::sort sorts range in ascending