LMCAS
Lamina Computer Algebra System
Toggle main menu visibility
载入中...
搜索中...
未找到
symbolic_matrix_support.cpp
浏览该文件的文档.
1
#include "
internal/symbolic_matrix_support.hpp
"
2
3
namespace
LMCAS::detail::matrix_api
{
4
5
Result<std::shared_ptr<const MatrixNode>
>
require_matrix
(
6
const
std::shared_ptr<SymbolicExpr>& expr,
7
ComputationContext
&
context
,
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)) {
13
return
Result<std::shared_ptr<const MatrixNode>
>::failure(
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) {
18
return
Result<std::shared_ptr<const MatrixNode>
>::failure(
19
CasErrc::InvalidArgument
,
"expression is not a matrix"
,
operation
);
20
}
21
return
Result<std::shared_ptr<const MatrixNode>
>::success(mat);
22
}
23
24
Result<std::shared_ptr<const MatrixNode>
>
require_square_matrix
(
25
const
std::shared_ptr<SymbolicExpr>& expr,
26
ComputationContext
&
context
,
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()) {
32
return
Result<std::shared_ptr<const MatrixNode>
>::failure(
33
CasErrc::InvalidArgument
,
"matrix must be square"
,
operation
);
34
}
35
return
mat;
36
}
37
38
ExpressionResult
require_matrix_result
(std::shared_ptr<SymbolicExpr> result,
39
const
std::string&
operation
)
40
{
41
if
(!result || !
LMCAS::detail::node
(result)) {
42
return
ExpressionResult::failure
(
CasErrc::InternalInvariant
,
43
"matrix operation returned an empty expression"
,
44
operation
);
45
}
46
return
ExpressionResult::success
(std::move(result));
47
}
48
49
detail::ExactMatrixData
exact_matrix_data
(
const
MatrixNode
& matrix) {
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
61
std::shared_ptr<SymbolicExpr>
exact_matrix_expression
(
62
const
detail::ExactMatrixData
& matrix) {
63
MatrixNode::DenseStorage
entries;
64
entries.reserve(matrix.
entries
.size());
65
for
(
const
auto
& entry : matrix.
entries
) {
66
entries.push_back(
LMCAS::detail::node
(entry));
67
}
68
return
LMCAS::detail::make_expression_ptr
(
69
LMCAS::detail::make_node<MatrixNode>
(
70
matrix.
rows
, matrix.
cols
, std::move(entries)));
71
}
72
73
}
LMCAS::ComputationContext
定义
computation_context.hpp:99
LMCAS::MatrixNode
矩阵节点,支持稠密和稀疏两种存储方式。
定义
matrix.hpp:12
LMCAS::MatrixNode::cols
size_t cols() const noexcept
定义
matrix.hpp:116
LMCAS::MatrixNode::rows
size_t rows() const noexcept
定义
matrix.hpp:115
LMCAS::MatrixNode::get
std::shared_ptr< const SymbolicNode > get(size_t r, size_t c) const
获取指定位置的矩阵元素。
定义
matrix.hpp:181
LMCAS::MatrixNode::DenseStorage
std::vector< std::shared_ptr< const SymbolicNode > > DenseStorage
稠密存储,按行优先展开。
定义
matrix.hpp:14
LMCAS::Result
定义
result.hpp:39
LMCAS::Result< std::shared_ptr< SymbolicExpr > >::failure
static Result failure(CasErrc code, std::string message, std::string operation={})
定义
result.hpp:52
LMCAS::Result< std::shared_ptr< SymbolicExpr > >::success
static Result success(std::shared_ptr< SymbolicExpr > value)
定义
result.hpp:48
LMCAS::detail::matrix_api
定义
symbolic_matrix_support.hpp:8
LMCAS::detail::matrix_api::exact_matrix_data
ExactMatrixData exact_matrix_data(const MatrixNode &matrix)
定义
symbolic_matrix_support.cpp:49
LMCAS::detail::matrix_api::exact_matrix_expression
std::shared_ptr< SymbolicExpr > exact_matrix_expression(const ExactMatrixData &matrix)
定义
symbolic_matrix_support.cpp:61
LMCAS::detail::matrix_api::require_matrix
Result< std::shared_ptr< const MatrixNode > > require_matrix(const std::shared_ptr< SymbolicExpr > &expr, ComputationContext &context, const std::string &operation)
定义
symbolic_matrix_support.cpp:5
LMCAS::detail::matrix_api::require_matrix_result
ExpressionResult require_matrix_result(std::shared_ptr< SymbolicExpr > result, const std::string &operation)
定义
symbolic_matrix_support.cpp:38
LMCAS::detail::matrix_api::require_square_matrix
Result< std::shared_ptr< const MatrixNode > > require_square_matrix(const std::shared_ptr< SymbolicExpr > &expr, ComputationContext &context, const std::string &operation)
定义
symbolic_matrix_support.cpp:24
LMCAS::detail::make_expression_ptr
std::shared_ptr<::LMCAS::SymbolicExpr > make_expression_ptr(SymbolicNodePtr root)
定义
base.hpp:111
LMCAS::detail::node
const SymbolicNodePtr & node(const ::LMCAS::SymbolicExpr &expression) noexcept
定义
base.hpp:91
LMCAS::detail::make_node
std::shared_ptr< const Node > make_node(Args &&... args)
定义
base.hpp:123
LMCAS::detail::operation
ComputationContext const std::string & operation
定义
exact_algebraic.hpp:32
LMCAS::detail::context
std::size_t std::size_t ComputationContext & context
定义
exact_algebraic.hpp:27
LMCAS::CasErrc::InvalidArgument
@ InvalidArgument
定义
result.hpp:13
LMCAS::CasErrc::InternalInvariant
@ InternalInvariant
定义
result.hpp:28
LMCAS::ExpressionResult
Result< std::shared_ptr< SymbolicExpr > > ExpressionResult
Result of an operation that produces a symbolic expression.
定义
symbolic.hpp:31
LMCAS::detail::ExactMatrixData
定义
exact_matrix.hpp:26
LMCAS::detail::ExactMatrixData::rows
std::size_t rows
定义
exact_matrix.hpp:27
LMCAS::detail::ExactMatrixData::cols
std::size_t cols
定义
exact_matrix.hpp:28
LMCAS::detail::ExactMatrixData::entries
std::vector< ExprPtr > entries
定义
exact_matrix.hpp:29
symbolic_matrix_support.hpp
src
symbolic_matrix_support.cpp
生成于
, 为 LMCAS使用
1.18.0