Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
16 changes: 8 additions & 8 deletions src/anm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,17 +41,17 @@ pub fn series<P: Precision<Native>, Pr: ResidualJet<P> + Jvp<P> + ?Sized>(
par: bool,
) -> Result<Branch<P>, String> {
let lu = exact::<P, Pr>(pr, z0, par)?.0;
let mut zp: Vec<Vec<Vec<f64>>> = vec![z0.iter().map(|p| p.to_vec()).collect()];
let mut orders = pr.jet_orders(order)?;
let mut last: Vec<Vec<f64>> = 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::<Vec<_>>()))
.collect()],
residual: Vec::new(),
};
for k in 1..=order {
let refs: Vec<Vec<&[f64]>> = zp.iter().map(|z| parts(z)).collect();
let f = pr.residual_jet(&refs, k + 1, par)?;
let b: Vec<P::Scalar> = f[k].iter().map(|&v| -v).collect();
for _ in 1..=order {
let f = orders.next(&parts(&last), par)?;
let b: Vec<P::Scalar> = f.iter().map(|&v| -v).collect();
let mut x = b.clone();
P::lu_solve(&lu, &mut x);
let resid = |x: &[P::Scalar]| -> Result<Vec<P::Scalar>, String> {
Expand All @@ -69,7 +69,7 @@ pub fn series<P: Precision<Native>, Pr: ResidualJet<P> + Jvp<P> + ?Sized>(
} else {
f64::NAN
});
zp.push(to_parts::<P>(&x));
last = to_parts::<P>(&x);
br.z.push(x);
}
Ok(br)
Expand Down
52 changes: 52 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,56 @@ pub trait ResidualJet<P: Precision<Native>>: Problem<P> {
/// 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<Vec<Vec<P::Scalar>>, 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<Box<dyn JetOrders<P> + '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<P: Precision<Native>> {
/// 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<Vec<P::Scalar>, 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<Native>, Pr: ResidualJet<P> + ?Sized> {
pr: &'a Pr,
zs: Vec<Vec<Vec<f64>>>,
_p: std::marker::PhantomData<P>,
}

impl<'a, P: Precision<Native>, Pr: ResidualJet<P> + ?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<P: Precision<Native>, Pr: ResidualJet<P> + ?Sized> JetOrders<P> for Recompute<'_, P, Pr> {
fn next(&mut self, z: &[&[f64]], par: bool) -> Result<Vec<P::Scalar>, String> {
self.zs.push(z.iter().map(|p| p.to_vec()).collect());
let k = self.zs.len();
let refs: Vec<Vec<&[f64]>> = 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))
}
}
104 changes: 102 additions & 2 deletions tests/anm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<P: Precision<Native>> {
a: Vec<P::Scalar>,
b: Vec<P::Scalar>,
c: Vec<P::Scalar>,
freeze: bool,
nan_jvp: bool,
online: u8,
}

fn s<P: Precision<Native>>(x: f64) -> P::Scalar {
Expand Down Expand Up @@ -53,6 +55,7 @@ impl<P: Precision<Native>> Poly<P> {
c: (0..N).map(|i| s::<P>(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
Expand Down Expand Up @@ -130,12 +133,76 @@ impl<P: Precision<Native>> ResidualJet<P> for Poly<P> {
.collect();
Ok((0..nout).map(|k| rows.iter().map(|r| r[k]).collect()).collect())
}
fn jet_orders<'a>(&'a self, _order: usize) -> Result<Box<dyn JetOrders<P> + '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<Native>> {
pr: &'a Poly<P>,
x: Vec<Vec<P::Scalar>>,
sq: Vec<Vec<P::Scalar>>,
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<P: Precision<Native>>(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<P: Precision<Native>> JetOrders<P> for PolyOrders<'_, P> {
fn next(&mut self, z: &[&[f64]], _par: bool) -> Result<Vec<P::Scalar>, String> {
let z = scalars::<P>(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::<P>(x, x, k - 1);
}
} else {
sq.push(conv_at::<P>(x, x, k - 1));
}
sq.push(conv_at::<P>(x, x, k));
}
Ok((0..N)
.map(|i| {
let j = (i + 1) % N;
let z3 = conv_at::<P>(&self.sq[i], &self.x[i], k);
let wi = self.pr.w(i, k + 1);
let w3 = conv::<P>(&conv::<P>(&wi, &wi, k + 1), &wi, k + 1)[k];
let wj = self.pr.w(j, k + 1)[k];
z3 - w3 + s::<P>(KAPPA) * (P::ZERO - wj)
})
.collect())
}
}

/// The series of order 8 at the root of the polynomial branch in P, refined once
fn poly_branch<P: Precision<Native>>(freeze: bool, nan_jvp: bool) -> Branch<P> {
poly_branch_by::<P>(freeze, nan_jvp, 0)
}

/// `poly_branch` with the residual's orders from `Poly::online`'s source
fn poly_branch_by<P: Precision<Native>>(freeze: bool, nan_jvp: bool, online: u8) -> Branch<P> {
let mut pr = Poly::<P>::new(freeze);
pr.nan_jvp = nan_jvp;
pr.online = online;
let z0 = pr.root();
series::<P, _>(&pr, &parts(&z0), 8, 1, false).unwrap()
}
Expand Down Expand Up @@ -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<P: Precision<Native>>(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<P: Precision<Native>>(name: &str) {
let base = poly_branch_by::<P>(false, false, 0);
let online = poly_branch_by::<P>(false, false, 1);
let stale = poly_branch_by::<P>(false, false, 2);
let per_order =
|b: &Branch<P>| -> Vec<usize> { base.z.iter().zip(&b.z).map(|(x, y)| bits_differ::<P>(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-double");
run::<Quad>("quad-double");
}
Loading