Skip to content

Instantly share code, notes, and snippets.

@nfoti
Last active August 29, 2015 14:09
Show Gist options
  • Save nfoti/9c14e3f591cd26500335 to your computer and use it in GitHub Desktop.
Save nfoti/9c14e3f591cd26500335 to your computer and use it in GitHub Desktop.
Eigen broadcasting example
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
ArrayXXd A(2,4);
A << 1, 2, 6, 9,
3, 1, 7, 2;
cout << "A:\n";
cout << A << endl;
ArrayXd a(4);
a << 0,1,2,3;
// multiply each row of A by a
// MUST have matrix.rowwise() on left and vector.transpose() on right
ArrayXXd res;
res = A.rowwise() * a.transpose();
// This doesn't work, despite being the same mathematically
//res = a.transpose() * A.rowwise();
cout << "Broadcasting result:\n";
cout << res << endl;
return 0;
}
all: test
test: main.cpp
g++-4.9 -std=c++11 -I/usr/local/include/eigen3 -o test eigen_rowwise.cpp
clean:
rm -rf test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment