eigen3

How can I calculate inverse of sparse matrix in Eigen library

被刻印的时光 ゝ 提交于 2019-12-03 14:58:12
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. 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 matrix. If there is a solution, X will be your inverse matrix. The eigen documentation has a page about

How to write/read an Eigen matrix from binary file

泄露秘密 提交于 2019-12-03 14:23:48
To write Eigen::Matrix to file I really like to use the following: typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> Matrix_MxN; Matrix_MxN J = Matrix_MxN::Zeros(10,10); std::ofstream("matrix.txt") << J; But unfortunately, something that can do the opposite is not defined: std::ifstream("matrix.txt") >> J; To circumvent this problem, how can you read/write an Eigen::Matrix to binary file instead ? andrea You can define these methods: namespace Eigen{ template<class Matrix> void write_binary(const char* filename, const Matrix& matrix){ std::ofstream out(filename, std::ios::out | std

Eigen::Tensor, how to access matrix from Tensor

对着背影说爱祢 提交于 2019-12-03 13:56:43
问题 I have the following Eigen Tensor: Eigen::Tensor<float, 3> m(3,10,10); I want to access the 1st matrix. In numpy I would do it as such m(0,:,:) How would I do this in Eigen 回答1: You can access parts of a tensor using .slice(...) or .chip(...) . Do this to access the first matrix, equivalent to numpy m(0,:,:) : Eigen::Tensor<double,3> m(3,10,10); //Initialize m.setRandom(); //Set random values Eigen::array<long,3> offset = {0,0,0}; //Starting point Eigen::array<long,3> extent = {1,10,10}; /

Using GDB with Eigen C++ library

别说谁变了你拦得住时间么 提交于 2019-12-03 07:17:48
I am using the Eigen C++ library downloadable from http://eigen.tuxfamily.org/ . This is a C++ library for easier handling of Matrices and Arrays. I use g++ compiler and gdb for debugging. However, I found that I am unable to print the content of a Matrix (provided by Eigen) while using gdb. ggael You have to install a gdb extension that you can find in eigen/debug/gdb/ . The comment at the beginning of the file explains how to install it. One trick you can use is the .data() member, it gives you a pointer to the raw array that contains the data. With that you can print it in GDB like so:

Eigen::Tensor, how to access matrix from Tensor

夙愿已清 提交于 2019-12-03 03:48:16
I have the following Eigen Tensor: Eigen::Tensor<float, 3> m(3,10,10); I want to access the 1st matrix. In numpy I would do it as such m(0,:,:) How would I do this in Eigen You can access parts of a tensor using .slice(...) or .chip(...) . Do this to access the first matrix, equivalent to numpy m(0,:,:) : Eigen::Tensor<double,3> m(3,10,10); //Initialize m.setRandom(); //Set random values Eigen::array<long,3> offset = {0,0,0}; //Starting point Eigen::array<long,3> extent = {1,10,10}; //Finish point std::cout << m.slice(offset, extent).reshape(Eigen::array<long,2>{10,10}) << std::endl; //Reshape

Eigen: replicate items along one dimension without useless allocations

眉间皱痕 提交于 2019-12-02 05:17:07
问题 I have some vector vec and i want to obtain a new "expression" vec2 by copying values along dimension of vector Eigen::VectorXf vec(5); vec << 1, 2, 3, 4, 5; const auto vec2 = vec.someAwesomeEigenMagic<3>(); //vec2 should contains (1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5)^T //Not (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)^T Of course i can create such vector manually or by using replicate + vectorization by Eigen::Map: MatrixXf tmp = vec.replicate(1, 3).transpose(); const Map<VectorXf>

Eigen: replicate items along one dimension without useless allocations

有些话、适合烂在心里 提交于 2019-12-01 23:19:54
I have some vector vec and i want to obtain a new "expression" vec2 by copying values along dimension of vector Eigen::VectorXf vec(5); vec << 1, 2, 3, 4, 5; const auto vec2 = vec.someAwesomeEigenMagic<3>(); //vec2 should contains (1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5)^T //Not (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)^T Of course i can create such vector manually or by using replicate + vectorization by Eigen::Map: MatrixXf tmp = vec.replicate(1, 3).transpose(); const Map<VectorXf> vec2(tmp.data(), vec.rows() * 3, 1); But i want vec2 to be some kind of "eigen template expression"

Eigen::Ref<> as a member variable

笑着哭i 提交于 2019-12-01 01:18:14
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/builds/fit3dceres/RHEL6_AMD64_GCC484_OPT/include/eigen3/Eigen/src/Core/Ref.h: In instantiation of ‘Eigen:

Issue casting C++ Eigen::Matrix types via templates

∥☆過路亽.° 提交于 2019-11-30 21:38:22
I'm writing a C++ function that is templated on type (either float or double ), and uses Eigen::Matrix internally. The function will be using a combination of float , double , and templated type Eigen:Matrix objects. Eigen::Matrix<>::cast() works just fine for double and float , though I'm hitting an odd issue when using it with templated types. See code below: #include "Eigen/Core" // Version 3.2.4 (eigen-eigen-10219c95fe65) template <typename Scalar> void Foo() { Eigen::Matrix<double, 3, 1> mat_d = Eigen::Matrix<double, 3, 1>::Zero(); Eigen::Matrix<float, 3, 1> mat_f = Eigen::Matrix<float, 3

Get matrix views/blocks from a Eigen::VectorXd without copying (shared memory)

拈花ヽ惹草 提交于 2019-11-30 15:58:22
Does anyone know a good way how i can extract blocks from an Eigen::VectorXf that can be interpreted as a specific Eigen::MatrixXf without copying data? (the vector should contains several flatten matrices) e.g. something like that (pseudocode): VectorXd W = VectorXd::Zero(8); // Use data from W and create a matrix view from first four elements Block<2,2> A = W.blockFromIndex(0, 2, 2); // Use data from W and create a matrix view from last four elements Block<2,2> B = W.blockFromIndex(4, 2, 2); // Should also change data in W A(0,0) = 1.0 B(0,0) = 1.0 The purpose is simple to have several