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 = "gamma")]
54pub mod gamma;
55#[cfg(feature = "logarithm")]
56pub mod logarithm;
57#[cfg(feature = "ops")]
58pub mod ops;
59#[cfg(feature = "op_assign")]
60pub mod op_assign;
61#[cfg(feature = "root")]
62pub mod root;
63#[cfg(feature = "rounding")]
64pub mod rounding;
65#[cfg(feature = "stat_error")]
66pub mod stat_error;
67#[cfg(feature = "trig")]
68pub mod trig;
69
70#[cfg(feature = "arithmetic")]
71pub use self::arithmetic::*;
72#[cfg(feature = "bessel")]
73pub use self::bessel::*;
74#[cfg(feature = "exponential")]
75pub use self::exponential::*;
76#[cfg(feature = "gamma")]
77pub use self::gamma::*;
78#[cfg(feature = "logarithm")]
79pub use self::logarithm::*;
80#[cfg(feature = "ops")]
81pub use self::ops::*;
82#[cfg(feature = "op_assign")]
83pub use self::op_assign::*;
84#[cfg(feature = "root")]
85pub use self::root::*;
86#[cfg(feature = "rounding")]
87pub use self::rounding::*;
88#[cfg(feature = "stat_error")]
89pub use self::stat_error::*;
90#[cfg(feature = "trig")]
91pub use self::trig::*;
92
93// ----------------------------------------------------------------------------
94// Math Library
95// ----------------------------------------------------------------------------
96
97#[macro_export]
98macro_rules! impl_math_fxns {
99  ($lib:ident) => {
100    impl_fxns!($lib,T,T,impl_binop);
101  }}
102
103#[macro_export]
104macro_rules! impl_urnop_match_arms2 {
105  ($lib:ident, $arg:expr, $($lhs_type:ident => $($matrix_kind:ident, $target_type:ident, $default:expr, $value_string:tt),+);+ $(;)?) => {
106    paste!{
107      match $arg {
108        $(
109          $(
110            #[cfg(feature = $value_string)]
111            (Value::$lhs_type(arg)) => Ok(Box::new([<$lib $lhs_type:camel S>]{arg: arg.clone(), out: Ref::new($default) })),
112            #[cfg(all(feature = $value_string, feature = "matrix1"))]
113            (Value::$matrix_kind(Matrix::Matrix1(arg))) => Ok(Box::new([<$lib $lhs_type:camel M1>]{arg, out: Ref::new(Matrix1::from_element($default))})),
114            #[cfg(all(feature = $value_string, feature = "matrix2"))]
115            (Value::$matrix_kind(Matrix::Matrix2(arg))) => Ok(Box::new([<$lib $lhs_type:camel M2>]{arg, out: Ref::new(Matrix2::from_element($default))})),
116            #[cfg(all(feature = $value_string, feature = "matrix3"))]
117            (Value::$matrix_kind(Matrix::Matrix3(arg))) => Ok(Box::new([<$lib $lhs_type:camel M3>]{arg, out: Ref::new(Matrix3::from_element($default))})),
118            #[cfg(all(feature = $value_string, feature = "matrix4"))]
119            (Value::$matrix_kind(Matrix::Matrix4(arg))) => Ok(Box::new([<$lib $lhs_type:camel M4>]{arg, out: Ref::new(Matrix4::from_element($default))})),
120            #[cfg(all(feature = $value_string, feature = "matrix2x3"))]
121            (Value::$matrix_kind(Matrix::Matrix2x3(arg))) => Ok(Box::new([<$lib $lhs_type:camel M2x3>]{arg, out: Ref::new(Matrix2x3::from_element($default))})),         
122            #[cfg(all(feature = $value_string, feature = "matrix3x2"))]
123            (Value::$matrix_kind(Matrix::Matrix3x2(arg))) => Ok(Box::new([<$lib $lhs_type:camel M3x2>]{arg, out: Ref::new(Matrix3x2::from_element($default))})),         
124            #[cfg(all(feature = $value_string, feature = "row_vector2"))]
125            (Value::$matrix_kind(Matrix::RowVector2(arg))) => Ok(Box::new([<$lib $lhs_type:camel R2>]{arg: arg.clone(), out: Ref::new(RowVector2::from_element($default)) })),
126            #[cfg(all(feature = $value_string, feature = "row_vector3"))]
127            (Value::$matrix_kind(Matrix::RowVector3(arg))) => Ok(Box::new([<$lib $lhs_type:camel R3>]{arg: arg.clone(), out: Ref::new(RowVector3::from_element($default)) })),
128            #[cfg(all(feature = $value_string, feature = "row_vector4"))]
129            (Value::$matrix_kind(Matrix::RowVector4(arg))) => Ok(Box::new([<$lib $lhs_type:camel R4>]{arg: arg.clone(), out: Ref::new(RowVector4::from_element($default)) })),
130            #[cfg(all(feature = $value_string, feature = "row_vectord"))]
131            (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))})),
132            #[cfg(all(feature = $value_string, feature = "vector2"))]
133            (Value::$matrix_kind(Matrix::Vector2(arg))) => Ok(Box::new([<$lib $lhs_type:camel V2>]{arg: arg.clone(), out: Ref::new(Vector2::from_element($default)) })),
134            #[cfg(all(feature = $value_string, feature = "vector3"))]
135            (Value::$matrix_kind(Matrix::Vector3(arg))) => Ok(Box::new([<$lib $lhs_type:camel V3>]{arg: arg.clone(), out: Ref::new(Vector3::from_element($default)) })),
136            #[cfg(all(feature = $value_string, feature = "vector4"))]
137            (Value::$matrix_kind(Matrix::Vector4(arg))) => Ok(Box::new([<$lib $lhs_type:camel V4>]{arg: arg.clone(), out: Ref::new(Vector4::from_element($default)) })),
138            #[cfg(all(feature = $value_string, feature = "vectord"))]
139            (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))})),
140            #[cfg(all(feature = $value_string, feature = "matrixd"))]
141            (Value::$matrix_kind(Matrix::DMatrix(arg))) => {
142              let (rows,cols) = {arg.borrow().shape()};
143              Ok(Box::new([<$lib $lhs_type:camel MD>]{arg, out: Ref::new(DMatrix::from_element(rows,cols,$default))}))},
144          )+
145        )+
146        x => Err(MechError2::new(
147          UnhandledFunctionArgumentKind1{arg: x.kind(), fxn_name: stringify!($lib).to_string()},
148          None
149        ).with_compiler_loc()),
150      }}}}
151
152#[macro_export]
153macro_rules! impl_math_unop {
154  ($fxn_name:ident, $type:ident, $op_fxn:ident, $feature_flag:expr) => {
155    paste!{
156      impl_unop!([<$fxn_name $type:camel S>], $type, $type, [<$op_fxn _op>], $feature_flag);
157      #[cfg(feature = "matrix1")]
158      impl_unop!([<$fxn_name $type:camel M1>], Matrix1<$type>, Matrix1<$type>, [<$op_fxn _vec_op>], $feature_flag);
159      #[cfg(feature = "matrix2")]
160      impl_unop!([<$fxn_name $type:camel M2>], Matrix2<$type>, Matrix2<$type>, [<$op_fxn _vec_op>], $feature_flag);
161      #[cfg(feature = "matrix3")]
162      impl_unop!([<$fxn_name $type:camel M3>], Matrix3<$type>, Matrix3<$type>, [<$op_fxn _vec_op>], $feature_flag);
163      #[cfg(feature = "matrix4")]
164      impl_unop!([<$fxn_name $type:camel M4>], Matrix4<$type>, Matrix4<$type>, [<$op_fxn _vec_op>], $feature_flag);
165      #[cfg(feature = "matrix2x3")]
166      impl_unop!([<$fxn_name $type:camel M2x3>], Matrix2x3<$type>, Matrix2x3<$type>, [<$op_fxn _vec_op>], $feature_flag);
167      #[cfg(feature = "matrix3x2")]
168      impl_unop!([<$fxn_name $type:camel M3x2>], Matrix3x2<$type>, Matrix3x2<$type>, [<$op_fxn _vec_op>], $feature_flag);
169      #[cfg(feature = "matrixd")]
170      impl_unop!([<$fxn_name $type:camel MD>], DMatrix<$type>, DMatrix<$type>, [<$op_fxn _vec_op>], $feature_flag);
171      #[cfg(feature = "row_vector2")]
172      impl_unop!([<$fxn_name $type:camel R2>], RowVector2<$type>, RowVector2<$type>, [<$op_fxn _vec_op>], $feature_flag);
173      #[cfg(feature = "row_vector3")]
174      impl_unop!([<$fxn_name $type:camel R3>], RowVector3<$type>, RowVector3<$type>, [<$op_fxn _vec_op>], $feature_flag);
175      #[cfg(feature = "row_vector4")]
176      impl_unop!([<$fxn_name $type:camel R4>], RowVector4<$type>, RowVector4<$type>, [<$op_fxn _vec_op>], $feature_flag);
177      #[cfg(feature = "row_vectord")]
178      impl_unop!([<$fxn_name $type:camel RD>], RowDVector<$type>, RowDVector<$type>, [<$op_fxn _vec_op>], $feature_flag);
179      #[cfg(feature = "vector2")]
180      impl_unop!([<$fxn_name $type:camel V2>], Vector2<$type>, Vector2<$type>, [<$op_fxn _vec_op>], $feature_flag);
181      #[cfg(feature = "vector3")]
182      impl_unop!([<$fxn_name $type:camel V3>], Vector3<$type>, Vector3<$type>, [<$op_fxn _vec_op>], $feature_flag);
183      #[cfg(feature = "vector4")]
184      impl_unop!([<$fxn_name $type:camel V4>], Vector4<$type>, Vector4<$type>, [<$op_fxn _vec_op>], $feature_flag);
185      #[cfg(feature = "vectord")]
186      impl_unop!([<$fxn_name $type:camel VD>], DVector<$type>, DVector<$type>, [<$op_fxn _vec_op>], $feature_flag);
187    }}}