mech_matrix/
transpose.rs

1use crate::*;
2use mech_core::*;
3
4// Transpose ------------------------------------------------------------------
5
6macro_rules! transpose_op {
7  ($arg:expr, $out:expr) => {
8    unsafe { *$out = (*$arg).transpose(); }
9  };}
10
11#[cfg(feature = "Matrix1")]
12impl_bool_urop!(TransposeM1, Matrix1<T>, Matrix1<T>, transpose_op);
13#[cfg(feature = "Matrix2")]
14impl_bool_urop!(TransposeM2, Matrix2<T>, Matrix2<T>, transpose_op);
15#[cfg(feature = "Matrix3")]
16impl_bool_urop!(TransposeM3, Matrix3<T>, Matrix3<T>, transpose_op);
17#[cfg(feature = "Matrix4")]
18impl_bool_urop!(TransposeM4, Matrix4<T>, Matrix4<T>, transpose_op);
19#[cfg(feature = "Matrix2x3")]
20impl_bool_urop!(TransposeM2x3, Matrix2x3<T>, Matrix3x2<T>, transpose_op);
21#[cfg(feature = "Matrix3x2")]
22impl_bool_urop!(TransposeM3x2, Matrix3x2<T>, Matrix2x3<T>, transpose_op);
23#[cfg(feature = "MatrixD")]
24impl_bool_urop!(TransposeMD, DMatrix<T>, DMatrix<T>, transpose_op);
25#[cfg(feature = "Vector2")]
26impl_bool_urop!(TransposeV2, Vector2<T>, RowVector2<T>, transpose_op);
27#[cfg(feature = "Vector3")]
28impl_bool_urop!(TransposeV3, Vector3<T>, RowVector3<T>, transpose_op);
29#[cfg(feature = "Vector4")]
30impl_bool_urop!(TransposeV4, Vector4<T>, RowVector4<T>, transpose_op); 
31#[cfg(feature = "VectorD")]
32impl_bool_urop!(TransposeVD, DVector<T>, RowDVector<T>, transpose_op);
33#[cfg(feature = "RowVector2")]
34impl_bool_urop!(TransposeR2, RowVector2<T>, Vector2<T>, transpose_op);
35#[cfg(feature = "RowVector3")]
36impl_bool_urop!(TransposeR3, RowVector3<T>, Vector3<T>, transpose_op);
37#[cfg(feature = "RowVector4")]
38impl_bool_urop!(TransposeR4, RowVector4<T>, Vector4<T>, transpose_op); 
39#[cfg(feature = "RowVectorD")]
40impl_bool_urop!(TransposeRD, RowDVector<T>, DVector<T>, transpose_op);
41
42macro_rules! impl_transpose_match_arms {
43  ($arg:expr, $($input_type:ident => $($matrix_kind:ident, $target_type:ident, $default:expr, $value_string:tt),+);+ $(;)?) => {
44    match $arg {
45      $(
46        $(
47          #[cfg(feature = "RowVector4")]
48          Value::$matrix_kind(Matrix::<$target_type>::RowVector4(arg)) => Ok(Box::new(TransposeR4{arg: arg.clone(), out: new_ref(Vector4::from_element($default)) })),
49          #[cfg(feature = "RowVector3")]
50          Value::$matrix_kind(Matrix::<$target_type>::RowVector3(arg)) => Ok(Box::new(TransposeR3{arg: arg.clone(), out: new_ref(Vector3::from_element($default)) })),
51          #[cfg(feature = "RowVector2")]
52          Value::$matrix_kind(Matrix::<$target_type>::RowVector2(arg)) => Ok(Box::new(TransposeR2{arg: arg.clone(), out: new_ref(Vector2::from_element($default)) })),
53          #[cfg(feature = "Vector4")]
54          Value::$matrix_kind(Matrix::<$target_type>::Vector4(arg))    => Ok(Box::new(TransposeV4{arg: arg.clone(), out: new_ref(RowVector4::from_element($default)) })),
55          #[cfg(feature = "Vector3")]
56          Value::$matrix_kind(Matrix::<$target_type>::Vector3(arg))    => Ok(Box::new(TransposeV3{arg: arg.clone(), out: new_ref(RowVector3::from_element($default)) })),
57          #[cfg(feature = "Vector2")]
58          Value::$matrix_kind(Matrix::<$target_type>::Vector2(arg))    => Ok(Box::new(TransposeV2{arg: arg.clone(), out: new_ref(RowVector2::from_element($default)) })),
59          #[cfg(feature = "Matrix4")]
60          Value::$matrix_kind(Matrix::<$target_type>::Matrix4(arg))    => Ok(Box::new(TransposeM4{arg: arg.clone(), out: new_ref(Matrix4::from_element($default))})),
61          #[cfg(feature = "Matrix3")]
62          Value::$matrix_kind(Matrix::<$target_type>::Matrix3(arg))    => Ok(Box::new(TransposeM3{arg: arg.clone(), out: new_ref(Matrix3::from_element($default))})),
63          #[cfg(feature = "Matrix2")]
64          Value::$matrix_kind(Matrix::<$target_type>::Matrix2(arg))    => Ok(Box::new(TransposeM2{arg: arg.clone(), out: new_ref(Matrix2::from_element($default))})),
65          #[cfg(feature = "Matrix1")]
66          Value::$matrix_kind(Matrix::<$target_type>::Matrix1(arg))    => Ok(Box::new(TransposeM1{arg: arg.clone(), out: new_ref(Matrix1::from_element($default))})),
67          #[cfg(feature = "Matrix2x3")]
68          Value::$matrix_kind(Matrix::<$target_type>::Matrix2x3(arg))  => Ok(Box::new(TransposeM2x3{arg: arg.clone(), out: new_ref(Matrix3x2::from_element($default))})),          
69          #[cfg(feature = "Matrix3x2")]
70          Value::$matrix_kind(Matrix::<$target_type>::Matrix3x2(arg))  => Ok(Box::new(TransposeM3x2{arg: arg.clone(), out: new_ref(Matrix2x3::from_element($default))})),          
71          #[cfg(feature = "VectorD")]
72          Value::$matrix_kind(Matrix::<$target_type>::DVector(arg))    => Ok(Box::new(TransposeVD{arg: arg.clone(), out: new_ref(RowDVector::from_element(arg.borrow().len(),$default))})),
73          #[cfg(feature = "RowVectorD")]
74          Value::$matrix_kind(Matrix::<$target_type>::RowDVector(arg)) => Ok(Box::new(TransposeRD{arg: arg.clone(), out: new_ref(DVector::from_element(arg.borrow().len(),$default))})),
75          #[cfg(feature = "MatrixD")]
76          Value::$matrix_kind(Matrix::<$target_type>::DMatrix(arg)) => {
77            let (rows,cols) = {arg.borrow().shape()};
78            Ok(Box::new(TransposeMD{arg, out: new_ref(DMatrix::from_element(rows,cols,$default))}))
79          },
80        )+
81      )+
82      x => Err(MechError{file: file!().to_string(),  tokens: vec![], msg: "".to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind }),
83    }
84  }
85}
86
87fn impl_transpose_fxn(lhs_value: Value) -> Result<Box<dyn MechFunction>, MechError> {
88  impl_transpose_match_arms!(
89    (lhs_value),
90    Bool => MatrixBool, bool, false, "Bool";
91    I8   => MatrixI8,   i8,   i8::zero(), "I8";
92    I16  => MatrixI16,  i16,  i16::zero(), "I16";
93    I32  => MatrixI32,  i32,  i32::zero(), "I32";
94    I64  => MatrixI64,  i64,  i64::zero(), "I64";
95    I128 => MatrixI128, i128, i128::zero(), "I128";
96    U8   => MatrixU8,   u8,   u8::zero(), "U8";
97    U16  => MatrixU16,  u16,  u16::zero(), "U16";
98    U32  => MatrixU32,  u32,  u32::zero(), "U32";
99    U64  => MatrixU64,  u64,  u64::zero(), "U64";
100    U128 => MatrixU128, u128, u128::zero(), "U128";
101    F32  => MatrixF32,  F32,  F32::zero(), "F32";
102    F64  => MatrixF64,  F64,  F64::zero(), "F64";
103  )
104}
105  
106impl_mech_urnop_fxn!(MatrixTranspose,impl_transpose_fxn);