mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-02 02:01:36 +08:00
geninv(): improve runtime performance and reduce stack usage
- use associativity of matrix operations to reduce size of temporary matrices and number of multiplications in the M <= N case - minimize the number of temporary matrices required
This commit is contained in:
@@ -28,23 +28,27 @@ Matrix<Type, N, M> geninv(const Matrix<Type, M, N> & G)
|
|||||||
SquareMatrix<Type, M> A = G * G.transpose();
|
SquareMatrix<Type, M> A = G * G.transpose();
|
||||||
SquareMatrix<Type, M> L = fullRankCholesky(A, rank);
|
SquareMatrix<Type, M> L = fullRankCholesky(A, rank);
|
||||||
|
|
||||||
SquareMatrix<Type, M> LtL = L.transpose() * L;
|
A = L.transpose() * L;
|
||||||
SquareMatrix<Type, M> X;
|
SquareMatrix<Type, M> X;
|
||||||
if (!inv(LtL, X, rank)) {
|
if (!inv(A, X, rank)) {
|
||||||
return Matrix<Type, N, M>(); // LCOV_EXCL_LINE -- this can only be hit from numerical issues
|
return Matrix<Type, N, M>(); // LCOV_EXCL_LINE -- this can only be hit from numerical issues
|
||||||
}
|
}
|
||||||
return G.transpose() * L * X * X * L.transpose();
|
// doing an intermediate assignment reduces stack usage
|
||||||
|
A = X * X * L.transpose();
|
||||||
|
return G.transpose() * (L * A);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
SquareMatrix<Type, N> A = G.transpose() * G;
|
SquareMatrix<Type, N> A = G.transpose() * G;
|
||||||
SquareMatrix<Type, N> L = fullRankCholesky(A, rank);
|
SquareMatrix<Type, N> L = fullRankCholesky(A, rank);
|
||||||
|
|
||||||
SquareMatrix<Type, N> LtL = L.transpose() * L;
|
A = L.transpose() * L;
|
||||||
SquareMatrix<Type, N> X;
|
SquareMatrix<Type, N> X;
|
||||||
if(!inv(LtL, X, rank)) {
|
if(!inv(A, X, rank)) {
|
||||||
return Matrix<Type, N, M>(); // LCOV_EXCL_LINE -- this can only be hit from numerical issues
|
return Matrix<Type, N, M>(); // LCOV_EXCL_LINE -- this can only be hit from numerical issues
|
||||||
}
|
}
|
||||||
return L * X * X * L.transpose() * G.transpose();
|
// doing an intermediate assignment reduces stack usage
|
||||||
|
A = X * X * L.transpose();
|
||||||
|
return (L * A) * G.transpose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user