qm/v/
v4.rs

1//! vector4
2//!
3
4use num::Float;
5
6use crate::v::TVector;
7use crate::m::TMatrix;
8
9/// Vector4
10pub type Vector4<F> = [F; 4];
11
12/// TVector for Vector4
13impl<F: Float + std::fmt::Debug + std::iter::Sum> TVector<F> for Vector4<F> {
14  /// constructor
15  fn new(v: &Vec<F>) -> Self {
16    (0..4).into_iter().map(|i| v[i]).collect::<Vec<_>>().try_into().unwrap()
17  }
18  /// check equal with precision
19  fn prec_eq(&self, e: F, v: &impl TVector<F>) -> bool {
20    for i in 0..4 {
21      if (self[i] - v.me()[i]).abs() >= e { return false; }
22    }
23    true
24  }
25  /// to_vec
26  fn to_vec(&self) -> Vec<F> {
27    self.iter().map(|&f| f).collect::<Vec<_>>()
28  }
29  /// like as slice
30  fn me(&self) -> &[F] {
31    self
32  }
33  /// a dot self
34  fn dot(&self, a: &impl TVector<F>) -> F {
35    let a = a.me();
36    (0..4).into_iter().map(|i| a[i] * self[i]).sum::<F>()
37  }
38  /// m dot self
39  fn dot_mv(&self, m: &impl TMatrix<F>) -> Self {
40    Self::new(&(0..4).into_iter().map(|j| self.dot(&m.rowv4(j))).collect())
41  }
42  /// self cross b
43  fn cross(&self, _b: &impl TVector<F>) -> Self {
44/*
45    let a = self.me();
46    let b = b.me();
47    let o = <F>::from(0).unwrap();
48    Vector8::<F>::new(&vec![
49      a[1] * b[2] - a[2] * b[1],
50      a[2] * b[0] - a[0] * b[2],
51      a[0] * b[1] - a[1] * b[0],
52      o,
53      a[0] * b[3] - a[3] * b[0],
54      a[1] * b[3] - a[3] * b[1],
55      a[2] * b[3] - a[3] * b[2],
56      o])
57*/
58    panic!("TODO: v4 x v4 cross generates v8");
59  }
60}