yufmath 0.1.1

A Rust CAS Lib.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! # 运行时增强计算引擎
//!
//! 集成运行时增强功能的计算引擎,支持变量管理和复杂度控制

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use crate::core::{Expression, Number, MathConstant};
use super::{ComputeEngine, ComputeError, EnhancedComputeEngine};
use super::runtime_enhancement::{RuntimeEnhancer, RuntimeConfig};

/// 运行时增强计算引擎
pub struct RuntimeEnhancedEngine {
    /// 基础增强计算引擎
    base_engine: EnhancedComputeEngine,
    /// 运行时增强器
    runtime_enhancer: Arc<Mutex<RuntimeEnhancer>>,
}

impl RuntimeEnhancedEngine {
    /// 创建新的运行时增强计算引擎
    pub fn new() -> Self {
        Self {
            base_engine: EnhancedComputeEngine::new(),
            runtime_enhancer: Arc::new(Mutex::new(RuntimeEnhancer::new(RuntimeConfig::default()))),
        }
    }
    
    /// 创建带配置的运行时增强计算引擎
    pub fn with_config(config: RuntimeConfig) -> Self {
        Self {
            base_engine: EnhancedComputeEngine::new(),
            runtime_enhancer: Arc::new(Mutex::new(RuntimeEnhancer::new(config))),
        }
    }
    
    /// 设置变量值
    pub fn set_variable(&self, name: String, value: Expression) -> Result<(), ComputeError> {
        let mut enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        // 检查循环引用
        if enhancer.variable_manager().has_circular_reference(&name) {
            return Err(ComputeError::domain_error(
                format!("变量 '{}' 存在循环引用", name)
            ));
        }
        
        enhancer.variable_manager_mut().set_variable(name, value)
    }
    
    /// 获取变量值
    pub fn get_variable(&self, name: &str) -> Result<Option<Expression>, ComputeError> {
        let enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        Ok(enhancer.variable_manager().get_variable(name).cloned())
    }
    
    /// 获取所有变量
    pub fn get_all_variables(&self) -> Result<HashMap<String, Expression>, ComputeError> {
        let enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        Ok(enhancer.variable_manager().get_all_variables().clone())
    }
    
    /// 清空所有变量
    pub fn clear_variables(&self) -> Result<(), ComputeError> {
        let mut enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        enhancer.variable_manager_mut().clear();
        Ok(())
    }
    
    /// 删除指定变量
    pub fn remove_variable(&self, name: &str) -> Result<bool, ComputeError> {
        let mut enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        Ok(enhancer.variable_manager_mut().remove_variable(name))
    }
    
    /// 更新运行时配置
    pub fn update_runtime_config(&self, config: RuntimeConfig) -> Result<(), ComputeError> {
        let mut enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        enhancer.update_config(config);
        Ok(())
    }
    
    /// 获取运行时配置
    pub fn get_runtime_config(&self) -> Result<RuntimeConfig, ComputeError> {
        let enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        Ok(enhancer.get_config().clone())
    }
    
    /// 安全计算表达式(带运行时增强)
    pub fn safe_compute(&self, expr: &Expression) -> Result<Expression, ComputeError> {
        let enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        enhancer.safe_compute(expr, &self.base_engine)
    }
    
    /// 计算表达式并自动替换变量
    pub fn compute_with_variables(&self, expr: &Expression) -> Result<Expression, ComputeError> {
        let enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        // 替换变量
        let substituted = enhancer.variable_manager().substitute_variables(expr);
        
        // 安全计算
        drop(enhancer); // 释放锁
        self.safe_compute(&substituted)
    }
    
    /// 获取数值变量(用于快速数值计算)
    pub fn get_numeric_variables(&self) -> Result<HashMap<String, Number>, ComputeError> {
        let enhancer = self.runtime_enhancer.lock()
            .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
        
        Ok(enhancer.variable_manager().get_all_numeric_variables().clone())
    }
}

impl ComputeEngine for RuntimeEnhancedEngine {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
    
    fn simplify(&self, expr: &Expression) -> Result<Expression, ComputeError> {
        // 使用安全计算
        self.safe_compute(expr)
    }
    
    fn evaluate(&self, expr: &Expression, vars: &HashMap<String, Number>) -> Result<Number, ComputeError> {
        // 首先替换内部变量,然后使用传入的变量进行求值
        let substituted = {
            let enhancer = self.runtime_enhancer.lock()
                .map_err(|_| ComputeError::internal("无法获取运行时增强器锁"))?;
            enhancer.variable_manager().substitute_variables(expr)
        };
        
        self.base_engine.evaluate(&substituted, vars)
    }
    
    fn differentiate(&self, expr: &Expression, var: &str) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.differentiate(&substituted, var)
    }
    
    fn integrate(&self, expr: &Expression, var: &str) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.integrate(&substituted, var)
    }
    
    fn limit(&self, expr: &Expression, var: &str, point: &Expression) -> Result<Expression, ComputeError> {
        let substituted_expr = self.compute_with_variables(expr)?;
        let substituted_point = self.compute_with_variables(point)?;
        self.base_engine.limit(&substituted_expr, var, &substituted_point)
    }
    
    fn series(&self, expr: &Expression, var: &str, point: &Expression, order: usize) -> Result<Expression, ComputeError> {
        let substituted_expr = self.compute_with_variables(expr)?;
        let substituted_point = self.compute_with_variables(point)?;
        self.base_engine.series(&substituted_expr, var, &substituted_point, order)
    }
    
    fn numerical_evaluate(&self, expr: &Expression, vars: &HashMap<String, f64>) -> Result<f64, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.numerical_evaluate(&substituted, vars)
    }
    
    fn constant_to_number(&self, constant: &MathConstant) -> Result<Number, ComputeError> {
        self.base_engine.constant_to_number(constant)
    }
    
    fn simplify_constants(&self, expr: &Expression) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.simplify_constants(&substituted)
    }
    
    fn expand(&self, expr: &Expression) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.expand(&substituted)
    }
    
    fn factor(&self, expr: &Expression) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.factor(&substituted)
    }
    
    fn collect(&self, expr: &Expression, var: &str) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.collect(&substituted, var)
    }
    
    fn polynomial_divide(&self, dividend: &Expression, divisor: &Expression) -> Result<(Expression, Expression), ComputeError> {
        let substituted_dividend = self.compute_with_variables(dividend)?;
        let substituted_divisor = self.compute_with_variables(divisor)?;
        self.base_engine.polynomial_divide(&substituted_dividend, &substituted_divisor)
    }
    
    fn polynomial_gcd(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.polynomial_gcd(&substituted_a, &substituted_b)
    }
    
    fn gcd(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.gcd(&substituted_a, &substituted_b)
    }
    
    fn lcm(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.lcm(&substituted_a, &substituted_b)
    }
    
    fn is_prime(&self, n: &Expression) -> Result<bool, ComputeError> {
        let substituted = self.compute_with_variables(n)?;
        self.base_engine.is_prime(&substituted)
    }
    
    fn prime_factors(&self, n: &Expression) -> Result<Vec<Expression>, ComputeError> {
        let substituted = self.compute_with_variables(n)?;
        self.base_engine.prime_factors(&substituted)
    }
    
    fn binomial(&self, n: &Expression, k: &Expression) -> Result<Expression, ComputeError> {
        let substituted_n = self.compute_with_variables(n)?;
        let substituted_k = self.compute_with_variables(k)?;
        self.base_engine.binomial(&substituted_n, &substituted_k)
    }
    
    fn permutation(&self, n: &Expression, k: &Expression) -> Result<Expression, ComputeError> {
        let substituted_n = self.compute_with_variables(n)?;
        let substituted_k = self.compute_with_variables(k)?;
        self.base_engine.permutation(&substituted_n, &substituted_k)
    }
    
    fn mean(&self, values: &[Expression]) -> Result<Expression, ComputeError> {
        let substituted_values: Result<Vec<_>, _> = values.iter()
            .map(|v| self.compute_with_variables(v))
            .collect();
        self.base_engine.mean(&substituted_values?)
    }
    
    fn variance(&self, values: &[Expression]) -> Result<Expression, ComputeError> {
        let substituted_values: Result<Vec<_>, _> = values.iter()
            .map(|v| self.compute_with_variables(v))
            .collect();
        self.base_engine.variance(&substituted_values?)
    }
    
    fn standard_deviation(&self, values: &[Expression]) -> Result<Expression, ComputeError> {
        let substituted_values: Result<Vec<_>, _> = values.iter()
            .map(|v| self.compute_with_variables(v))
            .collect();
        self.base_engine.standard_deviation(&substituted_values?)
    }
    
    fn solve(&self, equation: &Expression, var: &str) -> Result<Vec<Expression>, ComputeError> {
        let substituted = self.compute_with_variables(equation)?;
        self.base_engine.solve(&substituted, var)
    }
    
    fn solve_system(&self, equations: &[Expression], vars: &[String]) -> Result<Vec<HashMap<String, Expression>>, ComputeError> {
        let substituted_equations: Result<Vec<_>, _> = equations.iter()
            .map(|eq| self.compute_with_variables(eq))
            .collect();
        self.base_engine.solve_system(&substituted_equations?, vars)
    }
    
    fn matrix_add(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.matrix_add(&substituted_a, &substituted_b)
    }
    
    fn matrix_multiply(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.matrix_multiply(&substituted_a, &substituted_b)
    }
    
    fn matrix_determinant(&self, matrix: &Expression) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(matrix)?;
        self.base_engine.matrix_determinant(&substituted)
    }
    
    fn matrix_inverse(&self, matrix: &Expression) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(matrix)?;
        self.base_engine.matrix_inverse(&substituted)
    }
    
    fn complex_conjugate(&self, expr: &Expression) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.complex_conjugate(&substituted)
    }
    
    fn complex_modulus(&self, expr: &Expression) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.complex_modulus(&substituted)
    }
    
    fn complex_argument(&self, expr: &Expression) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(expr)?;
        self.base_engine.complex_argument(&substituted)
    }
    
    fn vector_dot(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.vector_dot(&substituted_a, &substituted_b)
    }
    
    fn vector_cross(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.vector_cross(&substituted_a, &substituted_b)
    }
    
    fn vector_norm(&self, v: &Expression) -> Result<Expression, ComputeError> {
        let substituted = self.compute_with_variables(v)?;
        self.base_engine.vector_norm(&substituted)
    }
    
    fn set_union(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.set_union(&substituted_a, &substituted_b)
    }
    
    fn set_intersection(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.set_intersection(&substituted_a, &substituted_b)
    }
    
    fn set_difference(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        let substituted_a = self.compute_with_variables(a)?;
        let substituted_b = self.compute_with_variables(b)?;
        self.base_engine.set_difference(&substituted_a, &substituted_b)
    }
}

impl Default for RuntimeEnhancedEngine {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::{Expression, Number, BinaryOperator};
    
    #[test]
    fn test_variable_management() {
        let engine = RuntimeEnhancedEngine::new();
        
        // 设置变量
        let x_value = Expression::number(Number::from(10));
        engine.set_variable("x".to_string(), x_value.clone()).unwrap();
        
        // 获取变量
        let retrieved = engine.get_variable("x").unwrap();
        assert_eq!(retrieved, Some(x_value));
        
        // 使用变量计算
        let expr = Expression::binary_op(
            BinaryOperator::Add,
            Expression::variable("x"),
            Expression::number(Number::from(5))
        );
        
        let result = engine.compute_with_variables(&expr).unwrap();
        // 结果应该是 15(如果能够计算的话)或者是替换了变量的表达式
        println!("计算结果: {:?}", result);
    }
    
    #[test]
    fn test_safe_power_computation() {
        let engine = RuntimeEnhancedEngine::new();
        
        // 测试安全的指数运算
        let safe_expr = Expression::binary_op(
            BinaryOperator::Power,
            Expression::number(Number::from(2)),
            Expression::number(Number::from(10))
        );
        
        let result = engine.safe_compute(&safe_expr).unwrap();
        println!("安全指数运算结果: {:?}", result);
        
        // 测试不安全的指数运算(应该保持符号形式)
        let unsafe_expr = Expression::binary_op(
            BinaryOperator::Power,
            Expression::number(Number::from(10)),
            Expression::number(Number::from(10000))
        );
        
        let result = engine.safe_compute(&unsafe_expr).unwrap();
        println!("不安全指数运算结果: {:?}", result);
        
        // 结果应该保持原始的符号形式,而不是计算出具体数值
        assert!(matches!(result, Expression::BinaryOp { .. }));
    }
    
    #[test]
    fn test_circular_reference_prevention() {
        let engine = RuntimeEnhancedEngine::new();
        
        // 设置第一个变量
        engine.set_variable("x".to_string(), Expression::variable("y")).unwrap();
        
        // 尝试设置循环引用(应该失败)
        let result = engine.set_variable("y".to_string(), Expression::variable("x"));
        assert!(result.is_err());
    }
}