Calibrate a new [`BbqCodec`] from a set of training vectors. Computes the centroid as the mean of all training vectors. # Panics Does not panic; returns a zero-centroid codec if `vectors` is empty.
(vectors: &[&[f32]], dim: usize, oversample: u8)
| 88 | /// |
| 89 | /// Does not panic; returns a zero-centroid codec if `vectors` is empty. |
| 90 | pub fn calibrate(vectors: &[&[f32]], dim: usize, oversample: u8) -> Self { |
| 91 | let mut centroid = vec![0.0f32; dim]; |
| 92 | if vectors.is_empty() { |
| 93 | return Self { |
| 94 | dim, |
| 95 | centroid, |
| 96 | oversample, |
| 97 | }; |
| 98 | } |
| 99 | for v in vectors { |
| 100 | for (c, &x) in centroid.iter_mut().zip(v.iter()) { |
| 101 | *c += x; |
| 102 | } |
| 103 | } |
| 104 | let n = vectors.len() as f32; |
| 105 | for c in &mut centroid { |
| 106 | *c /= n; |
| 107 | } |
| 108 | Self { |
| 109 | dim, |
| 110 | centroid, |
| 111 | oversample, |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// On-disk magic for BBQ codec envelopes. |
| 116 | pub const ENVELOPE_MAGIC: &'static [u8; codec_envelope::MAGIC_LEN] = b"NDBBQ"; |