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 paste::paste;
11use na::{Vector3, DVector, Vector2, Vector4, RowDVector, Matrix1, Matrix3, Matrix4, RowVector3, RowVector4, RowVector2, DMatrix, Rotation3, Matrix2x3, Matrix3x2, Matrix6, Matrix2};
12use std::ops::*;
13use num_traits::*;
14use std::fmt::Debug;
15use simba::scalar::ClosedNeg;
16use num_traits::Pow;
17use mech_core::matrix::Matrix;
18
19static PI: f64 = 3.14159265358979323846264338327950288;
20
21pub mod acos;
22pub mod acosh;
23pub mod acot;
24pub mod acsc;
25pub mod asec;
26pub mod asin;
27pub mod asinh;
28pub mod atan;
29pub mod atan2;
30pub mod cos;
31pub mod cosh;
32pub mod cot;
33pub mod csc;
34pub mod sec;
35pub mod sin;
36pub mod sinh;
37pub mod tan;
38pub mod tanh;
39
40pub use self::acos::*;
41pub use self::acosh::*;
42pub use self::acot::*;
43pub use self::acsc::*;
44pub use self::asec::*;
45pub use self::asin::*;
46pub use self::asinh::*;
47pub use self::atan::*;
48pub use self::atan2::*;
49pub use self::cos::*;
50pub use self::cosh::*;
51pub use self::cot::*;
52pub use self::csc::*;
53pub use self::sec::*;
54pub use self::sin::*;
55pub use self::sinh::*;
56pub use self::tan::*;
57pub use self::tanh::*;
58
59// ----------------------------------------------------------------------------
60// Math Library
61// ----------------------------------------------------------------------------
62
63#[macro_export]
64macro_rules! impl_math_fxns {
65  ($lib:ident) => {
66    impl_fxns!($lib,T,T,impl_binop);
67  }}
68
69#[macro_export]
70macro_rules! impl_urnop_match_arms2 {
71  ($lib:ident, $arg:expr, $($lhs_type:ident => $($matrix_kind:ident, $target_type:ident, $default:expr, $value_string:tt),+);+ $(;)?) => {
72    paste!{
73      match $arg {
74        $(
75          $(
76            (Value::$lhs_type(arg)) => Ok(Box::new([<$lib $lhs_type S>]{arg: arg.clone(), out: new_ref($default) })),
77            #[cfg(all(feature = $value_string, feature = "Matrix1"))]
78            (Value::$matrix_kind(Matrix::Matrix1(arg))) => Ok(Box::new([<$lib $lhs_type M1>]{arg, out: new_ref(Matrix1::from_element($default))})),
79            #[cfg(all(feature = $value_string, feature = "Matrix2"))]
80            (Value::$matrix_kind(Matrix::Matrix2(arg))) => Ok(Box::new([<$lib $lhs_type M2>]{arg, out: new_ref(Matrix2::from_element($default))})),
81            #[cfg(all(feature = $value_string, feature = "Matrix3"))]
82            (Value::$matrix_kind(Matrix::Matrix3(arg))) => Ok(Box::new([<$lib $lhs_type M3>]{arg, out: new_ref(Matrix3::from_element($default))})),
83            #[cfg(all(feature = $value_string, feature = "Matrix4"))]
84            (Value::$matrix_kind(Matrix::Matrix4(arg))) => Ok(Box::new([<$lib $lhs_type M4>]{arg, out: new_ref(Matrix4::from_element($default))})),
85            #[cfg(all(feature = $value_string, feature = "Matrix2x3"))]
86            (Value::$matrix_kind(Matrix::Matrix2x3(arg))) => Ok(Box::new([<$lib $lhs_type M2x3>]{arg, out: new_ref(Matrix2x3::from_element($default))})),         
87            #[cfg(all(feature = $value_string, feature = "Matrix3x2"))]
88            (Value::$matrix_kind(Matrix::Matrix3x2(arg))) => Ok(Box::new([<$lib $lhs_type M3x2>]{arg, out: new_ref(Matrix3x2::from_element($default))})),         
89            #[cfg(all(feature = $value_string, feature = "RowVector2"))]
90            (Value::$matrix_kind(Matrix::RowVector2(arg))) => Ok(Box::new([<$lib $lhs_type R2>]{arg: arg.clone(), out: new_ref(RowVector2::from_element($default)) })),
91            #[cfg(all(feature = $value_string, feature = "RowVector3"))]
92            (Value::$matrix_kind(Matrix::RowVector3(arg))) => Ok(Box::new([<$lib $lhs_type R3>]{arg: arg.clone(), out: new_ref(RowVector3::from_element($default)) })),
93            #[cfg(all(feature = $value_string, feature = "RowVector4"))]
94            (Value::$matrix_kind(Matrix::RowVector4(arg))) => Ok(Box::new([<$lib $lhs_type R4>]{arg: arg.clone(), out: new_ref(RowVector4::from_element($default)) })),
95            #[cfg(all(feature = $value_string, feature = "RowVectorD"))]
96            (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))})),
97            #[cfg(all(feature = $value_string, feature = "Vector2"))]
98            (Value::$matrix_kind(Matrix::Vector2(arg))) => Ok(Box::new([<$lib $lhs_type V2>]{arg: arg.clone(), out: new_ref(Vector2::from_element($default)) })),
99            #[cfg(all(feature = $value_string, feature = "Vector3"))]
100            (Value::$matrix_kind(Matrix::Vector3(arg))) => Ok(Box::new([<$lib $lhs_type V3>]{arg: arg.clone(), out: new_ref(Vector3::from_element($default)) })),
101            #[cfg(all(feature = $value_string, feature = "Vector4"))]
102            (Value::$matrix_kind(Matrix::Vector4(arg))) => Ok(Box::new([<$lib $lhs_type V4>]{arg: arg.clone(), out: new_ref(Vector4::from_element($default)) })),
103            #[cfg(all(feature = $value_string, feature = "VectorD"))]
104            (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))})),
105            #[cfg(all(feature = $value_string, feature = "MatrixD"))]
106            (Value::$matrix_kind(Matrix::DMatrix(arg))) => {
107              let (rows,cols) = {arg.borrow().shape()};
108              Ok(Box::new([<$lib $lhs_type MD>]{arg, out: new_ref(DMatrix::from_element(rows,cols,$default))}))},
109          )+
110        )+
111        x => Err(MechError{file: file!().to_string(),  tokens: vec![], msg: format!("{:?}", x).to_string(), id: line!(), kind: MechErrorKind::UnhandledFunctionArgumentKind }),
112      }}}}
113
114#[macro_export]
115macro_rules! impl_math_urop {
116  ($fxn_name:ident, $type:ident, $op_fxn:ident) => {
117    paste!{
118      impl_urop!([<$fxn_name $type S>], $type, $type, [<$op_fxn _op>]);
119      #[cfg(feature = "Matrix1")]
120      impl_urop!([<$fxn_name $type M1>], Matrix1<$type>, Matrix1<$type>, [<$op_fxn _vec_op>]);
121      #[cfg(feature = "Matrix2")]
122      impl_urop!([<$fxn_name $type M2>], Matrix2<$type>, Matrix2<$type>, [<$op_fxn _vec_op>]);
123      #[cfg(feature = "Matrix3")]
124      impl_urop!([<$fxn_name $type M3>], Matrix3<$type>, Matrix3<$type>, [<$op_fxn _vec_op>]);
125      #[cfg(feature = "Matrix4")]
126      impl_urop!([<$fxn_name $type M4>], Matrix4<$type>, Matrix4<$type>, [<$op_fxn _vec_op>]);
127      #[cfg(feature = "Matrix2x3")]
128      impl_urop!([<$fxn_name $type M2x3>], Matrix2x3<$type>, Matrix2x3<$type>, [<$op_fxn _vec_op>]);
129      #[cfg(feature = "Matrix3x2")]
130      impl_urop!([<$fxn_name $type M3x2>], Matrix3x2<$type>, Matrix3x2<$type>, [<$op_fxn _vec_op>]);
131      #[cfg(feature = "MatrixD")]
132      impl_urop!([<$fxn_name $type MD>], DMatrix<$type>, DMatrix<$type>, [<$op_fxn _vec_op>]);
133      #[cfg(feature = "RowVector2")]
134      impl_urop!([<$fxn_name $type R2>], RowVector2<$type>, RowVector2<$type>, [<$op_fxn _vec_op>]);
135      #[cfg(feature = "RowVector3")]
136      impl_urop!([<$fxn_name $type R3>], RowVector3<$type>, RowVector3<$type>, [<$op_fxn _vec_op>]);
137      #[cfg(feature = "RowVector4")]
138      impl_urop!([<$fxn_name $type R4>], RowVector4<$type>, RowVector4<$type>, [<$op_fxn _vec_op>]);
139      #[cfg(feature = "RowVectorD")]
140      impl_urop!([<$fxn_name $type RD>], RowDVector<$type>, RowDVector<$type>, [<$op_fxn _vec_op>]);
141      #[cfg(feature = "Vector2")]
142      impl_urop!([<$fxn_name $type V2>], Vector2<$type>, Vector2<$type>, [<$op_fxn _vec_op>]);
143      #[cfg(feature = "Vector3")]
144      impl_urop!([<$fxn_name $type V3>], Vector3<$type>, Vector3<$type>, [<$op_fxn _vec_op>]);
145      #[cfg(feature = "Vector4")]
146      impl_urop!([<$fxn_name $type V4>], Vector4<$type>, Vector4<$type>, [<$op_fxn _vec_op>]);
147      #[cfg(feature = "VectorD")]
148      impl_urop!([<$fxn_name $type VD>], DVector<$type>, DVector<$type>, [<$op_fxn _vec_op>]);
149    }}}