LMCAS
Lamina Computer Algebra System
Toggle main menu visibility
载入中...
搜索中...
未找到
facts_query_rational.cpp
浏览该文件的文档.
1
#include "
internal/facts_query.hpp
"
2
#include "
computation_context.hpp
"
3
#include "
internal/exact_constant_bounds.hpp
"
4
#include "
internal/normalization_utils.hpp
"
5
#include "
internal/symbolic_ast.hpp
"
6
7
#include <algorithm>
8
9
namespace
LMCAS::detail
{
10
using
Node
= std::shared_ptr<const SymbolicNode>;
11
12
std::optional<Rational>
closed_rational_value
(
13
const
Node
&
node
,
Domain
domain
,
ComputationContext
&
context
);
14
15
namespace
{
16
17
std::optional<Rational>
closed_rational_power
(
18
const
PowerNode
& power,
Domain
domain
,
ComputationContext
&
context
,
19
ExactBoundArithmetic
& arithmetic) {
20
auto
base =
closed_rational_value
(power.
base
(),
domain
,
context
);
21
if
(!base) {
return
std::nullopt; }
22
auto
exponent
=
closed_rational_value
(power.
exponent
(),
domain
,
context
);
23
if
(!
exponent
|| !
exponent
->is_integer()) {
return
std::nullopt; }
24
BigInt count =
exponent
->to_bigint();
25
if
(base->is_zero() && (count.is_negative() ||
26
(count.is_zero() &&
domain
==
Domain::Real
))) {
return
std::nullopt; }
27
if
(count.is_negative()) {
28
*base = arithmetic.
div
(Rational(1), *base);
29
count = -count;
30
}
31
Rational
value
(1);
32
while
(!count.is_zero()) {
33
arithmetic.
step
();
34
if
(count.is_odd()) {
value
= arithmetic.
mul
(
value
, *base); }
35
count = count >> 1;
36
if
(!count.is_zero()) { *base = arithmetic.
mul
(*base, *base); }
37
}
38
return
value
;
39
}
40
41
}
42
43
// Fold closed rational arithmetic without normalization or fact queries.
44
std::optional<Rational>
closed_rational_value
(
45
const
std::shared_ptr<const SymbolicNode>&
node
,
Domain
domain
,
ComputationContext
&
context
) {
46
ExactBoundArithmetic
arithmetic(
context
,
"facts.constant_exponent"
);
47
arithmetic.
require
(
context
.enter_recursion(
"facts.constant_exponent"
));
48
struct
Leave {
49
ComputationContext
&
context
;
50
~Leave() {
context
.leave_recursion(); }
51
} leave{
context
};
52
if
(
const
auto
* number =
dynamic_cast<
const
NumberNode
*
>
(
node
.get())) {
53
auto
value
=
exact_number_as_rational
(*number);
54
arithmetic.
bits
(std::max(
value
.get_numerator().bit_length(),
55
value
.get_denominator().bit_length()));
56
return
value
;
57
}
58
const
auto
* sum =
dynamic_cast<
const
AddNode
*
>
(
node
.get());
59
const
auto
* product =
dynamic_cast<
const
MultiplyNode
*
>
(
node
.get());
60
if
(sum || product) {
61
Rational
value
(product ? 1 : 0);
62
for
(
const
auto
& operand : sum ? sum->operands() : product->operands()) {
63
auto
child =
closed_rational_value
(operand,
domain
,
context
);
64
if
(!child) {
return
std::nullopt; }
65
value
= product ? arithmetic.
mul
(
value
, *child) : arithmetic.
add
(
value
, *child);
66
}
67
return
value
;
68
}
69
const
auto
* power =
dynamic_cast<
const
PowerNode
*
>
(
node
.get());
70
if
(!power) {
return
std::nullopt; }
71
return
closed_rational_power(*power,
domain
,
context
, arithmetic);
72
}
73
74
}
LMCAS::AddNode
加法节点,表示多个操作数的求和。
定义
arithmetic.hpp:55
LMCAS::ComputationContext
定义
computation_context.hpp:99
LMCAS::MultiplyNode
乘法节点,表示多个操作数的乘积。
定义
arithmetic.hpp:134
LMCAS::NumberNode
定义
numbers.hpp:8
LMCAS::PowerNode
幂运算节点,表示 base^exponent。
定义
arithmetic.hpp:218
LMCAS::PowerNode::exponent
const std::shared_ptr< const SymbolicNode > & exponent() const noexcept
定义
arithmetic.hpp:234
LMCAS::PowerNode::base
const std::shared_ptr< const SymbolicNode > & base() const noexcept
定义
arithmetic.hpp:233
LMCAS::detail::ExactBoundArithmetic
常数及根式包围区间共用的内部算术策略。 保守界覆盖未约分 Rational 的交叉乘积及结果;受检入口统一捕获 CasError,仅返回完整结果。
定义
exact_constant_bounds.hpp:23
LMCAS::detail::ExactBoundArithmetic::mul
Rational mul(const Rational &a, const Rational &b) const
定义
exact_constant_bounds.hpp:57
LMCAS::detail::ExactBoundArithmetic::step
void step() const
定义
exact_constant_bounds.hpp:31
LMCAS::detail::ExactBoundArithmetic::add
Rational add(const Rational &a, const Rational &b) const
定义
exact_constant_bounds.hpp:51
LMCAS::detail::ExactBoundArithmetic::div
Rational div(const Rational &a, const Rational &b) const
定义
exact_constant_bounds.hpp:60
LMCAS::detail::ExactBoundArithmetic::require
void require(Result< void > result) const
定义
exact_constant_bounds.hpp:28
LMCAS::detail::ExactBoundArithmetic::bits
void bits(std::size_t count) const
定义
exact_constant_bounds.hpp:32
computation_context.hpp
exact_constant_bounds.hpp
facts_query.hpp
LMCAS::detail::anonymous_namespace{facts_query_rational.cpp}::closed_rational_power
std::optional< Rational > closed_rational_power(const PowerNode &power, Domain domain, ComputationContext &context, ExactBoundArithmetic &arithmetic)
定义
facts_query_rational.cpp:17
LMCAS::detail
多元 Hensel 提升(单变量步)
定义
assumption_context.hpp:31
LMCAS::detail::node
const SymbolicNodePtr & node(const ::LMCAS::SymbolicExpr &expression) noexcept
定义
base.hpp:91
LMCAS::detail::Node
std::shared_ptr< const SymbolicNode > Node
定义
facts_query_rational.cpp:10
LMCAS::detail::domain
RewriteBudget const FactsQuery Domain domain
定义
expression_transform.hpp:17
LMCAS::detail::closed_rational_value
std::optional< Rational > closed_rational_value(const std::shared_ptr< const SymbolicNode > &node, Domain domain, ComputationContext &context)
定义
facts_query_rational.cpp:44
LMCAS::detail::context
std::size_t std::size_t ComputationContext & context
定义
exact_algebraic.hpp:27
LMCAS::Domain
Domain
Mathematical domain hierarchy (least specific → most specific). Complex ⊃ Real ⊃ Algebraic ⊃ Rational...
定义
assumption.hpp:15
LMCAS::Domain::Real
@ Real
Real numbers
定义
assumption.hpp:17
LMCAS::exponent
const ExprPtr & exponent
定义
expr.hpp:215
LMCAS::value
const std::string const ExprPtr & value
定义
expr.hpp:293
LMCAS::exact_number_as_rational
Rational exact_number_as_rational(const NumberNode &node)
定义
normalization_utils.cpp:137
normalization_utils.hpp
symbolic_ast.hpp
内部 AST 定义。
src
facts_query_rational.cpp
生成于
, 为 LMCAS使用
1.18.0