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
use crate::basis::{Composable, Projection, Projector};
use crate::geometry::{discrete::Partition, product::LinearSpace, Card, Space, Surjection, Vector};
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct UniformGrid {
n_features: usize,
feature_space: LinearSpace<Partition>,
}
impl UniformGrid {
pub fn new(feature_space: LinearSpace<Partition>) -> Self {
let n_features = feature_space.card().into();
UniformGrid {
n_features,
feature_space,
}
}
fn hash(&self, input: &[f64]) -> usize {
let mut in_it = input.iter().rev();
let mut d_it = self.feature_space.iter().rev();
let acc = d_it.next().unwrap().map(*in_it.next().unwrap());
d_it.zip(in_it)
.fold(acc, |acc, (d, v)| d.map(*v) + d.density() * acc)
}
}
impl Space for UniformGrid {
type Value = Projection;
fn dim(&self) -> usize { self.n_features }
fn card(&self) -> Card { unimplemented!() }
}
impl Projector<[f64]> for UniformGrid {
fn project(&self, input: &[f64]) -> Projection { vec![self.hash(input)].into() }
}
impl_array_proxies!(UniformGrid; f64);
impl Composable for UniformGrid {}
#[cfg(test)]
mod tests {
use super::*;
use ndarray::arr1;
#[test]
fn test_is_sparse() {
let ds = LinearSpace::new(vec![Partition::new(0.0, 10.0, 10)]);
let t = UniformGrid::new(ds);
let out = t.project(&vec![0.0]);
match out {
Projection::Sparse(_) => assert!(true),
Projection::Dense(_) => assert!(false),
}
}
#[test]
fn test_1d() {
let ds = LinearSpace::new(vec![Partition::new(0.0, 10.0, 10)]);
let t = UniformGrid::new(ds);
assert_eq!(t.dim(), 10);
for i in 0..10 {
let out = t.project(&vec![i as u32 as f64]);
let expected_bin = i;
match out {
Projection::Sparse(ref idx) => {
assert_eq!(idx.len(), 1);
assert!(idx.contains(&expected_bin));
},
_ => assert!(false),
}
let mut dense = arr1(&vec![0.0; 10]);
dense[expected_bin] = 1.0;
assert_eq!(out.expanded(t.dim()), dense);
}
}
#[test]
fn test_2d() {
let ds = LinearSpace::new(vec![Partition::new(0.0, 10.0, 10); 2]);
let t = UniformGrid::new(ds);
assert_eq!(t.dim(), 100);
for i in 0..10 {
for j in 0..10 {
let out = t.project(&vec![i as u32 as f64, j as u32 as f64]);
let expected_bin = j * 10 + i;
match out {
Projection::Sparse(ref idx) => {
assert_eq!(idx.len(), 1);
assert!(idx.contains(&expected_bin));
},
_ => assert!(false),
}
let mut dense = arr1(&vec![0.0; 100]);
dense[expected_bin] = 1.0;
assert_eq!(out.expanded(t.dim()), dense);
}
}
}
#[test]
fn test_3d() {
let ds = LinearSpace::new(vec![Partition::new(0.0, 10.0, 10); 3]);
let t = UniformGrid::new(ds);
assert_eq!(t.dim(), 1000);
for i in 0..10 {
for j in 0..10 {
for k in 0..10 {
let out = t.project(&vec![i as u32 as f64, j as u32 as f64, k as u32 as f64]);
let expected_bin = k * 100 + j * 10 + i;
match out {
Projection::Sparse(ref idx) => {
assert_eq!(idx.len(), 1);
assert!(idx.contains(&expected_bin));
},
_ => assert!(false),
}
let mut dense = arr1(&vec![0.0; 1000]);
dense[expected_bin] = 1.0;
assert_eq!(out.expanded(t.dim()), dense);
}
}
}
}
}