LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
value.hpp
浏览该文件的文档.
1/**
2 * @file value.hpp
3 * @brief 运行时值类型 Value,支持数值,符号,容器类型.
4 */
5#pragma once
6#include "lmcas_export.hpp"
7#include "bigint.hpp"
8#include "irrational.hpp"
9#include "rational.hpp"
10#include "symbolic.hpp"
11#include "result.hpp"
12#include "lmmc/config.h"
13
14#include <string>
15#include <utility>
16#include <variant>
17#include <vector>
18
19namespace LMCAS {
20
21/** @brief 运行时统一值类型,支持数值,符号,容器等类型的动态表示 */
22class LMCAS_API Value final {
23public:
24 /** @brief 值的类型枚举 */
25 enum class Type {
26 Null, Int, Float, BigInt,
27 Rational, Irrational, Symbolic,
28 Infinity, Array, Matrix,
29 String
30 };
31private:
32 Type type; ///< 当前值的类型
33
34public:
35 /** @brief 内部存储的 variant 类型 */
36 using DataType = std::variant<
37 std::nullptr_t,
38 int, lmmc_real_t,
39 ::LMCAS::BigInt, ::LMCAS::Rational, ::LMCAS::Irrational,
40 std::shared_ptr<SymbolicExpr>,
41 std::vector<Value>,
42 std::vector<std::vector<Value>>>;
43
44private:
45 DataType data; ///< 实际存储的数据
46
47public:
48 /** @brief 返回只读类型标记。 */
49 Type kind() const noexcept { return type; }
50
51 /** @brief 返回只读底层存储,调用方不能破坏类型不变量。 */
52 const DataType& storage() const noexcept { return data; }
53
54 ~Value() = default;
55
56 /** @brief 默认构造,初始化为 Null */
57 Value() : type(Type::Null), data(std::in_place_index<0>, nullptr) {}
58
59 Value(std::nullptr_t) : type(Type::Null), data(std::in_place_index<0>, nullptr) {}
60 Value(int i) : type(Type::Int), data(i) {}
61 Value(lmmc_real_t f);
62 Value(const ::LMCAS::BigInt& bi) : type(Type::BigInt), data(bi) {}
63 Value(const ::LMCAS::Rational& r) : type(Type::Rational), data(r) {}
64 Value(const ::LMCAS::Irrational& ir) : type(Type::Irrational), data(ir) {}
65 Value(const std::shared_ptr<SymbolicExpr>& sym)
66 : type(Type::Symbolic), data(require_symbolic(sym)) {}
67 Value(const std::vector<Value>& arr);
68 Value(const std::vector<std::vector<Value>>& mat)
69 : type(Type::Matrix), data(validate_matrix(mat)) {}
70
71 /// 字符串使用独立的 Type::String 标记,与 Null 保持类型区分.
72 Value(const std::string& s) : type(Type::String), data(nullptr), _str_cache(s) {}
73 Value(const char* s) : type(Type::String), data(nullptr), _str_cache(s ? s : "") {}
74
75 bool is_null() const { return type == Type::Null; }
76 bool is_string() const { return type == Type::String; }
77 bool is_infinity() const { return type == Type::Infinity; }
78 bool is_int() const { return type == Type::Int; }
79 bool is_float() const { return type == Type::Float; }
80 bool is_array() const { return type == Type::Array; }
81 bool is_matrix() const { return type == Type::Matrix; }
82 bool is_bigint() const { return type == Type::BigInt; }
83 bool is_rational() const { return type == Type::Rational; }
84 bool is_irrational() const { return type == Type::Irrational; }
85 bool is_symbolic() const { return type == Type::Symbolic; }
86 bool is_numeric() const;
87
88 /** @brief Checked conversion to a floating-point value. */
89 LMCAS::Result<lmmc_real_t> as_number_checked() const;
90
91
92 /** @brief Checked conversion to a rational value. */
93 LMCAS::Result<::LMCAS::Rational> as_rational_checked() const;
94
95
96 /** @brief Checked conversion to an irrational-value wrapper. */
97 LMCAS::Result<::LMCAS::Irrational> as_irrational_checked() const;
98
99
100 /** @brief Checked conversion to a symbolic expression. */
101 LMCAS::Result<std::shared_ptr<SymbolicExpr>> as_symbolic_checked() const;
102
103
104 /**
105 * @brief 判断值是否可转换为符号表达式
106 * @return 若可转换则返回 true
107 */
108 bool as_symbolic_compatible() const;
109
110 /**
111 * @brief 将值转换为可读字符串
112 * @return 格式化的字符串表示
113 */
114 std::string to_string() const;
115
116 bool operator==(const Value& other) const;
117
118 bool operator<(const Value& other) const;
119
120 /** @brief Checked element-wise vector addition. */
121 LMCAS::Result<Value> vector_add_checked(const Value& other) const;
122
123
124 /** @brief Checked vector dot product. */
125 LMCAS::Result<Value> dot_product_checked(const Value& other) const;
126
127
128 /** @brief Checked matrix multiplication. */
129 LMCAS::Result<Value> matrix_multiply_checked(
130 const Value& other) const;
131
132
133private:
134 static std::shared_ptr<SymbolicExpr> require_symbolic(
135 const std::shared_ptr<SymbolicExpr>& expression);
136
137 static std::vector<std::vector<Value>> validate_matrix(
138 std::vector<std::vector<Value>> matrix);
139
140 std::string _str_cache; ///< 字符串构造时的缓存(兼容旧测试)
141};
142
143} // namespace LMCAS
任意精度整数 BigInt,底层调用 LMMP 实现高性能大数运算。
精确实根式及根式、pi、e 的有理组合。
#define LMCAS_API
Result< void > validate_matrix(const ExactMatrixData &matrix, const std::string &operation)
__attribute__((visibility("default"))) ComplexSymbolicResult complex_exp_form_checked(std __attribute__((visibility("default"))) ComplexSymbolicResult complex_exp_form_checked(std __attribute__((visibility("default"))) ComplexSymbolicResult complex_trig_form_checked(std __attribute__((visibility("default"))) ComplexSymbolicResult complex_trig_form_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_nth_root_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_nth_root_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_quadratic_checked(std __attribute__((visibility("default"))) ComplexRootsResult solve_complex_quadratic_checked(std std::shared_ptr< SymbolicExpr > r
@ Rational
Rational numbers
ExpressionResult matrix_multiply_checked(const std::shared_ptr< SymbolicExpr > &A, const std::shared_ptr< SymbolicExpr > &B, ComputationContext &context)
ExprResult sym(const std::string &name)
精确有理数 Rational,基于 BigInt 分子/分母表示,自动约分。
符号表达式主类 SymbolicExpr 及其工厂方法,运算接口.