Crates.io | eqsolver |
lib.rs | eqsolver |
version | 0.1.3 |
source | src |
created_at | 2022-06-07 14:44:21.011367 |
updated_at | 2024-07-14 08:16:13.191085 |
description | A library that solves equations using numerical methods |
homepage | |
repository | https://github.com/AzeezDa/eqsolver |
max_upload_size | |
id | 601523 |
size | 70,266 |
eqsolver
- An Equation Solver and Optimisation Library for RustThis Rust library is aimed at numerically solving equations and optimising objective functions.
The library is passively-maintained, meaning no other features will be added. However, issues on the GitHub will be answered and solved.
Contributions and feedback to this library are more than welcome!
The following methods are available to use in the library. Their descriptions use the largest possible domain and codomain for the functions, which is Rn. However, any (well-behaved) subset of Rn also works. Additionally, the methods that use multivariate input or output heavily utilises the linear algebra library for Rust nalgebra.
There are two versions of this method, one requires the Jacobian matrix to be given and the other approximates it using finite differences. The latter version has, therefore, slightly longer wall time. Both methods require an initial guess.
For certain ill-posed problems, this method will fail. For a slower but more robust method, see the Levenberg-Marquardt method below.
There are two versions of this method, one requires the Jacobian matrix to be given and the other approximates it using finite differences. The latter version has, therefore, slightly longer wall time. Both methods require an initial guess.
For certain ill-posed problems, this method will fail. For a slower but more robust method, see the Levenberg-Marquardt method below.
There are two versions of this method, one requires the Jacobian matrix to be given and the other approximates it using finite differences. The latter version has, therefore, slightly longer wall time. Both methods require an initial guess.
Use this method if you know the bounds of the parameters.
Use this method if you DON'T KNOW the bounds of the parameters but KNOW how uncertain each parameter is.
There is a single struct
for ordinary differential equations (ODE) which can be modified (using the builder pattern) to use one of the following step methods:
use eqsolver::single_variable::FDNewton;
let f = |x: f64| x.exp() - 1./x; // e^x = 1/x
let solution = FDNewton::new(f).solve(0.5); // Starting guess is 0.5
use eqsolver::multivariable::MultiVarNewtonFD;
use nalgebra::{vector, Vector2};
// Want to solve x^2 - y = 1 and xy = 2
let f = |v: Vector2<f64>| vector![v[0].powi(2) - v[1] - 1., v[0] * v[1] - 2.];
let solution = MultiVarNewtonFD::new(f).solve(vector![1., 1.]); // Starting guess is (1, 1)
use eqsolver::ODESolver;
let f = |t: f64, y: f64| t * y; // y' = f(t, y) = ty
let (x0, y0) = (0., 0.2);
let x_end = 2.;
let step_size = 1e-3;
let solution = ODESolver::new(f, x0, y0, step_size).solve(x_end);
use eqsolver::multivariable::LevenbergMarquardtFD;
use nalgebra::{vector, Vector2};
let c0 = [3., 5., 3.];
let c1 = [1., 0., 4.];
let c2 = [6., 2., 2.];
// Function from R2 to R3
let f = |v: Vector2<f64>| {
vector!(
(v[0] - c0[0]).powi(2) + (v[1] - c0[1]).powi(2) - c0[2] * c0[2],
(v[0] - c1[0]).powi(2) + (v[1] - c1[1]).powi(2) - c1[2] * c1[2],
(v[0] - c2[0]).powi(2) + (v[1] - c2[1]).powi(2) - c2[2] * c2[2],
)
};
let solution_lm = LevenbergMarquardtFD::new(f)
.solve(vector![4.5, 2.5]) // Guess
.unwrap();
use eqsolver::global_optimisers::{CrossEntropy, ParticleSwarm};
use nalgebra::SVector;
use std::f64::consts::PI;
const SIZE: usize = 10;
let rastrigin = |v: SVector<f64, SIZE>| {
v.fold(10. * SIZE as f64, |acc, x| {
acc + x * x - 10. * f64::cos(2. * PI * x)
})
};
let bounds = SVector::repeat(10.);
let standard_deviations = SVector::repeat(10.);
let guess = SVector::repeat(5.);
let opt_pso = ParticleSwarm::new(rastrigin, -bounds, bounds).solve(guess);
let opt_ce = CrossEntropy::new(rastrigin)
.with_std_dev(standard_deviations)
.solve(guess);
For more examples, please see the examples directory.