lbfgsb_rs_pure/lib.rs
1//! A compact, self-contained reimplementation of an L-BFGS-B style solver in Rust.
2//!
3//! This crate focuses on a safe, idiomatic Rust port of the algorithmic core of
4//! the classical L-BFGS-B package. It aims for algorithmic parity and
5//! reproducibility rather than maximal performance — for production use link a
6//! tuned BLAS/LAPACK provider.
7//!
8//! The main public API is the `LBFGSB` type (in `solver`), together with the
9//! `Status` and `Solution` types returned by the solver.
10//!
11//! Example
12//! ```/dev/null/example.rs#L1-22
13//! use lbfgsb_rs::{LBFGSB, Status};
14//!
15//! let mut x = vec![0.0];
16//! let lower = vec![-1.0];
17//! let upper = vec![1.0];
18//!
19//! let mut solver = LBFGSB::new(5);
20//! let sol = solver
21//! .minimize(&mut x, &lower, &upper, |x| {
22//! let dx = x[0] - 0.5;
23//! (dx * dx, vec![2.0 * dx])
24//! })
25//! .unwrap();
26//!
27//! assert_eq!(sol.status, Status::Converged);
28//! assert!((sol.x[0] - 0.5).abs() < 1e-6);
29//! ```
30//!
31//! Modules
32//! - `blas` : lightweight level-1 BLAS helpers used throughout the crate.
33//! - `linesearch` : line-search helpers (dcstep / lnsrlb / convenience methods).
34//! - `linpack` : small linpack routines (Cholesky / triangular solve).
35//! - `subalgorithms`: helper routines (active-set initialization, bmv partial port).
36//! - `solver` : high-level LBFGS-B driver (uses the above modules).
37//!
38//! The crate re-exports `LBFGSB` from `solver` and provides the `Status` and
39//! `Solution` types here so `solver` can reference them as `crate::Status` and
40//! `crate::Solution` (matching the original design).
41
42#![allow(clippy::too_many_arguments)]
43
44use std::f64;
45
46mod blas;
47mod linesearch;
48pub mod linpack;
49pub mod solver;
50pub mod subalgorithms;
51
52pub use crate::blas::*;
53pub use crate::linesearch::*;
54pub use crate::solver::{IterationControl, IterationInfo, LBFGSB};
55
56/// Maximum number of backtracking line search iterations used by convenience routines.
57pub const MAX_BACKTRACK: usize = 50;
58
59/// Minimum curvature s'y to accept BFGS update.
60pub const CURVATURE_EPS: f64 = 1e-12;
61
62/// Enum describing solver termination.
63#[derive(Debug, PartialEq, Eq, Clone, Copy)]
64pub enum Status {
65 Converged,
66 MaxIter,
67 LineSearchFailure,
68 NumericalFailure,
69}
70
71/// Solution returned by the solver.
72#[derive(Debug, Clone)]
73pub struct Solution {
74 pub x: Vec<f64>,
75 pub f: f64,
76 pub iterations: usize,
77 pub status: Status,
78}