LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
irrational_symbolic.cpp
浏览该文件的文档.
1#include "irrational.hpp"
2#include "symbolic.hpp"
4#include <utility>
5
6namespace LMCAS {
7namespace {
8
9std::shared_ptr<SymbolicExpr> symbolic_named_power(const std::string& name,
10 const BigInt& exponent) {
13 return exponent == BigInt(1) ? base :
14 SymbolicExpr::power(base, SymbolicExpr::number(exponent));
15}
16
17}
18
19std::shared_ptr<SymbolicExpr> Irrational::Basis::to_symbolic() const {
20 std::shared_ptr<SymbolicExpr> result;
21 auto append = [&](std::shared_ptr<SymbolicExpr> factor) {
22 result = result ? SymbolicExpr::multiply(result, std::move(factor)) : std::move(factor);
23 };
24 if (!e_power.is_zero()) append(symbolic_named_power("e", e_power));
25 if (!pi_power.is_zero()) append(symbolic_named_power("π", pi_power));
26 if (radicand != BigInt(1)) append(SymbolicExpr::sqrt(SymbolicExpr::number(radicand)));
27 return result ? result : SymbolicExpr::number(1);
28}
29
30std::shared_ptr<SymbolicExpr> Irrational::terms_to_symbolic(const Terms& terms) {
31 std::shared_ptr<SymbolicExpr> result;
32 for (const auto& term : terms) {
33 auto expression = term.first.is_constant() ? SymbolicExpr::number(term.second) :
34 term.first.to_symbolic();
35 if (!term.first.is_constant() && term.second != Rational(1)) {
36 expression = SymbolicExpr::multiply(SymbolicExpr::number(term.second), expression);
37 }
38 result = result ? SymbolicExpr::add(result, expression) : expression;
39 }
40 return result ? result : SymbolicExpr::number(0);
41}
42
43std::shared_ptr<SymbolicExpr> Irrational::to_symbolic() const {
44 const auto rational = as_rational_checked();
45 if (rational) return SymbolicExpr::number(rational.value());
46 auto numerator = terms_to_symbolic(numerator_);
47 if (denominator_.size() == 1 && denominator_.begin()->first.is_constant() &&
48 denominator_.begin()->second == Rational(1)) return numerator;
49 return SymbolicExpr::divide(numerator, terms_to_symbolic(denominator_));
50}
51
52}
精确实根式及根式、pi、e 的有理组合。
std::shared_ptr< SymbolicExpr > symbolic_named_power(const std::string &name, const BigInt &exponent)
std::shared_ptr<::LMCAS::SymbolicExpr > make_expression_ptr(SymbolicNodePtr root)
std::shared_ptr< const Node > make_node(Args &&... args)
@ Rational
Rational numbers
const ExprPtr & exponent
__attribute__((visibility("default"))) Result< EqvProfile > eqv_profile_from_name(const std const std::string & name
ExprResult rational(const Rational &value)
符号表达式主类 SymbolicExpr 及其工厂方法,运算接口.
内部 AST 定义。