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(
    ($dvector: ident) => (
        vecn_dvec_common_impl!($dvector);

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

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

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

        impl<N: BaseFloat + ApproxEq<N>> $dvector<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_dimension(dimension: usize) -> Vec<$dvector<N>> {
                let mut res : Vec<$dvector<N>> = Vec::new();

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

                    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<$dvector<N>> {
                // compute the basis of the orthogonal subspace using Gram-Schmidt
                // orthogonalization algorithm
                let     dimension                 = self.len();
                let mut res : Vec<$dvector<N>> = Vec::new();

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

                    basis_element[i] = ::one();

                    if res.len() == dimension - 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::norm_squared(&elt), &::zero()) {
                        res.push(Norm::normalize(&elt));
                    }
                }

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

                res
            }
        }
    )
);

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

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

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

                $dvector {
                    at:  mem::uninitialized(),
                    dimension: dimension
                }
            }
        }

        impl<N: PartialEq> PartialEq for $dvector<N> {
            #[inline]
            fn eq(&self, other: &$dvector<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 $dvector<N> {
            fn clone(&self) -> $dvector<N> {
                let at: [N; $dimension] = [ $( self.at[$idx].clone(), )* ];

                $dvector {
                    at:  at,
                    dimension: self.dimension
                }
            }
        }
    )
);

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

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

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

                $dvector {
                    at:  at,
                    dimension: dimension
                }
            }
        }

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

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

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

                $dvector {
                    at:  at,
                    dimension: dimension
                }
            }
        }

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

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

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

                $dvector {
                    at:  at,
                    dimension: dimension
                }
            }
        }

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

                let mut dimension = 0;

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

                    at[dimension] = n;

                    dimension = dimension + 1;
                }

                $dvector {
                    at:  at,
                    dimension: dimension
                }
            }
        }

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