1 unstable release
Uses old Rust 2015
0.1.0 | Aug 25, 2016 |
---|
#1222 in Math
37KB
851 lines
det-rs
Calculate the determinant of a square matrix using a Rust macro
This macro should not be used directly. Instead, it should be called inside a function that is used elsewhere.
Usage
Calculate the area of a parallelogram:
pub fn find_orthonormal_4(a: &Vector2<f32>, b: &Vector2<f32>) -> f32 {
det_copy!(a.x, a.y,
b.x, b.y)
}
Calculate a 4D orthogonal vector using nalgebra:
pub fn find_orthonormal_4(a: &Vector4<f32>, b: &Vector4<f32>, c: &Vector4<f32>) -> Vector4<f32> {
det_copy!(Vector4::x(), Vector4::y(), Vector4::z(), Vector4::w(),
a.x, a.y, a.z, a.w,
b.x, b.y, b.z, b.w,
c.x, c.y, c.z, c.w )
}
Usually det!
should be used in these cases, but strangely, it causes a type recursion error,
so we use det_copy!
instead. Note, that the constructors (eg. Vector4::x()
) are executed multiple times.
To avoid this, bind the vectors to a variable first and pass in the variable instead.
Requirements
There are various macros for various types of values.
det_copy!
T: Copy
T: Mul<T, Output=T>
T: Sub<T, Output=T>
T: Add<T, Output=T>
det_clone!
T: Clone
T: Mul<T, Output=T>
T: Sub<T, Output=T>
T: Add<T, Output=T>
det!
T: Mul<T, Output=T>
for<'a> &'a T: Mul<T, Output=T>
for<'a, 'b> &'a T: Mul<&'b T, Output=T>
T: Sub<T, Output=T>
T: Add<T, Output=T>