gprx 0.2.0

Gaussian process regression in Rust: exact GPR, sparse GPR (VFE), and SVGP.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! Describes the reusable buffers for one batch GPR fit of size `n`, and query buffers for [`crate::FittedGpr::predict_into`].
//!
//! Fit buffers are allocated once when fit starts. Later optimizer iterations
//! overwrite the same storage. [`FitBuffers`] holds the shared core, the
//! distance cache when [`crate::DistanceCachePolicy::Cached`], and the
//! dedicated `W` matrix when [`crate::CholeskyBuffer::Retain`]. Query buffers
//! live on [`QueryWorkspace`]. Crate-private; faer types are not re-exported.

use dyn_stack::{MemBuffer, MemStack, StackReq};
use faer::linalg::cholesky::llt;
use faer::{Mat, MatRef};

use crate::error::GprError;
use crate::kernel::KernelScalar;
use crate::linalg::{faer_par, fill_identity};
use crate::precision::PrecisionPolicy;

/// Shared fit buffers: `L` (or `W` while a reuse gradient is in progress)
/// and Cholesky scratch. No distance cache.
pub struct WorkspaceCore<P: PrecisionPolicy> {
    /// `A = K + σn² I`, then the LLT factor `L` after Cholesky.
    pub(crate) k_matrix: Mat<P::Storage>,
    /// Kernel values and `∂K/∂θ` output. Reuse n-RHS solve also uses this.
    pub(crate) exp_buf: Mat<P::Storage>,
    /// Distinct `n×n` scratch for `∂K/∂θ` of a product or custom leaf. Empty
    /// until such a tree needs a gradient, so isotropic RBF does not carry an
    /// extra matrix.
    pub(crate) kernel_scratch: Mat<P::Storage>,
    /// One empty `0×0` matrix per Rayon worker. Detached with `mem::take`
    /// before a parallel kernel fill so closures never borrow `&mut Workspace`.
    pub(crate) thread_scratch: Vec<Mat<P::Storage>>,
    /// Right-hand side `y` then `α` for the training Cholesky solve (`n×1`).
    pub(crate) rhs: Mat<P::Storage>,
    /// Scratch for faer `cholesky_in_place` / `solve_in_place`.
    pub(crate) faer_scratch: MemBuffer,
    /// `θ` before the current hyperparameter write, to write back when `A`
    /// does not factor. Scratch: meaningless between writes.
    pub(crate) theta: Vec<f64>,
    /// Buffers for sum / product terms nested in another sum / product, one
    /// per nesting level. Empty until such a tree is evaluated. Scratch.
    pub(crate) nested: Vec<Mat<P::Storage>>,
    /// Buffers of the exact Hessian. Empty until the first Hessian. Scratch.
    pub(crate) hessian: HessianScratch<P::Storage>,
    /// `n×n` buffers of the joint gradient's walk of the kernel tree
    /// (`CompiledKernel::weighted_grads`). The leading
    /// `CompiledKernel::kept_buffers` hold the product factor Grams the
    /// joint gradient's factor step kept at the current `θ`; the rest are
    /// scratch. Empty until the first gradient.
    pub(crate) weighted: Vec<Mat<P::Storage>>,
    /// Scratch for the ARD lengthscale matrix product. Grown on the first
    /// gradient and kept, so a later evaluation does not allocate.
    pub(crate) ard_fold: Vec<f64>,
    /// Diagonal jitter `j` the last successful factor of `A + σn² I` added
    /// (`0` without a retry). `k_matrix` then holds the factor of `A + (σn² + j) I`.
    pub(crate) factor_jitter: f64,
}

/// Training-distance tensors ([`crate::DistanceCachePolicy::Cached`]).
///
/// Buffers here are caches: their contents stay valid across calls, so a
/// slot is `None` until it is filled from the training `X` (which never
/// changes for one set of fit buffers). Scratch buffers on
/// [`WorkspaceCore`] are the opposite: empty `Mat`s grown on demand whose
/// contents mean nothing between calls.
#[derive(Clone, Default)]
pub struct DistCache<S> {
    /// Pairwise squared distances for isotropic (distance-mode) leaves (`n × n`).
    pub(crate) dist: Option<Mat<S>>,
    /// Raw `(Δx_d)²` for ARD leaves (`d · n(n+1)/2`, lower triangles).
    pub(crate) ard_sq_diff: Option<crate::kernel::ArdSqDiffBuf<S>>,
}

/// Fit buffers for one training size: the shared core, plus the distance
/// cache and the dedicated `W` when the trainer's policies ask for them.
pub struct FitBuffers<P: PrecisionPolicy> {
    pub(crate) core: WorkspaceCore<P>,
    /// `Some` for [`crate::DistanceCachePolicy::Cached`].
    pub(crate) dist: Option<DistCache<P::Storage>>,
    /// Dedicated `W = ααᵀ - K⁻¹` for [`crate::CholeskyBuffer::Retain`].
    /// `None` reuses `k_matrix` as `W` and refactors afterwards.
    pub(crate) w_matrix: Option<Mat<P::Storage>>,
}

impl<P: PrecisionPolicy> Clone for FitBuffers<P> {
    fn clone(&self) -> Self {
        Self {
            core: self.core.clone(),
            dist: self.dist.clone(),
            w_matrix: self.w_matrix.clone(),
        }
    }
}

impl<P: PrecisionPolicy> FitBuffers<P> {
    /// Allocates the core, the cache for `cache`, and `W` for `buffer`.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::EmptyInput`] if `n` is zero.
    pub(crate) fn new(
        n: usize,
        cache: crate::policy::DistanceCachePolicy,
        buffer: crate::policy::CholeskyBuffer,
    ) -> Result<Self, GprError> {
        let core = WorkspaceCore::new(n)?;
        let dist = match cache {
            crate::policy::DistanceCachePolicy::Cached => Some(DistCache::default()),
            crate::policy::DistanceCachePolicy::Uncached => None,
        };
        let w_matrix = match buffer {
            crate::policy::CholeskyBuffer::Retain => Some(Mat::<P::Storage>::zeros(n, n)),
            crate::policy::CholeskyBuffer::Reuse => None,
        };
        Ok(Self {
            core,
            dist,
            w_matrix,
        })
    }

    /// Whether a gradient overwrites `L` with `W` (no dedicated `W`).
    pub(crate) fn overwrites_cholesky(&self) -> bool {
        self.w_matrix.is_none()
    }
}

/// Construction and core access for composed fit buffers.
pub trait FitWorkspace: Clone + Send + Sync + 'static {
    type Policy: PrecisionPolicy;

    fn core(&self) -> &WorkspaceCore<Self::Policy>;

    fn core_mut(&mut self) -> &mut WorkspaceCore<Self::Policy>;

    /// Splits core buffers from an optional distance cache.
    #[allow(clippy::type_complexity)]
    fn split_fit(
        &mut self,
    ) -> (
        &mut WorkspaceCore<Self::Policy>,
        Option<&mut DistCache<<Self::Policy as PrecisionPolicy>::Storage>>,
    );

    /// Forms `W = ααᵀ - K⁻¹` after `k_matrix` holds `L`.
    fn form_gradient_w(&mut self, alpha: &[<Self::Policy as PrecisionPolicy>::Storage], n: usize);

    /// `W` after [`Self::form_gradient_w`].
    fn gradient_w(&self) -> MatRef<'_, <Self::Policy as PrecisionPolicy>::Storage>;

    /// `W` with the buffers a kernel-gradient walk writes, borrowed apart.
    fn split_gradient(&mut self) -> GradientViews<'_, <Self::Policy as PrecisionPolicy>::Storage>;

    /// Whether this workspace stores `dist_cache` / `ard_sq_diff`.
    #[allow(dead_code)] // used by unit tests on `FittedGpr::workspace`
    fn has_distance_cache(&self) -> bool {
        false
    }

    /// Whether this workspace stores a dedicated `W` matrix.
    #[allow(dead_code)] // used by unit tests on `FittedGpr::workspace`
    fn has_dedicated_w(&self) -> bool {
        false
    }
}

/// `W` and the buffers a kernel-gradient walk writes, borrowed apart from
/// one [`FitWorkspace`] (see [`FitWorkspace::split_gradient`]).
pub(crate) struct GradientViews<'a, S> {
    pub(crate) w: MatRef<'a, S>,
    pub(crate) kernel_scratch: &'a mut Mat<S>,
    pub(crate) nested: &'a mut Vec<Mat<S>>,
    pub(crate) thread_scratch: &'a mut Vec<Mat<S>>,
    pub(crate) weighted: &'a mut Vec<Mat<S>>,
    pub(crate) dist: Option<&'a mut DistCache<S>>,
    pub(crate) ard_fold: &'a mut Vec<f64>,
}

/// The query columns a [`QueryWorkspace`] holds.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum QueryCols {
    /// A kernel with coordinate leaves: `d` columns, at least one.
    Points(usize),
    /// A kernel on supplied distances alone: no column.
    Distances,
}

impl QueryCols {
    /// The columns of a kernel whose coordinate leaves read `x` when
    /// `points`, `d` of them; none otherwise.
    pub(crate) fn of(points: bool, d: usize) -> Self {
        if points {
            Self::Points(d)
        } else {
            Self::Distances
        }
    }

    /// The number of columns.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::EmptyInput`] for [`Self::Points`] of zero.
    fn count(self) -> Result<usize, GprError> {
        match self {
            Self::Points(0) => Err(GprError::EmptyInput),
            Self::Points(d) => Ok(d),
            Self::Distances => Ok(0),
        }
    }
}

/// Predict-into buffers owned by [`crate::FittedGpr`].
///
/// Sized on the first `predict_into` for `(n, m, d)`. The same query length
/// reuses this storage. [`crate::FittedGpr::predict`] allocates locally and
/// does not touch these fields.
#[derive(Clone)]
pub(crate) struct QueryWorkspace<P: PrecisionPolicy> {
    /// Transformed query features, packed column-major.
    pub(crate) query_xs: Vec<f64>,
    /// Query points `m×d`.
    pub(crate) query_x: Mat<P::Storage>,
    /// `k(X, X*)` then `L⁻¹ k_*` (`n×m`).
    pub(crate) query_k_star: Mat<P::Storage>,
    /// Scratch for `apply_cross` (`n×m`).
    pub(crate) query_scratch: Mat<P::Storage>,
    /// Nested sum / product buffers for the train–query block. Scratch.
    pub(crate) query_nested: Vec<Mat<P::Storage>>,
    /// Train–test squared distances (`n×m`).
    pub(crate) query_dist: Mat<P::Storage>,
    /// `k(x*_j, x*_j)` for each query column.
    pub(crate) query_kss: Vec<P::Storage>,
}

pub(crate) fn empty_thread_scratch<T: KernelScalar>() -> Vec<Mat<T>> {
    let n = rayon::current_num_threads().max(1);
    (0..n).map(|_| Mat::<T>::zeros(0, 0)).collect()
}

fn faer_scratch_req<T: faer_traits::ComplexField>(n: usize) -> StackReq {
    let par = faer_par(n);
    let chol = llt::factor::cholesky_in_place_scratch::<T>(n, par, Default::default());
    let solve_vec = llt::solve::solve_in_place_scratch::<T>(n, 1, par);
    let solve_mat = llt::solve::solve_in_place_scratch::<T>(n, n, par);
    chol.or(solve_vec).or(solve_mat)
}

impl<P> WorkspaceCore<P>
where
    P: PrecisionPolicy,
{
    fn new(n: usize) -> Result<Self, GprError> {
        if n == 0 {
            return Err(GprError::EmptyInput);
        }
        Ok(Self {
            k_matrix: Mat::<P::Storage>::zeros(n, n),
            exp_buf: Mat::<P::Storage>::zeros(n, n),
            kernel_scratch: Mat::<P::Storage>::zeros(0, 0),
            thread_scratch: empty_thread_scratch::<P::Storage>(),
            rhs: Mat::<P::Storage>::zeros(n, 1),
            faer_scratch: MemBuffer::new(faer_scratch_req::<P::Storage>(n)),
            theta: Vec::new(),
            nested: Vec::new(),
            hessian: HessianScratch::default(),
            weighted: Vec::new(),
            ard_fold: Vec::new(),
            factor_jitter: 0.0,
        })
    }

    /// Returns the number of training points this workspace was sized for.
    pub(crate) fn n(&self) -> usize {
        self.k_matrix.nrows()
    }

    /// Ensures the `∂K/∂θ` scratch is `n×n`. No-op when already sized.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::EmptyInput`] if `n` is zero.
    pub(crate) fn ensure_kernel_scratch(&mut self, n: usize) -> Result<(), GprError> {
        if n == 0 {
            return Err(GprError::EmptyInput);
        }
        if self.kernel_scratch.nrows() == n && self.kernel_scratch.ncols() == n {
            return Ok(());
        }
        self.kernel_scratch = Mat::<P::Storage>::zeros(n, n);
        Ok(())
    }
}

/// Buffers of the exact Hessian (`hessian_into`): the half-solved
/// derivatives `S_i` and `v_i` of every parameter. Scratch.
pub(crate) struct HessianScratch<S> {
    /// `S_i = L⁻¹ ∂A/∂θ_i L⁻ᵀ`, one `n×n` per parameter.
    pub(crate) s: Vec<Mat<S>>,
    /// `v_i = L⁻¹ ∂A/∂θ_i α` in column `i` (`n × p`).
    pub(crate) v: Mat<S>,
    /// `∂A/∂θ_i α` before its solve.
    pub(crate) u: Vec<S>,
}

impl<S> Default for HessianScratch<S> {
    fn default() -> Self {
        Self {
            s: Vec::new(),
            v: Mat::new(),
            u: Vec::new(),
        }
    }
}

impl<S: KernelScalar> HessianScratch<S> {
    /// Sizes every buffer for `n` training points and `p` parameters.
    /// No-op when already sized.
    pub(crate) fn ensure(&mut self, n: usize, p: usize) {
        if self.s.len() != p || self.s.first().is_some_and(|m| m.nrows() != n) {
            self.s = (0..p).map(|_| Mat::zeros(n, n)).collect();
        }
        if self.v.nrows() != n || self.v.ncols() != p {
            self.v = Mat::zeros(n, p);
        }
        self.u.resize(n, S::from_f64(0.0));
    }
}

impl<P> Clone for WorkspaceCore<P>
where
    P: PrecisionPolicy,
{
    fn clone(&self) -> Self {
        Self {
            k_matrix: self.k_matrix.clone(),
            exp_buf: self.exp_buf.clone(),
            kernel_scratch: self.kernel_scratch.clone(),
            thread_scratch: self.thread_scratch.clone(),
            rhs: self.rhs.clone(),
            faer_scratch: MemBuffer::new(faer_scratch_req::<P::Storage>(self.n())),
            theta: self.theta.clone(),
            nested: Vec::new(),
            hessian: HessianScratch::default(),
            weighted: Vec::new(),
            ard_fold: Vec::new(),
            factor_jitter: self.factor_jitter,
        }
    }
}

fn form_w_lower<T>(mut w: faer::MatMut<'_, T>, alpha: &[T], n: usize)
where
    T: KernelScalar,
{
    for col in 0..n {
        for row in col..n {
            w[(row, col)] = alpha[row] * alpha[col] - w[(row, col)];
        }
    }
}

fn form_w_from_inverse<T>(
    mut dest: faer::MatMut<'_, T>,
    k_inv: MatRef<'_, T>,
    alpha: &[T],
    n: usize,
) where
    T: KernelScalar,
{
    for col in 0..n {
        for row in col..n {
            dest[(row, col)] = alpha[row] * alpha[col] - k_inv[(row, col)];
        }
    }
}

impl<P> FitWorkspace for FitBuffers<P>
where
    P: PrecisionPolicy + 'static,
{
    type Policy = P;

    fn core(&self) -> &WorkspaceCore<P> {
        &self.core
    }

    fn core_mut(&mut self) -> &mut WorkspaceCore<P> {
        &mut self.core
    }

    fn split_fit(&mut self) -> (&mut WorkspaceCore<P>, Option<&mut DistCache<P::Storage>>) {
        (&mut self.core, self.dist.as_mut())
    }

    fn form_gradient_w(&mut self, alpha: &[P::Storage], n: usize) {
        let core = &mut self.core;
        match &mut self.w_matrix {
            Some(w_matrix) => {
                fill_identity(w_matrix.as_mut());
                {
                    let stack = MemStack::new(&mut core.faer_scratch);
                    llt::solve::solve_in_place(
                        core.k_matrix.as_ref(),
                        w_matrix.as_mut(),
                        faer_par(n),
                        stack,
                    );
                }
                form_w_lower(w_matrix.as_mut(), alpha, n);
            }
            None => {
                fill_identity(core.exp_buf.as_mut());
                {
                    let stack = MemStack::new(&mut core.faer_scratch);
                    llt::solve::solve_in_place(
                        core.k_matrix.as_ref(),
                        core.exp_buf.as_mut(),
                        faer_par(n),
                        stack,
                    );
                }
                form_w_from_inverse(core.k_matrix.as_mut(), core.exp_buf.as_ref(), alpha, n);
            }
        }
    }

    fn gradient_w(&self) -> MatRef<'_, P::Storage> {
        match &self.w_matrix {
            Some(w_matrix) => w_matrix.as_ref(),
            None => self.core.k_matrix.as_ref(),
        }
    }

    fn split_gradient(&mut self) -> GradientViews<'_, P::Storage> {
        let core = &mut self.core;
        GradientViews {
            w: match &self.w_matrix {
                Some(w_matrix) => w_matrix.as_ref(),
                None => core.k_matrix.as_ref(),
            },
            kernel_scratch: &mut core.kernel_scratch,
            nested: &mut core.nested,
            thread_scratch: &mut core.thread_scratch,
            weighted: &mut core.weighted,
            dist: self.dist.as_mut(),
            ard_fold: &mut core.ard_fold,
        }
    }

    fn has_distance_cache(&self) -> bool {
        self.dist.is_some()
    }

    fn has_dedicated_w(&self) -> bool {
        self.w_matrix.is_some()
    }
}

impl<P> QueryWorkspace<P>
where
    P: PrecisionPolicy + 'static,
{
    /// Builds empty query buffers. [`Self::ensure`] sizes them on first use.
    pub(crate) fn new() -> Self {
        Self {
            query_xs: Vec::new(),
            query_x: Mat::<P::Storage>::zeros(0, 0),
            query_k_star: Mat::<P::Storage>::zeros(0, 0),
            query_scratch: Mat::<P::Storage>::zeros(0, 0),
            query_nested: Vec::new(),
            query_dist: Mat::<P::Storage>::zeros(0, 0),
            query_kss: Vec::new(),
        }
    }

    /// Sizes buffers for an `n×m` predict with the query columns `cols`.
    /// No-op when already sized.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::EmptyInput`] if `n` or `m` is zero, or a kernel
    /// with coordinate leaves has no column.
    pub(crate) fn ensure(&mut self, n: usize, m: usize, cols: QueryCols) -> Result<(), GprError> {
        let d = cols.count()?;
        if n == 0 || m == 0 {
            return Err(GprError::EmptyInput);
        }
        if self.query_k_star.nrows() == n
            && self.query_k_star.ncols() == m
            && self.query_x.ncols() == d
            && self.query_x.nrows() == m
        {
            return Ok(());
        }
        self.query_xs
            .resize(m.checked_mul(d).ok_or(GprError::SizeOverflow)?, 0.0);
        self.query_x = Mat::<P::Storage>::zeros(m, d);
        self.query_k_star = Mat::<P::Storage>::zeros(n, m);
        self.query_scratch = Mat::<P::Storage>::zeros(n, m);
        self.query_dist = Mat::<P::Storage>::zeros(n, m);
        self.query_kss.resize(m, P::Storage::from_f64(0.0));
        Ok(())
    }

    /// Grows query buffers so an `n×m` fill with feature count `d` fits.
    ///
    /// New sides are `max(needed, max(current, 1) * 2)` when a side is short.
    /// No-op when every buffer already fits. Does not shrink.
    ///
    /// # Errors
    ///
    /// Returns [`GprError::EmptyInput`] if `n` or `m` is zero, or `cols` is
    /// [`QueryCols::Points`] of zero.
    pub(crate) fn ensure_at_least(
        &mut self,
        n: usize,
        m: usize,
        cols: QueryCols,
    ) -> Result<(), GprError> {
        let d = cols.count()?;
        if n == 0 || m == 0 {
            return Err(GprError::EmptyInput);
        }
        let have_n = self.query_k_star.nrows();
        let have_m = self.query_k_star.ncols();
        let have_d = self.query_x.ncols();
        let have_xq = self.query_x.nrows();
        if have_n >= n && have_m >= m && have_d == d && have_xq >= m {
            if self.query_xs.len() < m.saturating_mul(d) {
                self.query_xs.resize(m * d, 0.0);
            }
            if self.query_kss.len() < m {
                self.query_kss.resize(m, P::Storage::from_f64(0.0));
            }
            return Ok(());
        }
        let new_n = if have_n >= n {
            have_n
        } else {
            n.max(have_n.max(1).saturating_mul(2))
        };
        let new_m = if have_m >= m && have_xq >= m {
            have_m.max(have_xq)
        } else {
            m.max(have_m.max(have_xq).max(1).saturating_mul(2))
        };
        self.query_xs
            .resize(new_m.checked_mul(d).ok_or(GprError::SizeOverflow)?, 0.0);
        self.query_x = Mat::<P::Storage>::zeros(new_m, d);
        self.query_k_star = Mat::<P::Storage>::zeros(new_n, new_m);
        self.query_scratch = Mat::<P::Storage>::zeros(new_n, new_m);
        self.query_dist = Mat::<P::Storage>::zeros(new_n, new_m);
        self.query_kss.resize(new_m, P::Storage::from_f64(0.0));
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::{
        FitBuffers, FitWorkspace, QueryCols, QueryWorkspace, WorkspaceCore, faer_scratch_req,
    };
    use crate::error::GprError;
    use crate::policy::{CholeskyBuffer, DistanceCachePolicy};
    use crate::precision::DoublePrecision;
    use crate::test_check::assert_send_sync;

    fn assert_square(mat: &faer::Mat<f64>, n: usize) {
        assert_eq!(mat.nrows(), n);
        assert_eq!(mat.ncols(), n);
    }

    fn speed(n: usize) -> Result<FitBuffers<DoublePrecision>, GprError> {
        FitBuffers::new(n, DistanceCachePolicy::Cached, CholeskyBuffer::Retain)
    }

    #[test]
    fn new_rejects_empty() {
        assert_eq!(speed(0).err(), Some(GprError::EmptyInput));
    }

    #[test]
    fn reuse_workspace_has_no_second_n_by_n_w() {
        let n = 8;
        let ws = FitBuffers::<DoublePrecision>::new(
            n,
            DistanceCachePolicy::Cached,
            CholeskyBuffer::Reuse,
        )
        .expect("n > 0");
        assert_eq!(ws.core().n(), n);
        assert_square(&ws.core().k_matrix, n);
        assert!(ws.dist.as_ref().expect("cached").dist.is_none());
        assert_square(&ws.core().exp_buf, n);
        assert!(ws.w_matrix.is_none());
        assert!(ws.overwrites_cholesky());
        let uncached = FitBuffers::<DoublePrecision>::new(
            n,
            DistanceCachePolicy::Uncached,
            CholeskyBuffer::Reuse,
        )
        .expect("n > 0");
        assert!(uncached.dist.is_none());
        assert!(!uncached.has_distance_cache());
    }

    #[test]
    fn new_allocates_n_by_n_buffers_and_scratch() {
        let n = 8;
        let ws = speed(n).expect("n > 0");
        let core = ws.core();
        assert_eq!(core.n(), n);
        let dist = ws.dist.as_ref().expect("cached");
        assert!(dist.dist.is_none());
        assert_square(&core.k_matrix, n);
        assert_square(ws.w_matrix.as_ref().expect("retain"), n);
        assert_square(&core.exp_buf, n);
        assert_eq!(core.kernel_scratch.nrows(), 0);
        assert!(dist.ard_sq_diff.is_none());
        assert_eq!(core.rhs.nrows(), n);
        assert_eq!(core.rhs.ncols(), 1);
        assert_eq!(
            core.thread_scratch.len(),
            rayon::current_num_threads().max(1)
        );
        assert!(
            core.thread_scratch
                .iter()
                .all(|m| m.nrows() == 0 && m.ncols() == 0)
        );
        assert_eq!(
            core.faer_scratch.len(),
            faer_scratch_req::<f64>(n).size_bytes()
        );
        assert_send_sync::<FitBuffers<DoublePrecision>>();
        assert_send_sync::<QueryWorkspace<DoublePrecision>>();
        let _core: &WorkspaceCore<DoublePrecision> = core;
    }

    #[test]
    fn ensure_kernel_scratch_allocates_when_needed() {
        let mut ws = speed(4).expect("n > 0");
        let core = ws.core_mut();
        assert_eq!(core.kernel_scratch.nrows(), 0);
        core.ensure_kernel_scratch(4).expect("n > 0");
        assert_square(&core.kernel_scratch, 4);
        core.ensure_kernel_scratch(4).expect("same n");
        assert_square(&core.kernel_scratch, 4);
        assert_eq!(
            core.ensure_kernel_scratch(0).err(),
            Some(GprError::EmptyInput)
        );
    }

    #[test]
    fn query_ensure_allocates_when_needed() {
        let mut query = QueryWorkspace::<DoublePrecision>::new();
        assert_eq!(query.query_k_star.ncols(), 0);
        query.ensure(4, 3, QueryCols::Points(2)).expect("m,d > 0");
        assert_eq!(query.query_x.nrows(), 3);
        assert_eq!(query.query_x.ncols(), 2);
        assert_eq!(query.query_k_star.nrows(), 4);
        assert_eq!(query.query_k_star.ncols(), 3);
        assert_eq!(query.query_xs.len(), 6);
        assert_eq!(query.query_kss.len(), 3);
        query.ensure(4, 3, QueryCols::Points(2)).expect("same size");
        assert_eq!(query.query_k_star.ncols(), 3);
        assert_eq!(
            query.ensure(4, 0, QueryCols::Points(2)).err(),
            Some(GprError::EmptyInput)
        );
        // A coordinate kernel reads at least one column; only a kernel on
        // supplied distances alone sizes a query without one.
        assert_eq!(
            query.ensure(4, 3, QueryCols::Points(0)).err(),
            Some(GprError::EmptyInput)
        );
        query.ensure(4, 3, QueryCols::Distances).expect("no column");
        assert_eq!(
            query.ensure_at_least(4, 1, QueryCols::Points(0)).err(),
            Some(GprError::EmptyInput)
        );
    }
}