LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
irrational.hpp
浏览该文件的文档.
1/** @file irrational.hpp
2 * @brief 精确实根式及根式、pi、e 的有理组合。
3 */
4#pragma once
5
6#include "assumption.hpp"
8#include "rational.hpp"
9#include "lmcas_export.hpp"
10#include <map>
11#include <memory>
12#include <iosfwd>
13#include <string>
14#include <utility>
15
16namespace LMCAS {
17
18class SymbolicExpr;
19
20/**
21 * @brief 保留精确系数和无平方因子的被开方数。
22 *
23 * 商保留精确分子与分母,仅 to_double() 执行舍入;相等性比较使用精确交叉乘积。
24 * 排序及 is_positive()/is_negative() 使用近似判断,不能作为精确证明。
25 * sign_checked()/abs_checked() 使用认证包围区间;含 pi/e 的组合在 4096 位时仍跨零,
26 * 返回 Inconclusive。纯根式持续细化,直至完成认证或耗尽预算。
27 * 无上下文的基本运算与 BigInt 一样不限预算;受检便捷入口及 abs() 使用默认 ComputationContext。
28 */
29class LMCAS_API Irrational {
30public:
31 enum class Type { SQRT, PI, E, LOG, COMPLEX };
32
33 Irrational() = default;
34 /** @brief 负被开方数返回 DomainError;预算耗尽时返回错误,保持精确性。 */
35 static Result<Irrational> sqrt_checked(const BigInt& n, lmmc_real_t coefficient,
36 ComputationContext& context);
37 static Result<Irrational> sqrt_checked(const BigInt& n,
38 lmmc_real_t coefficient = 1.0);
39 static Irrational pi(lmmc_real_t coefficient = 1.0);
40 static Irrational e(lmmc_real_t coefficient = 1.0);
41 static Irrational constant(lmmc_real_t value);
42 static Irrational constant(const Rational& value);
43
44 void to_complex() { type_ = Type::COMPLEX; }
45 Type get_type() const { return type_; }
46 Irrational operator+(const Irrational& other) const;
47 Irrational operator-(const Irrational& other) const;
48 Irrational operator*(const Irrational& other) const;
49 Irrational operator*(const Rational& scalar) const;
50 Irrational operator*(lmmc_real_t scalar) const;
51 Irrational operator/(const Irrational& other) const;
52 Irrational operator-() const;
53 Irrational pow(const BigInt& exponent) const;
54 bool operator==(const Irrational& other) const;
55 bool operator<(const Irrational& other) const;
56 bool operator<=(const Irrational& other) const;
57 bool operator>(const Irrational& other) const;
58 bool operator>=(const Irrational& other) const;
59 bool is_zero() const;
60 bool is_rational() const;
61 Result<Rational> as_rational_checked() const;
62 bool is_positive() const;
63 bool is_negative() const;
64 /** @brief 认证符号,成功值为 Negative、Zero 或 Positive。 */
65 Result<Sign> sign_checked(ComputationContext& context) const;
66 Result<Sign> sign_checked() const;
67 Result<Irrational> abs_checked(ComputationContext& context) const;
68 Result<Irrational> abs_checked() const;
69 /**
70 * @brief 计算经过认证的绝对值。
71 * @throws std::domain_error 对应 DomainError。
72 * @throws std::length_error 对应 ResourceLimit。
73 * @throws std::logic_error 对应 InternalInvariant。
74 * @throws std::runtime_error 对应其余受检失败(包括 Inconclusive、Cancelled),附带原因。
75 */
76 Irrational abs() const;
77 void simplify();
78 lmmc_real_t to_double() const;
79 std::string to_string() const;
80 std::shared_ptr<SymbolicExpr> to_symbolic() const;
81 friend LMCAS_API std::ostream& operator<<(std::ostream& os, const Irrational& value);
82
83private:
84 /** @brief 用任意精度整数保存数学指数与被开方数。 */
85 struct Basis {
86 BigInt radicand{1};
87 BigInt pi_power{0};
88 BigInt e_power{0};
89 bool operator<(const Basis& other) const;
90 bool operator==(const Basis& other) const;
91 bool is_constant() const;
92 std::string to_string() const;
93 lmmc_real_t to_double() const;
94 std::shared_ptr<SymbolicExpr> to_symbolic() const;
95 };
96 using Terms = std::map<Basis, Rational>;
97 Type type_ = Type::COMPLEX;
98 Terms numerator_;
99 Terms denominator_{{Basis{}, Rational(1)}};
100
101 static Terms multiply_terms(const Terms& left, const Terms& right);
102 static void add_term(Terms& terms, const Basis& basis, const Rational& coefficient);
103 static std::string terms_to_string(const Terms& terms);
104 static lmmc_real_t terms_to_double(const Terms& terms);
105 static std::shared_ptr<SymbolicExpr> terms_to_symbolic(const Terms& terms);
106 struct EnclosureRound;
107 static std::pair<Rational, Rational> terms_bounds(
108 const Terms& terms, EnclosureRound& round);
109 static Result<std::pair<BigInt, BigInt>> extract_square_factors(
110 const BigInt& n, ComputationContext& context);
111};
112
113} // namespace LMCAS
Core enumerations and forward declarations for the Assumption System.
#define LMCAS_API
bool is_zero(const SymbolicExpr &expression)
ExprResult pow(const ExprPtr &base, const ExprPtr &exponent, ComputationContext &context)
ExprResult e()
@ Rational
Rational numbers
ExprResult simplify(const ExprPtr &expression, ComputationContext &context)
ExprResult pi()
ExprResult abs(const ExprPtr &expression, ComputationContext &context)
std::ostream & operator<<(std::ostream &os, const Polynomial< T > &p)
多项式流输出运算符
精确有理数 Rational,基于 BigInt 分子/分母表示,自动约分。