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
//! What a kernel tree holds besides coordinate leaves: nothing
//! ([`NoSupply`], a coordinate tree) or leaves that read supplied squared
//! distances (a [`super::DistanceKernel`]).

use std::fmt;

use super::compiled::supplied::{ColRange, RectSlots, SquareSlots, SuppliedLeaf};
use super::{CompiledKernel, KernelScalar, SuppliedSpec};

/// Represents the leaves a kernel tree holds besides coordinate leaves.
/// Sealed.
///
/// A [`super::KernelSpec`] and a [`CompiledKernel`] take it as their last
/// type parameter. The default, [`NoSupply`], is a coordinate tree: it has
/// no value, so a coordinate tree cannot hold a leaf that reads supplied
/// distances. A [`super::DistanceKernel`] holds the other kind, which only
/// this crate names.
///
/// # Examples
///
/// ```rust
/// use gprx::kernel::{CompiledKernel, KernelSpec, NoSupply, RbfKernel};
///
/// # fn main() -> Result<(), gprx::GprError> {
/// let spec: KernelSpec<NoSupply> = KernelSpec::from(RbfKernel::new(1.0)?);
/// let compiled: CompiledKernel<f64, NoSupply> = spec.compile();
/// assert_eq!(compiled.num_params(), 1);
/// # Ok(())
/// # }
/// ```
pub trait Supply: sealed::Supply + Clone + fmt::Debug + PartialEq + Send + Sync + 'static {}

/// Represents a coordinate kernel tree: one with no leaf that reads supplied
/// distances. It has no value.
///
/// See the example on [`Supply`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NoSupply {}

impl Supply for NoSupply {}
impl Supply for SuppliedSpec {}

pub(crate) mod sealed {
    use super::{
        ColRange, CompiledKernel, KernelScalar, NoSupply, RectSlots, SquareSlots, SuppliedLeaf,
        SuppliedSpec,
    };
    use std::fmt;

    pub trait Supply: Sized {
        /// The compiled leaf for compute scalar `T`.
        type Compiled<T: KernelScalar>: Clone + fmt::Debug + PartialEq + Send + Sync;

        /// The leaf, as the crate reads it.
        fn spec(leaf: &Self) -> &SuppliedSpec;

        /// The leaf, as the crate writes it.
        fn spec_mut(leaf: &mut Self) -> &mut SuppliedSpec;

        /// The compiled leaf, as the crate reads it.
        fn compiled<T: KernelScalar>(leaf: &Self::Compiled<T>) -> &SuppliedLeaf<T>;

        /// The compiled leaf, as the crate writes it.
        fn compiled_mut<T: KernelScalar>(leaf: &mut Self::Compiled<T>) -> &mut SuppliedLeaf<T>;

        /// Compiles the leaf for `T`, numbering its slot in `order`.
        fn compile<T: KernelScalar>(leaf: &Self) -> Self::Compiled<T>;

        /// The squared distances of one set a tree of this kind reads:
        /// nothing for a coordinate tree, every slot's otherwise.
        type Squares<'a, T: KernelScalar>: Copy;

        /// The squared distances between two sets a tree of this kind
        /// reads: nothing for a coordinate tree, every slot's otherwise.
        type Rects<'a, T: KernelScalar>: Copy;

        /// The supplies of `slots` as this kind reads them.
        fn squares<'a, T: KernelScalar>(slots: &'a dyn SquareSlots<T>) -> Self::Squares<'a, T>;

        /// The supplies of `slots` as this kind reads them.
        fn rects<'a, T: KernelScalar>(slots: &'a dyn RectSlots<T>) -> Self::Rects<'a, T>;

        /// `slots` borrowed for the shorter `'b`: a projection is invariant
        /// in its lifetime, so the views that hold one shorten through here.
        fn shorter_squares<'a: 'b, 'b, T: KernelScalar>(
            slots: Self::Squares<'a, T>,
        ) -> Self::Squares<'b, T>;

        /// [`Self::shorter_squares`] of the rectangular supplies.
        fn shorter_rects<'a: 'b, 'b, T: KernelScalar>(
            slots: Self::Rects<'a, T>,
        ) -> Self::Rects<'b, T>;

        /// A compiled leaf and the square supplies it reads.
        fn square_leaf<'l, 'a, T: KernelScalar>(
            leaf: &'l Self::Compiled<T>,
            slots: Self::Squares<'a, T>,
        ) -> (&'l SuppliedLeaf<T>, &'a dyn SquareSlots<T>);

        /// A compiled leaf and the rectangular supplies it reads.
        fn rect_leaf<'l, 'a, T: KernelScalar>(
            leaf: &'l Self::Compiled<T>,
            slots: Self::Rects<'a, T>,
        ) -> (&'l SuppliedLeaf<T>, &'a dyn RectSlots<T>);

        /// Calls `f` on columns `start..start + len` of `slots`.
        fn with_cols<T: KernelScalar, R>(
            slots: Self::Rects<'_, T>,
            start: usize,
            len: usize,
            f: impl FnOnce(Self::Rects<'_, T>) -> R,
        ) -> R;

        /// The tree as a coordinate tree: itself for [`NoSupply`], `None` for
        /// a tree of the other kind, which reads its leaves one by one.
        fn coordinates<T: KernelScalar>(
            tree: &CompiledKernel<T, Self>,
        ) -> Option<&CompiledKernel<T, NoSupply>>
        where
            Self: super::Supply;

        /// A decoded supplied leaf in this kind of tree; `None` when this
        /// kind holds none.
        fn from_spec(leaf: SuppliedSpec) -> Option<Self>;

        /// What a model of this kind holds only when its kernel reads
        /// supplied distances: `X` for such a tree, nothing (`()`) for a
        /// coordinate tree, so a coordinate model has no such field.
        type Held<X: Clone + fmt::Debug + Send + Sync>: Clone + fmt::Debug + Send + Sync;

        /// `make()` for a tree that reads supplied distances; nothing (and
        /// `make` is not called) for a coordinate tree.
        fn hold<X: Clone + fmt::Debug + Send + Sync>(make: impl FnOnce() -> X) -> Self::Held<X>;

        /// [`Self::hold`] of a value that may fail to be made.
        fn try_hold<X: Clone + fmt::Debug + Send + Sync, E>(
            make: impl FnOnce() -> Result<X, E>,
        ) -> Result<Self::Held<X>, E>;

        /// The held value; `None` for a coordinate tree.
        fn held<X: Clone + fmt::Debug + Send + Sync>(held: &Self::Held<X>) -> Option<&X>;

        /// The held value, to change; `None` for a coordinate tree.
        fn held_mut<X: Clone + fmt::Debug + Send + Sync>(
            held: &mut Self::Held<X>,
        ) -> Option<&mut X>;

        /// The held value, borrowed.
        fn held_ref<X: Clone + fmt::Debug + Send + Sync>(held: &Self::Held<X>) -> Self::Held<&X>;

        /// The held value, leaving its default in its place.
        fn take_held<X: Clone + fmt::Debug + Send + Sync + Default>(
            held: &mut Self::Held<X>,
        ) -> Self::Held<X>;

        /// `f` of the held value.
        fn map_held<X: Clone + fmt::Debug + Send + Sync, Y: Clone + fmt::Debug + Send + Sync>(
            held: Self::Held<X>,
            f: impl FnOnce(X) -> Y,
        ) -> Self::Held<Y>;

        /// `f` of the held value, which may fail.
        fn try_map_held<
            X: Clone + fmt::Debug + Send + Sync,
            Y: Clone + fmt::Debug + Send + Sync,
            E,
        >(
            held: Self::Held<X>,
            f: impl FnOnce(X) -> Result<Y, E>,
        ) -> Result<Self::Held<Y>, E>;

        /// The squares a held store reads, as this kind reads them.
        fn held_squares<'a, T: KernelScalar, X: SquareSlots<T> + fmt::Debug + Sync + 'a>(
            held: Self::Held<&'a X>,
        ) -> Self::Squares<'a, T>;

        /// The rectangles a held store reads, as this kind reads them.
        fn held_rects<'a, T: KernelScalar, X: RectSlots<T> + fmt::Debug + Sync + 'a>(
            held: Self::Held<&'a X>,
        ) -> Self::Rects<'a, T>;
    }

    impl Supply for NoSupply {
        type Compiled<T: KernelScalar> = NoSupply;

        fn spec(leaf: &Self) -> &SuppliedSpec {
            match *leaf {}
        }

        fn spec_mut(leaf: &mut Self) -> &mut SuppliedSpec {
            match *leaf {}
        }

        fn compiled<T: KernelScalar>(leaf: &NoSupply) -> &SuppliedLeaf<T> {
            match *leaf {}
        }

        fn compiled_mut<T: KernelScalar>(leaf: &mut NoSupply) -> &mut SuppliedLeaf<T> {
            match *leaf {}
        }

        fn compile<T: KernelScalar>(leaf: &Self) -> NoSupply {
            match *leaf {}
        }

        type Squares<'a, T: KernelScalar> = ();

        type Rects<'a, T: KernelScalar> = ();

        fn squares<'a, T: KernelScalar>(_slots: &'a dyn SquareSlots<T>) -> Self::Squares<'a, T> {}

        fn rects<'a, T: KernelScalar>(_slots: &'a dyn RectSlots<T>) -> Self::Rects<'a, T> {}

        fn shorter_squares<'a: 'b, 'b, T: KernelScalar>(
            (): Self::Squares<'a, T>,
        ) -> Self::Squares<'b, T> {
        }

        fn shorter_rects<'a: 'b, 'b, T: KernelScalar>(
            (): Self::Rects<'a, T>,
        ) -> Self::Rects<'b, T> {
        }

        fn square_leaf<'l, 'a, T: KernelScalar>(
            leaf: &'l Self::Compiled<T>,
            _slots: Self::Squares<'a, T>,
        ) -> (&'l SuppliedLeaf<T>, &'a dyn SquareSlots<T>) {
            match *leaf {}
        }

        fn rect_leaf<'l, 'a, T: KernelScalar>(
            leaf: &'l Self::Compiled<T>,
            _slots: Self::Rects<'a, T>,
        ) -> (&'l SuppliedLeaf<T>, &'a dyn RectSlots<T>) {
            match *leaf {}
        }

        fn with_cols<T: KernelScalar, R>(
            (): (),
            _start: usize,
            _len: usize,
            f: impl FnOnce(()) -> R,
        ) -> R {
            f(())
        }

        fn coordinates<T: KernelScalar>(
            tree: &CompiledKernel<T, Self>,
        ) -> Option<&CompiledKernel<T, NoSupply>> {
            Some(tree)
        }

        fn from_spec(_leaf: SuppliedSpec) -> Option<Self> {
            None
        }

        type Held<X: Clone + fmt::Debug + Send + Sync> = ();

        fn hold<X: Clone + fmt::Debug + Send + Sync>(_make: impl FnOnce() -> X) {}

        fn try_hold<X: Clone + fmt::Debug + Send + Sync, E>(
            _make: impl FnOnce() -> Result<X, E>,
        ) -> Result<(), E> {
            Ok(())
        }

        fn held<X: Clone + fmt::Debug + Send + Sync>((): &()) -> Option<&X> {
            None
        }

        fn held_mut<X: Clone + fmt::Debug + Send + Sync>((): &mut ()) -> Option<&mut X> {
            None
        }

        fn held_ref<X: Clone + fmt::Debug + Send + Sync>((): &()) {}

        fn take_held<X: Clone + fmt::Debug + Send + Sync + Default>((): &mut ()) {}

        fn map_held<X: Clone + fmt::Debug + Send + Sync, Y: Clone + fmt::Debug + Send + Sync>(
            (): (),
            _f: impl FnOnce(X) -> Y,
        ) {
        }

        fn try_map_held<
            X: Clone + fmt::Debug + Send + Sync,
            Y: Clone + fmt::Debug + Send + Sync,
            E,
        >(
            (): (),
            _f: impl FnOnce(X) -> Result<Y, E>,
        ) -> Result<(), E> {
            Ok(())
        }

        fn held_squares<'a, T: KernelScalar, X: SquareSlots<T> + fmt::Debug + Sync + 'a>((): ()) {}

        fn held_rects<'a, T: KernelScalar, X: RectSlots<T> + fmt::Debug + Sync + 'a>((): ()) {}
    }

    impl Supply for SuppliedSpec {
        type Compiled<T: KernelScalar> = SuppliedLeaf<T>;

        fn spec(leaf: &Self) -> &SuppliedSpec {
            leaf
        }

        fn spec_mut(leaf: &mut Self) -> &mut SuppliedSpec {
            leaf
        }

        fn compiled<T: KernelScalar>(leaf: &SuppliedLeaf<T>) -> &SuppliedLeaf<T> {
            leaf
        }

        fn compiled_mut<T: KernelScalar>(leaf: &mut SuppliedLeaf<T>) -> &mut SuppliedLeaf<T> {
            leaf
        }

        fn compile<T: KernelScalar>(leaf: &Self) -> SuppliedLeaf<T> {
            SuppliedLeaf::compile(leaf)
        }

        type Squares<'a, T: KernelScalar> = &'a dyn SquareSlots<T>;

        type Rects<'a, T: KernelScalar> = &'a dyn RectSlots<T>;

        fn squares<'a, T: KernelScalar>(slots: &'a dyn SquareSlots<T>) -> Self::Squares<'a, T> {
            slots
        }

        fn rects<'a, T: KernelScalar>(slots: &'a dyn RectSlots<T>) -> Self::Rects<'a, T> {
            slots
        }

        fn shorter_squares<'a: 'b, 'b, T: KernelScalar>(
            slots: Self::Squares<'a, T>,
        ) -> Self::Squares<'b, T> {
            slots
        }

        fn shorter_rects<'a: 'b, 'b, T: KernelScalar>(
            slots: Self::Rects<'a, T>,
        ) -> Self::Rects<'b, T> {
            slots
        }

        fn square_leaf<'l, 'a, T: KernelScalar>(
            leaf: &'l Self::Compiled<T>,
            slots: Self::Squares<'a, T>,
        ) -> (&'l SuppliedLeaf<T>, &'a dyn SquareSlots<T>) {
            (leaf, slots)
        }

        fn rect_leaf<'l, 'a, T: KernelScalar>(
            leaf: &'l Self::Compiled<T>,
            slots: Self::Rects<'a, T>,
        ) -> (&'l SuppliedLeaf<T>, &'a dyn RectSlots<T>) {
            (leaf, slots)
        }

        fn with_cols<T: KernelScalar, R>(
            slots: &dyn RectSlots<T>,
            start: usize,
            len: usize,
            f: impl FnOnce(&dyn RectSlots<T>) -> R,
        ) -> R {
            f(&ColRange {
                inner: slots,
                start,
                len,
            })
        }

        fn coordinates<T: KernelScalar>(
            _tree: &CompiledKernel<T, Self>,
        ) -> Option<&CompiledKernel<T, NoSupply>> {
            None
        }

        fn from_spec(leaf: SuppliedSpec) -> Option<Self> {
            Some(leaf)
        }

        type Held<X: Clone + fmt::Debug + Send + Sync> = X;

        fn hold<X: Clone + fmt::Debug + Send + Sync>(make: impl FnOnce() -> X) -> X {
            make()
        }

        fn try_hold<X: Clone + fmt::Debug + Send + Sync, E>(
            make: impl FnOnce() -> Result<X, E>,
        ) -> Result<X, E> {
            make()
        }

        fn held<X: Clone + fmt::Debug + Send + Sync>(held: &X) -> Option<&X> {
            Some(held)
        }

        fn held_mut<X: Clone + fmt::Debug + Send + Sync>(held: &mut X) -> Option<&mut X> {
            Some(held)
        }

        fn held_ref<X: Clone + fmt::Debug + Send + Sync>(held: &X) -> &X {
            held
        }

        fn take_held<X: Clone + fmt::Debug + Send + Sync + Default>(held: &mut X) -> X {
            std::mem::take(held)
        }

        fn map_held<X: Clone + fmt::Debug + Send + Sync, Y: Clone + fmt::Debug + Send + Sync>(
            held: X,
            f: impl FnOnce(X) -> Y,
        ) -> Y {
            f(held)
        }

        fn try_map_held<
            X: Clone + fmt::Debug + Send + Sync,
            Y: Clone + fmt::Debug + Send + Sync,
            E,
        >(
            held: X,
            f: impl FnOnce(X) -> Result<Y, E>,
        ) -> Result<Y, E> {
            f(held)
        }

        fn held_squares<'a, T: KernelScalar, X: SquareSlots<T> + fmt::Debug + Sync + 'a>(
            held: &'a X,
        ) -> &'a dyn SquareSlots<T> {
            held
        }

        fn held_rects<'a, T: KernelScalar, X: RectSlots<T> + fmt::Debug + Sync + 'a>(
            held: &'a X,
        ) -> &'a dyn RectSlots<T> {
            held
        }
    }
}