c++矩陣運算庫Eigen
Read-write access to acolumnor arowof a matrix (or array):
mat1.row(i) = mat2.col(j);mat1.col(j1).swap(mat1.col(j2));Read-write access to sub-vectors:
Default versions | Optimized versions when the sizeis known at compile time | |
---|---|---|
vec1.head(n) | vec1.head<n>() | the firstn coeffs |
vec1.tail(n) | vec1.tail<n>() | the lastn coeffs |
vec1.segment(pos,n) | vec1.segment<n>(pos) | then coeffs in therange [pos :pos +n - 1] |
Read-write access to sub-matrices: |
||
mat1.block(i,j,rows,cols)(more) | mat1.block<rows,cols>(i,j)(more) | therows xcols sub-matrixstarting from position (i ,j ) |
mat1.topLeftCorner(rows,cols)mat1.topRightCorner(rows,cols)mat1.bottomLeftCorner(rows,cols)mat1.bottomRightCorner(rows,cols) | mat1.topLeftCorner<rows,cols>()mat1.topRightCorner<rows,cols>()mat1.bottomLeftCorner<rows,cols>()mat1.bottomRightCorner<rows,cols>() | therows xcols sub-matrixtaken in one of the four corners |
mat1.topRows(rows)mat1.bottomRows(rows)mat1.leftCols(cols)mat1.rightCols(cols) | mat1.topRows<rows>()mat1.bottomRows<rows>()mat1.leftCols<cols>()mat1.rightCols<cols>() | specialized versions of block()when the block fit two corners |
top
Miscellaneous operationsReverseVectors, rows, and/or columns of a matrix can be reversed (seeDenseBase::reverse(),DenseBase::reverseInPlace(),VectorwiseOp::reverse()).
vec.reverse() mat.colwise().reverse() mat.rowwise().reverse()vec.reverseInPlace()ReplicateVectors, matrices, rows, and/or columns can be replicated in any direction (seeDenseBase::replicate(),VectorwiseOp::replicate())
vec.replicate(times) vec.replicate<Times>mat.replicate(vertical_times, horizontal_times) mat.replicate<VerticalTimes, HorizontalTimes>()mat.colwise().replicate(vertical_times, horizontal_times) mat.colwise().replicate<VerticalTimes, HorizontalTimes>()mat.rowwise().replicate(vertical_times, horizontal_times) mat.rowwise().replicate<VerticalTimes, HorizontalTimes>()top
Diagonal, Triangular, and Self-adjoint matrices
(matrix world*)
Diagonal matricesOperation | Code |
---|---|
view a vectoras a diagonal matrix | mat1 = vec1.asDiagonal(); |
Declare a diagonal matrix | DiagonalMatrix<Scalar,SizeAtCompileTime> diag1(size);diag1.diagonal() = vector; |
Access thediagonalandsuper/sub diagonalsof a matrix as a vector (read/write) | vec1 = mat1.diagonal(); mat1.diagonal() = vec1; // main diagonalvec1 = mat1.diagonal(+n); mat1.diagonal(+n) = vec1; // n-th super diagonalvec1 = mat1.diagonal(-n); mat1.diagonal(-n) = vec1; // n-th sub diagonalvec1 = mat1.diagonal<1>(); mat1.diagonal<1>() = vec1; // first super diagonalvec1 = mat1.diagonal<-2>(); mat1.diagonal<-2>() = vec1; // second sub diagonal |
Optimized products and inverse | mat3 = scalar * diag1 * mat1;mat3 += scalar * mat1 * vec1.asDiagonal();mat3 = vec1.asDiagonal().inverse() * mat1mat3 = mat1 * diag1.inverse() |
Triangular views
TriangularViewgives a view on a triangular part of a dense matrix and allows to perform optimized operations on it. The opposite triangular part is never referenced and can be used to store other information.
- Note
- The .triangularView() template member function requires the
template
keyword if it is used on an object of a type that depends on a template parameter; seeThe template and typename keywords in C++for details.
Operation | Code |
---|---|
Reference to a triangular with optionalunit or null diagonal (read/write): | m.triangularView<Xxx>()Xxx =Upper,Lower,StrictlyUpper,StrictlyLower,UnitUpper,UnitLower |
Writing to a specific triangular part:(only the referenced triangular part is evaluated) | m1.triangularView<Eigen::Lower>() = m2 + m3 |
Conversion to a dense matrix setting the opposite triangular part to zero: | m2 = m1.triangularView<Eigen::UnitUpper>() |
Products: | m3 += s1 * m1.adjoint().triangularView<Eigen::UnitUpper>() * m2m3 -= s1 * m2.conjugate() * m1.adjoint().triangularView<Eigen::Lower>() |
Solving linear equations: | L1.triangularView<Eigen::UnitLower>().solveInPlace(M2)L1.triangularView<Eigen::Lower>().adjoint().solveInPlace(M3)U1.triangularView<Eigen::Upper>().solveInPlace<OnTheRight>(M4) |
Symmetric/selfadjoint views
Just as for triangular matrix, you can reference any triangular part of a square matrix to see it as a selfadjoint matrix and perform special and optimized operations. Again the opposite triangular part is never referenced and can be used to store other information.
- Note
- The .selfadjointView() template member function requires the
template
keyword if it is used on an object of a type that depends on a template parameter; seeThe template and typename keywords in C++for details.
Operation | Code |
---|---|
Conversion to a dense matrix: | m2 = m.selfadjointView<Eigen::Lower>(); |
Product with another general matrix or vector: | m3 = s1 * m1.conjugate().selfadjointView<Eigen::Upper>() * m3;m3 -= s1 * m3.adjoint() * m1.selfadjointView<Eigen::Lower>(); |
Rank 1 and rank K update: | M1.selfadjointView<Eigen::Upper>().rankUpdate(M2,s1);M1.selfadjointView<Eigen::Lower>().rankUpdate(M2.adjoint(),-1); |
Rank 2 update: () | M.selfadjointView<Eigen::Upper>().rankUpdate(u,v,s); |
Solving linear equations:() | // via a standard Cholesky factorizationm2 = m1.selfadjointView<Eigen::Upper>().llt().solve(m2);// via a Cholesky factorization with pivotingm2 = m1.selfadjointView<Eigen::Lower>().ldlt().solve(m2); |
推薦閱讀:
※從矩陣的角度來看待複數
※大白話講數據結構:矩陣的轉置和矩陣的乘法問題(2) (應該都能看懂的!)
※tensorflow實現非負矩陣分解(Non-negative matrix factorization)
※Eigen: C++開源矩陣計算工具