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
//! Leaves on supplied squared distances against the same leaves on
//! coordinates.

use crate::GprError;
use crate::kernel::compiled::CrossViews;
use crate::kernel::compiled::gram::GramInputs;
use crate::kernel::compiled::supplied::{ArdRect, ArdSquare, RectSlots, SquareTable, unbound};
use crate::kernel::compiled::weighted::{DiagAccum, WeightedWalk};
use crate::kernel::dist::{ArdBlocks, ArdSqDiffBuf, BlockList};
use crate::kernel::{
    ArdDistance, CompiledKernel, ConstantKernel, DistanceKernel, KernelSpec, MaternArdKernel,
    MaternKernel, MaternNu, PeriodicKernel, RationalQuadraticArdKernel, RationalQuadraticKernel,
    RbfArdKernel, RbfKernel, ScalarDistance, SuppliedSpec, Triangle, WhiteKernel, WithPoints,
};
use crate::math::Accurate;
use crate::test_check::assert_close;
use faer::{Mat, MatRef};

/// Rectangular supplies by shape in slot order.
struct RectTable<'a> {
    scalar: Vec<MatRef<'a, f64>>,
    ard: Vec<ArdRect<'a, f64>>,
}

impl RectSlots<f64> for RectTable<'_> {
    fn scalar(&self, at: usize) -> Result<MatRef<'_, f64>, GprError> {
        self.scalar.get(at).copied().ok_or_else(unbound)
    }

    fn ard(&self, at: usize) -> Result<ArdRect<'_, f64>, GprError> {
        self.ard.get(at).copied().ok_or_else(unbound)
    }
}

const N: usize = 5;
const M: usize = 3;

/// Column `k` of the training (`N`) or query (`M`) samples.
fn coords(k: usize, rows: usize, offset: f64) -> Vec<f64> {
    (0..rows)
        .map(|i| ((i as f64 + offset) * (0.37 + 0.11 * k as f64)).sin() * (1.0 + k as f64))
        .collect()
}

fn mat_from_cols(cols: &[Vec<f64>]) -> Mat<f64> {
    let rows = cols[0].len();
    Mat::from_fn(rows, cols.len(), |i, j| cols[j][i])
}

fn sq(a: &[f64], b: &[f64]) -> Mat<f64> {
    Mat::from_fn(a.len(), b.len(), |i, j| (a[i] - b[j]) * (a[i] - b[j]))
}

struct Problem {
    image: ScalarDistance,
    bands: ArdDistance,
    spec: DistanceKernel<WithPoints>,
    /// The coordinate leaf's column, train and query.
    x: Mat<f64>,
    xs: Mat<f64>,
    /// Scalar slot squares.
    train_sq: Mat<f64>,
    cross_sq: Mat<f64>,
    /// ARD slot.
    train_ard: ArdSqDiffBuf<f64>,
    cross_blocks: Vec<Vec<f64>>,
    /// Reference kernels on coordinates, one per slot plus the coordinate leaf.
    ref_image: CompiledKernel<f64>,
    ref_bands: CompiledKernel<f64>,
    ref_points: CompiledKernel<f64>,
    image_x: (Mat<f64>, Mat<f64>),
    bands_x: (Mat<f64>, Mat<f64>),
}

fn problem() -> Problem {
    let image = ScalarDistance::new();
    let bands = ArdDistance::of_dims(2);
    let rbf = RbfKernel::new(0.8).expect("ell");
    let ard = RbfArdKernel::new(&[0.7, 1.3]).expect("ell");
    let point = RbfKernel::new(0.9).expect("ell");
    let spec = image.kernel(rbf) * bands.kernel(ard.clone()).expect("dims")
        + ConstantKernel::new(0.3).expect("c")
        + image.kernel(MaternKernel::new(1.1, MaternNu::FiveHalves).expect("ell"))
            * KernelSpec::from(point);
    let c0 = (coords(0, N, 0.0), coords(0, M, 0.5));
    let c1 = (coords(1, N, 0.0), coords(1, M, 0.5));
    let c2 = (coords(2, N, 0.0), coords(2, M, 0.5));
    let c3 = (coords(3, N, 0.0), coords(3, M, 0.5));
    let bands_train = mat_from_cols(&[c1.0.clone(), c2.0.clone()]);
    let cross_blocks = vec![
        sq(&c1.0, &c1.1)
            .col_iter()
            .flat_map(|c| c.iter().copied().collect::<Vec<_>>())
            .collect(),
        sq(&c2.0, &c2.1)
            .col_iter()
            .flat_map(|c| c.iter().copied().collect::<Vec<_>>())
            .collect(),
    ];
    Problem {
        image,
        bands,
        spec,
        x: mat_from_cols(std::slice::from_ref(&c3.0)),
        xs: mat_from_cols(std::slice::from_ref(&c3.1)),
        train_sq: sq(&c0.0, &c0.0),
        cross_sq: sq(&c0.0, &c0.1),
        train_ard: ArdSqDiffBuf::new(bands_train.as_ref()).expect("cache"),
        cross_blocks,
        ref_image: (KernelSpec::from(rbf)).compile(),
        ref_bands: KernelSpec::from(ard).compile(),
        ref_points: KernelSpec::from(point).compile(),
        image_x: (mat_from_cols(&[c0.0]), mat_from_cols(&[c0.1])),
        bands_x: (bands_train, mat_from_cols(&[c1.1, c2.1])),
    }
}

impl Problem {
    fn square_table(&self) -> SquareTable<'_, f64> {
        SquareTable {
            scalar: vec![self.train_sq.as_ref()],
            ard: vec![ArdSquare::Packed(self.train_ard.view())],
        }
    }

    fn rect_table(&self) -> RectTable<'_> {
        RectTable {
            scalar: vec![self.cross_sq.as_ref()],
            ard: vec![ArdRect::Checked(ArdBlocks::new(
                BlockList::Vecs(&self.cross_blocks),
                N,
                M,
                0,
            ))],
        }
    }

    fn gram(&self, compiled: &CompiledKernel<f64, SuppliedSpec>) -> Mat<f64> {
        let table = self.square_table();
        let inputs = GramInputs::<_, SuppliedSpec>::supplied(self.x.as_ref(), &table);
        let mut out = Mat::zeros(N, N);
        let mut scratch = Mat::zeros(N, N);
        compiled
            .eval_gram::<Accurate>(
                inputs,
                out.as_mut(),
                Triangle::Full,
                scratch.as_mut(),
                &mut Vec::new(),
            )
            .expect("gram");
        out
    }

    fn cross(&self, compiled: &CompiledKernel<f64, SuppliedSpec>) -> Mat<f64> {
        let table = self.rect_table();
        let mut out = Mat::zeros(N, M);
        let mut scratch = Mat::zeros(N, M);
        compiled
            .eval_cross_slots::<Accurate>(
                self.x.as_ref(),
                self.xs.as_ref(),
                &table,
                None,
                out.as_mut(),
                scratch.as_mut(),
                &mut Vec::new(),
                &mut [],
            )
            .expect("cross");
        out
    }
}

/// `K` of one coordinate kernel on `(a, b)`.
fn coord_block(k: &CompiledKernel<f64>, a: MatRef<'_, f64>, b: MatRef<'_, f64>) -> Mat<f64> {
    let mut out = Mat::zeros(a.nrows(), b.nrows());
    let mut scratch = Mat::zeros(a.nrows(), b.nrows());
    k.apply_cross_points::<Accurate>(a, b, out.as_mut(), scratch.as_mut())
        .expect("coords");
    out
}

#[test]
fn gram_and_cross_match_the_coordinate_leaves() {
    let p = problem();
    let compiled = p.spec.spec().compile();
    let matern =
        KernelSpec::from(MaternKernel::new(1.1, MaternNu::FiveHalves).expect("ell")).compile();
    let check = |got: &Mat<f64>,
                 a: (MatRef<'_, f64>, MatRef<'_, f64>, MatRef<'_, f64>),
                 b: (MatRef<'_, f64>, MatRef<'_, f64>, MatRef<'_, f64>)| {
        let k_img = coord_block(&p.ref_image, a.0, b.0);
        let k_band = coord_block(&p.ref_bands, a.1, b.1);
        let k_pt = coord_block(&p.ref_points, a.2, b.2);
        let k_mat = coord_block(&matern, a.0, b.0);
        for j in 0..got.ncols() {
            for i in 0..got.nrows() {
                let expect = k_img[(i, j)] * k_band[(i, j)] + 0.3 + k_mat[(i, j)] * k_pt[(i, j)];
                assert_close(got[(i, j)], expect, 1e-12);
            }
        }
    };
    let gram = p.gram(&compiled);
    check(
        &gram,
        (p.image_x.0.as_ref(), p.bands_x.0.as_ref(), p.x.as_ref()),
        (p.image_x.0.as_ref(), p.bands_x.0.as_ref(), p.x.as_ref()),
    );
    let cross = p.cross(&compiled);
    check(
        &cross,
        (p.image_x.0.as_ref(), p.bands_x.0.as_ref(), p.x.as_ref()),
        (p.image_x.1.as_ref(), p.bands_x.1.as_ref(), p.xs.as_ref()),
    );
}

fn with_theta(
    spec: &DistanceKernel<WithPoints>,
    theta: &[f64],
) -> CompiledKernel<f64, SuppliedSpec> {
    let mut spec = spec.clone();
    spec.set_params(theta).expect("theta");
    spec.spec().compile()
}

#[test]
fn gradients_and_hessians_match_finite_differences() {
    let p = problem();
    check_derivatives(&p, &p.spec);
}

#[test]
fn the_other_leaves_match_finite_differences() {
    let p = problem();
    let spec = p
        .image
        .kernel(RationalQuadraticKernel::new(0.9, 1.4).expect("rq"))
        * p.bands
            .kernel(MaternArdKernel::new(&[0.8, 1.2], MaternNu::ThreeHalves).expect("matern"))
            .expect("dims")
        + p.image
            .kernel(PeriodicKernel::new(1.1, 2.3).expect("periodic"))
            * p.bands
                .kernel(RationalQuadraticArdKernel::new(&[0.6, 1.1], 0.9).expect("rq ard"))
                .expect("dims")
        + WhiteKernel::new(0.2).expect("white")
        + KernelSpec::from(RbfKernel::new(0.9).expect("ell"));
    check_derivatives(&p, &spec);
}

/// `∂K/∂θ` and `∂²K/∂θ∂θ` of `spec`, square and rectangular, against central
/// differences of its Gram and cross block.
fn check_derivatives(p: &Problem, spec: &DistanceKernel<WithPoints>) {
    let compiled = spec.spec().compile();
    let n_params = compiled.num_params();
    let mut theta = vec![0.0; n_params];
    spec.get_params(&mut theta).expect("theta");
    let h = 1e-6;
    let table = p.square_table();
    let rect = p.rect_table();
    let inputs = GramInputs::<_, SuppliedSpec>::supplied(p.x.as_ref(), &table);
    let views = CrossViews {
        x1: p.x.as_ref(),
        x2: p.xs.as_ref(),
        dist: None,
        slots: &rect as &dyn RectSlots<f64>,
    };
    let grad_at = |c: &CompiledKernel<f64, SuppliedSpec>, k: usize| {
        let mut d_k = Mat::zeros(N, N);
        let mut scratch = Mat::zeros(N, N);
        c.grad_gram::<Accurate>(
            inputs,
            d_k.as_mut(),
            k,
            Triangle::Full,
            scratch.as_mut(),
            &mut Vec::new(),
        )
        .expect("grad");
        d_k
    };
    let grad_cross_at = |c: &CompiledKernel<f64, SuppliedSpec>, k: usize| {
        let mut d_k = Mat::zeros(N, M);
        let mut scratch = Mat::zeros(N, M);
        c.grad_cross_views::<Accurate>(views, d_k.as_mut(), k, scratch.as_mut(), &mut [])
            .expect("grad cross");
        d_k
    };
    for k in 0..n_params {
        let mut up = theta.clone();
        up[k] += h;
        let mut down = theta.clone();
        down[k] -= h;
        let (c_up, c_down) = (with_theta(spec, &up), with_theta(spec, &down));
        let (g_up, g_down) = (p.gram(&c_up), p.gram(&c_down));
        let analytic = grad_at(&compiled, k);
        let (x_up, x_down) = (p.cross(&c_up), p.cross(&c_down));
        let cross = grad_cross_at(&compiled, k);
        for j in 0..N {
            for i in 0..N {
                let fd = (g_up[(i, j)] - g_down[(i, j)]) / (2.0 * h);
                assert_close(analytic[(i, j)], fd, 1e-6);
            }
            if j < M {
                for i in 0..N {
                    let fd = (x_up[(i, j)] - x_down[(i, j)]) / (2.0 * h);
                    assert_close(cross[(i, j)], fd, 1e-6);
                }
            }
        }
        for l in 0..n_params {
            let mut d2 = Mat::zeros(N, N);
            let mut scratch = Mat::zeros(N, N);
            compiled
                .hess_gram::<Accurate>(
                    inputs,
                    d2.as_mut(),
                    (k, l),
                    Triangle::Full,
                    scratch.as_mut(),
                    &mut Vec::new(),
                )
                .expect("hess");
            let (gl_up, gl_down) = (grad_at(&c_up, l), grad_at(&c_down, l));
            let mut d2x = Mat::zeros(N, M);
            let mut scratch = Mat::zeros(N, M);
            compiled
                .hess_cross_views::<Accurate>(
                    views,
                    d2x.as_mut(),
                    (k, l),
                    scratch.as_mut(),
                    &mut [],
                )
                .expect("hess cross");
            let (xl_up, xl_down) = (grad_cross_at(&c_up, l), grad_cross_at(&c_down, l));
            for j in 0..N {
                for i in 0..N {
                    let fd = (gl_up[(i, j)] - gl_down[(i, j)]) / (2.0 * h);
                    assert_close(d2[(i, j)], fd, 1e-5);
                }
                if j < M {
                    for i in 0..N {
                        let fd = (xl_up[(i, j)] - xl_down[(i, j)]) / (2.0 * h);
                        assert_close(d2x[(i, j)], fd, 1e-5);
                    }
                }
            }
        }
        // The diagonal: `∂k(x, x)/∂θ` from the zero distance.
        let mut diag = vec![0.0; N];
        compiled
            .grad_diag_points::<Accurate>(p.x.as_ref(), &mut diag, k)
            .expect("diag");
        for (i, value) in diag.iter().enumerate() {
            assert_close(*value, analytic[(i, i)], 1e-12);
        }
    }
}

#[test]
fn weighted_walks_match_one_parameter_at_a_time() {
    let p = problem();
    let compiled = p.spec.spec().compile();
    let n_params = compiled.num_params();
    let table = p.square_table();
    let rect = p.rect_table();
    let inputs = GramInputs::<_, SuppliedSpec>::supplied(p.x.as_ref(), &table);
    let weight = Mat::from_fn(N, N, |i, j| ((i + j) as f64 * 0.3).cos());
    let mut scratch = Mat::zeros(N, N);
    let mut nested = Vec::new();
    let mut fold = Vec::new();
    let mut walk = WeightedWalk {
        inputs,
        scratch: scratch.as_mut(),
        nested: &mut nested,
        kept: &[],
        kept_products: 0,
        fold: &mut fold,
    };
    let mut bufs: Vec<Mat<f64>> = (0..compiled.contraction_buffers())
        .map(|_| Mat::zeros(N, N))
        .collect();
    let mut out = vec![0.0; n_params];
    compiled
        .weighted_grads::<Accurate>(&mut walk, weight.as_ref(), &mut out, &mut bufs)
        .expect("walk");
    let rect_weight = Mat::from_fn(N, M, |i, j| ((i * 2 + j) as f64 * 0.7).sin());
    let views = CrossViews {
        x1: p.x.as_ref(),
        x2: p.xs.as_ref(),
        dist: None,
        slots: &rect as &dyn RectSlots<f64>,
    };
    let mut rect_bufs: Vec<Mat<f64>> = (0..compiled.contraction_buffers())
        .map(|_| Mat::zeros(N, M))
        .collect();
    let mut rect_out = vec![0.0; n_params];
    let mut rect_scratch = Mat::zeros(N, M);
    compiled
        .weighted_cross_grads_views::<Accurate>(
            views,
            rect_weight.as_ref(),
            &mut rect_out,
            &mut rect_bufs,
            rect_scratch.as_mut(),
            &mut [],
            &mut Vec::new(),
        )
        .expect("rect walk");
    let mut diag_out = vec![0.0; n_params];
    compiled
        .weighted_diag_sums::<Accurate>(p.x.as_ref(), &mut diag_out, &mut DiagAccum::new())
        .expect("diag walk");
    for k in 0..n_params {
        let mut d_k = Mat::zeros(N, N);
        let mut s = Mat::zeros(N, N);
        compiled
            .grad_gram::<Accurate>(
                inputs,
                d_k.as_mut(),
                k,
                Triangle::Full,
                s.as_mut(),
                &mut Vec::new(),
            )
            .expect("grad");
        let mut expect = 0.0;
        let mut diag = 0.0;
        for j in 0..N {
            diag += d_k[(j, j)];
            for i in 0..N {
                // The walk reads the weight's lower triangle as symmetric.
                let (r, c) = if i >= j { (i, j) } else { (j, i) };
                expect += weight[(r, c)] * d_k[(i, j)];
            }
        }
        assert_close(out[k], expect, 1e-10);
        assert_close(diag_out[k], diag, 1e-10);
        let mut d_x = Mat::zeros(N, M);
        let mut s = Mat::zeros(N, M);
        compiled
            .grad_cross_views::<Accurate>(views, d_x.as_mut(), k, s.as_mut(), &mut [])
            .expect("grad cross");
        let mut expect = 0.0;
        for j in 0..M {
            for i in 0..N {
                expect += rect_weight[(i, j)] * d_x[(i, j)];
            }
        }
        assert_close(rect_out[k], expect, 1e-10);
    }
}

#[test]
fn f32_reads_the_supplied_ard_tables() {
    let p = problem();
    let spec = p
        .bands
        .kernel(RationalQuadraticArdKernel::new(&[0.7, 1.3], 1.5).expect("leaf"))
        .expect("dims");
    let c64 = spec.spec().compile();
    let c32 = spec.spec().compile_as::<f32>();
    let cache32 =
        ArdSqDiffBuf::new(Mat::<f32>::from_fn(N, 2, |i, j| p.bands_x.0[(i, j)] as f32).as_ref())
            .expect("cache");
    let table32 = SquareTable {
        scalar: Vec::new(),
        ard: vec![ArdSquare::Packed(cache32.view())],
    };
    let x32 = Mat::<f32>::zeros(N, 0);
    let inputs32 = GramInputs::<_, SuppliedSpec>::supplied(x32.as_ref(), &table32);
    let mut out32 = Mat::<f32>::zeros(N, N);
    let mut s32 = Mat::<f32>::zeros(N, N);
    c32.eval_gram::<Accurate>(
        inputs32,
        out32.as_mut(),
        Triangle::Full,
        s32.as_mut(),
        &mut Vec::new(),
    )
    .expect("f32");
    let table = p.square_table();
    let x = Mat::<f64>::zeros(N, 0);
    let inputs = GramInputs::<_, SuppliedSpec>::supplied(x.as_ref(), &table);
    let mut out = Mat::zeros(N, N);
    let mut s = Mat::zeros(N, N);
    c64.eval_gram::<Accurate>(
        inputs,
        out.as_mut(),
        Triangle::Full,
        s.as_mut(),
        &mut Vec::new(),
    )
    .expect("f64");
    for j in 0..N {
        for i in 0..N {
            assert_close(f64::from(out32[(i, j)]), out[(i, j)], 1e-5);
        }
    }
}

#[test]
fn ard_leaves_on_the_diagonal_match_the_gram_diagonal() {
    let bands = ArdDistance::of_dims(2);
    let rq = RationalQuadraticArdKernel::new(&[0.7, 1.3], 0.8).expect("rq");
    let spec = bands.kernel(rq).expect("dims") * ConstantKernel::new(1.5).expect("c")
        + bands
            .kernel(RbfArdKernel::new(&[0.9, 0.4]).expect("ell"))
            .expect("dims");
    let compiled = spec.spec().compile();
    let n_params = compiled.num_params();
    let train = mat_from_cols(&[coords(1, N, 0.0), coords(2, N, 0.0)]);
    let cache = ArdSqDiffBuf::new(train.as_ref()).expect("cache");
    let table = SquareTable {
        scalar: Vec::new(),
        ard: vec![ArdSquare::Packed(cache.view())],
    };
    let x = Mat::<f64>::zeros(N, 0);
    let inputs = GramInputs::<_, SuppliedSpec>::supplied(x.as_ref(), &table);
    let mut full = Mat::zeros(N, N);
    let mut scratch = Mat::zeros(N, N);
    let mut diag = vec![0.0; N];
    for k in 0..n_params {
        compiled
            .grad_gram::<Accurate>(
                inputs,
                full.as_mut(),
                k,
                Triangle::Full,
                scratch.as_mut(),
                &mut Vec::new(),
            )
            .expect("grad");
        compiled
            .grad_diag_points::<Accurate>(x.as_ref(), &mut diag, k)
            .expect("grad diag");
        for (i, value) in diag.iter().enumerate() {
            assert_close(*value, full[(i, i)], 1e-12);
        }
        for l in 0..n_params {
            compiled
                .hess_gram::<Accurate>(
                    inputs,
                    full.as_mut(),
                    (k, l),
                    Triangle::Full,
                    scratch.as_mut(),
                    &mut Vec::new(),
                )
                .expect("hess");
            compiled
                .hess_diag_points::<Accurate>(x.as_ref(), &mut diag, k, l)
                .expect("hess diag");
            for (i, value) in diag.iter().enumerate() {
                assert_close(*value, full[(i, i)], 1e-12);
            }
        }
    }
    let past = n_params;
    assert!(
        compiled
            .grad_diag_points::<Accurate>(x.as_ref(), &mut diag, past)
            .is_err()
    );
}

#[test]
fn a_coordinate_tree_needs_columns_and_a_distance_tree_does_not() {
    use crate::error::GprError;
    use crate::kernel::WhiteKernel;
    let constant = ConstantKernel::new(1.5).expect("c");
    let white = WhiteKernel::new(0.2).expect("white");
    let x = Mat::<f64>::zeros(N, 0);
    let mut out = Mat::<f64>::zeros(N, N);
    let mut scratch = Mat::<f64>::zeros(N, N);
    let mut diag = vec![0.0; N];
    // The leaves and a coordinate tree keep their column check.
    assert_eq!(
        constant.apply_points(x.as_ref(), out.as_mut(), Triangle::Lower),
        Err(GprError::EmptyInput)
    );
    assert_eq!(
        white.apply_points(x.as_ref(), out.as_mut(), Triangle::Lower),
        Err(GprError::EmptyInput)
    );
    let coords = (KernelSpec::from(constant) + KernelSpec::from(white)).compile();
    assert_eq!(
        coords.apply_points::<Accurate>(
            x.as_ref(),
            out.as_mut(),
            Triangle::Lower,
            scratch.as_mut()
        ),
        Err(GprError::EmptyInput)
    );
    assert_eq!(
        coords.fill_diag_points(x.as_ref(), &mut diag),
        Err(GprError::EmptyInput)
    );
    // A tree that reads supplied distances takes none.
    let image = ScalarDistance::new();
    let dist = (image.kernel(RbfKernel::new(1.0).expect("ell")) * constant + white)
        .spec()
        .compile();
    dist.fill_diag_rows(x.as_ref(), &mut diag).expect("diag");
    assert_close(diag[0], 1.5 + 0.2, 1e-12);
}

/// The slot numbers of the supplied leaves of `k`, depth first.
fn leaf_numbers(k: &CompiledKernel<f64, SuppliedSpec>, out: &mut Vec<usize>) {
    match k {
        CompiledKernel::Supplied(leaf) => out.push(leaf.at),
        CompiledKernel::Sum(terms) | CompiledKernel::Product(terms) => {
            for t in terms {
                leaf_numbers(t, out);
            }
        }
        _ => {}
    }
}

/// A compiled leaf numbers its slot among the slots of its shape in the
/// order the tree lists them, the order every supply keeps.
#[test]
fn compiled_leaves_number_their_slots_per_shape_in_slot_order() {
    let (s1, s2) = (ScalarDistance::new(), ScalarDistance::new());
    let a = ArdDistance::of_dims(2);
    let rbf = RbfKernel::new(0.8).expect("ell");
    let ard = a
        .kernel(RbfArdKernel::new(&[0.7, 1.3]).expect("ell"))
        .expect("dims");
    let spec = s2.kernel(rbf) + ard * s1.kernel(rbf) + s2.kernel(rbf);
    let listed: Vec<_> = spec.slots().iter().map(|slot| slot.id()).collect();
    let expected = [
        crate::kernel::DistanceSlot::Scalar(s2).id(),
        crate::kernel::DistanceSlot::Ard(a).id(),
        crate::kernel::DistanceSlot::Scalar(s1).id(),
    ];
    assert_eq!(listed, expected);
    let mut numbers = Vec::new();
    leaf_numbers(&spec.spec().compile(), &mut numbers);
    assert_eq!(numbers, [0, 0, 1, 0]);
}

/// A supply bound for another kernel, which holds fewer slots than the
/// tree numbers, is reported, not read out of range.
#[test]
fn a_supply_without_the_trees_slot_is_an_error() {
    let p = problem();
    let (s1, s2) = (ScalarDistance::new(), ScalarDistance::new());
    let rbf = RbfKernel::new(0.8).expect("ell");
    let spec = s1.kernel(rbf) + s2.kernel(rbf);
    let compiled = spec.spec().compile();
    let table = p.square_table();
    let x = Mat::<f64>::zeros(N, 0);
    let mut out = Mat::<f64>::zeros(N, N);
    let mut scratch = Mat::<f64>::zeros(N, N);
    let got = compiled.eval_gram::<Accurate>(
        GramInputs::<_, SuppliedSpec>::supplied(x.as_ref(), &table),
        out.as_mut(),
        Triangle::Lower,
        scratch.as_mut(),
        &mut Vec::new(),
    );
    assert!(matches!(
        got,
        Err(GprError::UnsupportedKernelOperation { .. })
    ));
}