LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
interval.hpp
浏览该文件的文档.
1/**
2 * @file interval.hpp
3 * @brief 区间与区间并集 IntervalUnion,用于不等式解集表示。
4 */
5#pragma once
7#include "symbolic.hpp"
8#include <vector>
9#include <string>
10#include <memory>
11#include <optional>
12
13namespace LMCAS {
14
15class InequalitySolver;
16
17/** @brief 不等式类型 */
18enum class InequalityType {
19 GreaterThan, ///< 严格大于
20 GreaterEqual, ///< 大于等于
21 LessThan, ///< 严格小于
22 LessEqual ///< 小于等于
23};
24
25/** @brief 区间端点,支持有限值与正负无穷 */
26struct LMCAS_API Endpoint {
27 std::shared_ptr<SymbolicExpr> value{}; ///< 端点值(无穷时为空)
28 bool is_open = false; ///< 是否为开端点
29 bool is_neg_infinity = false; ///< 是否为负无穷
30 bool is_pos_infinity = false; ///< 是否为正无穷
31
32 /**
33 * @brief 构造负无穷端点
34 * @return 负无穷端点
35 */
36 static Endpoint neg_inf();
37
38 /**
39 * @brief 构造正无穷端点
40 * @return 正无穷端点
41 */
42 static Endpoint pos_inf();
43
44 /**
45 * @brief 构造闭端点
46 * @param val 端点值
47 * @return 闭端点
48 */
49 static Endpoint closed(std::shared_ptr<SymbolicExpr> val);
50
51 /**
52 * @brief 构造开端点
53 * @param val 端点值
54 * @return 开端点
55 */
56 static Endpoint open(std::shared_ptr<SymbolicExpr> val);
57};
58
59/** @brief 单个区间 [a, b] 或 (a, b) 等 */
60struct LMCAS_API Interval {
61 Endpoint lower; ///< 下端点
62 Endpoint upper; ///< 上端点
63
64 /**
65 * @brief 判断数值是否在区间内
66 * @param value 待判断的数值
67 * @return 包含返回 true
68 */
69 bool contains(double value) const;
70
71 /**
72 * @brief 判断区间是否为空
73 * @return 空区间返回 true
74 */
75 bool is_empty() const;
76
77 /**
78 * @brief 判断区间是否为整条实数轴
79 * @return 是整条实数轴返回 true
80 */
81 bool is_entire_line() const;
82
83 /**
84 * @brief 构造空区间
85 * @return 空区间
86 */
87 static Interval empty();
88
89 /**
90 * @brief 构造整条实数轴 (-∞, +∞)
91 * @return 全实数轴区间
92 */
93 static Interval entire_line();
94
95 /**
96 * @brief 构造单点区间 [val, val]
97 * @param val 点值
98 * @return 单点区间
99 */
100 static Interval point(std::shared_ptr<SymbolicExpr> val);
101};
102
103/** @brief 区间并集,表示不等式的解集 */
104class LMCAS_API IntervalUnion {
105public:
106 IntervalUnion();
107
108 /**
109 * @brief 从区间列表构造并集
110 * @param intervals 区间列表
111 */
112 explicit IntervalUnion(std::vector<Interval> intervals);
113
114 /**
115 * @brief 从单个区间构造并集
116 * @param iv 区间
117 * @return 包含单个区间的并集
118 */
119 static IntervalUnion from_single(const Interval& iv);
120
121 /** @brief Validate and construct an interval union with an explicit context. */
122 static Result<IntervalUnion> from_intervals_checked(
123 std::vector<Interval> intervals,
124 ComputationContext& context);
125
126 /** @brief Validate and construct an interval union with a default context. */
127 static Result<IntervalUnion> from_intervals_checked(
128 std::vector<Interval> intervals);
129
130 /**
131 * @brief 构造空集
132 * @return 空的区间并集
133 */
134 static IntervalUnion empty();
135
136 /**
137 * @brief 构造全实数轴
138 * @return 表示 (-∞, +∞) 的并集
139 */
140 static IntervalUnion entire_line();
141
142 /**
143 * @brief 与另一个并集求交
144 * @param other 另一个区间并集
145 * @return 交集结果
146 */
147 IntervalUnion intersect(const IntervalUnion& other) const;
148
149 /**
150 * @brief Checked intersection with explicit endpoint-validation errors.
151 * @param other 另一个区间并集
152 * @param context 计算上下文和资源预算
153 * @return 成功时返回交集;端点不可比较或资源耗尽时返回错误
154 */
155 Result<IntervalUnion> intersect_checked(
156 const IntervalUnion& other,
157 ComputationContext& context) const;
158
159 Result<IntervalUnion> intersect_checked(const IntervalUnion& other) const;
160
161 /**
162 * @brief 与另一个并集求并
163 * @param other 另一个区间并集
164 * @return 并集结果
165 */
166 IntervalUnion unite(const IntervalUnion& other) const;
167
168 /**
169 * @brief Checked union with exact endpoint ordering.
170 * @param other 另一个区间并集
171 * @param context 计算上下文和资源预算
172 * @return 成功时返回并集;端点不可比较或资源耗尽时返回错误
173 */
174 Result<IntervalUnion> unite_checked(
175 const IntervalUnion& other,
176 ComputationContext& context) const;
177
178 Result<IntervalUnion> unite_checked(const IntervalUnion& other) const;
179
180 /**
181 * @brief 求补集
182 * @return 补集结果
183 */
184 IntervalUnion complement() const;
185
186 /**
187 * @brief Checked complement with exact endpoint ordering.
188 * @param context 计算上下文和资源预算
189 * @return 成功时返回补集;端点不可比较或资源耗尽时返回错误
190 */
191 Result<IntervalUnion> complement_checked(ComputationContext& context) const;
192
193 Result<IntervalUnion> complement_checked() const;
194
195 /**
196 * @brief 判断数值是否在并集内
197 * @param value 待判断的数值
198 * @return 包含返回 true
199 */
200 bool contains(double value) const;
201
202 /**
203 * @brief 判断并集是否为空
204 * @return 空集返回 true
205 */
206 bool is_empty() const;
207
208 /**
209 * @brief 判断并集是否为全实数轴
210 * @return 是全实数轴返回 true
211 */
212 bool is_entire_line() const;
213
214 /**
215 * @brief 获取内部区间列表
216 * @return 区间列表的常引用
217 */
218 const std::vector<Interval>& intervals() const;
219
220 /**
221 * @brief 转换为字符串表示
222 * @return 区间并集的字符串形式
223 */
224 std::string to_string() const;
225
226 /**
227 * @brief 从规范字符串解析数值端点区间并集
228 * @param str 区间表示;端点支持任意精度十进制整数、精确分数和有限浮点数
229 * @return 解析及区间规范化成功时返回并集;无效输入返回 nullopt
230 */
231 static std::optional<IntervalUnion> parse(const std::string& str);
232
233 /**
234 * @brief 转换为符号表达式
235 * @param var 变量名
236 * @return 表示该区间并集的符号表达式;空集为有限空集,全实数域为恒真关系
237 */
238 std::shared_ptr<SymbolicExpr> to_expr(const std::string& var) const;
239
240private:
241 friend class InequalitySolver;
242
243 std::vector<Interval> intervals_;
244 void normalize();
245 static IntervalUnion from_checked_normalized(std::vector<Interval> intervals);
246};
247
248/** @brief Checked numeric membership test for an interval. */
250 const Interval& interval,
251 double value,
253
255 const Interval& interval,
256 double value);
257
258/** @brief Checked emptiness decision for an interval. */
260 const Interval& interval,
262
264
265/**
266 * @brief Validate, sort, remove empty intervals, and merge proven overlaps.
267 *
268 * Exact endpoints remain exact. Approximate endpoints are ordered by their
269 * exact IEEE binary values. Any incomparable or invalid endpoint returns an
270 * error before a normalized list is produced.
271 */
273 std::vector<Interval> intervals,
275
277 std::vector<Interval> intervals);
278
279}
#define LMCAS_API
ExpressionResult normalize(const Node &value, ComputationContext &context)
const std::string const LimitExprPtr & point
__attribute__((visibility("default"))) ExprResult function(const std __attribute__((visibility("default"))) ExprResult finite_set(std ExprPtr upper
const std::vector< Polynomial< BigInt > > const BigInt ComputationContext & context
ExprResult interval(ExprPtr lower, ExprPtr upper, bool lower_closed, bool upper_closed)
Result< bool > interval_is_empty_checked(const Interval &interval, ComputationContext &context)
const ExprPtr & lower
Result< std::vector< Interval > > normalize_intervals_checked(std::vector< Interval > intervals, ComputationContext &context)
bool contains(const SymbolicExpr &expression, const std::string &variable)
Result< bool > interval_contains_checked(const Interval &interval, double value, ComputationContext &context)
InequalityType
不等式类型
const std::string const ExprPtr & value
符号表达式主类 SymbolicExpr 及其工厂方法,运算接口.