Last active
August 29, 2015 14:09
-
-
Save nfoti/9c14e3f591cd26500335 to your computer and use it in GitHub Desktop.
Eigen broadcasting example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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