fetish_lib/
function_space_info.rs

1extern crate ndarray;
2extern crate ndarray_linalg;
3
4use ndarray::*;
5
6use crate::feature_space_info::*;
7use crate::data_points::*;
8use crate::schmear::*;
9use crate::func_schmear::*;
10use crate::data_point::*;
11
12///Represents information that's available about a function type `A -> B`
13///in terms of the [`FeatureSpaceInfo`] for the input and output types.
14///Here, it is important to bear in mind that the flow of data through
15///the application of a typical [`crate::term_model::TermModel`] is:
16///`input -(input sketcher)-> compressed input -(input feature mapping)->
17/// input features -(model matrix)-> compressed output`.
18#[derive(Clone)]
19pub struct FunctionSpaceInfo<'a> {
20    pub in_feat_info : &'a FeatureSpaceInfo,
21    pub out_feat_info : &'a FeatureSpaceInfo
22}
23
24impl <'a> FunctionSpaceInfo<'a> {
25    ///Gets the number of dimensions for the input feature space.
26    pub fn get_feature_dimensions(&self) -> usize {
27        self.in_feat_info.feature_dimensions
28    }
29    ///Gets the output dimensionality of mappings defined by matrices
30    ///relative to this [`FunctionSpaceInfo`], so the dimension of the compressed
31    ///output space.
32    pub fn get_output_dimensions(&self) -> usize {
33        self.out_feat_info.get_sketched_dimensions()
34    }
35    ///Gets the total number of dimensions required to define a model
36    ///matrix for a function within this [`FunctionSpaceInfo`].
37    pub fn get_full_dimensions(&self) -> usize {
38        self.get_feature_dimensions() * self.get_output_dimensions()
39    }
40
41    ///Gets the Jacobian for the composite mapping
42    ///`compressed input -(input feature mapping)-> input features -(mat)-> compressed output`
43    ///evaluated at the given compressed input vector.
44    pub fn jacobian(&self, mat : ArrayView2<f32>, input : ArrayView1<f32>) -> Array2<f32> {
45        let feat_jacobian = self.in_feat_info.get_feature_jacobian(input);
46        let result = mat.dot(&feat_jacobian);
47        result
48    }
49    ///Given a model matrix for a function with this [`FunctionSpaceInfo`] and a compressed
50    ///input vector, computes the compressed vector output which results from
51    ///applying the function to the argument.
52    pub fn apply(&self, mat : ArrayView2<f32>, input : ArrayView1<f32>) -> Array1<f32> {
53        let features = self.in_feat_info.get_features(input);
54        let result = mat.dot(&features);
55        result
56    }
57    ///Given a [`DataPoints`] whose input/output pairs are both in the input/output compressed
58    ///spaces, yields a new [`DataPoints`] whose inputs have been featurized.
59    pub fn get_data_points(&self, in_data_points : DataPoints) -> DataPoints {
60        let feat_vecs = self.in_feat_info.get_features_mat(in_data_points.in_vecs.view());
61        DataPoints {
62            in_vecs : feat_vecs,
63            ..in_data_points
64        }
65    }
66    ///Given a [`DataPoint`] whose input/output pair are both in the input/output compressed
67    ///spaces, yields a new [`DataPoint`] whose input has been featurized.
68    pub fn get_data(&self, in_data : DataPoint) -> DataPoint {
69        let feat_vec = self.in_feat_info.get_features(in_data.in_vec.view());
70
71        DataPoint {
72            in_vec : feat_vec,
73            ..in_data
74        }
75    }
76    ///Given a model [`FuncSchmear`] for this [`FunctionSpaceInfo`], and a
77    ///[`Schmear`] over compressed inputs, yields an estimated [`Schmear`]
78    ///over the result of applying drawn models to drawn inputs.
79    pub fn apply_schmears(&self, f : &FuncSchmear, x : &Schmear) -> Schmear {
80        let feat_schmear = self.in_feat_info.featurize_schmear(x);
81        let result = f.apply(&feat_schmear);
82        result
83    }
84}