| 1 |
#![cfg_attr(rustfmt, rustfmt_skip)] |
| 2 |
|
| 3 |
extern crate nalgebra as na; |
| 4 |
|
| 5 |
use na::{DMatrix, Dyn, Matrix2x3, Matrix3x2, Const}; |
| 6 |
|
| 7 |
fn main() { |
| 8 |
// Matrices can be reshaped in-place without moving or copying values. |
| 9 |
let m1 = Matrix2x3::new( |
| 10 |
1.1, 1.2, 1.3, |
| 11 |
2.1, 2.2, 2.3 |
| 12 |
); |
| 13 |
let m2 = Matrix3x2::new( |
| 14 |
1.1, 2.2, |
| 15 |
2.1, 1.3, |
| 16 |
1.2, 2.3 |
| 17 |
); |
| 18 |
|
| 19 |
let m3 = m1.reshape_generic(Const::<3>, Const::<2>); |
| 20 |
assert_eq!(m3, m2); |
| 21 |
|
| 22 |
// Note that, for statically sized matrices, invalid reshapes will not compile: |
| 23 |
//let m4 = m3.reshape_generic(U3, U3); |
| 24 |
|
| 25 |
// If dynamically sized matrices are used, the reshaping is checked at run-time. |
| 26 |
let dm1 = DMatrix::from_row_slice( |
| 27 |
4, |
| 28 |
3, |
| 29 |
&[ |
| 30 |
1.0, 0.0, 0.0, |
| 31 |
0.0, 0.0, 1.0, |
| 32 |
0.0, 0.0, 0.0, |
| 33 |
0.0, 1.0, 0.0 |
| 34 |
], |
| 35 |
); |
| 36 |
let dm2 = DMatrix::from_row_slice( |
| 37 |
6, |
| 38 |
2, |
| 39 |
&[ |
| 40 |
1.0, 0.0, |
| 41 |
0.0, 1.0, |
| 42 |
0.0, 0.0, |
| 43 |
0.0, 1.0, |
| 44 |
0.0, 0.0, |
| 45 |
0.0, 0.0, |
| 46 |
], |
| 47 |
); |
| 48 |
|
| 49 |
let dm3 = dm1.reshape_generic(Dyn(6), Dyn(2)); |
| 50 |
assert_eq!(dm3, dm2); |
| 51 |
|
| 52 |
// Invalid reshapings of dynamic matrices will panic at run-time. |
| 53 |
//let dm4 = dm3.reshape_generic(Dyn(6), Dyn(6)); |
| 54 |
} |