mech_math/
lib.rs

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