LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
normalization_trigonometric.cpp
浏览该文件的文档.
2
3namespace LMCAS {
4namespace {
5std::shared_ptr<const SymbolicNode> scaled_radical(
6 const Rational& coefficient, const BigInt& radicand, detail::RewriteBudget* budget) {
7 if (budget) { budget->require_nodes(coefficient == Rational(1) ? 3 : 5); }
9 detail::make_node<NumberNode>(Rational(1, 2)));
10 if (coefficient == Rational(1)) {
11 return root;
12 }
13 if (coefficient == Rational(-1)) {
14 return make_normalized_multiply_node({detail::make_node<NumberNode>(BigInt(-1)), root}, budget);
15 }
16 return make_normalized_multiply_node({detail::make_node<NumberNode>(coefficient), root}, budget);
17}
18
19std::shared_ptr<const SymbolicNode> thirds_pi(
20 FunctionNode::FuncType type, const BigInt& numerator, detail::RewriteBudget* budget) {
21 switch (type) {
23 return scaled_radical(numerator < BigInt(3) ? Rational(1, 2) : Rational(-1, 2), BigInt(3), budget);
25 const bool positive = numerator == BigInt(1) || numerator == BigInt(5);
26 return detail::make_node<NumberNode>(positive ? Rational(1, 2) : Rational(-1, 2));
27 }
29 const bool positive = numerator == BigInt(1) || numerator == BigInt(4);
30 return scaled_radical(positive ? Rational(1) : Rational(-1), BigInt(3), budget);
31 }
32 default:
33 return nullptr;
34 }
35}
36
37std::shared_ptr<const SymbolicNode> quarters_pi(
38 FunctionNode::FuncType type, const BigInt& numerator, detail::RewriteBudget* budget) {
39 switch (type) {
41 return scaled_radical(numerator < BigInt(4) ? Rational(1, 2) : Rational(-1, 2), BigInt(2), budget);
43 const bool positive = numerator == BigInt(1) || numerator == BigInt(7);
44 return scaled_radical(positive ? Rational(1, 2) : Rational(-1, 2), BigInt(2), budget);
45 }
47 const bool positive = numerator == BigInt(1) || numerator == BigInt(5);
48 return detail::make_node<NumberNode>(BigInt(positive ? 1 : -1));
49 }
50 default:
51 return nullptr;
52 }
53}
54
55std::shared_ptr<const SymbolicNode> sixths_pi(
56 FunctionNode::FuncType type, const BigInt& numerator, detail::RewriteBudget* budget) {
57 switch (type) {
59 return detail::make_node<NumberNode>(numerator < BigInt(6) ? Rational(1, 2) : Rational(-1, 2));
61 const bool positive = numerator == BigInt(1) || numerator == BigInt(11);
62 return scaled_radical(positive ? Rational(1, 2) : Rational(-1, 2), BigInt(3), budget);
63 }
65 const bool positive = numerator == BigInt(1) || numerator == BigInt(7);
66 return scaled_radical(positive ? Rational(1, 3) : Rational(-1, 3), BigInt(3), budget);
67 }
68 default:
69 return nullptr;
70 }
71}
72
73std::shared_ptr<const SymbolicNode> halves_pi(FunctionNode::FuncType type, const BigInt& numerator) {
74 if (type == FunctionNode::FuncType::Sin) {
75 return detail::make_node<NumberNode>(BigInt(numerator == BigInt(1) ? 1 : -1));
76 }
77 if (type == FunctionNode::FuncType::Cos) {
78 return detail::make_node<NumberNode>(BigInt(0));
79 }
80 return nullptr;
81}
82
83std::shared_ptr<const SymbolicNode> integer_pi(FunctionNode::FuncType type, const BigInt& numerator) {
85 return detail::make_node<NumberNode>(BigInt(0));
86 }
87 if (type == FunctionNode::FuncType::Cos) {
88 return detail::make_node<NumberNode>(BigInt(numerator.is_zero() ? 1 : -1));
89 }
90 return nullptr;
91}
92}
93
94std::shared_ptr<const SymbolicNode> normalization_pi_function(
95 FunctionNode::FuncType type, const Rational& coefficient, detail::RewriteBudget* budget) {
96 if (budget) { budget->require_nodes(1); }
97 const BigInt& denominator = coefficient.get_denominator();
98 const BigInt period = denominator * BigInt(2);
99 BigInt numerator = coefficient.get_numerator() % period;
100 if (numerator.is_negative()) numerator += period;
101 if (denominator == BigInt(1)) {
102 return integer_pi(type, numerator);
103 }
104 if (denominator == BigInt(2)) {
105 return halves_pi(type, numerator);
106 }
107 if (denominator == BigInt(3)) {
108 return thirds_pi(type, numerator, budget);
109 }
110 if (denominator == BigInt(4)) {
111 return quarters_pi(type, numerator, budget);
112 }
113 if (denominator == BigInt(6)) {
114 return sixths_pi(type, numerator, budget);
115 }
116 return nullptr;
117}
118}
void require_nodes(std::size_t nodes) const
std::shared_ptr< const SymbolicNode > thirds_pi(FunctionNode::FuncType type, const BigInt &numerator, detail::RewriteBudget *budget)
std::shared_ptr< const SymbolicNode > scaled_radical(const Rational &coefficient, const BigInt &radicand, detail::RewriteBudget *budget)
std::shared_ptr< const SymbolicNode > halves_pi(FunctionNode::FuncType type, const BigInt &numerator)
std::shared_ptr< const SymbolicNode > sixths_pi(FunctionNode::FuncType type, const BigInt &numerator, detail::RewriteBudget *budget)
std::shared_ptr< const SymbolicNode > integer_pi(FunctionNode::FuncType type, const BigInt &numerator)
std::shared_ptr< const SymbolicNode > quarters_pi(FunctionNode::FuncType type, const BigInt &numerator, detail::RewriteBudget *budget)
std::shared_ptr< const Node > make_node(Args &&... args)
std::shared_ptr< const SymbolicNode > normalization_pi_function(FunctionNode::FuncType type, const Rational &coefficient, detail::RewriteBudget *budget=nullptr)
std::shared_ptr< const SymbolicNode > make_normalized_multiply_node(const std::vector< std::shared_ptr< const SymbolicNode > > &operands, detail::RewriteBudget *budget=nullptr)
const ExprPtr & denominator