Matrix: Remark: The Created Matrices Are Square Matrices. (Indicated by The Number: 2, 3, 4)
Matrix: Remark: The Created Matrices Are Square Matrices. (Indicated by The Number: 2, 3, 4)
Matrix
Sometimes, we do not know the size during compile time, we may have dynamic size.
MatrixXd A; VectorXi a
Matrix Constructor
There are no initializations for the matrix, but just allocating the memories!
MatrixXf A(10, 15); VectorXf b(30);
Initialization
Remark: We can use X.rows() and X.cols() to assess the size of a matrix X.
Besides, we may use X.resize(r,c) to modify the size.
Remark: One may use X.conservativeResize(r,c) to maintain ALL the previous values.
Arithmetic
A + B =
MatrixXf A(2, 2);
0 -1
MatrixXf B(2, 2);
5 4
A << 1, 2,
3, 4; A * 2 =
2, 0; 6 8
We can define a complex-valued matrix using MatrixXcd. The operations can be computed as
follows.
a.transpose(); a.conjugate(); a.adjoint()
See example.
Array
Array Initialization
Array Arithmetic
ArrayXf v(6);
v << 1, 2, 3, 4, 5, 6;
cout << v.head(3) << endl << endl; // 1, 2, 3
cout << v.tail<3>() << endl << endl; // 4, 5, 6
v.segment(1, 4) *= 2;
cout << v << endl; // 1, 4, 6, 8, 10, 6
Fixed Dynamic
First n elements v.head(n) v.head<n>()
VectorXd v(3);
v << 1, 2, 3;
VectorXd v2(2);
v2 << 4, 5;
VectorXd joint(5);
joint << v, v2; // Join two vectors
cout << joint;
𝒍𝒍𝒑𝒑 norm
See example.
VectorXf v(2);
v << -1,
2;
a << 1, 2,
3, 4;
One can find the location for some characteristics by the Index type.
m << 1, 2,
3, 4;
cout << "Max: " << max << ", at: " << // Max 4 at (1,1)
maxRow << "," << maxCol << endl;
cout << "Min: " << min << ", at: " << // Min 1 at (0,0)
minRow << "," << minCol << endl;
If one wants to perform operators in row-wise/ col-wise manner, we may use the code .colwise()
or .rowwise().
Vector2d v;
v << 0,
1;
mat.colwise() += v; //add v to each column of m
int array[8];
for (int i = 0; i < 8; ++i) array[i] = i;
cout << Map<Matrix<int, 2, 4> >(array) << '\n'; // Column major
cout << Map<Matrix<int, 2, 4, RowMajor> >(array) << '\n'; // Row major
cout << Map<Matrix<int, 2, 4>, Unaligned, Stride<1, 4> >(array) << '\n';
Aliasing problem
Aliasing problem occurs when the same matrix appears on both sides of an assignment.
Component-wise operations are probably alright.
Matrix multiplication allows aliasing problem by default.
Matrix3f A;
Vector3f b;
A << 1, 2, 3, 4, 5, 6, 7, 8, 10;
b << 3, 3, 4;
Vector3f x = A.colPivHouseholderQr().solve(b);
One can check the relative error of the computation in order to see if a solution exists.
Remark: To find a matrix determinant and its inverse, we may use the following.
A.determinant(); A.inverse();
Least-squared Solutions
// SUV
cout << A.jacobiSvd(ComputeThinU | ComputeThinV).solve(b) << '\n';
// QR
cout << A.colPivHouseholderQr().solve(b) << '\n';
// normal equation
cout << (A.transpose() * A).ldlt().solve(A.transpose() * b) << '\n';
LU decomposition
See an example.
cout << "The Cholesky factor L is" << '\n' << L << '\n';
cout << "To check this, let us compute L * L.transpose()" << '\n';
cout << L * L.transpose() << '\n';
cout << "This should equal the matrix A" << '\n';