LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
assumption_serialization.cpp
浏览该文件的文档.
4#include <sstream>
5
6namespace LMCAS {
7
8namespace {
9
10std::string sign_to_string(Sign s) {
11 switch (s) {
12 case Sign::Positive: return "Positive";
13 case Sign::Negative: return "Negative";
14 case Sign::NonNegative: return "NonNegative";
15 case Sign::NonPositive: return "NonPositive";
16 case Sign::Zero: return "Zero";
17 case Sign::NonZero: return "NonZero";
18 }
19 return "Positive";
20}
21
22std::string parity_to_string(Parity p) {
23 switch (p) {
24 case Parity::Even: return "Even";
25 case Parity::Odd: return "Odd";
26 case Parity::Unknown: return "Unknown";
27 }
28 return "Unknown";
29}
30
32 switch (b) {
33 case Boundedness::Bounded: return "Bounded";
34 case Boundedness::Unbounded: return "Unbounded";
35 case Boundedness::Unknown: return "Unknown";
36 }
37 return "Unknown";
38}
39
41 switch (f) {
42 case Finiteness::Finite: return "Finite";
43 case Finiteness::Divergent: return "Divergent";
44 case Finiteness::Unknown: return "Unknown";
45 }
46 return "Unknown";
47}
48
50 switch (d) {
51 case Definiteness::PositiveDefinite: return "PositiveDefinite";
52 case Definiteness::PositiveSemiDefinite: return "PositiveSemiDefinite";
53 case Definiteness::NegativeDefinite: return "NegativeDefinite";
54 case Definiteness::NegativeSemiDefinite: return "NegativeSemiDefinite";
55 case Definiteness::Indefinite: return "Indefinite";
56 case Definiteness::Unknown: return "Unknown";
57 }
58 return "Unknown";
59}
60
62 switch (m) {
63 case Monotonicity::Increasing: return "Increasing";
64 case Monotonicity::Decreasing: return "Decreasing";
65 case Monotonicity::NonDecreasing: return "NonDecreasing";
66 case Monotonicity::NonIncreasing: return "NonIncreasing";
67 case Monotonicity::Unknown: return "Unknown";
68 }
69 return "Unknown";
70}
71
73 switch (op) {
74 case RelationalNode::Op::GT: return "GT";
75 case RelationalNode::Op::LT: return "LT";
76 case RelationalNode::Op::GEQ: return "GEQ";
77 case RelationalNode::Op::LEQ: return "LEQ";
78 case RelationalNode::Op::NEQ: return "NEQ";
79 case RelationalNode::Op::EQ: return "EQ";
80 }
81 return "EQ";
82}
83std::string endpoint_to_string(const Endpoint& ep) {
84 if (ep.is_neg_infinity) return "-inf";
85 if (ep.is_pos_infinity) return "+inf";
86 if (ep.value) return ep.value->to_string();
87 return "0";
88}
89std::string interval_to_string(const Interval& iv) {
90 std::string result;
91 result += iv.lower.is_open ? "(" : "[";
92 result += endpoint_to_string(iv.lower);
93 result += ", ";
94 result += endpoint_to_string(iv.upper);
95 result += iv.upper.is_open ? ")" : "]";
96 return result;
97}
98
99void serialize_symbol_properties(std::ostream& out, const PropertyStore& props,
100 const std::string& sym) {
101 Domain dom = props.get_domain(sym);
102 if (dom != Domain::Complex) {
103 out << "DOMAIN " << sym << " " << property_detail::domain_str(dom) << "\n";
104 }
105 auto signs = props.get_signs(sym);
106 for (Sign s : signs) {
107 out << "SIGN " << sym << " " << sign_to_string(s) << "\n";
108 }
109 Parity par = props.get_parity(sym);
110 if (par != Parity::Unknown) {
111 out << "PARITY " << sym << " " << parity_to_string(par) << "\n";
112 }
113 Boundedness bnd = props.get_boundedness(sym);
114 if (bnd != Boundedness::Unknown) {
115 out << "BOUNDED " << sym << " " << boundedness_to_string(bnd);
116 if (bnd == Boundedness::Bounded) {
117 const auto bounds = props.get_bounds(sym);
118 if (bounds) {
119 out << " " << interval_to_string(*bounds);
120 }
121 }
122 out << "\n";
123 }
124 if (props.is_transcendental(sym)) {
125 out << "TRANSCENDENTAL " << sym << "\n";
126 }
127 Finiteness fin = props.get_finiteness(sym);
128 if (fin != Finiteness::Unknown) {
129 out << "FINITENESS " << sym << " " << finiteness_to_string(fin) << "\n";
130 }
131 Definiteness def = props.get_definiteness(sym);
132 if (def != Definiteness::Unknown) {
133 out << "DEFINITENESS " << sym << " " << definiteness_to_string(def) << "\n";
134 }
135 for (const auto& [variable, period] : props.get_period_decls(sym)) {
136 out << "PERIODIC " << sym << " " << variable << " "
137 << period.to_string() << "\n";
138 }
139}
140
141void serialize_symbol_intervals(std::ostream& out, const PropertyStore& props,
142 const std::string& sym) {
143 auto cont_decls = props.get_continuity_decls(sym);
144 for (const auto& decl : cont_decls) {
145 if (decl.is_differentiable) {
146 out << "DIFFERENTIABLE " << sym << " "
147 << interval_to_string(decl.interval) << "\n";
148 } else {
149 out << "CONTINUOUS " << sym << " "
150 << interval_to_string(decl.interval) << "\n";
151 }
152 }
153
154 auto mono_decls = props.get_monotonicity_decls(sym);
155 for (const auto& decl : mono_decls) {
156 out << "MONOTONICITY " << sym << " " << decl.variable << " "
157 << interval_to_string(decl.interval) << " "
158 << monotonicity_to_string(decl.type) << "\n";
159 }
160}
161
162}
163
164std::string AssumptionContext::serialize() const {
165 std::ostringstream out;
166
167 for (int scope_idx = 0; scope_idx < static_cast<int>(scope_stack_.size()); ++scope_idx) {
168 out << "SCOPE " << scope_idx << "\n";
169 const auto& scope = scope_stack_[scope_idx];
170 const auto& props = scope.properties;
171 auto symbols = props.get_all_symbols();
172 for (const auto& sym : symbols) {
173 serialize_symbol_properties(out, props, sym);
174 }
175 for (const auto& sym : symbols) {
176 serialize_symbol_intervals(out, props, sym);
177 }
178 const auto& relations = scope.relations.get_relations();
179 for (const auto i : scope.relations.declaration_order_) {
180 const auto& rel = relations[i];
181 std::string lhs_str = rel.lhs.to_string();
182 std::string rhs_str = rel.rhs.to_string();
183 out << "RELATION " << lhs_str << " " << relop_to_string(rel.op)
184 << " " << rhs_str << "\n";
185 }
186 for (const auto& cond : scope.conditionals) {
187 std::string cond_str = cond.condition.to_string();
188 std::string concl_str = cond.conclusion.to_string();
189 out << "CONDITIONAL (" << cond_str << ") => (" << concl_str << ")\n";
190 }
191 }
192
193 out << "END\n";
194 return out.str();
195}
196
197}
AssumptionContext class with scoped push/pop management.
LMCAS::RelationOp Op
void serialize_symbol_intervals(std::ostream &out, const PropertyStore &props, const std::string &sym)
void serialize_symbol_properties(std::ostream &out, const PropertyStore &props, const std::string &sym)
std::string domain_str(Domain domain)
const std::string & variable
Domain
Mathematical domain hierarchy (least specific → most specific). Complex ⊃ Real ⊃ Algebraic ⊃ Rational...
@ Complex
Complex numbers (default, least specific)
Boundedness
Boundedness classification for symbols.
@ Unbounded
Symbol is unbounded
@ Unknown
Boundedness not determined
@ Bounded
Symbol has finite bounds
const MultiPoly & b
Monotonicity
Monotonicity classification for functions over intervals.
@ NonDecreasing
Non-decreasing (weakly increasing)
@ Decreasing
Strictly decreasing
@ Unknown
Monotonicity not determined
@ NonIncreasing
Non-increasing (weakly decreasing)
@ Increasing
Strictly increasing
Definiteness
Matrix definiteness classification.
@ NegativeSemiDefinite
All eigenvalues non-positive
@ NegativeDefinite
All eigenvalues negative
@ PositiveSemiDefinite
All eigenvalues non-negative
@ PositiveDefinite
All eigenvalues positive
@ Unknown
Definiteness not determined
@ Indefinite
Mixed eigenvalue signs
const ExprPtr RelationOp op
ExprResult sym(const std::string &name)
Finiteness
Finiteness classification for limits and sequences.
@ Finite
Has a finite value or limit
@ Unknown
Finiteness not determined
@ Divergent
Diverges to infinity or oscillates
Parity
Integer parity classification.
@ Even
Even integer
@ Unknown
Parity not determined
Sign
Sign classification for symbols and expressions.
内部 AST 定义。