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
#![macro_use]

macro_rules! dvec_impl(
    ($dvec: ident) => (
        vecn_dvec_common_impl!($dvec);

        impl<N: Zero + Copy + Clone> $dvec<N> {
            /// Builds a vector filled with zeros.
            ///
            /// # Arguments
            /// * `dim` - The dimension of the vector.
            #[inline]
            pub fn new_zeros(dim: usize) -> $dvec<N> {
                $dvec::from_elem(dim, ::zero())
            }
        }

        impl<N: One + Zero + Copy + Clone> $dvec<N> {
            /// Builds a vector filled with ones.
            ///
            /// # Arguments
            /// * `dim` - The dimension of the vector.
            #[inline]
            pub fn new_ones(dim: usize) -> $dvec<N> {
                $dvec::from_elem(dim, ::one())
            }
        }

        impl<N: Rand + Zero> $dvec<N> {
            /// Builds a vector filled with random values.
            #[inline]
            pub fn new_random(dim: usize) -> $dvec<N> {
                $dvec::from_fn(dim, |_| rand::random())
            }
        }

        impl<N: BaseFloat + ApproxEq<N>> $dvec<N> {
            /// Computes the canonical basis for the given dimension. A canonical basis is a set of
            /// vectors, mutually orthogonal, with all its component equal to 0.0 except one which is equal
            /// to 1.0.
            pub fn canonical_basis_with_dim(dim: usize) -> Vec<$dvec<N>> {
                let mut res : Vec<$dvec<N>> = Vec::new();

                for i in 0 .. dim {
                    let mut basis_element : $dvec<N> = $dvec::new_zeros(dim);

                    basis_element[i] = ::one();

                    res.push(basis_element);
                }

                res
            }

            /// Computes a basis of the space orthogonal to the vector. If the input vector is of dimension
            /// `n`, this will return `n - 1` vectors.
            pub fn orthogonal_subspace_basis(&self) -> Vec<$dvec<N>> {
                // compute the basis of the orthogonal subspace using Gram-Schmidt
                // orthogonalization algorithm
                let     dim                 = self.len();
                let mut res : Vec<$dvec<N>> = Vec::new();

                for i in 0 .. dim {
                    let mut basis_element : $dvec<N> = $dvec::new_zeros(self.len());

                    basis_element[i] = ::one();

                    if res.len() == dim - 1 {
                        break;
                    }

                    let mut elt = basis_element.clone();

                    elt.axpy(&-::dot(&basis_element, self), self);

                    for v in res.iter() {
                        let proj = ::dot(&elt, v);
                        elt.axpy(&-proj, v)
                    };

                    if !ApproxEq::approx_eq(&Norm::sqnorm(&elt), &::zero()) {
                        res.push(Norm::normalize(&elt));
                    }
                }

                assert!(res.len() == dim - 1);

                res
            }
        }
    )
);

macro_rules! small_dvec_impl (
    ($dvec: ident, $dim: expr, $($idx: expr),*) => (
        dvec_impl!($dvec);

        impl<N> $dvec<N> {
            /// The number of elements of this vector.
            #[inline]
            pub fn len(&self) -> usize {
                self.dim
            }

            /// Creates an uninitialized vec of dimension `dim`.
            #[inline]
            pub unsafe fn new_uninitialized(dim: usize) -> $dvec<N> {
                assert!(dim <= $dim, "The chosen dimension is too high for that type of \
                                      stack-allocated dynamic vector. Consider using the \
                                      heap-allocated vector: DVec.");

                $dvec {
                    at:  mem::uninitialized(),
                    dim: dim
                }
            }
        }

        impl<N: PartialEq> PartialEq for $dvec<N> {
            #[inline]
            fn eq(&self, other: &$dvec<N>) -> bool {
                if self.len() != other.len() {
                    return false; // FIXME: fail instead?
                }

                for (a, b) in self.as_ref().iter().zip(other.as_ref().iter()) {
                    if *a != *b {
                        return false;
                    }
                }

                true
            }
        }

        impl<N: Clone> Clone for $dvec<N> {
            fn clone(&self) -> $dvec<N> {
                let at: [N; $dim] = [ $( self.at[$idx].clone(), )* ];

                $dvec {
                    at:  at,
                    dim: self.dim
                }
            }
        }
    )
);

macro_rules! small_dvec_from_impl (
    ($dvec: ident, $dim: expr, $($zeros: expr),*) => (
        impl<N: Copy + Zero> $dvec<N> {
            /// Builds a vector filled with a constant.
            #[inline]
            pub fn from_elem(dim: usize, elem: N) -> $dvec<N> {
                assert!(dim <= $dim);

                let mut at: [N; $dim] = [ $( $zeros, )* ];

                for n in &mut at[.. dim] {
                    *n = elem;
                }

                $dvec {
                    at:  at,
                    dim: dim
                }
            }
        }

        impl<N: Copy + Zero> $dvec<N> {
            /// Builds a vector filled with the components provided by a vector.
            ///
            /// The vector must have at least `dim` elements.
            #[inline]
            pub fn from_slice(dim: usize, vec: &[N]) -> $dvec<N> {
                assert!(dim <= vec.len() && dim <= $dim);

                // FIXME: not safe.
                let mut at: [N; $dim] = [ $( $zeros, )* ];

                for (curr, other) in vec.iter().zip(at.iter_mut()) {
                    *other = *curr;
                }

                $dvec {
                    at:  at,
                    dim: dim
                }
            }
        }

        impl<N: Zero> $dvec<N> {
            /// Builds a vector filled with the result of a function.
            #[inline(always)]
            pub fn from_fn<F: FnMut(usize) -> N>(dim: usize, mut f: F) -> $dvec<N> {
                assert!(dim <= $dim);

                let mut at: [N; $dim] = [ $( $zeros, )* ];

                for i in 0 .. dim {
                    at[i] = f(i);
                }

                $dvec {
                    at:  at,
                    dim: dim
                }
            }
        }

        impl<N: Zero> FromIterator<N> for $dvec<N> {
            #[inline]
            fn from_iter<I: IntoIterator<Item = N>>(param: I) -> $dvec<N> {
                let mut at: [N; $dim] = [ $( $zeros, )* ];

                let mut dim = 0;

                for n in param.into_iter() {
                    if dim == $dim {
                        break;
                    }

                    at[dim] = n;

                    dim = dim + 1;
                }

                $dvec {
                    at:  at,
                    dim: dim
                }
            }
        }

        #[cfg(feature="arbitrary")]
        impl<N: Arbitrary + Zero> Arbitrary for $dvec<N> {
            #[inline]
            fn arbitrary<G: Gen>(g: &mut G) -> $dvec<N> {
                $dvec::from_fn(g.gen_range(0, $dim), |_| Arbitrary::arbitrary(g))
            }
        }
    )
);