Skip to content

Instantly share code, notes, and snippets.

@milhidaka
Created May 5, 2017 02:15
Show Gist options
  • Save milhidaka/f8da85447f5d90883a8908adf17eb803 to your computer and use it in GitHub Desktop.
Save milhidaka/f8da85447f5d90883a8908adf17eb803 to your computer and use it in GitHub Desktop.
matrix multiplication using provided memory area
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
// matrix multiplication using provided memory area
// compile with -std=c++11
int main()
{
const int size = 2;
float* data = new float[size*size]{1.0,3.0,5.0,7.0};
Map<Matrix<float, Dynamic, Dynamic, RowMajor> > m(data, size, size);
cout << m << endl;
// [1 3; 5 7]
float* data2 = new float[size*size]{2.0,3.0,4.0,5.0};
Map<Matrix<float, Dynamic, Dynamic, ColMajor> > m2(data2, size, size);
cout << m2 << endl;
// [2 4; 3 5]
float* data3 = new float[size*size];
Map<Matrix<float, Dynamic, Dynamic, RowMajor> > m3(data3, size, size);
m3.noalias() = m * m2;
cout << m3 << endl;// [11 19; 31 55]
for (int i = 0; i < size*size; i++) {
cout << data3[i] << " ";
}
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment