|
6 | 6 | #include <optional> |
7 | 7 |
|
8 | 8 | #include <tinyopt/types.h> |
| 9 | +#include <Eigen/src/SVD/JacobiSVD.h> |
9 | 10 |
|
10 | 11 | namespace tinyopt { |
11 | 12 |
|
@@ -276,6 +277,110 @@ std::optional<Vector<Scalar, RowsAtCompileTime>> SolveLDLT( |
276 | 277 | return X; |
277 | 278 | } |
278 | 279 |
|
| 280 | +/** |
| 281 | + * @brief Solves the linear system A * X = B for X using LU decomposition. |
| 282 | + * |
| 283 | + * @tparam Derived The type of the matrix A. |
| 284 | + * @tparam Derived2 The type of the vector B. |
| 285 | + * |
| 286 | + * @param A The coefficient matrix A. |
| 287 | + * @param b The right-hand side vector B. |
| 288 | + * |
| 289 | + * @return An `std::optional` containing the solution vector X if the system is solvable, or |
| 290 | + * `std::nullopt` otherwise. |
| 291 | + */ |
| 292 | +template <typename Derived, typename Derived2> |
| 293 | +std::optional<Vector<typename Derived::Scalar, Derived::RowsAtCompileTime>> SolveLU( |
| 294 | + const MatrixBase<Derived> &A, const MatrixBase<Derived2> &b) { |
| 295 | + auto lu = A.partialPivLu(); |
| 296 | + if (lu.determinant() != 0) { |
| 297 | + return lu.solve(b); |
| 298 | + } |
| 299 | + return std::nullopt; |
| 300 | +} |
| 301 | + |
| 302 | +/** |
| 303 | + * @brief Solves the linear system A * X = B for X using QR decomposition. |
| 304 | + * |
| 305 | + * @tparam Derived The type of the matrix A. |
| 306 | + * @tparam Derived2 The type of the vector B. |
| 307 | + * |
| 308 | + * @param A The coefficient matrix A. |
| 309 | + * @param b The right-hand side vector B. |
| 310 | + * |
| 311 | + * @return An `std::optional` containing the solution vector X. |
| 312 | + */ |
| 313 | +template <typename Derived, typename Derived2> |
| 314 | +std::optional<Vector<typename Derived::Scalar, Derived::RowsAtCompileTime>> SolveQR( |
| 315 | + const MatrixBase<Derived> &A, const MatrixBase<Derived2> &b) { |
| 316 | + return A.colPivHouseholderQr().solve(b); |
| 317 | +} |
| 318 | + |
| 319 | +/** |
| 320 | + * @brief Solves the linear system A * X = B for X using SVD decomposition. |
| 321 | + * |
| 322 | + * @tparam Derived The type of the matrix A. |
| 323 | + * @tparam Derived2 The type of the vector B. |
| 324 | + * |
| 325 | + * @param A The coefficient matrix A. |
| 326 | + * @param b The right-hand side vector B. |
| 327 | + * |
| 328 | + * @return An `std::optional` containing the solution vector X. |
| 329 | + */ |
| 330 | +template <typename Derived, typename Derived2> |
| 331 | +std::optional<Vector<typename Derived::Scalar, Derived::RowsAtCompileTime>> SolveSVD( |
| 332 | + const MatrixBase<Derived> &A, const MatrixBase<Derived2> &b) { |
| 333 | + return A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b); |
| 334 | +} |
| 335 | + |
| 336 | + |
| 337 | +/** |
| 338 | + * @brief Solves the sparse linear system A * X = B for X using LU decomposition. |
| 339 | + * |
| 340 | + * @tparam Scalar The scalar type. |
| 341 | + * @tparam RowsAtCompileTime The compile-time number of rows of vector B. |
| 342 | + * |
| 343 | + * @param A The sparse coefficient matrix A. |
| 344 | + * @param b The right-hand side vector B. |
| 345 | + * |
| 346 | + * @return An `std::optional` containing the solution vector X if the system is solvable, or |
| 347 | + * `std::nullopt` otherwise. |
| 348 | + */ |
| 349 | +template <typename Scalar, int RowsAtCompileTime = Dynamic> |
| 350 | +std::optional<Vector<Scalar, RowsAtCompileTime>> SolveLU( |
| 351 | + const SparseMatrix<Scalar> &A, const Vector<Scalar, RowsAtCompileTime> &b) { |
| 352 | + Eigen::SparseLU<SparseMatrix<Scalar>> solver; |
| 353 | + solver.compute(A); |
| 354 | + if (solver.info() != Eigen::Success) return std::nullopt; |
| 355 | + auto X = solver.solve(b); |
| 356 | + if (solver.info() != Eigen::Success) return std::nullopt; |
| 357 | + return X; |
| 358 | +} |
| 359 | + |
| 360 | +/** |
| 361 | + * @brief Solves the sparse linear system A * X = B for X using QR decomposition. |
| 362 | + * |
| 363 | + * @tparam Scalar The scalar type. |
| 364 | + * @tparam RowsAtCompileTime The compile-time number of rows of vector B. |
| 365 | + * |
| 366 | + * @param A The sparse coefficient matrix A. |
| 367 | + * @param b The right-hand side vector B. |
| 368 | + * |
| 369 | + * @return An `std::optional` containing the solution vector X if the system is solvable, or |
| 370 | + * `std::nullopt` otherwise. |
| 371 | + */ |
| 372 | +template <typename Scalar, int RowsAtCompileTime = Dynamic> |
| 373 | +std::optional<Vector<Scalar, RowsAtCompileTime>> SolveQR( |
| 374 | + const SparseMatrix<Scalar> &A, const Vector<Scalar, RowsAtCompileTime> &b) { |
| 375 | + Eigen::SparseQR<SparseMatrix<Scalar>, Eigen::COLAMDOrdering<int>> solver; |
| 376 | + solver.compute(A); |
| 377 | + if (solver.info() != Eigen::Success) return std::nullopt; |
| 378 | + auto X = solver.solve(b); |
| 379 | + if (solver.info() != Eigen::Success) return std::nullopt; |
| 380 | + return X; |
| 381 | +} |
| 382 | + |
| 383 | + |
279 | 384 | /// Integer square root function for positive integers |
280 | 385 | /// Will return `N` for negative or 0 values |
281 | 386 | constexpr inline int SQRT(int N) { |
|
0 commit comments