diff --git a/README.md b/README.md index fae7482..b8e4a69 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,10 @@ Solvers of nonlinear systems F(z) = 0 in double-, quad- and octo-double precisio A problem enters through the `Problem` trait: its residual and its exact Jacobian in one of [multidouble](https://github.com/nmoinvaz/multidouble)'s precisions (`Double`, `Quad`, `Octo`). The `Jvp` and `ResidualJet` traits add the Jacobian-vector product and the residual's Taylor series in a parameter, which the Krylov -solver, the series of a branch and the exact tangent need. The solvers need nothing else of the problem. +solver, the series of a branch and the exact tangent need. The series of a branch takes the residual's orders one at a +time (`ResidualJet::jet_orders`): by default each is a fresh `residual_jet` of all the lower orders, and a problem that +keeps its lower orders between calls overrides it, so that a series of order N costs N orders instead of N +evaluations of growing length. The solvers need nothing else of the problem. [multishoot](https://github.com/nmoinvaz/multishoot)'s multiple-shooting layout is one such problem. | Module | What it does | diff --git a/src/anm.rs b/src/anm.rs index 8b20ce3..e8a3442 100644 --- a/src/anm.rs +++ b/src/anm.rs @@ -3,8 +3,8 @@ //! Cochelin, Damil and Potier-Ferry, Int. J. Numer. Methods Eng. 37 (1994) 1187). //! //! Order k of F(z(h), p0 + h) is J z_k plus what the lower orders give, with J the Jacobian at z_0, so each -//! coefficient solves J z_k = -F_k, where F_k is order k of the residual's series with z_k left out -//! ([`ResidualJet`]). One factorization of J serves every order. With `refine` > 0 each solve is refined by the exact +//! coefficient solves J z_k = -F_k, where F_k is order k of the residual's series with z_k left out, taken one order +//! at a time from the problem's [`ResidualJet::jet_orders`]. One factorization of J serves every order. With `refine` > 0 each solve is refined by the exact //! products J x ([`Jvp`]): the residual b - J x, solved with the same factors, corrects x. //! //! The series is valid up to about [`Branch::radius`], Cochelin's estimate (delta |z_1| / |z_N|)^(1 / (N - 1)), where @@ -41,17 +41,17 @@ pub fn series, Pr: ResidualJet

+ Jvp

+ ?Sized>( par: bool, ) -> Result, String> { let lu = exact::(pr, z0, par)?.0; - let mut zp: Vec>> = vec![z0.iter().map(|p| p.to_vec()).collect()]; + let mut orders = pr.jet_orders(order)?; + let mut last: Vec> = z0.iter().map(|p| p.to_vec()).collect(); let mut br = Branch { z: vec![(0..pr.len()) .map(|i| P::from_raw(&z0.iter().map(|p| p[i]).collect::>())) .collect()], residual: Vec::new(), }; - for k in 1..=order { - let refs: Vec> = zp.iter().map(|z| parts(z)).collect(); - let f = pr.residual_jet(&refs, k + 1, par)?; - let b: Vec = f[k].iter().map(|&v| -v).collect(); + for _ in 1..=order { + let f = orders.next(&parts(&last), par)?; + let b: Vec = f.iter().map(|&v| -v).collect(); let mut x = b.clone(); P::lu_solve(&lu, &mut x); let resid = |x: &[P::Scalar]| -> Result, String> { @@ -69,7 +69,7 @@ pub fn series, Pr: ResidualJet

+ Jvp

+ ?Sized>( } else { f64::NAN }); - zp.push(to_parts::

(&x)); + last = to_parts::

(&x); br.z.push(x); } Ok(br) diff --git a/src/lib.rs b/src/lib.rs index cfba136..4fc8227 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,4 +93,56 @@ pub trait ResidualJet>: Problem

{ /// The coefficients of orders 0..nout - 1, one scalar per row, for the coefficient vectors zs (each P::PARTS part /// arrays of length `len()`, z_k zero past them). Order k is linear in z_k: J z_k plus what the lower orders give. fn residual_jet(&self, zs: &[Vec<&[f64]>], nout: usize, par: bool) -> Result>, String>; + + /// The residual's series one order at a time ([`JetOrders`]), for orders up to `order`. By default each order is + /// `residual_jet`'s, which forms every lower order again ([`Recompute`]), so a series of order N costs N + /// evaluations of growing length. A problem that keeps its lower orders between calls overrides it. + fn jet_orders<'a>(&'a self, order: usize) -> Result + 'a>, String> + where + P: 'a, + { + let _ = order; + Ok(Box::new(Recompute::new(self))) + } +} + +/// The residual's series in the parameter formed one order at a time, as the series of a branch needs it: order k +/// once z_0..z_(k-1) are known, with z_k left out. +pub trait JetOrders> { + /// Order k of the residual's series (one scalar per row), where z, the newest coefficient vector (P::PARTS part + /// arrays of length `len()`), is z_(k-1): the first call gives z_0 and returns order 1. z_k and the orders past it + /// are zero, so the result is F_k of the solve J z_k = -F_k. + fn next(&mut self, z: &[&[f64]], par: bool) -> Result, String>; +} + +/// [`JetOrders`] by [`ResidualJet::residual_jet`]: each call evaluates the series of orders 0..=k on every vector +/// given so far and returns its order k. +pub struct Recompute<'a, P: Precision, Pr: ResidualJet

+ ?Sized> { + pr: &'a Pr, + zs: Vec>>, + _p: std::marker::PhantomData

, +} + +impl<'a, P: Precision, Pr: ResidualJet

+ ?Sized> Recompute<'a, P, Pr> { + /// no vectors given yet + pub fn new(pr: &'a Pr) -> Self { + Recompute { + pr, + zs: Vec::new(), + _p: std::marker::PhantomData, + } + } +} + +impl, Pr: ResidualJet

+ ?Sized> JetOrders

for Recompute<'_, P, Pr> { + fn next(&mut self, z: &[&[f64]], par: bool) -> Result, String> { + self.zs.push(z.iter().map(|p| p.to_vec()).collect()); + let k = self.zs.len(); + let refs: Vec> = self.zs.iter().map(|v| prec::parts(v)).collect(); + let mut f = self.pr.residual_jet(&refs, k + 1, par)?; + if f.len() <= k { + return Err(format!("residual_jet gave {} orders, not {}", f.len(), k + 1)); + } + Ok(f.swap_remove(k)) + } } diff --git a/tests/anm.rs b/tests/anm.rs index a0792a8..ec67155 100644 --- a/tests/anm.rs +++ b/tests/anm.rs @@ -4,7 +4,7 @@ use multisolver::anm::{series, Branch}; use multisolver::multidouble::linalg::ld_for; use multisolver::prec::{nan_max, parts}; -use multisolver::{Double, Jvp, Native, Precision, Problem, Quad, ResidualJet, DD}; +use multisolver::{Double, JetOrders, Jvp, Native, Precision, Problem, Quad, Recompute, ResidualJet, DD}; const N: usize = 10; const KAPPA: f64 = 0.3; @@ -13,13 +13,15 @@ const LAM0: f64 = 0.7; /// F_i(z, lam) = z_i^3 - w_i(lam)^3 + kappa (z_(i+1) - w_(i+1)(lam)), the index cyclic, with the quadratic /// w_i(lam) = a_i + b_i lam + c_i lam^2, so the branch through lam0 is z(lam0 + h) = w(lam0 + h): z_1 = b + 2 c lam0, /// z_2 = c, z_k = 0 for k >= 3. `freeze`: the series with lam a constant (the control). `nan_jvp`: the products J v -/// all NaN (a broken refinement, the control of the recorded residuals). +/// all NaN (a broken refinement, the control of the recorded residuals). `online`: the series one order at a time +/// by `residual_jet` (0), by `PolyOrders` (1), or by `PolyOrders` without its redo (2, the control). struct Poly> { a: Vec, b: Vec, c: Vec, freeze: bool, nan_jvp: bool, + online: u8, } fn s>(x: f64) -> P::Scalar { @@ -53,6 +55,7 @@ impl> Poly

{ c: (0..N).map(|i| s::

(0.05 * (2.0 * i as f64).sin())).collect(), freeze, nan_jvp: false, + online: 0, } } /// w_i(lam0 + h) as a series of n terms @@ -130,12 +133,76 @@ impl> ResidualJet

for Poly

{ .collect(); Ok((0..nout).map(|k| rows.iter().map(|r| r[k]).collect()).collect()) } + fn jet_orders<'a>(&'a self, _order: usize) -> Result + 'a>, String> + where + P: 'a, + { + if self.online == 0 { + return Ok(Box::new(Recompute::new(self))); + } + Ok(Box::new(PolyOrders { + pr: self, + x: vec![Vec::new(); N], + sq: vec![Vec::new(); N], + redo: self.online == 1, + })) + } +} + +/// The polynomial's series one order at a time: each unknown's coefficients and its square's, kept between calls. +/// The square's order k - 1, formed with z_(k-1) zero by the call before, is formed again once z_(k-1) is given +/// (`redo`; the control keeps it). Every sum is `conv`'s, term for term, so order k is `residual_jet`'s, bitwise. +struct PolyOrders<'a, P: Precision> { + pr: &'a Poly

, + x: Vec>, + sq: Vec>, + redo: bool, +} + +/// order m of the product of the series x and y, their coefficients past the given ones zero, summed as `conv` does +fn conv_at>(x: &[P::Scalar], y: &[P::Scalar], m: usize) -> P::Scalar { + let at = |v: &[P::Scalar], j: usize| if j < v.len() { v[j] } else { P::ZERO }; + (0..=m).fold(P::ZERO, |t, j| t + at(x, j) * at(y, m - j)) +} + +impl> JetOrders

for PolyOrders<'_, P> { + fn next(&mut self, z: &[&[f64]], _par: bool) -> Result, String> { + let z = scalars::

(z); + let k = self.x[0].len() + 1; + for ((x, sq), &zi) in self.x.iter_mut().zip(&mut self.sq).zip(&z) { + x.push(zi); + if sq.len() == k { + if self.redo { + sq[k - 1] = conv_at::

(x, x, k - 1); + } + } else { + sq.push(conv_at::

(x, x, k - 1)); + } + sq.push(conv_at::

(x, x, k)); + } + Ok((0..N) + .map(|i| { + let j = (i + 1) % N; + let z3 = conv_at::

(&self.sq[i], &self.x[i], k); + let wi = self.pr.w(i, k + 1); + let w3 = conv::

(&conv::

(&wi, &wi, k + 1), &wi, k + 1)[k]; + let wj = self.pr.w(j, k + 1)[k]; + z3 - w3 + s::

(KAPPA) * (P::ZERO - wj) + }) + .collect()) + } } /// The series of order 8 at the root of the polynomial branch in P, refined once fn poly_branch>(freeze: bool, nan_jvp: bool) -> Branch

{ + poly_branch_by::

(freeze, nan_jvp, 0) +} + +/// `poly_branch` with the residual's orders from `Poly::online`'s source +fn poly_branch_by>(freeze: bool, nan_jvp: bool, online: u8) -> Branch

{ let mut pr = Poly::

::new(freeze); pr.nan_jvp = nan_jvp; + pr.online = online; let z0 = pr.root(); series::(&pr, &parts(&z0), 8, 1, false).unwrap() } @@ -239,3 +306,36 @@ fn anm_records_its_solve_residuals() { "control did not fire" ); } + +/// the number of entries of x and y whose parts differ in any bit +fn bits_differ>(x: &[P::Scalar], y: &[P::Scalar]) -> usize { + x.iter() + .zip(y) + .filter(|(&u, &v)| (0..P::PARTS).any(|q| P::part(u, q).to_bits() != P::part(v, q).to_bits())) + .count() +} + +/// Pass, written before the first run: the series of order 8 whose residual orders come one at a time from a +/// problem that keeps its lower orders (`PolyOrders`) is the series from `residual_jet`, bit for bit, in double- and +/// quad-double. The control: without the redo of order k - 1 once z_(k-1) is given, the coefficients differ, first +/// at order 2. +#[test] +fn anm_takes_the_orders_from_jet_orders() { + fn run>(name: &str) { + let base = poly_branch_by::

(false, false, 0); + let online = poly_branch_by::

(false, false, 1); + let stale = poly_branch_by::

(false, false, 2); + let per_order = + |b: &Branch

| -> Vec { base.z.iter().zip(&b.z).map(|(x, y)| bits_differ::

(x, y)).collect() }; + let (d1, d2) = (per_order(&online), per_order(&stale)); + let first = d2.iter().position(|&n| n > 0); + println!("{name}: differing entries per order, online {d1:?}, control {d2:?}"); + assert!( + d1.iter().all(|&n| n == 0), + "{name}: the orders from jet_orders changed the series" + ); + assert_eq!(first, Some(2), "{name}: control did not fire as written"); + } + run::("double-double"); + run::("quad-double"); +}