LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
irrational_representation.cpp
浏览该文件的文档.
1#include "irrational.hpp"
2#include "lmmc/numeric.h"
3#include <cmath>
4#include <ostream>
5
6namespace LMCAS {
7namespace {
8
9std::string named_power(const std::string& name, const BigInt& exponent) {
10 if (exponent.is_zero()) return {};
11 return exponent == BigInt(1) ? name : name + "^(" + exponent.to_string() + ")";
12}
13
14void append_factor(std::string& text, const std::string& factor) {
15 if (factor.empty()) return;
16 if (!text.empty()) text += "*";
17 text += factor;
18}
19
20}
21
22std::string Irrational::Basis::to_string() const {
23 std::string text;
24 append_factor(text, named_power("e", e_power));
25 append_factor(text, named_power("π", pi_power));
26 if (radicand != BigInt(1)) append_factor(text, "√" + radicand.to_string());
27 return text;
28}
29
30lmmc_real_t Irrational::Basis::to_double() const {
31 lmmc_real_t value = 1.0;
32 if (!e_power.is_zero()) value *= std::exp(e_power.to_double());
33 if (!pi_power.is_zero()) value *= std::pow(LMMC_PI, pi_power.to_double());
34 if (radicand != BigInt(1)) value *= std::sqrt(radicand.to_double());
35 return value;
36}
37
38std::string Irrational::terms_to_string(const Terms& terms) {
39 std::string result;
40 for (const auto& term : terms) {
41 const bool negative = term.second < Rational(0);
42 if (result.empty()) {
43 if (negative) result = "-";
44 } else {
45 result += negative ? " - " : " + ";
46 }
47 const Rational magnitude = term.second.abs();
48 if (term.first.is_constant()) {
49 result += magnitude.to_string();
50 } else {
51 if (magnitude != Rational(1)) {
52 result += magnitude.is_integer() ? magnitude.to_string() :
53 "(" + magnitude.to_string() + ")";
54 }
55 result += term.first.to_string();
56 }
57 }
58 return result.empty() ? "0" : result;
59}
60
61lmmc_real_t Irrational::terms_to_double(const Terms& terms) {
62 lmmc_real_t result = 0.0;
63 for (const auto& term : terms) result += term.second.to_double() * term.first.to_double();
64 return result;
65}
66
67lmmc_real_t Irrational::to_double() const {
68 const auto rational = as_rational_checked();
69 if (rational) return rational.value().to_double();
70 return terms_to_double(numerator_) / terms_to_double(denominator_);
71}
72
73std::string Irrational::to_string() const {
74 const auto rational = as_rational_checked();
75 if (rational) return rational.value().to_string();
76 const std::string numerator = terms_to_string(numerator_);
77 if (denominator_.size() == 1 && denominator_.begin()->first.is_constant() &&
78 denominator_.begin()->second == Rational(1)) return numerator;
79 return "(" + numerator + ")/(" + terms_to_string(denominator_) + ")";
80}
81
82std::ostream& operator<<(std::ostream& os, const Irrational& value) {
83 return os << value.to_string();
84}
85
86}
精确实根式及根式、pi、e 的有理组合。
void append_factor(std::string &text, const std::string &factor)
std::string named_power(const std::string &name, const BigInt &exponent)
@ Rational
Rational numbers
const ExprPtr & exponent
const std::string const ExprPtr & value
__attribute__((visibility("default"))) Result< EqvProfile > eqv_profile_from_name(const std const std::string & name
std::ostream & operator<<(std::ostream &os, const Polynomial< T > &p)
多项式流输出运算符
ExprResult rational(const Rational &value)