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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! # 数论和组合数学模块
//!
//! 实现数论相关的算法,包括最大公约数、最小公倍数、素数判断、
//! 质因数分解、二项式系数、排列组合等功能。

use num_bigint::{BigInt, ToBigInt};
use num_rational::BigRational;
use num_traits::{Zero, One, Signed};
use crate::core::{Expression, Number};
use super::ComputeError;

/// 数论和组合数学引擎
pub struct NumberTheoryEngine;

impl NumberTheoryEngine {
    /// 创建新的数论引擎
    pub fn new() -> Self {
        Self
    }
    
    /// 计算两个整数的最大公约数(欧几里得算法)
    pub fn gcd(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        match (a, b) {
            (Expression::Number(Number::Integer(a)), Expression::Number(Number::Integer(b))) => {
                let result = self.gcd_bigint(a, b);
                Ok(Expression::Number(Number::Integer(result)))
            }
            (Expression::Number(Number::Rational(a)), Expression::Number(Number::Rational(b))) => {
                // 对于有理数,gcd(a/b, c/d) = gcd(a*d, c*b) / (b*d)
                let a_num = a.numer();
                let a_den = a.denom();
                let b_num = b.numer();
                let b_den = b.denom();
                
                let numerator_gcd = self.gcd_bigint(&(a_num * b_den), &(b_num * a_den));
                let denominator = a_den * b_den;
                
                let result = BigRational::new(numerator_gcd, denominator.clone());
                Ok(Expression::Number(Number::Rational(result)))
            }
            _ => Err(ComputeError::unsupported_operation(
                "gcd 函数只支持整数和有理数,请确保参数是整数或有理数"
            ))
        }
    }
    
    /// 计算两个整数的最小公倍数
    pub fn lcm(&self, a: &Expression, b: &Expression) -> Result<Expression, ComputeError> {
        match (a, b) {
            (Expression::Number(Number::Integer(a)), Expression::Number(Number::Integer(b))) => {
                if a.is_zero() || b.is_zero() {
                    return Ok(Expression::Number(Number::Integer(BigInt::zero())));
                }
                
                let gcd_val = self.gcd_bigint(a, b);
                let result = (a * b).abs() / gcd_val;
                Ok(Expression::Number(Number::Integer(result)))
            }
            _ => Err(ComputeError::unsupported_operation(
                "lcm 函数只支持整数,请确保参数是整数"
            ))
        }
    }
    
    /// 判断一个数是否为素数
    pub fn is_prime(&self, n: &Expression) -> Result<bool, ComputeError> {
        match n {
            Expression::Number(Number::Integer(n)) => {
                Ok(self.is_prime_bigint(n))
            }
            _ => Err(ComputeError::unsupported_operation(
                "is_prime 函数只支持整数,请确保参数是正整数"
            ))
        }
    }
    
    /// 质因数分解
    pub fn prime_factors(&self, n: &Expression) -> Result<Vec<Expression>, ComputeError> {
        match n {
            Expression::Number(Number::Integer(n)) => {
                if n <= &BigInt::one() {
                    return Err(ComputeError::domain_error(
                        "质因数分解要求输入大于1的正整数"
                    ));
                }
                
                let factors = self.prime_factors_bigint(n);
                let result = factors.into_iter()
                    .map(|f| Expression::Number(Number::Integer(f)))
                    .collect();
                Ok(result)
            }
            _ => Err(ComputeError::unsupported_operation(
                "prime_factors 函数只支持整数,请确保参数是大于1的正整数"
            ))
        }
    }
    
    /// 计算二项式系数 C(n, k) = n! / (k! * (n-k)!)
    pub fn binomial(&self, n: &Expression, k: &Expression) -> Result<Expression, ComputeError> {
        match (n, k) {
            (Expression::Number(Number::Integer(n)), Expression::Number(Number::Integer(k))) => {
                if n < &BigInt::zero() || k < &BigInt::zero() || k > n {
                    return Ok(Expression::Number(Number::Integer(BigInt::zero())));
                }
                
                let result = self.binomial_coefficient(n, k)?;
                Ok(Expression::Number(Number::Integer(result)))
            }
            _ => Err(ComputeError::unsupported_operation(
                "binomial 函数只支持非负整数,请确保参数是非负整数且 k <= n"
            ))
        }
    }
    
    /// 计算排列数 P(n, k) = n! / (n-k)!
    pub fn permutation(&self, n: &Expression, k: &Expression) -> Result<Expression, ComputeError> {
        match (n, k) {
            (Expression::Number(Number::Integer(n)), Expression::Number(Number::Integer(k))) => {
                if n < &BigInt::zero() || k < &BigInt::zero() || k > n {
                    return Ok(Expression::Number(Number::Integer(BigInt::zero())));
                }
                
                let result = self.permutation_count(n, k)?;
                Ok(Expression::Number(Number::Integer(result)))
            }
            _ => Err(ComputeError::unsupported_operation(
                "permutation 函数只支持非负整数,请确保参数是非负整数且 k <= n"
            ))
        }
    }
    
    /// 计算平均值
    pub fn mean(&self, values: &[Expression]) -> Result<Expression, ComputeError> {
        if values.is_empty() {
            return Err(ComputeError::domain_error(
                "无法计算空列表的平均值,请提供至少一个数值"
            ));
        }
        
        let mut sum = Number::Integer(BigInt::zero());
        let count = values.len();
        
        for value in values {
            match value {
                Expression::Number(num) => {
                    sum = self.add_numbers(&sum, num)?;
                }
                _ => return Err(ComputeError::unsupported_operation(
                    "mean 函数只支持数值,请确保所有参数都是数值"
                ))
            }
        }
        
        // 计算平均值:sum / count
        let count_num = Number::Integer(count.to_bigint().unwrap());
        let result = self.divide_numbers(&sum, &count_num)?;
        Ok(Expression::Number(result))
    }
    
    /// 计算方差
    pub fn variance(&self, values: &[Expression]) -> Result<Expression, ComputeError> {
        if values.len() < 2 {
            return Err(ComputeError::domain_error(
                "计算方差需要至少两个数值"
            ));
        }
        
        // 计算平均值
        let mean_expr = self.mean(values)?;
        let mean_num = match mean_expr {
            Expression::Number(num) => num,
            _ => unreachable!()
        };
        
        // 计算方差:Σ(xi - mean)² / n
        let mut sum_squares = Number::Integer(BigInt::zero());
        
        for value in values {
            match value {
                Expression::Number(num) => {
                    let diff = self.subtract_numbers(num, &mean_num)?;
                    let square = self.multiply_numbers(&diff, &diff)?;
                    sum_squares = self.add_numbers(&sum_squares, &square)?;
                }
                _ => return Err(ComputeError::unsupported_operation(
                    "variance 函数只支持数值,请确保所有参数都是数值"
                ))
            }
        }
        
        let count_num = Number::Integer(values.len().to_bigint().unwrap());
        let result = self.divide_numbers(&sum_squares, &count_num)?;
        Ok(Expression::Number(result))
    }
    
    /// 计算标准差
    pub fn standard_deviation(&self, values: &[Expression]) -> Result<Expression, ComputeError> {
        let variance_expr = self.variance(values)?;
        
        match variance_expr {
            Expression::Number(variance_num) => {
                // 计算平方根
                let sqrt_result = self.sqrt_number(&variance_num)?;
                Ok(Expression::Number(sqrt_result))
            }
            _ => unreachable!()
        }
    }
    
    // 私有辅助方法
    
    /// 计算两个 BigInt 的最大公约数
    fn gcd_bigint(&self, a: &BigInt, b: &BigInt) -> BigInt {
        let mut a = a.abs();
        let mut b = b.abs();
        
        while !b.is_zero() {
            let temp = b.clone();
            b = &a % &b;
            a = temp;
        }
        
        a
    }
    
    /// 判断 BigInt 是否为素数(试除法)
    fn is_prime_bigint(&self, n: &BigInt) -> bool {
        if n <= &BigInt::one() {
            return false;
        }
        if n == &(BigInt::from(2)) {
            return true;
        }
        if n % 2 == BigInt::zero() {
            return false;
        }
        
        // 只检查到 sqrt(n)
        let mut i = BigInt::from(3);
        let sqrt_n = self.integer_sqrt(n);
        
        while i <= sqrt_n {
            if n % &i == BigInt::zero() {
                return false;
            }
            i += 2; // 只检查奇数
        }
        
        true
    }
    
    /// 质因数分解(试除法)
    fn prime_factors_bigint(&self, n: &BigInt) -> Vec<BigInt> {
        let mut factors = Vec::new();
        let mut n = n.clone();
        
        // 处理因子2
        while &n % 2 == BigInt::zero() {
            factors.push(BigInt::from(2));
            n /= 2;
        }
        
        // 处理奇数因子
        let mut i = BigInt::from(3);
        let sqrt_n = self.integer_sqrt(&n);
        
        while i <= sqrt_n && n > BigInt::one() {
            while &n % &i == BigInt::zero() {
                factors.push(i.clone());
                n /= &i;
            }
            i += 2;
        }
        
        // 如果 n 仍然大于1,那么它本身就是一个素因子
        if n > BigInt::one() {
            factors.push(n);
        }
        
        factors
    }
    
    /// 计算二项式系数
    fn binomial_coefficient(&self, n: &BigInt, k: &BigInt) -> Result<BigInt, ComputeError> {
        let k = if k > &(n - k) { n - k } else { k.clone() };
        
        if k.is_zero() {
            return Ok(BigInt::one());
        }
        
        let mut result = BigInt::one();
        let mut i = BigInt::zero();
        
        while i < k {
            result = result * (n - &i) / (&i + 1);
            i += 1;
        }
        
        Ok(result)
    }
    
    /// 计算排列数
    fn permutation_count(&self, n: &BigInt, k: &BigInt) -> Result<BigInt, ComputeError> {
        if k.is_zero() {
            return Ok(BigInt::one());
        }
        
        let mut result = BigInt::one();
        let mut i = BigInt::zero();
        
        while i < *k {
            result *= n - &i;
            i += 1;
        }
        
        Ok(result)
    }
    
    /// 计算整数平方根(牛顿法)
    fn integer_sqrt(&self, n: &BigInt) -> BigInt {
        if n.is_zero() {
            return BigInt::zero();
        }
        
        let mut x = n.clone();
        let mut y: BigInt = (n + 1) / 2;
        
        while y < x {
            x = y.clone();
            y = (&x + n / &x) / 2;
        }
        
        x
    }
    
    /// 数值加法
    fn add_numbers(&self, a: &Number, b: &Number) -> Result<Number, ComputeError> {
        match (a, b) {
            (Number::Integer(a), Number::Integer(b)) => {
                Ok(Number::Integer(a + b))
            }
            (Number::Rational(a), Number::Rational(b)) => {
                Ok(Number::Rational(a + b))
            }
            (Number::Integer(a), Number::Rational(b)) => {
                let a_rational = BigRational::from(a.clone());
                Ok(Number::Rational(a_rational + b))
            }
            (Number::Rational(a), Number::Integer(b)) => {
                let b_rational = BigRational::from(b.clone());
                Ok(Number::Rational(a + b_rational))
            }
            _ => Err(ComputeError::unsupported_operation(
                "不支持的数值类型组合"
            ))
        }
    }
    
    /// 数值减法
    fn subtract_numbers(&self, a: &Number, b: &Number) -> Result<Number, ComputeError> {
        match (a, b) {
            (Number::Integer(a), Number::Integer(b)) => {
                Ok(Number::Integer(a - b))
            }
            (Number::Rational(a), Number::Rational(b)) => {
                Ok(Number::Rational(a - b))
            }
            (Number::Integer(a), Number::Rational(b)) => {
                let a_rational = BigRational::from(a.clone());
                Ok(Number::Rational(a_rational - b))
            }
            (Number::Rational(a), Number::Integer(b)) => {
                let b_rational = BigRational::from(b.clone());
                Ok(Number::Rational(a - b_rational))
            }
            _ => Err(ComputeError::unsupported_operation(
                "不支持的数值类型组合"
            ))
        }
    }
    
    /// 数值乘法
    fn multiply_numbers(&self, a: &Number, b: &Number) -> Result<Number, ComputeError> {
        match (a, b) {
            (Number::Integer(a), Number::Integer(b)) => {
                Ok(Number::Integer(a * b))
            }
            (Number::Rational(a), Number::Rational(b)) => {
                Ok(Number::Rational(a * b))
            }
            (Number::Integer(a), Number::Rational(b)) => {
                let a_rational = BigRational::from(a.clone());
                Ok(Number::Rational(a_rational * b))
            }
            (Number::Rational(a), Number::Integer(b)) => {
                let b_rational = BigRational::from(b.clone());
                Ok(Number::Rational(a * b_rational))
            }
            _ => Err(ComputeError::unsupported_operation(
                "不支持的数值类型组合"
            ))
        }
    }
    
    /// 数值除法
    fn divide_numbers(&self, a: &Number, b: &Number) -> Result<Number, ComputeError> {
        match (a, b) {
            (Number::Integer(a), Number::Integer(b)) => {
                if b.is_zero() {
                    return Err(ComputeError::DivisionByZero);
                }
                Ok(Number::Rational(BigRational::new(a.clone(), b.clone())))
            }
            (Number::Rational(a), Number::Rational(b)) => {
                if b.is_zero() {
                    return Err(ComputeError::DivisionByZero);
                }
                Ok(Number::Rational(a / b))
            }
            (Number::Integer(a), Number::Rational(b)) => {
                if b.is_zero() {
                    return Err(ComputeError::DivisionByZero);
                }
                let a_rational = BigRational::from(a.clone());
                Ok(Number::Rational(a_rational / b))
            }
            (Number::Rational(a), Number::Integer(b)) => {
                if b.is_zero() {
                    return Err(ComputeError::DivisionByZero);
                }
                let b_rational = BigRational::from(b.clone());
                Ok(Number::Rational(a / b_rational))
            }
            _ => Err(ComputeError::unsupported_operation(
                "不支持的数值类型组合"
            ))
        }
    }
    
    /// 数值平方根(简化实现,返回符号形式)
    fn sqrt_number(&self, n: &Number) -> Result<Number, ComputeError> {
        match n {
            Number::Integer(n) => {
                if n < &BigInt::zero() {
                    return Err(ComputeError::domain_error(
                        "负数没有实数平方根,请使用复数运算"
                    ));
                }
                
                // 检查是否为完全平方数
                let sqrt_int = self.integer_sqrt(n);
                if &sqrt_int * &sqrt_int == *n {
                    Ok(Number::Integer(sqrt_int))
                } else {
                    // 返回符号形式
                    Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
                        op: crate::core::UnaryOperator::Sqrt,
                        operand: Box::new(Expression::Number(Number::Integer(n.clone()))),
                    })))
                }
            }
            Number::Rational(r) => {
                let num_sqrt = self.integer_sqrt(r.numer());
                let den_sqrt = self.integer_sqrt(r.denom());
                
                if &num_sqrt * &num_sqrt == *r.numer() && &den_sqrt * &den_sqrt == *r.denom() {
                    Ok(Number::Rational(BigRational::new(num_sqrt, den_sqrt)))
                } else {
                    // 返回符号形式
                    Ok(Number::Symbolic(Box::new(Expression::UnaryOp {
                        op: crate::core::UnaryOperator::Sqrt,
                        operand: Box::new(Expression::Number(Number::Rational(r.clone()))),
                    })))
                }
            }
            _ => Err(ComputeError::unsupported_operation(
                "不支持的数值类型"
            ))
        }
    }
}

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

// 包含测试模块
#[cfg(test)]
#[path = "number_theory_tests.rs"]
mod number_theory_tests;