Skip to main content

flow_dimensional_reduction/pca/
state.rs

1//! Typestate markers for [`super::Pca`].
2//!
3//! `Unfitted` and `Fitted` are declared here rather than at `pca` top level to
4//! avoid stuttering inside `Pca<Unfitted>` / `Pca<Fitted>` and to keep them
5//! out of the crate root, where nothing outside this crate needs to name
6//! them — callers rely on type inference from `Pca::new(k).fit(..)`.
7
8use super::PcaComponent;
9
10/// Unfitted state: holds only the requested component count.
11#[derive(Debug, Clone, Copy)]
12pub struct Unfitted {
13    pub(super) n_components: usize,
14}
15
16impl super::sealed::Sealed for Unfitted {}
17
18impl PcaComponent for Unfitted {
19    fn n_components(&self) -> usize {
20        self.n_components
21    }
22}
23
24/// Fitted state: holds the basis produced by [`super::Pca::fit`].
25#[derive(Debug, Clone)]
26pub struct Fitted {
27    pub(super) n_components: usize,
28    /// `k * d` row-major: axis `i` occupies `[i*d .. (i+1)*d]`.
29    pub(super) components: Vec<f32>,
30    pub(super) explained_variance_ratio: Vec<f32>,
31    pub(super) mean: Vec<f32>,
32}
33
34impl super::sealed::Sealed for Fitted {}
35
36impl PcaComponent for Fitted {
37    fn n_components(&self) -> usize {
38        self.n_components
39    }
40}