#equation-solver #root-finding #newton-raphson #string #system #expressions #solving

geqslib

在 Rust 及其他语言中轻松实现方程求解!

5 个版本

0.1.4 2024年5月30日
0.1.3 2024年2月16日
0.1.2 2024年2月13日
0.1.1 2024年1月25日
0.1.0 2024年1月21日

#497 in 算法


用于 2 crates

MIT 许可证

63KB
1K SLoC

geqslib - Grant's Equation Solver Library

Geqslib 定义了几个用于评估表达式和解决作为字符串给出的方程的函数。该库提供了牛顿-拉夫森根查找算法的单变量和多变量实现,并提供它们在其他场景中使用,其中表达式可能比字符串更适合用闭包表示。

然而,大部分提供的函数都集中在评估表达式或方程作为字符串

示例

use geqslib::solve_equation_from_str;

let (var, soln) = solve_equation_from_str("x + 4 = 12", 0.0001, 10).unwrap();

assert_eq!(var, "x");
assert!((soln - 8.0).abs() < 0.001);

Geqslib 还提供了一个 SystemBuilder 结构体,用于正确约束方程组以供后续求解。

示例

use geqslib::system::{System, SystemBuilder};
use geqslib::shunting::new_context;
let mut ctx = new_context();

// Build up the system:
let mut builder = SystemBuilder::new("x + y = 9", ctx).unwrap();
builder.try_constrain_with("x - y = 4");

// Convert to a constrained system
let mut sys = builder
    .build_system()
    .expect("Failed to constrain system...");

// Specify guess value and domain for variables if desired
sys.specify_variable("x", 6.5, 0.0, 7.0);

// Specify tolerance and iteration limit, then solve!
let soln = sys.solve(0.0001, 10)
    .expect("Failed to find a solution...");

// Solution is x = 6.5, y = 2.5
assert!((6.5 - soln["x"]).abs() < 0.001);
assert!((2.5 - soln["y"]).abs() < 0.001);

依赖关系

~2.4–3.5MB
~59K SLoC