| 1 |
extern crate nalgebra as na; |
| 2 |
|
| 3 |
use na::{DMatrix, Matrix2x3, RowVector3, Vector2}; |
| 4 |
|
| 5 |
fn main() { |
| 6 |
// All the following matrices are equal but constructed in different ways. |
| 7 |
let m = Matrix2x3::new(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); |
| 8 |
|
| 9 |
let m1 = Matrix2x3::from_rows(&[ |
| 10 |
RowVector3::new(1.1, 1.2, 1.3), |
| 11 |
RowVector3::new(2.1, 2.2, 2.3), |
| 12 |
]); |
| 13 |
|
| 14 |
let m2 = Matrix2x3::from_columns(&[ |
| 15 |
Vector2::new(1.1, 2.1), |
| 16 |
Vector2::new(1.2, 2.2), |
| 17 |
Vector2::new(1.3, 2.3), |
| 18 |
]); |
| 19 |
|
| 20 |
let m3 = Matrix2x3::from_row_slice(&[1.1, 1.2, 1.3, 2.1, 2.2, 2.3]); |
| 21 |
|
| 22 |
let m4 = Matrix2x3::from_column_slice(&[1.1, 2.1, 1.2, 2.2, 1.3, 2.3]); |
| 23 |
|
| 24 |
let m5 = Matrix2x3::from_fn(|r, c| (r + 1) as f32 + (c + 1) as f32 / 10.0); |
| 25 |
|
| 26 |
let m6 = Matrix2x3::from_iterator([1.1f32, 2.1, 1.2, 2.2, 1.3, 2.3].iter().cloned()); |
| 27 |
|
| 28 |
assert_eq!(m, m1); |
| 29 |
assert_eq!(m, m2); |
| 30 |
assert_eq!(m, m3); |
| 31 |
assert_eq!(m, m4); |
| 32 |
assert_eq!(m, m5); |
| 33 |
assert_eq!(m, m6); |
| 34 |
|
| 35 |
// All the following matrices are equal but constructed in different ways. |
| 36 |
// This time, we used a dynamically-sized matrix to show the extra arguments |
| 37 |
// for the matrix shape. |
| 38 |
let dm = DMatrix::from_row_slice( |
| 39 |
4, |
| 40 |
3, |
| 41 |
&[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0], |
| 42 |
); |
| 43 |
|
| 44 |
let dm1 = DMatrix::from_diagonal_element(4, 3, 1.0); |
| 45 |
let dm2 = DMatrix::identity(4, 3); |
| 46 |
let dm3 = DMatrix::from_fn(4, 3, |r, c| if r == c { 1.0 } else { 0.0 }); |
| 47 |
let dm4 = DMatrix::from_iterator( |
| 48 |
4, |
| 49 |
3, |
| 50 |
[ |
| 51 |
// Components listed column-by-column. |
| 52 |
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, |
| 53 |
] |
| 54 |
.iter() |
| 55 |
.cloned(), |
| 56 |
); |
| 57 |
|
| 58 |
assert_eq!(dm, dm1); |
| 59 |
assert_eq!(dm, dm2); |
| 60 |
assert_eq!(dm, dm3); |
| 61 |
assert_eq!(dm, dm4); |
| 62 |
} |