Here are some examples of matrix operations in Eigen:
1. Summation
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main() {
MatrixXd A(2, 2);
A << 1, 2,
3, 4;
MatrixXd B(2, 2);
B << 5, 6,
7, 8;
// Matrix summation
MatrixXd C = A + B;
cout << "C = " << endl << C << endl; // Output: C = 6 8
// 10 12
return 0;
}
2. Subtraction:
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main() {
MatrixXd A(2, 2);
A << 1, 2,
3, 4;
MatrixXd B(2, 2);
B << 5, 6,
7, 8;
// Matrix subtraction
MatrixXd C = A - B;
cout << "C = " << endl << C << endl; // Output: C = -4 -4
// -4 -4
return 0;
}
3. Inner product:
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main() {
VectorXd a(2);
a << 1, 2;
VectorXd b(2);
b << 3, 4;
// Inner product
double inner = a.dot(b);
cout << "inner = " << inner << endl; // Output: inner = 11
return 0;
}
4. Cross product:
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main() {
Vector3d a(1, 2, 3);
Vector3d b(4, 5, 6);
// Cross product
Vector3d c = a.cross(b);
cout << "c = " << c << endl; // Output: c = [-3, 6, -3]
return 0;
}
#Eigen3 #InnerProduct #CrossProduct #Summation of Matrix #Substration of Matrix
Leave a Reply