1#[cfg(feature = "parsable")]
2mod parsable;
3
4use num_traits::{real::Real, Zero};
5use vector_space::{DotProduct, InnerSpace, VectorSpace};
6use vector_basis::Basis;
7
8use std::ops::{
9 Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Sub, SubAssign,
10};
11
12#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
13pub struct Vector<T, const N: usize>([T; N]);
14
15impl<T, const N: usize> Vector<T, N> {
16 pub const fn new(elements: [T; N]) -> Self {
17 Vector(elements)
18 }
19}
20
21impl<T: Real, const N: usize, const I: usize> Basis<I> for Vector<T, N> {
22 fn unit_basis() -> Self {
23 let mut result = Self::zero();
24 result.0[I] = T::one();
25 result
26 }
27
28 fn basis_of(magnitude: T) -> Self {
29 let mut result = Self::zero();
30 result.0[I] = magnitude;
31 result
32 }
33
34 fn basis(&self) -> Self::Scalar {
35 self.0[I]
36 }
37
38 fn basis_mut(&mut self) -> &mut Self::Scalar {
39 &mut self.0[I]
40 }
41}
42
43impl<T: Default + Copy, const N: usize> Default for Vector<T, N> {
44 fn default() -> Self {
45 Vector([T::default(); N])
46 }
47}
48
49impl<T, const N: usize> Add<Self> for Vector<T, N>
50where
51 T: Add<Output = T> + Copy,
52{
53 type Output = Self;
54 fn add(mut self, other: Self) -> Self {
55 for i in 0..N {
56 self.0[i] = self.0[i] + other.0[i];
57 }
58 self
59 }
60}
61
62impl<T, const N: usize> AddAssign<Self> for Vector<T, N>
63where
64 T: AddAssign + Copy,
65{
66 fn add_assign(&mut self, other: Self) {
67 for i in 0..N {
68 self.0[i] += other.0[i];
69 }
70 }
71}
72
73impl<T, const N: usize> Sub<Self> for Vector<T, N>
74where
75 T: Sub<Output = T> + Copy,
76{
77 type Output = Self;
78 fn sub(mut self, other: Self) -> Self {
79 for i in 0..N {
80 self.0[i] = self.0[i] - other.0[i];
81 }
82 self
83 }
84}
85
86impl<T, const N: usize> SubAssign<Self> for Vector<T, N>
87where
88 T: SubAssign + Copy,
89{
90 fn sub_assign(&mut self, other: Self) {
91 for i in 0..N {
92 self.0[i] -= other.0[i];
93 }
94 }
95}
96
97impl<T, const N: usize> Neg for Vector<T, N>
98where
99 T: Neg<Output = T> + Copy,
100{
101 type Output = Self;
102 fn neg(mut self) -> Self {
103 for i in 0..N {
104 self.0[i] = -self.0[i];
105 }
106 self
107 }
108}
109
110impl<T, const N: usize> Mul<T> for Vector<T, N>
111where
112 T: Mul<Output = T> + Copy,
113{
114 type Output = Self;
115 fn mul(mut self, other: T) -> Self {
116 for i in 0..N {
117 self.0[i] = self.0[i] * other;
118 }
119 self
120 }
121}
122
123impl<T, const N: usize> MulAssign<T> for Vector<T, N>
124where
125 T: MulAssign + Copy,
126{
127 fn mul_assign(&mut self, other: T) {
128 for i in 0..N {
129 self.0[i] *= other;
130 }
131 }
132}
133
134impl<T, const N: usize> Div<T> for Vector<T, N>
135where
136 T: Div<Output = T> + Copy,
137{
138 type Output = Self;
139 fn div(mut self, other: T) -> Self {
140 for i in 0..N {
141 self.0[i] = self.0[i] / other;
142 }
143 self
144 }
145}
146
147impl<T, const N: usize> DivAssign<T> for Vector<T, N>
148where
149 T: DivAssign + Copy,
150{
151 fn div_assign(&mut self, other: T) {
152 for i in 0..N {
153 self.0[i] /= other;
154 }
155 }
156}
157
158impl<T, const N: usize> Mul<Self> for Vector<T, N>
159where
160 T: Add<Output = T> + Mul<Output = T> + Zero + Copy,
161{
162 type Output = T;
163 fn mul(self, other: Self) -> T {
164 self.0
165 .iter()
166 .zip(other.0.iter())
167 .fold(T::zero(), |result, (&left, &right)| result + left * right)
168 }
169}
170
171impl<T: Zero + Copy, const N: usize> Zero for Vector<T, N> {
172 fn zero() -> Self {
173 Vector([T::zero(); N])
174 }
175
176 fn is_zero(&self) -> bool {
177 for i in 0..N {
178 if !self.0[i].is_zero() {
179 return false;
180 }
181 }
182 true
183 }
184}
185
186impl<T: Real, const N: usize> VectorSpace for Vector<T, N> {
187 type Scalar = T;
188}
189
190impl<T: Real, const N: usize> DotProduct for Vector<T, N> {
191 type Output = Self::Scalar;
192 fn dot(self, other: Self) -> T {
193 self * other
194 }
195}
196
197impl<T: Real, const N: usize> InnerSpace for Vector<T, N> {
198 fn scalar(self, other: Self) -> T {
199 self * other
200 }
201}
202
203impl<T, const N: usize> Into<[T; N]> for Vector<T, N> {
204 fn into(self) -> [T; N] {
205 self.0
206 }
207}
208
209impl<'a, T, const N: usize> Into<&'a [T; N]> for &'a Vector<T, N> {
210 fn into(self) -> &'a [T; N] {
211 &self.0
212 }
213}
214
215impl<I, T, const N: usize> Index<I> for Vector<T, N>
216where
217 [T; N]: Index<I>,
218{
219 type Output = <[T; N] as Index<I>>::Output;
220 fn index(&self, index: I) -> &Self::Output {
221 &self.0[index]
222 }
223}
224
225impl<I, T, const N: usize> IndexMut<I> for Vector<T, N>
226where
227 [T; N]: IndexMut<I>,
228{
229 fn index_mut(&mut self, index: I) -> &mut Self::Output {
230 &mut self.0[index]
231 }
232}