devela/num/alg/linear/vector/array/
ops.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// devela::num::alg::linear::vector::array::ops
//
//! implement overloadable operators
//
// NOTE: Because of T: Clone bound the `_assign` ops take a reference to Rhs.

#![expect(clippy::needless_range_loop, reason = "prefer using for loops")]

use super::super::Vector;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

/* ops between vectors */

impl<T: Clone + Add<Output = T>, const D: usize> Vector<T, D> {
    /// Adds two vectors together
    pub fn clone_add(&self, other: &Self) -> Self {
        let mut coords: [T; D] = self.coords.clone();
        for i in 0..D {
            coords[i] = self.coords[i].clone() + other.coords[i].clone();
        }
        Vector { coords }
    }
}
impl<T: Clone + Add<Output = T>, const D: usize> Add for Vector<T, D> {
    type Output = Self;
    fn add(self, other: Self) -> Self::Output {
        Self::clone_add(&self, &other)
    }
}
impl<T: Clone + Add<Output = T>, const D: usize> AddAssign<&Self> for Vector<T, D> {
    fn add_assign(&mut self, other: &Self) {
        *self = Self::clone_add(self, other);
    }
}

impl<T: Clone + Sub<Output = T>, const D: usize> Vector<T, D> {
    /// Subtracts two vectors together.
    pub fn clone_sub(&self, other: &Self) -> Self {
        let mut coords: [T; D] = self.coords.clone();
        for i in 0..D {
            coords[i] = self.coords[i].clone() - other.coords[i].clone();
        }
        Vector { coords }
    }
}
impl<T: Clone + Sub<Output = T>, const D: usize> Sub for Vector<T, D> {
    type Output = Self;
    fn sub(self, other: Self) -> Self::Output {
        Self::clone_sub(&self, &other)
    }
}
impl<T: Clone + Sub<Output = T>, const D: usize> SubAssign<&Self> for Vector<T, D> {
    fn sub_assign(&mut self, other: &Self) {
        *self = Self::clone_sub(self, other);
    }
}

/* ops between vectors and scalars */

impl<T: Clone + Mul<Output = T>, const D: usize> Vector<T, D> {
    /// Multiplies a vector by a scalar.
    pub fn clone_mul_scalar(&self, scalar: &T) -> Self {
        let mut coords: [T; D] = self.coords.clone();
        for i in 0..D {
            coords[i] = self.coords[i].clone() * scalar.clone();
        }
        Vector { coords }
    }
}
impl<T: Clone + Mul<Output = T>, const D: usize> Mul<T> for Vector<T, D> {
    type Output = Self;
    fn mul(self, scalar: T) -> Self::Output {
        Self::clone_mul_scalar(&self, &scalar)
    }
}
impl<T: Clone + Mul<Output = T>, const D: usize> MulAssign<T> for Vector<T, D> {
    fn mul_assign(&mut self, scalar: T) {
        *self = Self::clone_mul_scalar(self, &scalar);
    }
}
impl<T: Clone + Mul<Output = T>, const D: usize> Mul<&T> for Vector<T, D> {
    type Output = Self;
    fn mul(self, scalar: &T) -> Self::Output {
        Self::clone_mul_scalar(&self, scalar)
    }
}
impl<T: Clone + Mul<Output = T>, const D: usize> MulAssign<&T> for Vector<T, D> {
    fn mul_assign(&mut self, scalar: &T) {
        *self = Self::clone_mul_scalar(self, scalar);
    }
}

impl<T: Clone + Div<Output = T>, const D: usize> Vector<T, D> {
    /// Divides a vector by a scalar.
    pub fn clone_div_scalar(&self, scalar: &T) -> Self {
        let mut coords: [T; D] = self.coords.clone();
        for i in 0..D {
            coords[i] = self.coords[i].clone() / scalar.clone();
        }
        Vector { coords }
    }
}
impl<T: Clone + Div<Output = T>, const D: usize> Div<T> for Vector<T, D> {
    type Output = Self;
    fn div(self, scalar: T) -> Self::Output {
        Self::clone_div_scalar(&self, &scalar)
    }
}
impl<T: Clone + Div<Output = T>, const D: usize> DivAssign<T> for Vector<T, D> {
    fn div_assign(&mut self, scalar: T) {
        *self = Self::clone_div_scalar(self, &scalar);
    }
}
impl<T: Clone + Div<Output = T>, const D: usize> Div<&T> for Vector<T, D> {
    type Output = Self;
    fn div(self, scalar: &T) -> Self::Output {
        Self::clone_div_scalar(&self, scalar)
    }
}
impl<T: Clone + Div<Output = T>, const D: usize> DivAssign<&T> for Vector<T, D> {
    fn div_assign(&mut self, scalar: &T) {
        *self = Self::clone_div_scalar(self, scalar);
    }
}