LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
expr_values.cpp
浏览该文件的文档.
1#include "expr.hpp"
6#include <cmath>
7#include <exception>
8#include <memory>
9#include <string>
10#include <utility>
11
12namespace LMCAS {
13
14using namespace expr_detail::expr_common;
15
16std::optional<std::string_view> symbol_name(const ExprPtr& expression) noexcept {
17 const auto* variable =
18 dynamic_cast<const VariableNode*>(detail::node(expression).get());
19 if (!variable || variable->is_constant()) return std::nullopt;
20 return variable->name();
21}
22
23std::optional<RelationOp> relation_op(const ExprPtr& expression) noexcept {
24 const auto* relation =
25 dynamic_cast<const RelationalNode*>(detail::node(expression).get());
26 if (!relation) return std::nullopt;
27 return relation->op();
28}
29
30ExprResult sym(const std::string& name) {
31 if (name.empty()) {
33 "symbol name cannot be empty", kSymOperation);
34 }
38 "imaginary unit symbol is reserved",
40 }
42 "reserved mathematical constants cannot be shadowed",
44 }
45 try {
46 auto expression = SymbolicExpr::variable(name);
47 if (!expression) {
49 "symbol factory returned null", kSymOperation);
50 }
51 return ExprResult::success(std::move(expression));
52 } catch (const std::bad_alloc&) {
54 "symbol allocation failed", kSymOperation);
55 } catch (const std::exception& error) {
57 }
58}
59
61 try {
62 return ExprResult::success(SymbolicExpr::number(value));
63 } catch (const std::bad_alloc&) {
65 "integer allocation failed", kIntegerOperation);
66 } catch (const std::exception& error) {
68 }
69}
70
71ExprResult integer(const BigInt& value) {
72 try {
73 return ExprResult::success(SymbolicExpr::number(value));
74 } catch (const std::bad_alloc&) {
76 "integer allocation failed", kIntegerOperation);
77 } catch (const std::exception& error) {
79 }
80}
81
82ExprResult rational(const Rational& value) {
83 try {
84 return ExprResult::success(SymbolicExpr::number(value));
85 } catch (const std::bad_alloc&) {
87 "rational allocation failed", kRationalOperation);
88 } catch (const std::exception& error) {
90 }
91}
92
94 if (!std::isfinite(value)) {
96 "approximate real must be finite", kApproxOperation);
97 }
98 try {
99 return ExprResult::success(SymbolicExpr::number(value));
100 } catch (const std::bad_alloc&) {
102 "approximate real allocation failed", kApproxOperation);
103 } catch (const std::exception& error) {
105 }
106}
107
109 return detail::constant_expression("pi");
110}
111
113 return detail::constant_expression("e");
114}
115
117 return detail::constant_expression("phi");
118}
119
121 auto zero = integer(0);
122 if (!zero) return ExprResult::failure(zero.error());
123 auto one = integer(1);
124 if (!one) return ExprResult::failure(one.error());
125 return complex(zero.value(), one.value());
126}
127
129 if (!real || !imag) {
131 "complex expression parts cannot be null",
133 }
134 try {
138 } catch (const std::bad_alloc&) {
140 "complex expression allocation failed",
142 } catch (const std::exception& error) {
145 }
146}
147
148}
static Result failure(CasErrc code, std::string message, std::string operation={})
static Result success(ExprPtr value)
static std::shared_ptr< const SymbolicNode > create_complex(std::shared_ptr< const SymbolicNode > real, std::shared_ptr< const SymbolicNode > imag)
创建复数节点,自动简化(若虚部为 0,则返回实部).
变量节点,表示一个符号变量。
bool is_imaginary_unit_name(const std::string &name)
std::shared_ptr<::LMCAS::SymbolicExpr > make_expression_ptr(SymbolicNodePtr root)
const SymbolicNodePtr & node(const ::LMCAS::SymbolicExpr &expression) noexcept
ExpressionResult constant_expression(const char *name)
constexpr const char * kIntegerOperation
constexpr const char * kSymOperation
constexpr const char * kApproxOperation
constexpr const char * kComplexOperation
constexpr const char * kRationalOperation
ExprResult expression_failure(CasErrc code, std::string message, const char *operation)
bool is_reserved_symbol_name(const std::string &name)
const std::string & variable
ExprResult imaginary_unit()
ExprResult e()
ExprResult integer(Integer)=delete
ExprResult real(const ExprPtr &expression, ComputationContext &context)
ExprResult approx_real(double value)
Result< ExprPtr > ExprResult
ExprResult complex(ExprPtr real, ExprPtr imag)
std::optional< std::string_view > symbol_name(const ExprPtr &expression) noexcept
ExprPtr imag
std::shared_ptr< SymbolicExpr > ExprPtr
ExprResult sym(const std::string &name)
ExprResult pi()
ExprResult relation(const ExprPtr &lhs, const ExprPtr &rhs, RelationOp op)
const std::string const ExprPtr & value
ExprResult phi()
__attribute__((visibility("default"))) Result< EqvProfile > eqv_profile_from_name(const std const std::string & name
ExprResult rational(const Rational &value)
std::optional< RelationOp > relation_op(const ExprPtr &expression) noexcept
内部 AST 定义。