I want to divide each row of a square array(b
) by the transpose of a column array (a
).
Test code is also at godbolt.org, which has an installation of the eigen 3 library.
Expected Result:
// before (a)
2
2
2
// before (b)
2 2 2
4 4 4
6 6 6
// after
1 1 1
2 2 2
3 3 3
Test:
#include <Eigen/Eigen>
#include <iostream>
using namespace Eigen;
int main() {
ArrayXXf a(3, 1);
a << 2, 2, 2;
ArrayXXf b(3, 3);
b << 2, 2, 2, 4, 4, 4, 6, 6, 6;
std::cout << a << "\n";
std::cout << b << "\n";
b.rowwise() /= a.transpose();
std::cout << b << "\n";
}
Compilation Error
...
error: static assertion failed: YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX
...
Related Question:
Eigen: Divide each row by last row
The accepted answer is using rowwise on Eigen matrices converted to Eigen arrays. I just don't get why it doesn't work in my case...
The assertion tells you that you are using a matrix (aka 2D array), whereas a compile-time vector (aka 1D array) is expected. So the solution is to define a
as a compile-time 1D array:
ArrayXf a(3);
You can also see this operation in terms of standard linear algebra:
VectorXd a(3);
MatrixXd b(3,3);
b = b * a.asDiagonal().inverse();