LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
relations.hpp
浏览该文件的文档.
1/** @file internal/symbolic_ast/relations.hpp */
2#pragma once
4
5namespace LMCAS {
7public:
9
10private:
12
13 const std::shared_ptr<const SymbolicNode> left_;
14 const std::shared_ptr<const SymbolicNode> right_;
15 const Op op_;
16
17 RelationalNode(std::shared_ptr<const SymbolicNode> l, std::shared_ptr<const SymbolicNode> r, Op o)
18 : left_(std::move(l)), right_(std::move(r)), op_(o) {
19 if (!left_ || !right_) {
20 throw std::invalid_argument("RelationalNode operands cannot be null");
21 }
22 }
23
24public:
25 const std::shared_ptr<const SymbolicNode>& left() const noexcept { return left_; }
26 const std::shared_ptr<const SymbolicNode>& right() const noexcept { return right_; }
27 Op op() const noexcept { return op_; }
28
29 void accept(LMCAS::detail::SymbolicVisitor& visitor) const override { LMCAS::detail::SymbolicVisitor::DepthGuard guard(visitor); visitor.visit(*this); }
30
31 std::shared_ptr<const SymbolicNode> clone() const override {
32 return LMCAS::detail::make_node<RelationalNode>(left_->clone(), right_->clone(), op_);
33 }
34
35 int type_priority() const override { return 100; }
36
37 std::size_t compute_hash() const override {
38 std::size_t h = std::hash<int>{}((int)op_);
39 hash_combine(h, left_->hash());
40 hash_combine(h, right_->hash());
41 return h;
42 }
43
44 int compare_same_type(const SymbolicNode& other) const override {
45 const auto& o = static_cast<const RelationalNode&>(other);
46 if (op_ != o.op_) {
47 return (int)op_ < (int)o.op_ ? -1 : 1;
48 }
49 int cmp = left_->compare(*o.left_);
50 if (cmp != 0) {
51 return cmp;
52 }
53 return right_->compare(*o.right_);
54 }
55
56 /**
57 * @brief 将关系运算符转换为字符串表示。
58 * @param op 关系运算符
59 * @return 对应的字符串(如 "=", "<", ">=" 等)
60 */
61 static std::string op_to_string(Op op) {
62 switch(op) {
63 case Op::EQ: return "=";
64 case Op::NEQ: return "!=";
65 case Op::LT: return "<";
66 case Op::GT: return ">";
67 case Op::LEQ: return "<=";
68 case Op::GEQ: return ">=";
69 default: return "?";
70 }
71 }
72};
73class LogicalNode : public SymbolicNode {
74public:
75 enum class Op {
76 And, /**< 逻辑与。 */
77 Or, /**< 逻辑或。 */
78 Not, /**< 一元逻辑非,right 为 nullptr。 */
79 Implies /**< 逻辑蕴含 A ⇒ B。 */
80 };
81
82private:
84
85 const std::shared_ptr<const SymbolicNode> left_;
86 const std::shared_ptr<const SymbolicNode> right_;
87 const Op op_;
88
89 LogicalNode(std::shared_ptr<const SymbolicNode> l, std::shared_ptr<const SymbolicNode> r, Op o)
90 : left_(std::move(l)), right_(std::move(r)), op_(o) {
91 if (!left_) {
92 throw std::invalid_argument("LogicalNode left operand cannot be null");
93 }
94 if (op_ != Op::Not && !right_) {
95 throw std::invalid_argument("LogicalNode binary right operand cannot be null");
96 }
97 }
98
99public:
100 const std::shared_ptr<const SymbolicNode>& left() const noexcept { return left_; }
101 const std::shared_ptr<const SymbolicNode>& right() const noexcept { return right_; }
102 Op op() const noexcept { return op_; }
103
104 void accept(LMCAS::detail::SymbolicVisitor& visitor) const override { LMCAS::detail::SymbolicVisitor::DepthGuard guard(visitor); visitor.visit(*this); }
105
106 std::shared_ptr<const SymbolicNode> clone() const override {
108 left_ ? left_->clone() : nullptr,
109 right_ ? right_->clone() : nullptr,
110 op_);
111 }
112
113 int type_priority() const override { return 101; }
114
115 std::size_t compute_hash() const override {
116 std::size_t h = std::hash<int>{}((int)op_);
117 if (left_) hash_combine(h, left_->hash());
118 if (right_) hash_combine(h, right_->hash());
119 return h;
120 }
121
122 int compare_same_type(const SymbolicNode& other) const override {
123 const auto& o = static_cast<const LogicalNode&>(other);
124 if (op_ != o.op_) {
125 return (int)op_ < (int)o.op_ ? -1 : 1;
126 }
127 bool l_null = !left_;
128 bool ol_null = !o.left_;
129 if (l_null != ol_null) {
130 return l_null ? -1 : 1;
131 }
132 if (!l_null) {
133 int cmp = left_->compare(*o.left_);
134 if (cmp != 0) {
135 return cmp;
136 }
137 }
138 bool r_null = !right_;
139 bool or_null = !o.right_;
140 if (r_null != or_null) {
141 return r_null ? -1 : 1;
142 }
143 if (!r_null) {
144 return right_->compare(*o.right_);
145 }
146 return 0;
147 }
148
149 /**
150 * @brief 将逻辑运算符转换为字符串表示。
151 * @param op 逻辑运算符
152 * @return "And"、"Or"、"Not" 或 "Implies"
153 */
154 static std::string op_to_string(Op op) {
155 switch(op) {
156 case Op::And: return "And";
157 case Op::Or: return "Or";
158 case Op::Not: return "Not";
159 case Op::Implies: return "Implies";
160 default: return "?";
161 }
162 }
163};
164
165}
@ Not
一元逻辑非,right 为 nullptr。
@ Implies
逻辑蕴含 A ⇒ B。
int type_priority() const override
LogicalNode(std::shared_ptr< const SymbolicNode > l, std::shared_ptr< const SymbolicNode > r, Op o)
const std::shared_ptr< const SymbolicNode > left_
std::shared_ptr< const SymbolicNode > clone() const override
const std::shared_ptr< const SymbolicNode > right_
Op op() const noexcept
void accept(LMCAS::detail::SymbolicVisitor &visitor) const override
const std::shared_ptr< const SymbolicNode > & right() const noexcept
int compare_same_type(const SymbolicNode &other) const override
const std::shared_ptr< const SymbolicNode > & left() const noexcept
static std::string op_to_string(Op op)
将逻辑运算符转换为字符串表示。
std::size_t compute_hash() const override
RelationalNode(std::shared_ptr< const SymbolicNode > l, std::shared_ptr< const SymbolicNode > r, Op o)
void accept(LMCAS::detail::SymbolicVisitor &visitor) const override
Op op() const noexcept
std::shared_ptr< const SymbolicNode > clone() const override
int type_priority() const override
LMCAS::RelationOp Op
const std::shared_ptr< const SymbolicNode > & right() const noexcept
const std::shared_ptr< const SymbolicNode > left_
const std::shared_ptr< const SymbolicNode > & left() const noexcept
const std::shared_ptr< const SymbolicNode > right_
static std::string op_to_string(Op op)
将关系运算符转换为字符串表示。
int compare_same_type(const SymbolicNode &other) const override
std::size_t compute_hash() const override
SymbolicNode()=default
virtual void visit(const NumberNode &node)=0
std::shared_ptr< const Node > make_node(Args &&... args)
__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
void hash_combine(std::size_t &seed, std::size_t value)
将一个哈希值混合到种子中,用于组合多个字段的哈希.
RelationOp
Stable relational operator used by public APIs.