Crate yufmath

Source
Expand description

§Yufmath - 计算机代数系统

Yufmath 是一个基于 Rust 编写的高性能计算机代数系统(CAS)库, 提供符号数学计算、精确算术运算和多种接口支持。

§主要特性

  • 精确计算:支持任意精度整数、有理数和复数运算
  • 符号计算:代数表达式的符号操作和简化
  • 微积分:符号求导和积分
  • 多接口支持:Rust API、C++ 绑定、命令行工具
  • 高性能:优化的算法和缓存机制
  • 进度监控:支持长时间计算的进度跟踪和取消
  • 批量处理:支持批量计算和并行处理

§快速开始

§基本使用

use yufmath::Yufmath;

// 创建 Yufmath 实例
let yuf = Yufmath::new();

// 基本计算
let result = yuf.compute("2 + 3 * 4").unwrap();
println!("计算结果: {}", result); // 输出: 14

// 符号计算
let result = yuf.compute("x + x").unwrap();
println!("简化结果: {}", result); // 输出: 2*x

// 求导
let expr = yuf.parse("x^2 + 2*x + 1").unwrap();
let derivative = yuf.diff(&expr, "x").unwrap();
println!("导数: {:?}", derivative);

// 积分
let integral = yuf.integrate(&expr, "x").unwrap();
println!("积分: {:?}", integral);

§配置使用

use yufmath::{Yufmath, ComputeConfig, PrecisionConfig};
use std::time::Duration;

// 创建自定义配置
let precision_config = PrecisionConfig::new()
    .with_force_exact(true)
    .with_max_precision(1000);

let config = ComputeConfig::new()
    .with_progress(true)
    .with_max_compute_time(Duration::from_secs(60))
    .with_precision(precision_config);

// 使用配置创建实例
let yuf = Yufmath::with_config(config);

§进度监控

use yufmath::Yufmath;

let mut yuf = Yufmath::new();

// 设置进度回调
yuf.set_progress_callback(Box::new(|progress| {
    println!("进度: {:.1}% - {}", 
            progress.progress * 100.0, 
            progress.current_step);
    true // 返回 true 继续计算,false 取消计算
}));

// 带进度的计算
let result = yuf.compute_with_progress("integrate(sin(x^2), x)").unwrap();

§批量计算

use yufmath::Yufmath;

let yuf = Yufmath::new();

// 批量计算
let expressions = vec!["2 + 3", "x^2 + 1", "sin(pi/2)"];
let results = yuf.batch_compute(&expressions);

for (expr, result) in expressions.iter().zip(results.iter()) {
    match result {
        Ok(value) => println!("{} = {}", expr, value),
        Err(e) => println!("{} -> 错误: {}", expr, e),
    }
}

§高级数学功能

use yufmath::Yufmath;

let yuf = Yufmath::new();

// 多项式运算
let poly = yuf.parse("(x + 1)^3").unwrap();
let expanded = yuf.expand(&poly).unwrap();
println!("展开: {:?}", expanded);

// 方程求解
let equation = yuf.parse("x^2 - 4").unwrap();
let solutions = yuf.solve(&equation, "x").unwrap();
println!("解: {:?}", solutions);

// 数论函数
let gcd_result = yuf.gcd(&yuf.parse("48").unwrap(), &yuf.parse("18").unwrap()).unwrap();
println!("最大公约数: {:?}", gcd_result);

// 矩阵运算
let matrix_a = yuf.parse("[[1,2],[3,4]]").unwrap();
let matrix_b = yuf.parse("[[5,6],[7,8]]").unwrap();
let product = yuf.matrix_multiply(&matrix_a, &matrix_b).unwrap();
println!("矩阵乘积: {:?}", product);

§错误处理

Yufmath 提供了完善的错误处理机制:

use yufmath::{Yufmath, YufmathError};

let yuf = Yufmath::new();

match yuf.compute("2 + + 3") {
    Ok(result) => println!("结果: {}", result),
    Err(e) => {
        println!("错误: {}", e.user_friendly_message());
        println!("建议: {:?}", e.suggestions());
         
        // 生成完整的错误报告
        println!("{}", e.format_error_report(Some("2 + + 3")));
    }
}

§性能监控

use yufmath::Yufmath;

let mut yuf = Yufmath::new();

// 执行一些计算
yuf.compute("x^2 + 2*x + 1").unwrap();
yuf.compute("integrate(sin(x), x)").unwrap();

// 获取性能统计
if let Some(stats) = yuf.get_performance_stats() {
    println!("总计算次数: {}", stats.total_computations);
    println!("成功率: {:.2}%", stats.success_rate() * 100.0);
    println!("平均计算时间: {:?}", stats.avg_compute_time);
    println!("精确计算比例: {:.2}%", stats.exact_computation_ratio * 100.0);
}

Re-exports§

pub use api::Yufmath;
pub use api::YufmathError;
pub use api::ComputeConfig;
pub use api::PrecisionConfig;
pub use api::ParallelConfig;
pub use api::CacheConfig;
pub use api::MemoryConfig;
pub use api::ComputeProgress;
pub use api::ComputePhase;
pub use api::PerformanceStats;
pub use api::PerformanceMonitor;
pub use api::ProgressCallback;
pub use api::AsyncComputation;
pub use api::BatchAsyncComputer;
pub use api::AsyncConfig;
pub use api::TaskStatus;
pub use core::Expression;
pub use core::Number;
pub use core::MathConstant;
pub use core::BinaryOperator;
pub use core::UnaryOperator;
pub use core::SharedExpression;
pub use core::CowExpression;
pub use core::MemoryManager;
pub use core::MemoryMonitor;
pub use core::MemoryStats;
pub use core::ExpressionComparator;
pub use core::ExpressionBuilder;
pub use core::ExpressionFactory;
pub use engine::ComputeEngine;
pub use engine::ComputeError;
pub use engine::LazyExpression;
pub use engine::DependencyGraph;
pub use engine::LazyState;
pub use engine::DependencyGraphStats;
pub use engine::ParallelComputeEngine;
pub use engine::TaskScheduler;
pub use engine::ComputeTask;
pub use engine::SchedulerStats;
pub use engine::ExpressionPreprocessor;
pub use engine::ParallelizationAnalysis;
pub use engine::RuntimeEnhancedEngine;
pub use engine::RuntimeEnhancer;
pub use engine::VariableManager;
pub use engine::ComplexityAnalyzer;
pub use engine::RuntimeConfig;
pub use parser::Parser;
pub use parser::ParseError;
pub use formatter::Formatter;
pub use formatter::FormatOptions;
pub use formatter::FormatType;
pub use notebook::NotebookCell;
pub use notebook::CellType;
pub use notebook::CellContent;
pub use notebook::CellMetadata;
pub use notebook::CellId;
pub use notebook::Notebook;
pub use notebook::NotebookManager;
pub use notebook::NotebookMetadata;
pub use notebook::ExecutionEngine;
pub use notebook::ExecutionResult;
pub use notebook::ExecutionContext;
pub use notebook::VariableScope;
pub use notebook::ScopeManager;
pub use notebook::VariableBinding;
pub use notebook::NotebookFormat;
pub use notebook::NotebookSerializer;
pub use notebook::NotebookDeserializer;
pub use notebook::NotebookUI;
pub use notebook::UIEvent;
pub use notebook::UICommand;
pub use notebook::KeyBinding;
pub use notebook::NotebookExporter;
pub use notebook::ExportFormat;
pub use notebook::ExportOptions;
pub use notebook::NotebookError;
pub use notebook::NotebookResult;

Modules§

api
Rust API 接口
cli
命令行工具
core
核心数据结构和类型定义
engine
计算引擎
ffi
外部函数接口(FFI)
formatter
表达式格式化器
notebook
笔记本模式模块
parser
表达式解析器

Constants§

DESCRIPTION
库的描述
NAME
库的名称
VERSION
库的版本信息