math_ops/
operations.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Arithmetic operations for `Vector<T>`.

use crate::vector::Vector;
use num_traits::{Float, Num};
use std::ops::{Add, Div, Mul, Rem, Sub};

/// Trait providing arithmetic operations for `Vector<T>`.
pub trait VectorOps<T> {
  /// Adds another vector to this vector and returns a new vector.
  fn add_vec(&self, other: &Vector<T>) -> Vector<T>;

  /// Subtracts another vector from this vector and returns a new vector.
  fn sub_vec(&self, other: &Vector<T>) -> Vector<T>;

  /// Multiplies this vector with another vector element-wise and returns a new vector.
  fn mul_vec(&self, other: &Vector<T>) -> Vector<T>;

  /// Divides this vector by another vector element-wise and returns a new vector.
  fn div_vec(&self, other: &Vector<T>) -> Vector<T>;

  /// Computes the modulus of this vector by another vector element-wise and returns a new vector.
  fn rem_vec(&self, other: &Vector<T>) -> Vector<T>;

  /// Adds a scalar to each element of the vector and returns a new vector.
  fn add_scalar(&self, scalar: T) -> Vector<T>;

  /// Subtracts a scalar from each element of the vector and returns a new vector.
  fn sub_scalar(&self, scalar: T) -> Vector<T>;

  /// Multiplies each element of the vector by a scalar and returns a new vector.
  fn mul_scalar(&self, scalar: T) -> Vector<T>;

  /// Divides each element of the vector by a scalar and returns a new vector.
  fn div_scalar(&self, scalar: T) -> Vector<T>;

  /// Computes the modulus of each element of the vector by a scalar and returns a new vector.
  fn rem_scalar(&self, scalar: T) -> Vector<T>;
}

impl<T> VectorOps<T> for Vector<T>
where
  T: Num + Copy + PartialOrd + Float,
{
  fn add_vec(&self, other: &Vector<T>) -> Vector<T> {
    assert_eq!(
      self.len(),
      other.len(),
      "Vectors must be of the same length for addition."
    );
    let data = self
      .iter()
      .zip(other.iter())
      .map(|(&a, &b)| a + b)
      .collect();
    Vector::new(data)
  }

  fn sub_vec(&self, other: &Vector<T>) -> Vector<T> {
    assert_eq!(
      self.len(),
      other.len(),
      "Vectors must be of the same length for subtraction."
    );
    let data = self
      .iter()
      .zip(other.iter())
      .map(|(&a, &b)| a - b)
      .collect();
    Vector::new(data)
  }

  fn mul_vec(&self, other: &Vector<T>) -> Vector<T> {
    assert_eq!(
      self.len(),
      other.len(),
      "Vectors must be of the same length for multiplication."
    );
    let data = self
      .iter()
      .zip(other.iter())
      .map(|(&a, &b)| a * b)
      .collect();
    Vector::new(data)
  }

  fn div_vec(&self, other: &Vector<T>) -> Vector<T> {
    assert_eq!(
      self.len(),
      other.len(),
      "Vectors must be of the same length for division."
    );
    let data = self
      .iter()
      .zip(other.iter())
      .map(|(&a, &b)| a / b)
      .collect();
    Vector::new(data)
  }

  fn rem_vec(&self, other: &Vector<T>) -> Vector<T> {
    assert_eq!(
      self.len(),
      other.len(),
      "Vectors must be of the same length for modulus."
    );
    let data = self
      .iter()
      .zip(other.iter())
      .map(|(&a, &b)| a % b)
      .collect();
    Vector::new(data)
  }

  fn add_scalar(&self, scalar: T) -> Vector<T> {
    let data = self.iter().map(|&x| x + scalar).collect();
    Vector::new(data)
  }

  fn sub_scalar(&self, scalar: T) -> Vector<T> {
    let data = self.iter().map(|&x| x - scalar).collect();
    Vector::new(data)
  }

  fn mul_scalar(&self, scalar: T) -> Vector<T> {
    let data = self.iter().map(|&x| x * scalar).collect();
    Vector::new(data)
  }

  fn div_scalar(&self, scalar: T) -> Vector<T> {
    let data = self.iter().map(|&x| x / scalar).collect();
    Vector::new(data)
  }

  fn rem_scalar(&self, scalar: T) -> Vector<T> {
    let data = self.iter().map(|&x| x % scalar).collect();
    Vector::new(data)
  }
}

/// Implement operator overloading for `Vector<T> + Vector<T>`.
impl<T> Add for &Vector<T>
where
  T: Num + Copy + PartialOrd + Float,
{
  type Output = Vector<T>;

  fn add(self, rhs: &Vector<T>) -> Vector<T> {
    self.add_vec(rhs)
  }
}

/// Implement operator overloading for `Vector<T> - Vector<T>`.
impl<T> Sub for &Vector<T>
where
  T: Num + Copy + PartialOrd + Float,
{
  type Output = Vector<T>;

  fn sub(self, rhs: &Vector<T>) -> Vector<T> {
    self.sub_vec(rhs)
  }
}

/// Implement operator overloading for `Vector<T> * Vector<T>`.
impl<T> Mul for &Vector<T>
where
  T: Num + Copy + PartialOrd + Float,
{
  type Output = Vector<T>;

  fn mul(self, rhs: &Vector<T>) -> Vector<T> {
    self.mul_vec(rhs)
  }
}

/// Implement operator overloading for `Vector<T> / Vector<T>`.
impl<T> Div for &Vector<T>
where
  T: Num + Copy + PartialOrd + Float,
{
  type Output = Vector<T>;

  fn div(self, rhs: &Vector<T>) -> Vector<T> {
    self.div_vec(rhs)
  }
}

/// Implement operator overloading for `Vector<T> % Vector<T>`.
impl<T> Rem for &Vector<T>
where
  T: Num + Copy + PartialOrd + Float,
{
  type Output = Vector<T>;

  fn rem(self, rhs: &Vector<T>) -> Vector<T> {
    self.rem_vec(rhs)
  }
}