Skip to main content

conspire/math/tensor/rank_1/cross/
mod.rs

1#[cfg(test)]
2mod test;
3
4use super::TensorRank1;
5use crate::units::UnitMul;
6
7/// The cross product of two rank-1 tensors.
8pub trait CrossProduct<T> {
9    /// The specific return type.
10    type Output;
11    /// Returns the cross product with another rank-1 tensor.
12    fn cross(self, other: T) -> Self::Output;
13}
14
15impl<I, U, V> CrossProduct<TensorRank1<3, I, V>> for &TensorRank1<3, I, U>
16where
17    U: UnitMul<V>,
18{
19    type Output = TensorRank1<3, I, <U as UnitMul<V>>::Output>;
20    fn cross(self, other: TensorRank1<3, I, V>) -> Self::Output {
21        TensorRank1::from([
22            self[1] * other[2] - self[2] * other[1],
23            self[2] * other[0] - self[0] * other[2],
24            self[0] * other[1] - self[1] * other[0],
25        ])
26    }
27}
28
29impl<'a, I, U, V> CrossProduct<&'a TensorRank1<3, I, V>> for &TensorRank1<3, I, U>
30where
31    U: UnitMul<V>,
32{
33    type Output = TensorRank1<3, I, <U as UnitMul<V>>::Output>;
34    fn cross(self, other: &'a TensorRank1<3, I, V>) -> Self::Output {
35        TensorRank1::from([
36            self[1] * other[2] - self[2] * other[1],
37            self[2] * other[0] - self[0] * other[2],
38            self[0] * other[1] - self[1] * other[0],
39        ])
40    }
41}