1 #[cfg(feature = "arbitrary")]
2 use crate::base::storage::Owned;
3 #[cfg(feature = "arbitrary")]
4 use quickcheck::{Arbitrary, Gen};
5 
6 use num::{Bounded, One, Zero};
7 use rand::distributions::{Distribution, Standard};
8 use rand::Rng;
9 #[cfg(feature = "std")]
10 use rand::{self, distributions::StandardNormal};
11 use std::iter;
12 use typenum::{self, Cmp, Greater};
13 
14 #[cfg(feature = "std")]
15 use alga::general::RealField;
16 use alga::general::{ClosedAdd, ClosedMul};
17 
18 use crate::base::allocator::Allocator;
19 use crate::base::dimension::{Dim, DimName, Dynamic, U1, U2, U3, U4, U5, U6};
20 use crate::base::storage::Storage;
21 use crate::base::{DefaultAllocator, Matrix, MatrixMN, MatrixN, Scalar, Unit, Vector, VectorN};
22 
23 /*
24  *
25  * Generic constructors.
26  *
27  */
28 impl<N: Scalar, R: Dim, C: Dim> MatrixMN<N, R, C>
29 where DefaultAllocator: Allocator<N, R, C>
30 {
31     /// Creates a new uninitialized matrix. If the matrix has a compile-time dimension, this panics
32     /// if `nrows != R::to_usize()` or `ncols != C::to_usize()`.
33     #[inline]
new_uninitialized_generic(nrows: R, ncols: C) -> Self34     pub unsafe fn new_uninitialized_generic(nrows: R, ncols: C) -> Self {
35         Self::from_data(DefaultAllocator::allocate_uninitialized(nrows, ncols))
36     }
37 
38     /// Creates a matrix with all its elements set to `elem`.
39     #[inline]
from_element_generic(nrows: R, ncols: C, elem: N) -> Self40     pub fn from_element_generic(nrows: R, ncols: C, elem: N) -> Self {
41         let len = nrows.value() * ncols.value();
42         Self::from_iterator_generic(nrows, ncols, iter::repeat(elem).take(len))
43     }
44 
45     /// Creates a matrix with all its elements set to `elem`.
46     ///
47     /// Same as `from_element_generic`.
48     #[inline]
repeat_generic(nrows: R, ncols: C, elem: N) -> Self49     pub fn repeat_generic(nrows: R, ncols: C, elem: N) -> Self {
50         let len = nrows.value() * ncols.value();
51         Self::from_iterator_generic(nrows, ncols, iter::repeat(elem).take(len))
52     }
53 
54     /// Creates a matrix with all its elements set to 0.
55     #[inline]
zeros_generic(nrows: R, ncols: C) -> Self where N: Zero56     pub fn zeros_generic(nrows: R, ncols: C) -> Self
57     where N: Zero {
58         Self::from_element_generic(nrows, ncols, N::zero())
59     }
60 
61     /// Creates a matrix with all its elements filled by an iterator.
62     #[inline]
from_iterator_generic<I>(nrows: R, ncols: C, iter: I) -> Self where I: IntoIterator<Item = N>63     pub fn from_iterator_generic<I>(nrows: R, ncols: C, iter: I) -> Self
64     where I: IntoIterator<Item = N> {
65         Self::from_data(DefaultAllocator::allocate_from_iterator(nrows, ncols, iter))
66     }
67 
68     /// Creates a matrix with its elements filled with the components provided by a slice in
69     /// row-major order.
70     ///
71     /// The order of elements in the slice must follow the usual mathematic writing, i.e.,
72     /// row-by-row.
73     #[inline]
from_row_slice_generic(nrows: R, ncols: C, slice: &[N]) -> Self74     pub fn from_row_slice_generic(nrows: R, ncols: C, slice: &[N]) -> Self {
75         assert!(
76             slice.len() == nrows.value() * ncols.value(),
77             "Matrix init. error: the slice did not contain the right number of elements."
78         );
79 
80         let mut res = unsafe { Self::new_uninitialized_generic(nrows, ncols) };
81         let mut iter = slice.iter();
82 
83         for i in 0..nrows.value() {
84             for j in 0..ncols.value() {
85                 unsafe { *res.get_unchecked_mut((i, j)) = *iter.next().unwrap() }
86             }
87         }
88 
89         res
90     }
91 
92     /// Creates a matrix with its elements filled with the components provided by a slice. The
93     /// components must have the same layout as the matrix data storage (i.e. column-major).
94     #[inline]
from_column_slice_generic(nrows: R, ncols: C, slice: &[N]) -> Self95     pub fn from_column_slice_generic(nrows: R, ncols: C, slice: &[N]) -> Self {
96         Self::from_iterator_generic(nrows, ncols, slice.iter().cloned())
97     }
98 
99     /// Creates a matrix filled with the results of a function applied to each of its component
100     /// coordinates.
101     #[inline]
from_fn_generic<F>(nrows: R, ncols: C, mut f: F) -> Self where F: FnMut(usize, usize) -> N102     pub fn from_fn_generic<F>(nrows: R, ncols: C, mut f: F) -> Self
103     where F: FnMut(usize, usize) -> N {
104         let mut res = unsafe { Self::new_uninitialized_generic(nrows, ncols) };
105 
106         for j in 0..ncols.value() {
107             for i in 0..nrows.value() {
108                 unsafe { *res.get_unchecked_mut((i, j)) = f(i, j) }
109             }
110         }
111 
112         res
113     }
114 
115     /// Creates a new identity matrix.
116     ///
117     /// If the matrix is not square, the largest square submatrix starting at index `(0, 0)` is set
118     /// to the identity matrix. All other entries are set to zero.
119     #[inline]
identity_generic(nrows: R, ncols: C) -> Self where N: Zero + One120     pub fn identity_generic(nrows: R, ncols: C) -> Self
121     where N: Zero + One {
122         Self::from_diagonal_element_generic(nrows, ncols, N::one())
123     }
124 
125     /// Creates a new matrix with its diagonal filled with copies of `elt`.
126     ///
127     /// If the matrix is not square, the largest square submatrix starting at index `(0, 0)` is set
128     /// to the identity matrix. All other entries are set to zero.
129     #[inline]
from_diagonal_element_generic(nrows: R, ncols: C, elt: N) -> Self where N: Zero + One130     pub fn from_diagonal_element_generic(nrows: R, ncols: C, elt: N) -> Self
131     where N: Zero + One {
132         let mut res = Self::zeros_generic(nrows, ncols);
133 
134         for i in 0..crate::min(nrows.value(), ncols.value()) {
135             unsafe { *res.get_unchecked_mut((i, i)) = elt }
136         }
137 
138         res
139     }
140 
141     /// Creates a new matrix that may be rectangular. The first `elts.len()` diagonal elements are
142     /// filled with the content of `elts`. Others are set to 0.
143     ///
144     /// Panics if `elts.len()` is larger than the minimum among `nrows` and `ncols`.
145     #[inline]
from_partial_diagonal_generic(nrows: R, ncols: C, elts: &[N]) -> Self where N: Zero146     pub fn from_partial_diagonal_generic(nrows: R, ncols: C, elts: &[N]) -> Self
147     where N: Zero {
148         let mut res = Self::zeros_generic(nrows, ncols);
149         assert!(
150             elts.len() <= crate::min(nrows.value(), ncols.value()),
151             "Too many diagonal elements provided."
152         );
153 
154         for (i, elt) in elts.iter().enumerate() {
155             unsafe { *res.get_unchecked_mut((i, i)) = *elt }
156         }
157 
158         res
159     }
160 
161     /// Builds a new matrix from its rows.
162     ///
163     /// Panics if not enough rows are provided (for statically-sized matrices), or if all rows do
164     /// not have the same dimensions.
165     ///
166     /// # Example
167     /// ```
168     /// # use nalgebra::{RowVector3, Matrix3};
169     /// # use std::iter;
170     ///
171     /// let m = Matrix3::from_rows(&[ RowVector3::new(1.0, 2.0, 3.0),  RowVector3::new(4.0, 5.0, 6.0),  RowVector3::new(7.0, 8.0, 9.0) ]);
172     ///
173     /// assert!(m.m11 == 1.0 && m.m12 == 2.0 && m.m13 == 3.0 &&
174     ///         m.m21 == 4.0 && m.m22 == 5.0 && m.m23 == 6.0 &&
175     ///         m.m31 == 7.0 && m.m32 == 8.0 && m.m33 == 9.0);
176     /// ```
177     #[inline]
from_rows<SB>(rows: &[Matrix<N, U1, C, SB>]) -> Self where SB: Storage<N, U1, C>178     pub fn from_rows<SB>(rows: &[Matrix<N, U1, C, SB>]) -> Self
179     where SB: Storage<N, U1, C> {
180         assert!(rows.len() > 0, "At least one row must be given.");
181         let nrows = R::try_to_usize().unwrap_or(rows.len());
182         let ncols = rows[0].len();
183         assert!(
184             rows.len() == nrows,
185             "Invalid number of rows provided to build this matrix."
186         );
187 
188         if C::try_to_usize().is_none() {
189             assert!(
190                 rows.iter().all(|r| r.len() == ncols),
191                 "The provided rows must all have the same dimension."
192             );
193         }
194 
195         // FIXME: optimize that.
196         Self::from_fn_generic(R::from_usize(nrows), C::from_usize(ncols), |i, j| {
197             rows[i][(0, j)]
198         })
199     }
200 
201     /// Builds a new matrix from its columns.
202     ///
203     /// Panics if not enough columns are provided (for statically-sized matrices), or if all
204     /// columns do not have the same dimensions.
205     ///
206     /// # Example
207     /// ```
208     /// # use nalgebra::{Vector3, Matrix3};
209     /// # use std::iter;
210     ///
211     /// let m = Matrix3::from_columns(&[ Vector3::new(1.0, 2.0, 3.0),  Vector3::new(4.0, 5.0, 6.0),  Vector3::new(7.0, 8.0, 9.0) ]);
212     ///
213     /// assert!(m.m11 == 1.0 && m.m12 == 4.0 && m.m13 == 7.0 &&
214     ///         m.m21 == 2.0 && m.m22 == 5.0 && m.m23 == 8.0 &&
215     ///         m.m31 == 3.0 && m.m32 == 6.0 && m.m33 == 9.0);
216     /// ```
217     #[inline]
from_columns<SB>(columns: &[Vector<N, R, SB>]) -> Self where SB: Storage<N, R>218     pub fn from_columns<SB>(columns: &[Vector<N, R, SB>]) -> Self
219     where SB: Storage<N, R> {
220         assert!(columns.len() > 0, "At least one column must be given.");
221         let ncols = C::try_to_usize().unwrap_or(columns.len());
222         let nrows = columns[0].len();
223         assert!(
224             columns.len() == ncols,
225             "Invalid number of columns provided to build this matrix."
226         );
227 
228         if R::try_to_usize().is_none() {
229             assert!(
230                 columns.iter().all(|r| r.len() == nrows),
231                 "The columns provided must all have the same dimension."
232             );
233         }
234 
235         // FIXME: optimize that.
236         Self::from_fn_generic(R::from_usize(nrows), C::from_usize(ncols), |i, j| {
237             columns[j][i]
238         })
239     }
240 
241     /// Creates a matrix filled with random values.
242     #[inline]
243     #[cfg(feature = "std")]
new_random_generic(nrows: R, ncols: C) -> Self where Standard: Distribution<N>244     pub fn new_random_generic(nrows: R, ncols: C) -> Self
245     where Standard: Distribution<N> {
246         Self::from_fn_generic(nrows, ncols, |_, _| rand::random())
247     }
248 
249     /// Creates a matrix filled with random values from the given distribution.
250     #[inline]
from_distribution_generic<Distr: Distribution<N> + ?Sized, G: Rng + ?Sized>( nrows: R, ncols: C, distribution: &Distr, rng: &mut G, ) -> Self251     pub fn from_distribution_generic<Distr: Distribution<N> + ?Sized, G: Rng + ?Sized>(
252         nrows: R,
253         ncols: C,
254         distribution: &Distr,
255         rng: &mut G,
256     ) -> Self
257     {
258         Self::from_fn_generic(nrows, ncols, |_, _| distribution.sample(rng))
259     }
260 
261     /// Creates a matrix backed by a given `Vec`.
262     ///
263     /// The output matrix is filled column-by-column.
264     ///
265     /// # Example
266     /// ```
267     /// # use nalgebra::{Dynamic, DMatrix, Matrix, U1};
268     ///
269     /// let vec = vec![0, 1, 2, 3, 4, 5];
270     /// let vec_ptr = vec.as_ptr();
271     ///
272     /// let matrix = Matrix::from_vec_generic(Dynamic::new(vec.len()), U1, vec);
273     /// let matrix_storage_ptr = matrix.data.as_vec().as_ptr();
274     ///
275     /// // `matrix` is backed by exactly the same `Vec` as it was constructed from.
276     /// assert_eq!(matrix_storage_ptr, vec_ptr);
277     /// ```
278     #[inline]
279     #[cfg(feature = "std")]
from_vec_generic(nrows: R, ncols: C, data: Vec<N>) -> Self280     pub fn from_vec_generic(nrows: R, ncols: C, data: Vec<N>) -> Self {
281         Self::from_iterator_generic(nrows, ncols, data)
282     }
283 }
284 
285 impl<N, D: Dim> MatrixN<N, D>
286 where
287     N: Scalar,
288     DefaultAllocator: Allocator<N, D, D>,
289 {
290     /// Creates a square matrix with its diagonal set to `diag` and all other entries set to 0.
291     ///
292     /// # Example
293     /// ```
294     /// # use nalgebra::{Vector3, DVector, Matrix3, DMatrix};
295     /// # use std::iter;
296     ///
297     /// let m = Matrix3::from_diagonal(&Vector3::new(1.0, 2.0, 3.0));
298     /// // The two additional arguments represent the matrix dimensions.
299     /// let dm = DMatrix::from_diagonal(&DVector::from_row_slice(&[1.0, 2.0, 3.0]));
300     ///
301     /// assert!(m.m11 == 1.0 && m.m12 == 0.0 && m.m13 == 0.0 &&
302     ///         m.m21 == 0.0 && m.m22 == 2.0 && m.m23 == 0.0 &&
303     ///         m.m31 == 0.0 && m.m32 == 0.0 && m.m33 == 3.0);
304     /// assert!(dm[(0, 0)] == 1.0 && dm[(0, 1)] == 0.0 && dm[(0, 2)] == 0.0 &&
305     ///         dm[(1, 0)] == 0.0 && dm[(1, 1)] == 2.0 && dm[(1, 2)] == 0.0 &&
306     ///         dm[(2, 0)] == 0.0 && dm[(2, 1)] == 0.0 && dm[(2, 2)] == 3.0);
307     /// ```
308     #[inline]
from_diagonal<SB: Storage<N, D>>(diag: &Vector<N, D, SB>) -> Self where N: Zero309     pub fn from_diagonal<SB: Storage<N, D>>(diag: &Vector<N, D, SB>) -> Self
310     where N: Zero {
311         let (dim, _) = diag.data.shape();
312         let mut res = Self::zeros_generic(dim, dim);
313 
314         for i in 0..diag.len() {
315             unsafe {
316                 *res.get_unchecked_mut((i, i)) = *diag.vget_unchecked(i);
317             }
318         }
319 
320         res
321     }
322 }
323 
324 /*
325  *
326  * Generate constructors with varying number of arguments, depending on the object type.
327  *
328  */
329 macro_rules! impl_constructors(
330     ($($Dims: ty),*; $(=> $DimIdent: ident: $DimBound: ident),*; $($gargs: expr),*; $($args: ident),*) => {
331         impl<N: Scalar, $($DimIdent: $DimBound, )*> MatrixMN<N $(, $Dims)*>
332             where DefaultAllocator: Allocator<N $(, $Dims)*> {
333 
334             /// Creates a new uninitialized matrix or vector.
335             #[inline]
336             pub unsafe fn new_uninitialized($($args: usize),*) -> Self {
337                 Self::new_uninitialized_generic($($gargs),*)
338             }
339 
340             /// Creates a matrix or vector with all its elements set to `elem`.
341             ///
342             /// # Example
343             /// ```
344             /// # use nalgebra::{Matrix2x3, Vector3, DVector, DMatrix};
345             ///
346             /// let v = Vector3::from_element(2.0);
347             /// // The additional argument represents the vector dimension.
348             /// let dv = DVector::from_element(3, 2.0);
349             /// let m = Matrix2x3::from_element(2.0);
350             /// // The two additional arguments represent the matrix dimensions.
351             /// let dm = DMatrix::from_element(2, 3, 2.0);
352             ///
353             /// assert!(v.x == 2.0 && v.y == 2.0 && v.z == 2.0);
354             /// assert!(dv[0] == 2.0 && dv[1] == 2.0 && dv[2] == 2.0);
355             /// assert!(m.m11 == 2.0 && m.m12 == 2.0 && m.m13 == 2.0 &&
356             ///         m.m21 == 2.0 && m.m22 == 2.0 && m.m23 == 2.0);
357             /// assert!(dm[(0, 0)] == 2.0 && dm[(0, 1)] == 2.0 && dm[(0, 2)] == 2.0 &&
358             ///         dm[(1, 0)] == 2.0 && dm[(1, 1)] == 2.0 && dm[(1, 2)] == 2.0);
359             /// ```
360             #[inline]
361             pub fn from_element($($args: usize,)* elem: N) -> Self {
362                 Self::from_element_generic($($gargs, )* elem)
363             }
364 
365             /// Creates a matrix or vector with all its elements set to `elem`.
366             ///
367             /// Same as `.from_element`.
368             ///
369             /// # Example
370             /// ```
371             /// # use nalgebra::{Matrix2x3, Vector3, DVector, DMatrix};
372             ///
373             /// let v = Vector3::repeat(2.0);
374             /// // The additional argument represents the vector dimension.
375             /// let dv = DVector::repeat(3, 2.0);
376             /// let m = Matrix2x3::repeat(2.0);
377             /// // The two additional arguments represent the matrix dimensions.
378             /// let dm = DMatrix::repeat(2, 3, 2.0);
379             ///
380             /// assert!(v.x == 2.0 && v.y == 2.0 && v.z == 2.0);
381             /// assert!(dv[0] == 2.0 && dv[1] == 2.0 && dv[2] == 2.0);
382             /// assert!(m.m11 == 2.0 && m.m12 == 2.0 && m.m13 == 2.0 &&
383             ///         m.m21 == 2.0 && m.m22 == 2.0 && m.m23 == 2.0);
384             /// assert!(dm[(0, 0)] == 2.0 && dm[(0, 1)] == 2.0 && dm[(0, 2)] == 2.0 &&
385             ///         dm[(1, 0)] == 2.0 && dm[(1, 1)] == 2.0 && dm[(1, 2)] == 2.0);
386             /// ```
387             #[inline]
388             pub fn repeat($($args: usize,)* elem: N) -> Self {
389                 Self::repeat_generic($($gargs, )* elem)
390             }
391 
392             /// Creates a matrix or vector with all its elements set to `0`.
393             ///
394             /// # Example
395             /// ```
396             /// # use nalgebra::{Matrix2x3, Vector3, DVector, DMatrix};
397             ///
398             /// let v = Vector3::<f32>::zeros();
399             /// // The argument represents the vector dimension.
400             /// let dv = DVector::<f32>::zeros(3);
401             /// let m = Matrix2x3::<f32>::zeros();
402             /// // The two arguments represent the matrix dimensions.
403             /// let dm = DMatrix::<f32>::zeros(2, 3);
404             ///
405             /// assert!(v.x == 0.0 && v.y == 0.0 && v.z == 0.0);
406             /// assert!(dv[0] == 0.0 && dv[1] == 0.0 && dv[2] == 0.0);
407             /// assert!(m.m11 == 0.0 && m.m12 == 0.0 && m.m13 == 0.0 &&
408             ///         m.m21 == 0.0 && m.m22 == 0.0 && m.m23 == 0.0);
409             /// assert!(dm[(0, 0)] == 0.0 && dm[(0, 1)] == 0.0 && dm[(0, 2)] == 0.0 &&
410             ///         dm[(1, 0)] == 0.0 && dm[(1, 1)] == 0.0 && dm[(1, 2)] == 0.0);
411             /// ```
412             #[inline]
413             pub fn zeros($($args: usize),*) -> Self
414                 where N: Zero {
415                 Self::zeros_generic($($gargs),*)
416             }
417 
418             /// Creates a matrix or vector with all its elements filled by an iterator.
419             ///
420             /// The output matrix is filled column-by-column.
421             ///
422             /// # Example
423             /// ```
424             /// # use nalgebra::{Matrix2x3, Vector3, DVector, DMatrix};
425             /// # use std::iter;
426             ///
427             /// let v = Vector3::from_iterator((0..3).into_iter());
428             /// // The additional argument represents the vector dimension.
429             /// let dv = DVector::from_iterator(3, (0..3).into_iter());
430             /// let m = Matrix2x3::from_iterator((0..6).into_iter());
431             /// // The two additional arguments represent the matrix dimensions.
432             /// let dm = DMatrix::from_iterator(2, 3, (0..6).into_iter());
433             ///
434             /// assert!(v.x == 0 && v.y == 1 && v.z == 2);
435             /// assert!(dv[0] == 0 && dv[1] == 1 && dv[2] == 2);
436             /// assert!(m.m11 == 0 && m.m12 == 2 && m.m13 == 4 &&
437             ///         m.m21 == 1 && m.m22 == 3 && m.m23 == 5);
438             /// assert!(dm[(0, 0)] == 0 && dm[(0, 1)] == 2 && dm[(0, 2)] == 4 &&
439             ///         dm[(1, 0)] == 1 && dm[(1, 1)] == 3 && dm[(1, 2)] == 5);
440             /// ```
441             #[inline]
442             pub fn from_iterator<I>($($args: usize,)* iter: I) -> Self
443                 where I: IntoIterator<Item = N> {
444                 Self::from_iterator_generic($($gargs, )* iter)
445             }
446 
447             /// Creates a matrix or vector filled with the results of a function applied to each of its
448             /// component coordinates.
449             ///
450             /// # Example
451             /// ```
452             /// # use nalgebra::{Matrix2x3, Vector3, DVector, DMatrix};
453             /// # use std::iter;
454             ///
455             /// let v = Vector3::from_fn(|i, _| i);
456             /// // The additional argument represents the vector dimension.
457             /// let dv = DVector::from_fn(3, |i, _| i);
458             /// let m = Matrix2x3::from_fn(|i, j| i * 3 + j);
459             /// // The two additional arguments represent the matrix dimensions.
460             /// let dm = DMatrix::from_fn(2, 3, |i, j| i * 3 + j);
461             ///
462             /// assert!(v.x == 0 && v.y == 1 && v.z == 2);
463             /// assert!(dv[0] == 0 && dv[1] == 1 && dv[2] == 2);
464             /// assert!(m.m11 == 0 && m.m12 == 1 && m.m13 == 2 &&
465             ///         m.m21 == 3 && m.m22 == 4 && m.m23 == 5);
466             /// assert!(dm[(0, 0)] == 0 && dm[(0, 1)] == 1 && dm[(0, 2)] == 2 &&
467             ///         dm[(1, 0)] == 3 && dm[(1, 1)] == 4 && dm[(1, 2)] == 5);
468             /// ```
469             #[inline]
470             pub fn from_fn<F>($($args: usize,)* f: F) -> Self
471                 where F: FnMut(usize, usize) -> N {
472                 Self::from_fn_generic($($gargs, )* f)
473             }
474 
475             /// Creates an identity matrix. If the matrix is not square, the largest square
476             /// submatrix (starting at the first row and column) is set to the identity while all
477             /// other entries are set to zero.
478             ///
479             /// # Example
480             /// ```
481             /// # use nalgebra::{Matrix2x3, DMatrix};
482             /// # use std::iter;
483             ///
484             /// let m = Matrix2x3::<f32>::identity();
485             /// // The two additional arguments represent the matrix dimensions.
486             /// let dm = DMatrix::<f32>::identity(2, 3);
487             ///
488             /// assert!(m.m11 == 1.0 && m.m12 == 0.0 && m.m13 == 0.0 &&
489             ///         m.m21 == 0.0 && m.m22 == 1.0 && m.m23 == 0.0);
490             /// assert!(dm[(0, 0)] == 1.0 && dm[(0, 1)] == 0.0 && dm[(0, 2)] == 0.0 &&
491             ///         dm[(1, 0)] == 0.0 && dm[(1, 1)] == 1.0 && dm[(1, 2)] == 0.0);
492             /// ```
493             #[inline]
494             pub fn identity($($args: usize,)*) -> Self
495                 where N: Zero + One {
496                 Self::identity_generic($($gargs),* )
497             }
498 
499             /// Creates a matrix filled with its diagonal filled with `elt` and all other
500             /// components set to zero.
501             ///
502             /// # Example
503             /// ```
504             /// # use nalgebra::{Matrix2x3, DMatrix};
505             /// # use std::iter;
506             ///
507             /// let m = Matrix2x3::from_diagonal_element(5.0);
508             /// // The two additional arguments represent the matrix dimensions.
509             /// let dm = DMatrix::from_diagonal_element(2, 3, 5.0);
510             ///
511             /// assert!(m.m11 == 5.0 && m.m12 == 0.0 && m.m13 == 0.0 &&
512             ///         m.m21 == 0.0 && m.m22 == 5.0 && m.m23 == 0.0);
513             /// assert!(dm[(0, 0)] == 5.0 && dm[(0, 1)] == 0.0 && dm[(0, 2)] == 0.0 &&
514             ///         dm[(1, 0)] == 0.0 && dm[(1, 1)] == 5.0 && dm[(1, 2)] == 0.0);
515             /// ```
516             #[inline]
517             pub fn from_diagonal_element($($args: usize,)* elt: N) -> Self
518                 where N: Zero + One {
519                 Self::from_diagonal_element_generic($($gargs, )* elt)
520             }
521 
522             /// Creates a new matrix that may be rectangular. The first `elts.len()` diagonal
523             /// elements are filled with the content of `elts`. Others are set to 0.
524             ///
525             /// Panics if `elts.len()` is larger than the minimum among `nrows` and `ncols`.
526             ///
527             /// # Example
528             /// ```
529             /// # use nalgebra::{Matrix3, DMatrix};
530             /// # use std::iter;
531             ///
532             /// let m = Matrix3::from_partial_diagonal(&[1.0, 2.0]);
533             /// // The two additional arguments represent the matrix dimensions.
534             /// let dm = DMatrix::from_partial_diagonal(3, 3, &[1.0, 2.0]);
535             ///
536             /// assert!(m.m11 == 1.0 && m.m12 == 0.0 && m.m13 == 0.0 &&
537             ///         m.m21 == 0.0 && m.m22 == 2.0 && m.m23 == 0.0 &&
538             ///         m.m31 == 0.0 && m.m32 == 0.0 && m.m33 == 0.0);
539             /// assert!(dm[(0, 0)] == 1.0 && dm[(0, 1)] == 0.0 && dm[(0, 2)] == 0.0 &&
540             ///         dm[(1, 0)] == 0.0 && dm[(1, 1)] == 2.0 && dm[(1, 2)] == 0.0 &&
541             ///         dm[(2, 0)] == 0.0 && dm[(2, 1)] == 0.0 && dm[(2, 2)] == 0.0);
542             /// ```
543             #[inline]
544             pub fn from_partial_diagonal($($args: usize,)* elts: &[N]) -> Self
545                 where N: Zero {
546                 Self::from_partial_diagonal_generic($($gargs, )* elts)
547             }
548 
549             /// Creates a matrix or vector filled with random values from the given distribution.
550             #[inline]
551             pub fn from_distribution<Distr: Distribution<N> + ?Sized, G: Rng + ?Sized>(
552                 $($args: usize,)*
553                 distribution: &Distr,
554                 rng: &mut G,
555             ) -> Self {
556                 Self::from_distribution_generic($($gargs, )* distribution, rng)
557             }
558         }
559 
560         impl<N: Scalar, $($DimIdent: $DimBound, )*> MatrixMN<N $(, $Dims)*>
561             where
562             DefaultAllocator: Allocator<N $(, $Dims)*>,
563             Standard: Distribution<N> {
564 
565             /// Creates a matrix filled with random values.
566             #[inline]
567             #[cfg(feature = "std")]
568             pub fn new_random($($args: usize),*) -> Self {
569                 Self::new_random_generic($($gargs),*)
570             }
571         }
572     }
573 );
574 
575 // FIXME: this is not very pretty. We could find a better call syntax.
576 impl_constructors!(R, C;                         // Arguments for Matrix<N, ..., S>
577                    => R: DimName, => C: DimName; // Type parameters for impl<N, ..., S>
578                    R::name(), C::name();         // Arguments for `_generic` constructors.
579                    ); // Arguments for non-generic constructors.
580 
581 impl_constructors!(R, Dynamic;
582                    => R: DimName;
583                    R::name(), Dynamic::new(ncols);
584                    ncols);
585 
586 impl_constructors!(Dynamic, C;
587                    => C: DimName;
588                    Dynamic::new(nrows), C::name();
589                    nrows);
590 
591 impl_constructors!(Dynamic, Dynamic;
592                    ;
593                    Dynamic::new(nrows), Dynamic::new(ncols);
594                    nrows, ncols);
595 
596 /*
597  *
598  * Constructors that don't necessarily require all dimensions
599  * to be specified whon one dimension is already known.
600  *
601  */
602 macro_rules! impl_constructors_from_data(
603     ($data: ident; $($Dims: ty),*; $(=> $DimIdent: ident: $DimBound: ident),*; $($gargs: expr),*; $($args: ident),*) => {
604         impl<N: Scalar, $($DimIdent: $DimBound, )*> MatrixMN<N $(, $Dims)*>
605         where DefaultAllocator: Allocator<N $(, $Dims)*> {
606             /// Creates a matrix with its elements filled with the components provided by a slice
607             /// in row-major order.
608             ///
609             /// The order of elements in the slice must follow the usual mathematic writing, i.e.,
610             /// row-by-row.
611             ///
612             /// # Example
613             /// ```
614             /// # use nalgebra::{Matrix2x3, Vector3, DVector, DMatrix};
615             /// # use std::iter;
616             ///
617             /// let v = Vector3::from_row_slice(&[0, 1, 2]);
618             /// // The additional argument represents the vector dimension.
619             /// let dv = DVector::from_row_slice(&[0, 1, 2]);
620             /// let m = Matrix2x3::from_row_slice(&[0, 1, 2, 3, 4, 5]);
621             /// // The two additional arguments represent the matrix dimensions.
622             /// let dm = DMatrix::from_row_slice(2, 3, &[0, 1, 2, 3, 4, 5]);
623             ///
624             /// assert!(v.x == 0 && v.y == 1 && v.z == 2);
625             /// assert!(dv[0] == 0 && dv[1] == 1 && dv[2] == 2);
626             /// assert!(m.m11 == 0 && m.m12 == 1 && m.m13 == 2 &&
627             ///         m.m21 == 3 && m.m22 == 4 && m.m23 == 5);
628             /// assert!(dm[(0, 0)] == 0 && dm[(0, 1)] == 1 && dm[(0, 2)] == 2 &&
629             ///         dm[(1, 0)] == 3 && dm[(1, 1)] == 4 && dm[(1, 2)] == 5);
630             /// ```
631             #[inline]
632             pub fn from_row_slice($($args: usize,)* $data: &[N]) -> Self {
633                 Self::from_row_slice_generic($($gargs, )* $data)
634             }
635 
636             /// Creates a matrix with its elements filled with the components provided by a slice
637             /// in column-major order.
638             ///
639             /// # Example
640             /// ```
641             /// # use nalgebra::{Matrix2x3, Vector3, DVector, DMatrix};
642             /// # use std::iter;
643             ///
644             /// let v = Vector3::from_column_slice(&[0, 1, 2]);
645             /// // The additional argument represents the vector dimension.
646             /// let dv = DVector::from_column_slice(&[0, 1, 2]);
647             /// let m = Matrix2x3::from_column_slice(&[0, 1, 2, 3, 4, 5]);
648             /// // The two additional arguments represent the matrix dimensions.
649             /// let dm = DMatrix::from_column_slice(2, 3, &[0, 1, 2, 3, 4, 5]);
650             ///
651             /// assert!(v.x == 0 && v.y == 1 && v.z == 2);
652             /// assert!(dv[0] == 0 && dv[1] == 1 && dv[2] == 2);
653             /// assert!(m.m11 == 0 && m.m12 == 2 && m.m13 == 4 &&
654             ///         m.m21 == 1 && m.m22 == 3 && m.m23 == 5);
655             /// assert!(dm[(0, 0)] == 0 && dm[(0, 1)] == 2 && dm[(0, 2)] == 4 &&
656             ///         dm[(1, 0)] == 1 && dm[(1, 1)] == 3 && dm[(1, 2)] == 5);
657             /// ```
658             #[inline]
659             pub fn from_column_slice($($args: usize,)* $data: &[N]) -> Self {
660                 Self::from_column_slice_generic($($gargs, )* $data)
661             }
662 
663             /// Creates a matrix backed by a given `Vec`.
664             ///
665             /// The output matrix is filled column-by-column.
666             ///
667             /// # Example
668             /// ```
669             /// # use nalgebra::{DMatrix, Matrix2x3};
670             ///
671             /// let m = Matrix2x3::from_vec(vec![0, 1, 2, 3, 4, 5]);
672             ///
673             /// assert!(m.m11 == 0 && m.m12 == 2 && m.m13 == 4 &&
674             ///         m.m21 == 1 && m.m22 == 3 && m.m23 == 5);
675             ///
676             ///
677             /// // The two additional arguments represent the matrix dimensions.
678             /// let dm = DMatrix::from_vec(2, 3, vec![0, 1, 2, 3, 4, 5]);
679             ///
680             /// assert!(dm[(0, 0)] == 0 && dm[(0, 1)] == 2 && dm[(0, 2)] == 4 &&
681             ///         dm[(1, 0)] == 1 && dm[(1, 1)] == 3 && dm[(1, 2)] == 5);
682             /// ```
683             #[inline]
684             #[cfg(feature = "std")]
685             pub fn from_vec($($args: usize,)* $data: Vec<N>) -> Self {
686                 Self::from_vec_generic($($gargs, )* $data)
687             }
688         }
689     }
690 );
691 
692 // FIXME: this is not very pretty. We could find a better call syntax.
693 impl_constructors_from_data!(data; R, C;                  // Arguments for Matrix<N, ..., S>
694                             => R: DimName, => C: DimName; // Type parameters for impl<N, ..., S>
695                             R::name(), C::name();         // Arguments for `_generic` constructors.
696                             ); // Arguments for non-generic constructors.
697 
698 impl_constructors_from_data!(data; R, Dynamic;
699                             => R: DimName;
700                             R::name(), Dynamic::new(data.len() / R::dim());
701                             );
702 
703 impl_constructors_from_data!(data; Dynamic, C;
704                             => C: DimName;
705                             Dynamic::new(data.len() / C::dim()), C::name();
706                             );
707 
708 impl_constructors_from_data!(data; Dynamic, Dynamic;
709                             ;
710                             Dynamic::new(nrows), Dynamic::new(ncols);
711                             nrows, ncols);
712 
713 
714 
715 /*
716  *
717  * Zero, One, Rand traits.
718  *
719  */
720 impl<N, R: DimName, C: DimName> Zero for MatrixMN<N, R, C>
721 where
722     N: Scalar + Zero + ClosedAdd,
723     DefaultAllocator: Allocator<N, R, C>,
724 {
725     #[inline]
zero() -> Self726     fn zero() -> Self {
727         Self::from_element(N::zero())
728     }
729 
730     #[inline]
is_zero(&self) -> bool731     fn is_zero(&self) -> bool {
732         self.iter().all(|e| e.is_zero())
733     }
734 }
735 
736 impl<N, D: DimName> One for MatrixN<N, D>
737 where
738     N: Scalar + Zero + One + ClosedMul + ClosedAdd,
739     DefaultAllocator: Allocator<N, D, D>,
740 {
741     #[inline]
one() -> Self742     fn one() -> Self {
743         Self::identity()
744     }
745 }
746 
747 impl<N, R: DimName, C: DimName> Bounded for MatrixMN<N, R, C>
748 where
749     N: Scalar + Bounded,
750     DefaultAllocator: Allocator<N, R, C>,
751 {
752     #[inline]
max_value() -> Self753     fn max_value() -> Self {
754         Self::from_element(N::max_value())
755     }
756 
757     #[inline]
min_value() -> Self758     fn min_value() -> Self {
759         Self::from_element(N::min_value())
760     }
761 }
762 
763 impl<N: Scalar, R: Dim, C: Dim> Distribution<MatrixMN<N, R, C>> for Standard
764 where
765     DefaultAllocator: Allocator<N, R, C>,
766     Standard: Distribution<N>,
767 {
768     #[inline]
sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> MatrixMN<N, R, C>769     fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> MatrixMN<N, R, C> {
770         let nrows = R::try_to_usize().unwrap_or(rng.gen_range(0, 10));
771         let ncols = C::try_to_usize().unwrap_or(rng.gen_range(0, 10));
772 
773         MatrixMN::from_fn_generic(R::from_usize(nrows), C::from_usize(ncols), |_, _| rng.gen())
774     }
775 }
776 
777 #[cfg(feature = "arbitrary")]
778 impl<N, R, C> Arbitrary for MatrixMN<N, R, C>
779 where
780     R: Dim,
781     C: Dim,
782     N: Scalar + Arbitrary + Send,
783     DefaultAllocator: Allocator<N, R, C>,
784     Owned<N, R, C>: Clone + Send,
785 {
786     #[inline]
arbitrary<G: Gen>(g: &mut G) -> Self787     fn arbitrary<G: Gen>(g: &mut G) -> Self {
788         let nrows = R::try_to_usize().unwrap_or(g.gen_range(0, 10));
789         let ncols = C::try_to_usize().unwrap_or(g.gen_range(0, 10));
790 
791         Self::from_fn_generic(R::from_usize(nrows), C::from_usize(ncols), |_, _| {
792             N::arbitrary(g)
793         })
794     }
795 }
796 
797 #[cfg(feature = "std")]
798 impl<N: RealField, D: DimName> Distribution<Unit<VectorN<N, D>>> for Standard
799 where
800     DefaultAllocator: Allocator<N, D>,
801     StandardNormal: Distribution<N>,
802 {
803     /// Generate a uniformly distributed random unit vector.
804     #[inline]
sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> Unit<VectorN<N, D>>805     fn sample<'a, G: Rng + ?Sized>(&self, rng: &'a mut G) -> Unit<VectorN<N, D>> {
806         Unit::new_normalize(VectorN::from_distribution_generic(
807             D::name(),
808             U1,
809             &StandardNormal,
810             rng,
811         ))
812     }
813 }
814 
815 /*
816  *
817  * Constructors for small matrices and vectors.
818  *
819  */
820 macro_rules! componentwise_constructors_impl(
821     ($($R: ty, $C: ty, $($args: ident:($irow: expr,$icol: expr)),*);* $(;)*) => {$(
822         impl<N> MatrixMN<N, $R, $C>
823             where N: Scalar,
824                   DefaultAllocator: Allocator<N, $R, $C> {
825             /// Initializes this matrix from its components.
826             #[inline]
827             pub fn new($($args: N),*) -> Self {
828                 unsafe {
829                     let mut res = Self::new_uninitialized();
830                     $( *res.get_unchecked_mut(($irow, $icol)) = $args; )*
831 
832                     res
833                 }
834             }
835         }
836     )*}
837 );
838 
839 componentwise_constructors_impl!(
840     /*
841      * Square matrices 1 .. 6.
842      */
843     U2, U2, m11:(0,0), m12:(0,1),
844             m21:(1,0), m22:(1,1);
845     U3, U3, m11:(0,0), m12:(0,1), m13:(0,2),
846             m21:(1,0), m22:(1,1), m23:(1,2),
847             m31:(2,0), m32:(2,1), m33:(2,2);
848     U4, U4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
849             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3),
850             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3),
851             m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3);
852     U5, U5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
853             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4),
854             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4),
855             m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4),
856             m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4);
857     U6, U6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
858             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5),
859             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5),
860             m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), m46:(3,5),
861             m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4), m56:(4,5),
862             m61:(5,0), m62:(5,1), m63:(5,2), m64:(5,3), m65:(5,4), m66:(5,5);
863 
864     /*
865      * Rectangular matrices with 2 rows.
866      */
867     U2, U3, m11:(0,0), m12:(0,1), m13:(0,2),
868             m21:(1,0), m22:(1,1), m23:(1,2);
869     U2, U4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
870             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3);
871     U2, U5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
872             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4);
873     U2, U6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
874             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5);
875 
876     /*
877      * Rectangular matrices with 3 rows.
878      */
879     U3, U2, m11:(0,0), m12:(0,1),
880             m21:(1,0), m22:(1,1),
881             m31:(2,0), m32:(2,1);
882     U3, U4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
883             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3),
884             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3);
885     U3, U5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
886             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4),
887             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4);
888     U3, U6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
889             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5),
890             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5);
891 
892     /*
893      * Rectangular matrices with 4 rows.
894      */
895     U4, U2, m11:(0,0), m12:(0,1),
896             m21:(1,0), m22:(1,1),
897             m31:(2,0), m32:(2,1),
898             m41:(3,0), m42:(3,1);
899     U4, U3, m11:(0,0), m12:(0,1), m13:(0,2),
900             m21:(1,0), m22:(1,1), m23:(1,2),
901             m31:(2,0), m32:(2,1), m33:(2,2),
902             m41:(3,0), m42:(3,1), m43:(3,2);
903     U4, U5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
904             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4),
905             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4),
906             m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4);
907     U4, U6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
908             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5),
909             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5),
910             m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), m46:(3,5);
911 
912     /*
913      * Rectangular matrices with 5 rows.
914      */
915     U5, U2, m11:(0,0), m12:(0,1),
916             m21:(1,0), m22:(1,1),
917             m31:(2,0), m32:(2,1),
918             m41:(3,0), m42:(3,1),
919             m51:(4,0), m52:(4,1);
920     U5, U3, m11:(0,0), m12:(0,1), m13:(0,2),
921             m21:(1,0), m22:(1,1), m23:(1,2),
922             m31:(2,0), m32:(2,1), m33:(2,2),
923             m41:(3,0), m42:(3,1), m43:(3,2),
924             m51:(4,0), m52:(4,1), m53:(4,2);
925     U5, U4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
926             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3),
927             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3),
928             m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3),
929             m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3);
930     U5, U6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
931             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5),
932             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5),
933             m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), m46:(3,5),
934             m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4), m56:(4,5);
935 
936     /*
937      * Rectangular matrices with 6 rows.
938      */
939     U6, U2, m11:(0,0), m12:(0,1),
940             m21:(1,0), m22:(1,1),
941             m31:(2,0), m32:(2,1),
942             m41:(3,0), m42:(3,1),
943             m51:(4,0), m52:(4,1),
944             m61:(5,0), m62:(5,1);
945     U6, U3, m11:(0,0), m12:(0,1), m13:(0,2),
946             m21:(1,0), m22:(1,1), m23:(1,2),
947             m31:(2,0), m32:(2,1), m33:(2,2),
948             m41:(3,0), m42:(3,1), m43:(3,2),
949             m51:(4,0), m52:(4,1), m53:(4,2),
950             m61:(5,0), m62:(5,1), m63:(5,2);
951     U6, U4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
952             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3),
953             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3),
954             m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3),
955             m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3),
956             m61:(5,0), m62:(5,1), m63:(5,2), m64:(5,3);
957     U6, U5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
958             m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4),
959             m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4),
960             m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4),
961             m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4),
962             m61:(5,0), m62:(5,1), m63:(5,2), m64:(5,3), m65:(5,4);
963 
964     /*
965      * Row vectors 1 .. 6.
966      */
967     U1, U1, x:(0,0);
968     U1, U2, x:(0,0), y:(0,1);
969     U1, U3, x:(0,0), y:(0,1), z:(0,2);
970     U1, U4, x:(0,0), y:(0,1), z:(0,2), w:(0,3);
971     U1, U5, x:(0,0), y:(0,1), z:(0,2), w:(0,3), a:(0,4);
972     U1, U6, x:(0,0), y:(0,1), z:(0,2), w:(0,3), a:(0,4), b:(0,5);
973 
974     /*
975      * Column vectors 1 .. 6.
976      */
977     U2, U1, x:(0,0), y:(1,0);
978     U3, U1, x:(0,0), y:(1,0), z:(2,0);
979     U4, U1, x:(0,0), y:(1,0), z:(2,0), w:(3,0);
980     U5, U1, x:(0,0), y:(1,0), z:(2,0), w:(3,0), a:(4,0);
981     U6, U1, x:(0,0), y:(1,0), z:(2,0), w:(3,0), a:(4,0), b:(5,0);
982 );
983 
984 /*
985  *
986  * Axis constructors.
987  *
988  */
989 impl<N, R: DimName> VectorN<N, R>
990 where
991     N: Scalar + Zero + One,
992     DefaultAllocator: Allocator<N, R>,
993 {
994     /// The column vector with a 1 as its first component, and zero elsewhere.
995     #[inline]
x() -> Self where R::Value: Cmp<typenum::U0, Output = Greater>996     pub fn x() -> Self
997     where R::Value: Cmp<typenum::U0, Output = Greater> {
998         let mut res = Self::zeros();
999         unsafe {
1000             *res.vget_unchecked_mut(0) = N::one();
1001         }
1002 
1003         res
1004     }
1005 
1006     /// The column vector with a 1 as its second component, and zero elsewhere.
1007     #[inline]
y() -> Self where R::Value: Cmp<typenum::U1, Output = Greater>1008     pub fn y() -> Self
1009     where R::Value: Cmp<typenum::U1, Output = Greater> {
1010         let mut res = Self::zeros();
1011         unsafe {
1012             *res.vget_unchecked_mut(1) = N::one();
1013         }
1014 
1015         res
1016     }
1017 
1018     /// The column vector with a 1 as its third component, and zero elsewhere.
1019     #[inline]
z() -> Self where R::Value: Cmp<typenum::U2, Output = Greater>1020     pub fn z() -> Self
1021     where R::Value: Cmp<typenum::U2, Output = Greater> {
1022         let mut res = Self::zeros();
1023         unsafe {
1024             *res.vget_unchecked_mut(2) = N::one();
1025         }
1026 
1027         res
1028     }
1029 
1030     /// The column vector with a 1 as its fourth component, and zero elsewhere.
1031     #[inline]
w() -> Self where R::Value: Cmp<typenum::U3, Output = Greater>1032     pub fn w() -> Self
1033     where R::Value: Cmp<typenum::U3, Output = Greater> {
1034         let mut res = Self::zeros();
1035         unsafe {
1036             *res.vget_unchecked_mut(3) = N::one();
1037         }
1038 
1039         res
1040     }
1041 
1042     /// The column vector with a 1 as its fifth component, and zero elsewhere.
1043     #[inline]
a() -> Self where R::Value: Cmp<typenum::U4, Output = Greater>1044     pub fn a() -> Self
1045     where R::Value: Cmp<typenum::U4, Output = Greater> {
1046         let mut res = Self::zeros();
1047         unsafe {
1048             *res.vget_unchecked_mut(4) = N::one();
1049         }
1050 
1051         res
1052     }
1053 
1054     /// The column vector with a 1 as its sixth component, and zero elsewhere.
1055     #[inline]
b() -> Self where R::Value: Cmp<typenum::U5, Output = Greater>1056     pub fn b() -> Self
1057     where R::Value: Cmp<typenum::U5, Output = Greater> {
1058         let mut res = Self::zeros();
1059         unsafe {
1060             *res.vget_unchecked_mut(5) = N::one();
1061         }
1062 
1063         res
1064     }
1065 
1066     /// The unit column vector with a 1 as its first component, and zero elsewhere.
1067     #[inline]
x_axis() -> Unit<Self> where R::Value: Cmp<typenum::U0, Output = Greater>1068     pub fn x_axis() -> Unit<Self>
1069     where R::Value: Cmp<typenum::U0, Output = Greater> {
1070         Unit::new_unchecked(Self::x())
1071     }
1072 
1073     /// The unit column vector with a 1 as its second component, and zero elsewhere.
1074     #[inline]
y_axis() -> Unit<Self> where R::Value: Cmp<typenum::U1, Output = Greater>1075     pub fn y_axis() -> Unit<Self>
1076     where R::Value: Cmp<typenum::U1, Output = Greater> {
1077         Unit::new_unchecked(Self::y())
1078     }
1079 
1080     /// The unit column vector with a 1 as its third component, and zero elsewhere.
1081     #[inline]
z_axis() -> Unit<Self> where R::Value: Cmp<typenum::U2, Output = Greater>1082     pub fn z_axis() -> Unit<Self>
1083     where R::Value: Cmp<typenum::U2, Output = Greater> {
1084         Unit::new_unchecked(Self::z())
1085     }
1086 
1087     /// The unit column vector with a 1 as its fourth component, and zero elsewhere.
1088     #[inline]
w_axis() -> Unit<Self> where R::Value: Cmp<typenum::U3, Output = Greater>1089     pub fn w_axis() -> Unit<Self>
1090     where R::Value: Cmp<typenum::U3, Output = Greater> {
1091         Unit::new_unchecked(Self::w())
1092     }
1093 
1094     /// The unit column vector with a 1 as its fifth component, and zero elsewhere.
1095     #[inline]
a_axis() -> Unit<Self> where R::Value: Cmp<typenum::U4, Output = Greater>1096     pub fn a_axis() -> Unit<Self>
1097     where R::Value: Cmp<typenum::U4, Output = Greater> {
1098         Unit::new_unchecked(Self::a())
1099     }
1100 
1101     /// The unit column vector with a 1 as its sixth component, and zero elsewhere.
1102     #[inline]
b_axis() -> Unit<Self> where R::Value: Cmp<typenum::U5, Output = Greater>1103     pub fn b_axis() -> Unit<Self>
1104     where R::Value: Cmp<typenum::U5, Output = Greater> {
1105         Unit::new_unchecked(Self::b())
1106     }
1107 }
1108