mech_math/
lib.rs

1#![no_main]
2#![allow(warnings)]
3
4#[cfg(feature = "matrix")]
5extern crate nalgebra as na;
6
7use mech_core::*;
8
9use paste::paste;
10
11#[cfg(feature = "vector2")]
12use na::Vector2;
13#[cfg(feature = "vector3")]
14use na::Vector3;
15#[cfg(feature = "vector4")]
16use na::Vector4;
17#[cfg(feature = "vectord")]
18use na::DVector;
19#[cfg(feature = "matrix1")]
20use na::Matrix1;
21#[cfg(feature = "matrix2")]
22use na::Matrix2;
23#[cfg(feature = "matrix3")]
24use na::Matrix3;
25#[cfg(feature = "matrix4")]
26use na::Matrix4;
27#[cfg(feature = "matrix2x3")]
28use na::Matrix2x3;
29#[cfg(feature = "matrix3x2")]
30use na::Matrix3x2;
31#[cfg(feature = "matrixd")]
32use na::DMatrix;
33#[cfg(feature = "row_vector2")]
34use na::RowVector2;
35#[cfg(feature = "row_vector3")]
36use na::RowVector3;
37#[cfg(feature = "row_vector4")]
38use na::RowVector4;
39#[cfg(feature = "row_vectord")]
40use na::RowDVector;
41
42use std::ops::*;
43use std::fmt::{Display, Debug};
44use std::marker::PhantomData;
45
46#[cfg(feature = "trig")]
47pub mod trig;
48#[cfg(feature = "ops")]
49pub mod ops;
50#[cfg(feature = "op_assign")]
51pub mod op_assign;
52
53#[cfg(feature = "trig")]
54pub use self::trig::*;
55#[cfg(feature = "ops")]
56pub use self::ops::*;
57#[cfg(feature = "op_assign")]
58pub use self::op_assign::*;
59
60// ----------------------------------------------------------------------------
61// Math Library
62// ----------------------------------------------------------------------------
63
64#[macro_export]
65macro_rules! impl_math_fxns {
66  ($lib:ident) => {
67    impl_fxns!($lib,T,T,impl_binop);
68  }}
69
70#[macro_export]
71macro_rules! impl_urnop_match_arms2 {
72  ($lib:ident, $arg:expr, $($lhs_type:ident => $($matrix_kind:ident, $target_type:ident, $default:expr, $value_string:tt),+);+ $(;)?) => {
73    paste!{
74      match $arg {
75        $(
76          $(
77            #[cfg(feature = $value_string)]
78            (Value::$lhs_type(arg)) => Ok(Box::new([<$lib $lhs_type S>]{arg: arg.clone(), out: Ref::new($default) })),
79            #[cfg(all(feature = $value_string, feature = "matrix1"))]
80            (Value::$matrix_kind(Matrix::Matrix1(arg))) => Ok(Box::new([<$lib $lhs_type M1>]{arg, out: Ref::new(Matrix1::from_element($default))})),
81            #[cfg(all(feature = $value_string, feature = "matrix2"))]
82            (Value::$matrix_kind(Matrix::Matrix2(arg))) => Ok(Box::new([<$lib $lhs_type M2>]{arg, out: Ref::new(Matrix2::from_element($default))})),
83            #[cfg(all(feature = $value_string, feature = "matrix3"))]
84            (Value::$matrix_kind(Matrix::Matrix3(arg))) => Ok(Box::new([<$lib $lhs_type M3>]{arg, out: Ref::new(Matrix3::from_element($default))})),
85            #[cfg(all(feature = $value_string, feature = "matrix4"))]
86            (Value::$matrix_kind(Matrix::Matrix4(arg))) => Ok(Box::new([<$lib $lhs_type M4>]{arg, out: Ref::new(Matrix4::from_element($default))})),
87            #[cfg(all(feature = $value_string, feature = "matrix2x3"))]
88            (Value::$matrix_kind(Matrix::Matrix2x3(arg))) => Ok(Box::new([<$lib $lhs_type M2x3>]{arg, out: Ref::new(Matrix2x3::from_element($default))})),         
89            #[cfg(all(feature = $value_string, feature = "matrix3x2"))]
90            (Value::$matrix_kind(Matrix::Matrix3x2(arg))) => Ok(Box::new([<$lib $lhs_type M3x2>]{arg, out: Ref::new(Matrix3x2::from_element($default))})),         
91            #[cfg(all(feature = $value_string, feature = "row_vector2"))]
92            (Value::$matrix_kind(Matrix::RowVector2(arg))) => Ok(Box::new([<$lib $lhs_type R2>]{arg: arg.clone(), out: Ref::new(RowVector2::from_element($default)) })),
93            #[cfg(all(feature = $value_string, feature = "row_vector3"))]
94            (Value::$matrix_kind(Matrix::RowVector3(arg))) => Ok(Box::new([<$lib $lhs_type R3>]{arg: arg.clone(), out: Ref::new(RowVector3::from_element($default)) })),
95            #[cfg(all(feature = $value_string, feature = "row_vector4"))]
96            (Value::$matrix_kind(Matrix::RowVector4(arg))) => Ok(Box::new([<$lib $lhs_type R4>]{arg: arg.clone(), out: Ref::new(RowVector4::from_element($default)) })),
97            #[cfg(all(feature = $value_string, feature = "row_vectord"))]
98            (Value::$matrix_kind(Matrix::RowDVector(arg))) => Ok(Box::new([<$lib $lhs_type RD>]{arg: arg.clone(), out: Ref::new(RowDVector::from_element(arg.borrow().len(),$default))})),
99            #[cfg(all(feature = $value_string, feature = "vector2"))]
100            (Value::$matrix_kind(Matrix::Vector2(arg))) => Ok(Box::new([<$lib $lhs_type V2>]{arg: arg.clone(), out: Ref::new(Vector2::from_element($default)) })),
101            #[cfg(all(feature = $value_string, feature = "vector3"))]
102            (Value::$matrix_kind(Matrix::Vector3(arg))) => Ok(Box::new([<$lib $lhs_type V3>]{arg: arg.clone(), out: Ref::new(Vector3::from_element($default)) })),
103            #[cfg(all(feature = $value_string, feature = "vector4"))]
104            (Value::$matrix_kind(Matrix::Vector4(arg))) => Ok(Box::new([<$lib $lhs_type V4>]{arg: arg.clone(), out: Ref::new(Vector4::from_element($default)) })),
105            #[cfg(all(feature = $value_string, feature = "vectord"))]
106            (Value::$matrix_kind(Matrix::DVector(arg))) => Ok(Box::new([<$lib $lhs_type VD>]{arg: arg.clone(), out: Ref::new(DVector::from_element(arg.borrow().len(),$default))})),
107            #[cfg(all(feature = $value_string, feature = "matrixd"))]
108            (Value::$matrix_kind(Matrix::DMatrix(arg))) => {
109              let (rows,cols) = {arg.borrow().shape()};
110              Ok(Box::new([<$lib $lhs_type MD>]{arg, out: Ref::new(DMatrix::from_element(rows,cols,$default))}))},
111          )+
112        )+
113        x => Err(MechError{file: file!().to_string(),  tokens: vec![], msg: format!("{:?}", x).to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind }),
114      }}}}
115
116#[macro_export]
117macro_rules! impl_math_unop {
118  ($fxn_name:ident, $type:ident, $op_fxn:ident, $feature_flag:expr) => {
119    paste!{
120      impl_unop!([<$fxn_name $type S>], $type, $type, [<$op_fxn _op>], $feature_flag);
121      #[cfg(feature = "matrix1")]
122      impl_unop!([<$fxn_name $type M1>], Matrix1<$type>, Matrix1<$type>, [<$op_fxn _vec_op>], $feature_flag);
123      #[cfg(feature = "matrix2")]
124      impl_unop!([<$fxn_name $type M2>], Matrix2<$type>, Matrix2<$type>, [<$op_fxn _vec_op>], $feature_flag);
125      #[cfg(feature = "matrix3")]
126      impl_unop!([<$fxn_name $type M3>], Matrix3<$type>, Matrix3<$type>, [<$op_fxn _vec_op>], $feature_flag);
127      #[cfg(feature = "matrix4")]
128      impl_unop!([<$fxn_name $type M4>], Matrix4<$type>, Matrix4<$type>, [<$op_fxn _vec_op>], $feature_flag);
129      #[cfg(feature = "matrix2x3")]
130      impl_unop!([<$fxn_name $type M2x3>], Matrix2x3<$type>, Matrix2x3<$type>, [<$op_fxn _vec_op>], $feature_flag);
131      #[cfg(feature = "matrix3x2")]
132      impl_unop!([<$fxn_name $type M3x2>], Matrix3x2<$type>, Matrix3x2<$type>, [<$op_fxn _vec_op>], $feature_flag);
133      #[cfg(feature = "matrixd")]
134      impl_unop!([<$fxn_name $type MD>], DMatrix<$type>, DMatrix<$type>, [<$op_fxn _vec_op>], $feature_flag);
135      #[cfg(feature = "row_vector2")]
136      impl_unop!([<$fxn_name $type R2>], RowVector2<$type>, RowVector2<$type>, [<$op_fxn _vec_op>], $feature_flag);
137      #[cfg(feature = "row_vector3")]
138      impl_unop!([<$fxn_name $type R3>], RowVector3<$type>, RowVector3<$type>, [<$op_fxn _vec_op>], $feature_flag);
139      #[cfg(feature = "row_vector4")]
140      impl_unop!([<$fxn_name $type R4>], RowVector4<$type>, RowVector4<$type>, [<$op_fxn _vec_op>], $feature_flag);
141      #[cfg(feature = "row_vectord")]
142      impl_unop!([<$fxn_name $type RD>], RowDVector<$type>, RowDVector<$type>, [<$op_fxn _vec_op>], $feature_flag);
143      #[cfg(feature = "vector2")]
144      impl_unop!([<$fxn_name $type V2>], Vector2<$type>, Vector2<$type>, [<$op_fxn _vec_op>], $feature_flag);
145      #[cfg(feature = "vector3")]
146      impl_unop!([<$fxn_name $type V3>], Vector3<$type>, Vector3<$type>, [<$op_fxn _vec_op>], $feature_flag);
147      #[cfg(feature = "vector4")]
148      impl_unop!([<$fxn_name $type V4>], Vector4<$type>, Vector4<$type>, [<$op_fxn _vec_op>], $feature_flag);
149      #[cfg(feature = "vectord")]
150      impl_unop!([<$fxn_name $type VD>], DVector<$type>, DVector<$type>, [<$op_fxn _vec_op>], $feature_flag);
151    }}}