LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
symbolic_matrix_solve.cpp
浏览该文件的文档.
2
3namespace LMCAS {
4using namespace detail::matrix_api;
6 const std::shared_ptr<SymbolicExpr>& A,
8 const std::string operation = "matrix_nullspace";
9 auto matrix = require_matrix(A, context, operation);
10 if (!matrix) return MatrixNullspaceResult::failure(matrix.error());
11 auto nullspace = detail::nullspace_exact(
12 exact_matrix_data(*matrix.value()), context, operation);
13 if (!nullspace) return MatrixNullspaceResult::failure(nullspace.error());
14 return MatrixNullspaceResult::success(std::move(nullspace.value()));
15}
16
18 const std::shared_ptr<SymbolicExpr>& A) {
21}
22
24 const std::shared_ptr<SymbolicExpr>& coefficients,
25 const std::shared_ptr<SymbolicExpr>& right_hand_side,
27 const std::string operation = "matrix_solve_linear";
28 auto matrix = require_matrix(coefficients, context, operation);
29 if (!matrix) return MatrixLinearSolveResult::failure(matrix.error());
30 auto rhs = require_matrix(right_hand_side, context, operation);
31 if (!rhs) return MatrixLinearSolveResult::failure(rhs.error());
32 if (rhs.value()->rows() != matrix.value()->rows() ||
33 rhs.value()->cols() != 1) {
36 "right-hand side must be a matching column matrix", operation);
37 }
38
40 matrix.value()->rows(), matrix.value()->cols() + 1, {}};
41 augmented.entries.reserve(augmented.rows * augmented.cols);
42 for (std::size_t row = 0; row < augmented.rows; ++row) {
43 for (std::size_t column = 0;
44 column < matrix.value()->cols(); ++column) {
45 augmented.entries.push_back(
47 matrix.value()->get(row, column)));
48 }
49 augmented.entries.push_back(
50 LMCAS::detail::make_expression_ptr(rhs.value()->get(row, 0)));
51 }
52 auto solved = detail::solve_linear_exact(
53 std::move(augmented), matrix.value()->cols(), context, operation);
54 if (!solved) return MatrixLinearSolveResult::failure(solved.error());
55 if (auto* unique =
56 std::get_if<detail::UniqueLinearSolution>(&solved.value())) {
58 MatrixUniqueLinearSolution{std::move(unique->values)});
59 }
60 if (auto* parametric =
61 std::get_if<detail::ParametricLinearSolution>(&solved.value())) {
64 std::move(parametric->particular),
65 std::move(parametric->nullspace_basis),
66 std::move(parametric->free_columns)});
67 }
70}
71
73 const std::shared_ptr<SymbolicExpr>& coefficients,
74 const std::shared_ptr<SymbolicExpr>& right_hand_side) {
77 coefficients, right_hand_side, context);
78}
79
80}
static Result failure(CasErrc code, std::string message, std::string operation={})
static Result success(std::vector< std::vector< std::shared_ptr< SymbolicExpr > > > value)
ExactMatrixData exact_matrix_data(const MatrixNode &matrix)
Result< std::shared_ptr< const MatrixNode > > require_matrix(const std::shared_ptr< SymbolicExpr > &expr, ComputationContext &context, const std::string &operation)
Result< std::vector< std::vector< ExprPtr > > > nullspace_exact(ExactMatrixData coefficients, ComputationContext &context, const std::string &operation)
std::shared_ptr<::LMCAS::SymbolicExpr > make_expression_ptr(SymbolicNodePtr root)
Result< ExactLinearSolution > solve_linear_exact(ExactMatrixData augmented, std::size_t coefficient_columns, ComputationContext &context, const std::string &operation)
Result< MatrixLinearSolution > MatrixLinearSolveResult
const std::vector< Polynomial< BigInt > > const BigInt ComputationContext & context
MatrixLinearSolveResult matrix_solve_linear_checked(const std::shared_ptr< SymbolicExpr > &coefficients, const std::shared_ptr< SymbolicExpr > &right_hand_side, ComputationContext &context)
const ExprPtr & rhs
Result< std::vector< std::vector< std::shared_ptr< SymbolicExpr > > > > MatrixNullspaceResult
MatrixNullspaceResult matrix_nullspace_checked(const std::shared_ptr< SymbolicExpr > &A, ComputationContext &context)