mech_math/
lib.rs

1#![no_main]
2#![allow(warnings)]
3#[macro_use]
4extern crate mech_core;
5extern crate libm;
6extern crate nalgebra as na;
7extern crate paste;
8extern crate simba;
9
10use mech_core::*;
11
12use paste::paste;
13use na::{Vector3, DVector, Vector2, Vector4, RowDVector, Matrix1, Matrix3, Matrix4, RowVector3, RowVector4, RowVector2, DMatrix, Rotation3, Matrix2x3, Matrix3x2, Matrix6, Matrix2};
14use std::ops::*;
15use num_traits::*;
16use std::fmt::Debug;
17use simba::scalar::ClosedNeg;
18use num_traits::Pow;
19use std::marker::PhantomData;
20use mech_core::matrix::Matrix;
21
22static PI: f64 = 3.14159265358979323846264338327950288;
23
24pub mod trig;
25pub mod ops;
26
27pub use self::trig::*;
28pub use self::ops::*;
29
30// ----------------------------------------------------------------------------
31// Math Library
32// ----------------------------------------------------------------------------
33
34#[macro_export]
35macro_rules! impl_math_fxns {
36  ($lib:ident) => {
37    impl_fxns!($lib,T,T,impl_binop);
38  }}
39
40#[macro_export]
41macro_rules! impl_urnop_match_arms2 {
42  ($lib:ident, $arg:expr, $($lhs_type:ident => $($matrix_kind:ident, $target_type:ident, $default:expr, $value_string:tt),+);+ $(;)?) => {
43    paste!{
44      match $arg {
45        $(
46          $(
47            (Value::$lhs_type(arg)) => Ok(Box::new([<$lib $lhs_type S>]{arg: arg.clone(), out: new_ref($default) })),
48            #[cfg(all(feature = $value_string, feature = "Matrix1"))]
49            (Value::$matrix_kind(Matrix::Matrix1(arg))) => Ok(Box::new([<$lib $lhs_type M1>]{arg, out: new_ref(Matrix1::from_element($default))})),
50            #[cfg(all(feature = $value_string, feature = "Matrix2"))]
51            (Value::$matrix_kind(Matrix::Matrix2(arg))) => Ok(Box::new([<$lib $lhs_type M2>]{arg, out: new_ref(Matrix2::from_element($default))})),
52            #[cfg(all(feature = $value_string, feature = "Matrix3"))]
53            (Value::$matrix_kind(Matrix::Matrix3(arg))) => Ok(Box::new([<$lib $lhs_type M3>]{arg, out: new_ref(Matrix3::from_element($default))})),
54            #[cfg(all(feature = $value_string, feature = "Matrix4"))]
55            (Value::$matrix_kind(Matrix::Matrix4(arg))) => Ok(Box::new([<$lib $lhs_type M4>]{arg, out: new_ref(Matrix4::from_element($default))})),
56            #[cfg(all(feature = $value_string, feature = "Matrix2x3"))]
57            (Value::$matrix_kind(Matrix::Matrix2x3(arg))) => Ok(Box::new([<$lib $lhs_type M2x3>]{arg, out: new_ref(Matrix2x3::from_element($default))})),         
58            #[cfg(all(feature = $value_string, feature = "Matrix3x2"))]
59            (Value::$matrix_kind(Matrix::Matrix3x2(arg))) => Ok(Box::new([<$lib $lhs_type M3x2>]{arg, out: new_ref(Matrix3x2::from_element($default))})),         
60            #[cfg(all(feature = $value_string, feature = "RowVector2"))]
61            (Value::$matrix_kind(Matrix::RowVector2(arg))) => Ok(Box::new([<$lib $lhs_type R2>]{arg: arg.clone(), out: new_ref(RowVector2::from_element($default)) })),
62            #[cfg(all(feature = $value_string, feature = "RowVector3"))]
63            (Value::$matrix_kind(Matrix::RowVector3(arg))) => Ok(Box::new([<$lib $lhs_type R3>]{arg: arg.clone(), out: new_ref(RowVector3::from_element($default)) })),
64            #[cfg(all(feature = $value_string, feature = "RowVector4"))]
65            (Value::$matrix_kind(Matrix::RowVector4(arg))) => Ok(Box::new([<$lib $lhs_type R4>]{arg: arg.clone(), out: new_ref(RowVector4::from_element($default)) })),
66            #[cfg(all(feature = $value_string, feature = "RowVectorD"))]
67            (Value::$matrix_kind(Matrix::RowDVector(arg))) => Ok(Box::new([<$lib $lhs_type RD>]{arg: arg.clone(), out: new_ref(RowDVector::from_element(arg.borrow().len(),$default))})),
68            #[cfg(all(feature = $value_string, feature = "Vector2"))]
69            (Value::$matrix_kind(Matrix::Vector2(arg))) => Ok(Box::new([<$lib $lhs_type V2>]{arg: arg.clone(), out: new_ref(Vector2::from_element($default)) })),
70            #[cfg(all(feature = $value_string, feature = "Vector3"))]
71            (Value::$matrix_kind(Matrix::Vector3(arg))) => Ok(Box::new([<$lib $lhs_type V3>]{arg: arg.clone(), out: new_ref(Vector3::from_element($default)) })),
72            #[cfg(all(feature = $value_string, feature = "Vector4"))]
73            (Value::$matrix_kind(Matrix::Vector4(arg))) => Ok(Box::new([<$lib $lhs_type V4>]{arg: arg.clone(), out: new_ref(Vector4::from_element($default)) })),
74            #[cfg(all(feature = $value_string, feature = "VectorD"))]
75            (Value::$matrix_kind(Matrix::DVector(arg))) => Ok(Box::new([<$lib $lhs_type VD>]{arg: arg.clone(), out: new_ref(DVector::from_element(arg.borrow().len(),$default))})),
76            #[cfg(all(feature = $value_string, feature = "MatrixD"))]
77            (Value::$matrix_kind(Matrix::DMatrix(arg))) => {
78              let (rows,cols) = {arg.borrow().shape()};
79              Ok(Box::new([<$lib $lhs_type MD>]{arg, out: new_ref(DMatrix::from_element(rows,cols,$default))}))},
80          )+
81        )+
82        x => Err(MechError{file: file!().to_string(),  tokens: vec![], msg: format!("{:?}", x).to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind }),
83      }}}}
84
85#[macro_export]
86macro_rules! impl_math_urop {
87  ($fxn_name:ident, $type:ident, $op_fxn:ident) => {
88    paste!{
89      impl_urop!([<$fxn_name $type S>], $type, $type, [<$op_fxn _op>]);
90      #[cfg(feature = "Matrix1")]
91      impl_urop!([<$fxn_name $type M1>], Matrix1<$type>, Matrix1<$type>, [<$op_fxn _vec_op>]);
92      #[cfg(feature = "Matrix2")]
93      impl_urop!([<$fxn_name $type M2>], Matrix2<$type>, Matrix2<$type>, [<$op_fxn _vec_op>]);
94      #[cfg(feature = "Matrix3")]
95      impl_urop!([<$fxn_name $type M3>], Matrix3<$type>, Matrix3<$type>, [<$op_fxn _vec_op>]);
96      #[cfg(feature = "Matrix4")]
97      impl_urop!([<$fxn_name $type M4>], Matrix4<$type>, Matrix4<$type>, [<$op_fxn _vec_op>]);
98      #[cfg(feature = "Matrix2x3")]
99      impl_urop!([<$fxn_name $type M2x3>], Matrix2x3<$type>, Matrix2x3<$type>, [<$op_fxn _vec_op>]);
100      #[cfg(feature = "Matrix3x2")]
101      impl_urop!([<$fxn_name $type M3x2>], Matrix3x2<$type>, Matrix3x2<$type>, [<$op_fxn _vec_op>]);
102      #[cfg(feature = "MatrixD")]
103      impl_urop!([<$fxn_name $type MD>], DMatrix<$type>, DMatrix<$type>, [<$op_fxn _vec_op>]);
104      #[cfg(feature = "RowVector2")]
105      impl_urop!([<$fxn_name $type R2>], RowVector2<$type>, RowVector2<$type>, [<$op_fxn _vec_op>]);
106      #[cfg(feature = "RowVector3")]
107      impl_urop!([<$fxn_name $type R3>], RowVector3<$type>, RowVector3<$type>, [<$op_fxn _vec_op>]);
108      #[cfg(feature = "RowVector4")]
109      impl_urop!([<$fxn_name $type R4>], RowVector4<$type>, RowVector4<$type>, [<$op_fxn _vec_op>]);
110      #[cfg(feature = "RowVectorD")]
111      impl_urop!([<$fxn_name $type RD>], RowDVector<$type>, RowDVector<$type>, [<$op_fxn _vec_op>]);
112      #[cfg(feature = "Vector2")]
113      impl_urop!([<$fxn_name $type V2>], Vector2<$type>, Vector2<$type>, [<$op_fxn _vec_op>]);
114      #[cfg(feature = "Vector3")]
115      impl_urop!([<$fxn_name $type V3>], Vector3<$type>, Vector3<$type>, [<$op_fxn _vec_op>]);
116      #[cfg(feature = "Vector4")]
117      impl_urop!([<$fxn_name $type V4>], Vector4<$type>, Vector4<$type>, [<$op_fxn _vec_op>]);
118      #[cfg(feature = "VectorD")]
119      impl_urop!([<$fxn_name $type VD>], DVector<$type>, DVector<$type>, [<$op_fxn _vec_op>]);
120    }}}