ตัวแก้สมการหาค่าราก 1 มิติ (1D Root-Finding Solvers)
การหาค่ารากของสมการ (Root Finding) ปรากฏอยู่ทั่วไปในงาน Quantitative Finance ตั้งแต่การ Invert หาผลตอบแทนพันธบัตรและ Implied Volatility ไปจนถึงการหาราคาดุลยภาพในการชำระราคา SabiQuant มีชุดตัวแก้สมการเชิงตัวเลขที่ออกแบบมาโดยไม่มีการจอง Heap Allocation
ระเบียบวิธีที่มีให้ใช้งาน (Implemented Solvers)
1. ระเบียบวิธีของเบรนต์ (BrentSolver)
ผสานระหว่าง Inverse Quadratic Interpolation, Secant Method, และ Bisection การันตีการลู่เข้าอย่างสมบูรณ์สำหรับฟังก์ชันต่อเนื่องที่มีขอบเขต คร่อมรากสมการ
2. Safeguarded Newton-Raphson (SafeguardedNewtonSolver)
ใช้อนุพันธ์เชิงวิเคราะห์ เพื่อการลู่เข้าแบบ Quadratic:
หากจุด กระโดดออกนอกขอบเขตที่ถูกต้อง หรือความคลาดเคลื่อนลดลงไม่เร็วพอ ระบบจะสลับไปใช้ขั้นตอนแบ่งครึ่งช่วง (Bisection Step) โดยอัตโนมัติ
3. ระเบียบวิธีแบ่งครึ่งช่วง (BisectionSolver)
วิธีมาตรฐานที่มีความเสถียรอย่างไร้เงื่อนไข โดยมีอัตราการลู่เข้าแบบเส้นตรงที่ระดับ
ตัวอย่างโค้ดในภาษา Rust
use sabiquant::math::solvers::{BrentSolver, Solver1D};
fn main() -> Result<(), Box<dyn std::error::Error>> { // แก้สมการ f(x) = x^3 - 2x - 5 = 0 (รากที่แท้จริง ~ 2.09455148) let f = |x: f64| x.powi(3) - 2.0 * x - 5.0;
let solver = BrentSolver::new() .max_iterations(100) .tolerance(1e-12);
let bracket_low = 1.0; let bracket_high = 3.0;
let root = solver.solve(&f, bracket_low, bracket_high)?;
println!("Found Root: {:.12}", root); println!("Residual: {:.2e}", f(root));
assert!((f(root)).abs() < 1e-12);
Ok(())}