1#![deny(missing_docs)]
2
3use inner_space::{DotProduct, VectorSpace};
82use scalars::{Real, Zero};
83use vector_basis::Basis;
84
85use std::{
86 iter::Sum,
87 ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Sub, SubAssign},
88};
89
90#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
124pub struct Vector<T, const N: usize>([T; N]);
125
126impl<T, const N: usize> Vector<T, N> {
127 pub const fn new(elements: [T; N]) -> Self {
151 Self(elements)
152 }
153}
154
155impl<T: Real, const N: usize, const I: usize> Basis<I> for Vector<T, N> {
156 fn unit_basis() -> Self {
157 let mut result = Self::zero();
158 result.0[I] = T::one();
159 result
160 }
161
162 fn basis_of(magnitude: T) -> Self {
163 let mut result = Self::zero();
164 result.0[I] = magnitude;
165 result
166 }
167
168 fn basis(&self) -> Self::Scalar {
169 self.0[I]
170 }
171
172 fn basis_mut(&mut self) -> &mut Self::Scalar {
173 &mut self.0[I]
174 }
175}
176
177impl<T: Default + Copy, const N: usize> Default for Vector<T, N> {
178 fn default() -> Self {
179 Self([T::default(); N])
180 }
181}
182
183impl<T, const N: usize> Add<Self> for Vector<T, N>
184where
185 T: Add<Output = T> + Copy,
186{
187 type Output = Self;
188 fn add(mut self, other: Self) -> Self {
189 for i in 0..N {
190 self.0[i] = self.0[i] + other.0[i];
191 }
192 self
193 }
194}
195
196impl<T, const N: usize> AddAssign<Self> for Vector<T, N>
197where
198 T: AddAssign + Copy,
199{
200 fn add_assign(&mut self, other: Self) {
201 for i in 0..N {
202 self.0[i] += other.0[i];
203 }
204 }
205}
206
207impl<T, const N: usize> Sub<Self> for Vector<T, N>
208where
209 T: Sub<Output = T> + Copy,
210{
211 type Output = Self;
212 fn sub(mut self, other: Self) -> Self {
213 for i in 0..N {
214 self.0[i] = self.0[i] - other.0[i];
215 }
216 self
217 }
218}
219
220impl<T, const N: usize> SubAssign<Self> for Vector<T, N>
221where
222 T: SubAssign + Copy,
223{
224 fn sub_assign(&mut self, other: Self) {
225 for i in 0..N {
226 self.0[i] -= other.0[i];
227 }
228 }
229}
230
231impl<T, const N: usize> Neg for Vector<T, N>
232where
233 T: Neg<Output = T> + Copy,
234{
235 type Output = Self;
236 fn neg(mut self) -> Self {
237 for i in 0..N {
238 self.0[i] = -self.0[i];
239 }
240 self
241 }
242}
243
244impl<T, const N: usize> Mul<T> for Vector<T, N>
245where
246 T: Mul<Output = T> + Copy,
247{
248 type Output = Self;
249 fn mul(mut self, other: T) -> Self {
250 for i in 0..N {
251 self.0[i] = self.0[i] * other;
252 }
253 self
254 }
255}
256
257impl<T, const N: usize> MulAssign<T> for Vector<T, N>
258where
259 T: MulAssign + Copy,
260{
261 fn mul_assign(&mut self, other: T) {
262 for i in 0..N {
263 self.0[i] *= other;
264 }
265 }
266}
267
268impl<T, const N: usize> Div<T> for Vector<T, N>
269where
270 T: Div<Output = T> + Copy,
271{
272 type Output = Self;
273 fn div(mut self, other: T) -> Self {
274 for i in 0..N {
275 self.0[i] = self.0[i] / other;
276 }
277 self
278 }
279}
280
281impl<T, const N: usize> DivAssign<T> for Vector<T, N>
282where
283 T: DivAssign + Copy,
284{
285 fn div_assign(&mut self, other: T) {
286 for i in 0..N {
287 self.0[i] /= other;
288 }
289 }
290}
291
292impl<T, const N: usize> Mul<Self> for Vector<T, N>
293where
294 T: Add<Output = T> + Mul<Output = T> + Zero + Copy,
295{
296 type Output = T;
297 fn mul(self, other: Self) -> T {
298 self.0
299 .iter()
300 .zip(other.0.iter())
301 .fold(T::zero(), |result, (&left, &right)| result + left * right)
302 }
303}
304
305impl<T: Zero + Copy, const N: usize> Zero for Vector<T, N> {
306 fn zero() -> Self {
307 Self([T::zero(); N])
308 }
309
310 fn is_zero(&self) -> bool {
311 for i in 0..N {
312 if !self.0[i].is_zero() {
313 return false;
314 }
315 }
316 true
317 }
318}
319
320impl<T: Real, const N: usize> VectorSpace for Vector<T, N> {
321 type Scalar = T;
322}
323
324impl<T: Real, const N: usize> DotProduct for Vector<T, N> {
325 type Output = Self::Scalar;
326 fn dot(&self, other: &Self) -> T {
327 *self * *other
328 }
329}
330
331impl<T, const N: usize> From<[T; N]> for Vector<T, N> {
332 fn from(elements: [T; N]) -> Self {
333 Self::new(elements)
334 }
335}
336
337impl<T, const N: usize> From<Vector<T, N>> for [T; N] {
338 fn from(val: Vector<T, N>) -> Self {
339 val.0
340 }
341}
342
343impl<'a, T, const N: usize> From<&'a Vector<T, N>> for &'a [T; N] {
344 fn from(val: &'a Vector<T, N>) -> Self {
345 &val.0
346 }
347}
348
349impl<'a, T, const N: usize> From<&'a mut Vector<T, N>> for &'a mut [T; N] {
350 fn from(val: &'a mut Vector<T, N>) -> Self {
351 &mut val.0
352 }
353}
354
355impl<'a, T, const N: usize> From<&'a Vector<T, N>> for &'a [T] {
356 fn from(val: &'a Vector<T, N>) -> Self {
357 &val.0
358 }
359}
360
361impl<'a, T, const N: usize> From<&'a mut Vector<T, N>> for &'a mut [T] {
362 fn from(val: &'a mut Vector<T, N>) -> Self {
363 &mut val.0
364 }
365}
366
367impl<I, T, const N: usize> Index<I> for Vector<T, N>
368where
369 [T; N]: Index<I>,
370{
371 type Output = <[T; N] as Index<I>>::Output;
372 fn index(&self, index: I) -> &Self::Output {
373 &self.0[index]
374 }
375}
376
377impl<I, T, const N: usize> IndexMut<I> for Vector<T, N>
378where
379 [T; N]: IndexMut<I>,
380{
381 fn index_mut(&mut self, index: I) -> &mut Self::Output {
382 &mut self.0[index]
383 }
384}
385
386impl<T, const N: usize> Sum for Vector<T, N>
387where
388 T: Add<Output = T> + Zero + Copy,
389{
390 fn sum<I>(iter: I) -> Self
391 where
392 I: Iterator<Item = Self>,
393 {
394 iter.fold(Self::zero(), |acc, v| acc + v)
395 }
396}
397
398mod point;
399
400pub use point::Point;
401
402#[cfg(feature = "parsable")]
403mod parsable;