LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
symbolic_matrix_support.cpp
浏览该文件的文档.
2
4
6 const std::shared_ptr<SymbolicExpr>& expr,
8 const std::string& operation)
9{
10 auto step = context.consume_steps(1, operation);
11 if (!step) return Result<std::shared_ptr<const MatrixNode>>::failure(step.error());
12 if (!expr || !LMCAS::detail::node(expr)) {
14 CasErrc::InvalidArgument, "matrix expression cannot be null", operation);
15 }
16 auto mat = std::dynamic_pointer_cast<const MatrixNode>(LMCAS::detail::node(expr));
17 if (!mat) {
19 CasErrc::InvalidArgument, "expression is not a matrix", operation);
20 }
21 return Result<std::shared_ptr<const MatrixNode>>::success(mat);
22}
23
25 const std::shared_ptr<SymbolicExpr>& expr,
27 const std::string& operation)
28{
29 auto mat = require_matrix(expr, context, operation);
30 if (!mat) return mat;
31 if (mat.value()->rows() != mat.value()->cols()) {
33 CasErrc::InvalidArgument, "matrix must be square", operation);
34 }
35 return mat;
36}
37
38ExpressionResult require_matrix_result(std::shared_ptr<SymbolicExpr> result,
39 const std::string& operation)
40{
41 if (!result || !LMCAS::detail::node(result)) {
43 "matrix operation returned an empty expression",
44 operation);
45 }
46 return ExpressionResult::success(std::move(result));
47}
48
50 detail::ExactMatrixData data{matrix.rows(), matrix.cols(), {}};
51 data.entries.reserve(matrix.rows() * matrix.cols());
52 for (std::size_t row = 0; row < matrix.rows(); ++row) {
53 for (std::size_t column = 0; column < matrix.cols(); ++column) {
54 data.entries.push_back(
55 LMCAS::detail::make_expression_ptr(matrix.get(row, column)));
56 }
57 }
58 return data;
59}
60
61std::shared_ptr<SymbolicExpr> exact_matrix_expression(
62 const detail::ExactMatrixData& matrix) {
64 entries.reserve(matrix.entries.size());
65 for (const auto& entry : matrix.entries) {
66 entries.push_back(LMCAS::detail::node(entry));
67 }
70 matrix.rows, matrix.cols, std::move(entries)));
71}
72
73}
矩阵节点,支持稠密和稀疏两种存储方式。
size_t cols() const noexcept
size_t rows() const noexcept
std::shared_ptr< const SymbolicNode > get(size_t r, size_t c) const
获取指定位置的矩阵元素。
std::vector< std::shared_ptr< const SymbolicNode > > DenseStorage
稠密存储,按行优先展开。
static Result failure(CasErrc code, std::string message, std::string operation={})
static Result success(std::shared_ptr< SymbolicExpr > value)
ExactMatrixData exact_matrix_data(const MatrixNode &matrix)
std::shared_ptr< SymbolicExpr > exact_matrix_expression(const ExactMatrixData &matrix)
Result< std::shared_ptr< const MatrixNode > > require_matrix(const std::shared_ptr< SymbolicExpr > &expr, ComputationContext &context, const std::string &operation)
ExpressionResult require_matrix_result(std::shared_ptr< SymbolicExpr > result, const std::string &operation)
Result< std::shared_ptr< const MatrixNode > > require_square_matrix(const std::shared_ptr< SymbolicExpr > &expr, ComputationContext &context, const std::string &operation)
std::shared_ptr<::LMCAS::SymbolicExpr > make_expression_ptr(SymbolicNodePtr root)
const SymbolicNodePtr & node(const ::LMCAS::SymbolicExpr &expression) noexcept
std::shared_ptr< const Node > make_node(Args &&... args)
ComputationContext const std::string & operation
std::size_t std::size_t ComputationContext & context
Result< std::shared_ptr< SymbolicExpr > > ExpressionResult
Result of an operation that produces a symbolic expression.