LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
LMCAS::MatrixNode类 参考

矩阵节点,支持稠密和稀疏两种存储方式。 更多...

#include </home/runner/work/docs/docs/LMCAS/src/internal/symbolic_ast/matrix.hpp>

类 LMCAS::MatrixNode 继承关系图:
LMCAS::MatrixNode 的协作图:

Public 类型

using DenseStorage = std::vector<std::shared_ptr<const SymbolicNode>>
 稠密存储,按行优先展开。
using SparseStorage = std::map<size_t, std::shared_ptr<const SymbolicNode>>
 稀疏存储,索引映射到节点。

Public 成员函数

void accept (LMCAS::detail::SymbolicVisitor &visitor) const override
std::shared_ptr< const SymbolicNode > clone () const override
size_t cols () const noexcept
int compare (const SymbolicNode &other) const
 与另一个节点进行全序比较.
bool equals (const SymbolicNode &other) const
 判断两个节点是否结构相等.
std::shared_ptr< const SymbolicNode > get (size_t r, size_t c) const
 获取指定位置的矩阵元素。
std::size_t hash () const
 获取节点哈希值(带缓存).
virtual bool is_number () const
virtual bool is_one () const
virtual bool is_positive () const
bool is_sparse () const
virtual bool is_zero () const
size_t rows () const noexcept
const std::variant< DenseStorage, SparseStorage > & storage () const noexcept
int type_priority () const override

静态 Public 成员函数

static std::variant< DenseStorage, SparseStorage > create_storage_from_grid (const std::vector< std::vector< std::shared_ptr< const SymbolicNode > > > &grid, size_t total_elements, size_t ncols)
 根据稀疏度选择存储方式,从二维网格创建存储。
static DenseStorage validate_dense_storage (size_t r, size_t c, DenseStorage dense)
static size_t validate_grid_columns (const std::vector< std::vector< std::shared_ptr< const SymbolicNode > > > &grid)
 验证并返回网格的列数(要求各行列数一致)。
static SparseStorage validate_sparse_storage (size_t r, size_t c, SparseStorage sparse)

Protected 成员函数

int compare_same_type (const SymbolicNode &other) const override
std::size_t compute_hash () const override

Protected 属性

std::atomic< std::size_t > cached_hash {0}
std::atomic< bool > hash_computed {false}

Private 成员函数

 MatrixNode (const std::vector< std::vector< std::shared_ptr< const SymbolicNode > > > &grid)
 MatrixNode (size_t r, size_t c, DenseStorage dense)
 MatrixNode (size_t r, size_t c, SparseStorage sparse)

Private 属性

const size_t cols_
 LMCAS_AST_NODE_FACTORY_FRIEND
const size_t rows_
const std::variant< DenseStorage, SparseStorage > storage_

详细描述

矩阵节点,支持稠密和稀疏两种存储方式。

当非零元素占比低于 20% 时自动使用稀疏存储,否则使用稠密存储。

在文件 matrix.hpp 第 12 行定义.

成员类型定义说明

◆ DenseStorage

using LMCAS::MatrixNode::DenseStorage = std::vector<std::shared_ptr<const SymbolicNode>>

稠密存储,按行优先展开。

在文件 matrix.hpp 第 14 行定义.

◆ SparseStorage

using LMCAS::MatrixNode::SparseStorage = std::map<size_t, std::shared_ptr<const SymbolicNode>>

稀疏存储,索引映射到节点。

在文件 matrix.hpp 第 15 行定义.

构造及析构函数说明

◆ MatrixNode() [1/3]

LMCAS::MatrixNode::MatrixNode ( const std::vector< std::vector< std::shared_ptr< const SymbolicNode > > > & grid)
inlineprivate

在文件 matrix.hpp 第 103 行定义.

104 : rows_(grid.size()),
const size_t rows_
static std::variant< DenseStorage, SparseStorage > create_storage_from_grid(const std::vector< std::vector< std::shared_ptr< const SymbolicNode > > > &grid, size_t total_elements, size_t ncols)
根据稀疏度选择存储方式,从二维网格创建存储。
static size_t validate_grid_columns(const std::vector< std::vector< std::shared_ptr< const SymbolicNode > > > &grid)
验证并返回网格的列数(要求各行列数一致)。
const std::variant< DenseStorage, SparseStorage > storage_

引用了 cols_, create_storage_from_grid(), rows_, storage_ , 以及 validate_grid_columns().

被这些函数引用 compare_same_type().

函数调用图:
这是这个函数的调用关系图:

◆ MatrixNode() [2/3]

LMCAS::MatrixNode::MatrixNode ( size_t r,
size_t c,
DenseStorage dense )
inlineprivate

在文件 matrix.hpp 第 108 行定义.

109 : rows_(r), cols_(c), storage_(validate_dense_storage(r, c, std::move(dense))) {}
static DenseStorage validate_dense_storage(size_t r, size_t c, DenseStorage dense)
__attribute__((visibility("default"))) ComplexSymbolicResult complex_exp_form_checked(std __attribute__((visibility("default"))) ComplexSymbolicResult complex_exp_form_checked(std __attribute__((visibility("default"))) ComplexSymbolicResult complex_trig_form_checked(std __attribute__((visibility("default"))) ComplexSymbolicResult complex_trig_form_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_nth_root_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_nth_root_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_quadratic_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_quadratic_checked(std std::shared_ptr< SymbolicExpr > r
const VectorField const VectorField & c

引用了 LMCAS::c, cols_, LMCAS::r, rows_, storage_ , 以及 validate_dense_storage().

函数调用图:

◆ MatrixNode() [3/3]

LMCAS::MatrixNode::MatrixNode ( size_t r,
size_t c,
SparseStorage sparse )
inlineprivate

在文件 matrix.hpp 第 111 行定义.

112 : rows_(r), cols_(c), storage_(validate_sparse_storage(r, c, std::move(sparse))) {}
static SparseStorage validate_sparse_storage(size_t r, size_t c, SparseStorage sparse)

引用了 LMCAS::c, cols_, LMCAS::r, rows_, storage_ , 以及 validate_sparse_storage().

函数调用图:

成员函数说明

◆ accept()

void LMCAS::MatrixNode::accept ( LMCAS::detail::SymbolicVisitor & visitor) const
inlineoverridevirtual

实现了 LMCAS::SymbolicNode.

在文件 matrix.hpp 第 154 行定义.

154{ LMCAS::detail::SymbolicVisitor::DepthGuard guard(visitor); visitor.visit(*this); }
virtual void visit(const NumberNode &node)=0

引用了 LMCAS::detail::SymbolicVisitor::visit().

函数调用图:

◆ clone()

std::shared_ptr< const SymbolicNode > LMCAS::MatrixNode::clone ( ) const
inlineoverridevirtual

实现了 LMCAS::SymbolicNode.

在文件 matrix.hpp 第 156 行定义.

156 {
157 if (std::holds_alternative<DenseStorage>(storage_)) {
158 const auto& dense = std::get<DenseStorage>(storage_);
159 DenseStorage new_dense;
160 new_dense.reserve(dense.size());
161 for (const auto& e : dense) {
162 new_dense.push_back(e->clone());
163 }
164 return LMCAS::detail::make_node<MatrixNode>(rows_, cols_, std::move(new_dense));
165 } else {
166 const auto& sparse = std::get<SparseStorage>(storage_);
167 SparseStorage new_sparse;
168 for(const auto& [idx, node] : sparse) {
169 new_sparse[idx] = node->clone();
170 }
171 return LMCAS::detail::make_node<MatrixNode>(rows_, cols_, std::move(new_sparse));
172 }
173 }
std::map< size_t, std::shared_ptr< const SymbolicNode > > SparseStorage
稀疏存储,索引映射到节点。
std::vector< std::shared_ptr< const SymbolicNode > > DenseStorage
稠密存储,按行优先展开。
const SymbolicNodePtr & node(const ::LMCAS::SymbolicExpr &expression) noexcept
std::shared_ptr< const Node > make_node(Args &&... args)
ExprResult e()

引用了 cols_, LMCAS::e(), LMCAS::detail::make_node(), rows_ , 以及 storage_.

函数调用图:

◆ cols()

◆ compare()

int LMCAS::SymbolicNode::compare ( const SymbolicNode & other) const
inlineinherited

与另一个节点进行全序比较.

参数
other待比较的节点
返回
小于返回 -1,等于返回 0,大于返回 1

在文件 base.hpp 第 183 行定义.

183 {
184 if (type_priority() != other.type_priority()) {
185 return type_priority() < other.type_priority() ? -1 : 1;
186 }
187 return compare_same_type(other);
188 }
virtual int compare_same_type(const SymbolicNode &other) const =0
virtual int type_priority() const =0

引用了 SymbolicNode(), compare_same_type() , 以及 type_priority().

被这些函数引用 LMCAS::anonymous_namespace{symbolic_cancel.cpp}::combine_fraction_terms() , 以及 LMCAS::endpoints_equivalent().

函数调用图:
这是这个函数的调用关系图:

◆ compare_same_type()

int LMCAS::MatrixNode::compare_same_type ( const SymbolicNode & other) const
overrideprotectedvirtual

实现了 LMCAS::SymbolicNode.

在文件 symbolic_node_order.cpp 第 24 行定义.

24 {
25 const auto& matrix = static_cast<const MatrixNode&>(other);
26 if (rows_ != matrix.rows_) {
27 return rows_ < matrix.rows_ ? -1 : 1;
28 }
29 if (cols_ != matrix.cols_) {
30 return cols_ < matrix.cols_ ? -1 : 1;
31 }
32 for (size_t row = 0; row < rows_; ++row) {
33 for (size_t column = 0; column < cols_; ++column) {
34 const int comparison = compare_matrix_elements(get(row, column), matrix.get(row, column));
35 if (comparison != 0) {
36 return comparison;
37 }
38 }
39 }
40 return 0;
41}
MatrixNode(const std::vector< std::vector< std::shared_ptr< const SymbolicNode > > > &grid)
std::shared_ptr< const SymbolicNode > get(size_t r, size_t c) const
获取指定位置的矩阵元素。
int compare_matrix_elements(const std::shared_ptr< const SymbolicNode > &left, const std::shared_ptr< const SymbolicNode > &right)

引用了 MatrixNode(), LMCAS::SymbolicNode::SymbolicNode(), cols_, get() , 以及 rows_.

函数调用图:

◆ compute_hash()

std::size_t LMCAS::MatrixNode::compute_hash ( ) const
inlineoverrideprotectedvirtual

实现了 LMCAS::SymbolicNode.

在文件 matrix.hpp 第 124 行定义.

124 {
125 std::size_t seed = 0;
127 hash_combine(seed, rows_);
128 hash_combine(seed, cols_);
129
130 if (std::holds_alternative<DenseStorage>(storage_)) {
131 const auto& dense = std::get<DenseStorage>(storage_);
132 for (size_t i = 0; i < dense.size(); ++i) {
133 if (dense[i] && !dense[i]->is_zero()) {
134 hash_combine(seed, i);
135 hash_combine(seed, dense[i]->hash());
136 }
137 }
138 } else {
139 const auto& sparse = std::get<SparseStorage>(storage_);
140 for (const auto& [idx, val] : sparse) {
141
142 if (val && !val->is_zero()) {
143 hash_combine(seed, idx);
144 hash_combine(seed, val->hash());
145 }
146 }
147 }
148 return seed;
149 }
int type_priority() const override
std::size_t hash() const
获取节点哈希值(带缓存).
virtual bool is_zero() const
void hash_combine(std::size_t &seed, std::size_t value)
将一个哈希值混合到种子中,用于组合多个字段的哈希.

引用了 cols_, LMCAS::SymbolicNode::hash(), LMCAS::hash_combine(), LMCAS::SymbolicNode::is_zero(), rows_, storage_ , 以及 type_priority().

函数调用图:

◆ create_storage_from_grid()

std::variant< MatrixNode::DenseStorage, MatrixNode::SparseStorage > LMCAS::MatrixNode::create_storage_from_grid ( const std::vector< std::vector< std::shared_ptr< const SymbolicNode > > > & grid,
size_t total_elements,
size_t ncols )
inlinestatic

根据稀疏度选择存储方式,从二维网格创建存储。

参数
grid二维节点网格
total_elements总元素数
ncols列数
返回
稠密或稀疏存储

在文件 matrix.hpp 第 222 行定义.

223 {
224
225 size_t non_zeros = 0;
226 for (const auto& row : grid) {
227 for (const auto& item : row) {
228 if (item && !item->is_zero()) non_zeros++;
229 }
230 }
231
232 bool use_sparse = total_elements > 0 && ((double)non_zeros / total_elements < 0.2);
233
234 if (use_sparse) {
236 size_t r = 0;
237 for (const auto& row : grid) {
238 size_t c = 0;
239 for (const auto& item : row) {
240 if (item && !item->is_zero()) {
241 s[r * ncols + c] = item;
242 }
243 c++;
244 }
245 r++;
246 }
247 return s;
248 } else {
249 DenseStorage d;
250 d.reserve(total_elements);
251 for (const auto& row : grid) {
252 d.insert(d.end(), row.begin(), row.end());
253 }
254 return d;
255 }
256}

引用了 LMCAS::c , 以及 LMCAS::r.

被这些函数引用 MatrixNode().

这是这个函数的调用关系图:

◆ equals()

bool LMCAS::SymbolicNode::equals ( const SymbolicNode & other) const
inlineinherited

判断两个节点是否结构相等.

参数
other待比较的节点
返回
相等返回 true

在文件 base.hpp 第 195 行定义.

195 {
196 if (this == &other) return true;
197 if (hash() != other.hash()) return false;
198 if (type_priority() != other.type_priority()) return false;
199 return compare_same_type(other) == 0;
200 }

引用了 SymbolicNode(), compare_same_type(), hash() , 以及 type_priority().

被这些函数引用 LMCAS::complementary_square(), LMCAS::anonymous_namespace{relation_store.cpp}::expr_equals(), LMCAS::match_recursive(), LMCAS::tf_remove_exp_factor() , 以及 LMCAS::tf_substitute_expr().

函数调用图:
这是这个函数的调用关系图:

◆ get()

std::shared_ptr< const SymbolicNode > LMCAS::MatrixNode::get ( size_t r,
size_t c ) const
inline

获取指定位置的矩阵元素。

参数
r行索引(从 0 开始)
c列索引(从 0 开始)
返回
对应节点,越界返回 nullptr,稀疏缺失返回零节点

在文件 matrix.hpp 第 181 行定义.

181 {
182 if (r >= rows_ || c >= cols_) return nullptr;
183
184 size_t idx = r * cols_ + c;
185 if (std::holds_alternative<DenseStorage>(storage_)) {
186 return std::get<DenseStorage>(storage_)[idx];
187 } else {
188 const auto& sparse = std::get<SparseStorage>(storage_);
189 auto it = sparse.find(idx);
190 if (it != sparse.end()) return it->second;
191 static const std::shared_ptr<const SymbolicNode> zero =
193 return zero;
194 }
195 }

引用了 LMCAS::c, cols_, LMCAS::detail::make_node(), LMCAS::r, rows_ , 以及 storage_.

被这些函数引用 compare_same_type(), LMCAS::anonymous_namespace{symbolic_matrix_eigen.cpp}::eigenvalue_nullspace(), LMCAS::detail::matrix_api::exact_matrix_data(), LMCAS::anonymous_namespace{matrix_decomposition.cpp}::matrix_axis_sum() , 以及 LMCAS::anonymous_namespace{expr_serialization.cpp}::Writer::visit().

函数调用图:
这是这个函数的调用关系图:

◆ hash()

std::size_t LMCAS::SymbolicNode::hash ( ) const
inlineinherited

获取节点哈希值(带缓存).

返回
哈希值

在文件 base.hpp 第 168 行定义.

168 {
169 if (!hash_computed.load(std::memory_order_acquire)) {
170 std::size_t h = compute_hash();
171 cached_hash.store(h, std::memory_order_relaxed);
172 hash_computed.store(true, std::memory_order_release);
173 return h;
174 }
175 return cached_hash.load(std::memory_order_relaxed);
176 }
virtual std::size_t compute_hash() const =0
std::atomic< std::size_t > cached_hash
std::atomic< bool > hash_computed

引用了 cached_hash, compute_hash() , 以及 hash_computed.

被这些函数引用 LMCAS::MatrixNode::compute_hash(), LMCAS::VariableNode::compute_hash() , 以及 equals().

函数调用图:
这是这个函数的调用关系图:

◆ is_number()

virtual bool LMCAS::SymbolicNode::is_number ( ) const
inlinevirtualinherited

被 LMCAS::NumberNode 重载.

在文件 base.hpp 第 201 行定义.

201{ return false; }

◆ is_one()

virtual bool LMCAS::SymbolicNode::is_one ( ) const
inlinevirtualinherited

被 LMCAS::NumberNode 重载.

在文件 base.hpp 第 202 行定义.

202{ return false; }

◆ is_positive()

virtual bool LMCAS::SymbolicNode::is_positive ( ) const
inlinevirtualinherited

被 LMCAS::NumberNode 重载.

在文件 base.hpp 第 204 行定义.

204{ return false; }

◆ is_sparse()

bool LMCAS::MatrixNode::is_sparse ( ) const
inline

在文件 matrix.hpp 第 196 行定义.

196{ return std::holds_alternative<SparseStorage>(storage_); }

引用了 storage_.

◆ is_zero()

virtual bool LMCAS::SymbolicNode::is_zero ( ) const
inlinevirtualinherited

◆ rows()

◆ storage()

const std::variant< DenseStorage, SparseStorage > & LMCAS::MatrixNode::storage ( ) const
inlinenoexcept

在文件 matrix.hpp 第 117 行定义.

117 {
118 return storage_;
119 }

引用了 storage_.

被这些函数引用 LMCAS::anonymous_namespace{normalization_matrices.cpp}::matrix_element() , 以及 LMCAS::NormalizationVisitor::matrix_product_element().

这是这个函数的调用关系图:

◆ type_priority()

int LMCAS::MatrixNode::type_priority ( ) const
inlineoverridevirtual

实现了 LMCAS::SymbolicNode.

在文件 matrix.hpp 第 121 行定义.

121{ return 6; }

被这些函数引用 compute_hash().

这是这个函数的调用关系图:

◆ validate_dense_storage()

DenseStorage LMCAS::MatrixNode::validate_dense_storage ( size_t r,
size_t c,
DenseStorage dense )
inlinestatic

在文件 matrix.hpp 第 49 行定义.

49 {
50 if (r == 0 || c == 0) {
51 throw std::invalid_argument("MatrixNode: matrix dimensions must be non-zero");
52 }
53 if (r > std::numeric_limits<size_t>::max() / c) {
54 throw std::length_error("MatrixNode: matrix dimensions overflow");
55 }
56 if (dense.size() != r * c) {
57 throw std::invalid_argument("MatrixNode: dense storage size does not match dimensions");
58 }
59 for (const auto& item : dense) {
60 if (!item) {
61 throw std::invalid_argument("MatrixNode: dense storage elements cannot be null");
62 }
63 }
64 return dense;
65 }

引用了 LMCAS::c , 以及 LMCAS::r.

被这些函数引用 MatrixNode().

这是这个函数的调用关系图:

◆ validate_grid_columns()

size_t LMCAS::MatrixNode::validate_grid_columns ( const std::vector< std::vector< std::shared_ptr< const SymbolicNode > > > & grid)
inlinestatic

验证并返回网格的列数(要求各行列数一致)。

参数
grid二维节点网格
返回
列数
异常
std::invalid_argument各行列数不一致时抛出

在文件 matrix.hpp 第 23 行定义.

23 {
24 if (grid.empty()) {
25 throw std::invalid_argument("MatrixNode: matrix must have at least one row");
26 }
27 size_t ncols = grid[0].size();
28 if (ncols == 0) {
29 throw std::invalid_argument("MatrixNode: matrix must have at least one column");
30 }
31 for (const auto& item : grid[0]) {
32 if (!item) {
33 throw std::invalid_argument("MatrixNode: matrix elements cannot be null");
34 }
35 }
36 for (size_t i = 1; i < grid.size(); ++i) {
37 if (grid[i].size() != ncols) {
38 throw std::invalid_argument("MatrixNode: all rows must have the same number of columns");
39 }
40 for (const auto& item : grid[i]) {
41 if (!item) {
42 throw std::invalid_argument("MatrixNode: matrix elements cannot be null");
43 }
44 }
45 }
46 return ncols;
47 }

被这些函数引用 MatrixNode().

这是这个函数的调用关系图:

◆ validate_sparse_storage()

SparseStorage LMCAS::MatrixNode::validate_sparse_storage ( size_t r,
size_t c,
SparseStorage sparse )
inlinestatic

在文件 matrix.hpp 第 67 行定义.

67 {
68 if (r == 0 || c == 0) {
69 throw std::invalid_argument("MatrixNode: matrix dimensions must be non-zero");
70 }
71 if (r > std::numeric_limits<size_t>::max() / c) {
72 throw std::length_error("MatrixNode: matrix dimensions overflow");
73 }
74 const size_t total = r * c;
75 for (const auto& [idx, item] : sparse) {
76 if (idx >= total) {
77 throw std::invalid_argument("MatrixNode: sparse storage index is out of bounds");
78 }
79 if (!item) {
80 throw std::invalid_argument("MatrixNode: sparse storage elements cannot be null");
81 }
82 }
83 return sparse;
84 }

引用了 LMCAS::c , 以及 LMCAS::r.

被这些函数引用 MatrixNode().

这是这个函数的调用关系图:

类成员变量说明

◆ cached_hash

std::atomic<std::size_t> LMCAS::SymbolicNode::cached_hash {0}
mutableprotectedinherited

在文件 base.hpp 第 149 行定义.

被这些函数引用 SymbolicNode() , 以及 hash().

◆ cols_

const size_t LMCAS::MatrixNode::cols_
private

在文件 matrix.hpp 第 100 行定义.

被这些函数引用 MatrixNode(), MatrixNode(), MatrixNode(), clone(), cols(), compare_same_type(), compute_hash() , 以及 get().

◆ hash_computed

std::atomic<bool> LMCAS::SymbolicNode::hash_computed {false}
mutableprotectedinherited

在文件 base.hpp 第 150 行定义.

被这些函数引用 SymbolicNode() , 以及 hash().

◆ LMCAS_AST_NODE_FACTORY_FRIEND

LMCAS::MatrixNode::LMCAS_AST_NODE_FACTORY_FRIEND
private

在文件 matrix.hpp 第 97 行定义.

◆ rows_

const size_t LMCAS::MatrixNode::rows_
private

在文件 matrix.hpp 第 99 行定义.

被这些函数引用 MatrixNode(), MatrixNode(), MatrixNode(), clone(), compare_same_type(), compute_hash(), get() , 以及 rows().

◆ storage_

const std::variant<DenseStorage, SparseStorage> LMCAS::MatrixNode::storage_
private

在文件 matrix.hpp 第 101 行定义.

被这些函数引用 MatrixNode(), MatrixNode(), MatrixNode(), clone(), compute_hash(), get(), is_sparse() , 以及 storage().


该类的文档由以下文件生成: