2#include "lmmc/numeric.h"
15 if (factor.empty())
return;
16 if (!text.empty()) text +=
"*";
22std::string Irrational::Basis::to_string()
const {
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());
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());
38std::string Irrational::terms_to_string(
const Terms& terms) {
40 for (
const auto& term : terms) {
41 const bool negative = term.second <
Rational(0);
43 if (negative) result =
"-";
45 result += negative ?
" - " :
" + ";
47 const Rational magnitude = term.second.abs();
48 if (term.first.is_constant()) {
49 result += magnitude.to_string();
52 result += magnitude.is_integer() ? magnitude.to_string() :
53 "(" + magnitude.to_string() +
")";
55 result += term.first.to_string();
58 return result.empty() ?
"0" : result;
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();
67lmmc_real_t Irrational::to_double()
const {
68 const auto rational = as_rational_checked();
70 return terms_to_double(numerator_) / terms_to_double(denominator_);
73std::string Irrational::to_string()
const {
74 const auto rational = as_rational_checked();
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_) +
")";
83 return os <<
value.to_string();
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 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)