Questions tagged [eigen]

Eigen is a C++ template library for linear algebra: matrices, vectors, and related algorithms.

Eigen is a C++ template library for linear algebra: matrices, numerical solvers, and related algorithms.

Despite its name, this tag should not be mistaken with the and ones.

Eigen covers a large set of features through an easy to use unified API. Here is quick and incomplete overview:

  • Dense algebra:
    • array/matrix manipulation
    • linear solvers and factorizations (Cholesky, LU, QR, SVD, EVD)
  • Sparse algebra:
    • compressed sparse representation
    • direct solvers and factorizations (Cholesky, LU, QR)
    • Krylov subspace methods (CG, BiCGSTAB, GMRES) with preconditioners (Jacobi, ILLT, ILUT)
  • Geometry:
    • space transformation and subspace primitives

Find more on Eigen's home page and its online documentation.

3438 questions
17
votes
2 answers

Efficient weighted covariances in RcppEigen

I am trying to produce a function that can compute a series of weighted products where W is a diagonal matrix. There are many W matrices but only a single X matrix. To be efficient I can represent W as an array (w) containing the diagonal part.…
JCWong
  • 1,163
  • 1
  • 10
  • 29
17
votes
7 answers

Eigen - Re-orthogonalization of Rotation Matrix

After multiplying a lot of rotation matrices, the end result might not be a valid rotation matrix any more, due to rounding issues (de-orthogonalized) One way to re-orthogonalize is to follow these steps: Convert the rotation matrix to an…
dim_tz
  • 1,451
  • 5
  • 20
  • 37
17
votes
2 answers

Eigen: Is there an inbuilt way to calculate sample covariance

I am using the Eigen library in C++: I am currently calculating the covariance matrix myself as follows: Eigen::MatrixXd covariance_matrix = Eigen::MatrixXd::Constant(21, 21, 0); data mean = calc_mean(all_data) for(int j = 0; j < 21; j++){ …
Aly
  • 15,865
  • 47
  • 119
  • 191
17
votes
2 answers

How to compare vectors approximately in Eigen?

Is there a function in Eigen to compare vectors (matrices) using both relative and absolute tolerance aka numpy.allclose? Standard isApprox fails if one of the vectors is very close to zero.
DikobrAz
  • 3,557
  • 4
  • 35
  • 53
16
votes
3 answers

How to access a specific (row,col) index in an C++ Eigen sparse matrix?

I'm working in C++ with a sparse matrix in Eigen. I would like to read the data stored in a specific row and column index just like I would with a regular eigen matrix. std::vector> tripletList; // TODO: populate triplet list…
MattKelly
  • 411
  • 1
  • 5
  • 11
16
votes
2 answers

How should I initialize the contents of a large matrix in Eigen?

I am trying to initialize a matrix (using the Eigen library) to have a nonzero value when I create it. Is there a nice way to do this without a for loop? For example, if I wanted to initialize the whole matrix to 1.0, I would like to do something…
andyras
  • 15,542
  • 6
  • 55
  • 77
16
votes
6 answers

How to extract a subvector (of an Eigen::Vector) from a vector of indices in Eigen?

Suppose I have Eigen::VectorXd x; //{1,2,3,4,5,6,7,8} and Eigen::VectorXd ind_vec; //{0,2,4,5} Is there a way an easy way to extract the ind_vec elements of x? Something like: x.extract(ind_vec) returning {1, 3, 5, 6}
user1526533
  • 441
  • 1
  • 5
  • 9
16
votes
1 answer

How do I compute the absolute value of a vector in Eigen?

How do I compute the absolute value of a vector in Eigen? Since the obvious way Eigen::VectorXf v(-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0); v.abs(); // Compute abs value. does not work.
Reed Richards
  • 4,178
  • 8
  • 41
  • 55
16
votes
2 answers

Is it possible to initialize a const Eigen matrix?

I have the following class: class Foo { public: Foo(double a, double b, double c, double d, double e) // This does not work: // : m_bar(a, b, c, d, e) { m_bar << a, b, c, d, e; } private: // How can I make this const? …
Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
16
votes
1 answer

call C++ using Eigen Library function in python

I'm doing some calculations in C++ with help of Eigen Library, the function is like this: MatrixXd Cov(MatrixXd Data) { VectorXd meanVector; ... return Covariance; } ..in the wrap python function: static PyObject *Wrap_Cov(PyObject…
Ysée
  • 173
  • 1
  • 4
16
votes
1 answer

How to convert row vector to column vector in Eigen?

The documentation says: ... in Eigen, vectors are just a special case of matrices, with either 1 row or 1 column. The case where they have 1 column is the most common; such vectors are called column-vectors, often abbreviated as just vectors.…
Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146
15
votes
4 answers

Copy select rows into new matrix

I want to copy the rows 0, 2 and 4 of my matrix A into B, in this order. Let A = [a0, a1, a2, a3, a4]^T , with a_i being row-vectors, then B should be: [a0, a2, a4]^T. The code below does what I want but I wonder whether there is a prettier…
Unapiedra
  • 15,037
  • 12
  • 64
  • 93
15
votes
1 answer

how to concatenate Vectors in Eigen?

I have two vectorXd in my program and I like to concatenate them into one vector, so that the second one's values goes after the first one, I found this for matrix but it doesn't seem to work on Vectors: Eigen how to concatenate matrix along a…
user3178756
  • 555
  • 1
  • 5
  • 17
15
votes
5 answers

Most efficient way to loop through an Eigen matrix

I'm creating some functions to do things like the "separated sum" of negative and positive number, kahan, pairwise and other stuff where it doesn't matter the order I take the elements from the matrix, for example: template
random_user
  • 820
  • 1
  • 7
  • 18
15
votes
4 answers

Submatrices and indices using Eigen

I'm currently working on a MATLAB project and I'd like to re-implement the most computational-heavy parts using C++ and Eigen. I'd like to know if there's a way to perform the following operation (MATLAB syntax): B = A(A < 3); For those who are not…
Ilio Catallo
  • 3,152
  • 2
  • 22
  • 40