1use crate::vector::Vector;
4use num_traits::{Float, Num};
5use std::ops::{Add, Div, Mul, Rem, Sub};
6
7pub trait VectorOps<T> {
9 fn add_vec(&self, other: &Vector<T>) -> Vector<T>;
11
12 fn sub_vec(&self, other: &Vector<T>) -> Vector<T>;
14
15 fn mul_vec(&self, other: &Vector<T>) -> Vector<T>;
17
18 fn div_vec(&self, other: &Vector<T>) -> Vector<T>;
20
21 fn rem_vec(&self, other: &Vector<T>) -> Vector<T>;
23
24 fn add_scalar(&self, scalar: T) -> Vector<T>;
26
27 fn sub_scalar(&self, scalar: T) -> Vector<T>;
29
30 fn mul_scalar(&self, scalar: T) -> Vector<T>;
32
33 fn div_scalar(&self, scalar: T) -> Vector<T>;
35
36 fn rem_scalar(&self, scalar: T) -> Vector<T>;
38}
39
40impl<T> VectorOps<T> for Vector<T>
41where
42 T: Num + Copy + PartialOrd + Float,
43{
44 fn add_vec(&self, other: &Vector<T>) -> Vector<T> {
45 assert_eq!(
46 self.len(),
47 other.len(),
48 "Vectors must be of the same length for addition."
49 );
50 let data = self
51 .iter()
52 .zip(other.iter())
53 .map(|(&a, &b)| a + b)
54 .collect();
55 Vector::new(data)
56 }
57
58 fn sub_vec(&self, other: &Vector<T>) -> Vector<T> {
59 assert_eq!(
60 self.len(),
61 other.len(),
62 "Vectors must be of the same length for subtraction."
63 );
64 let data = self
65 .iter()
66 .zip(other.iter())
67 .map(|(&a, &b)| a - b)
68 .collect();
69 Vector::new(data)
70 }
71
72 fn mul_vec(&self, other: &Vector<T>) -> Vector<T> {
73 assert_eq!(
74 self.len(),
75 other.len(),
76 "Vectors must be of the same length for multiplication."
77 );
78 let data = self
79 .iter()
80 .zip(other.iter())
81 .map(|(&a, &b)| a * b)
82 .collect();
83 Vector::new(data)
84 }
85
86 fn div_vec(&self, other: &Vector<T>) -> Vector<T> {
87 assert_eq!(
88 self.len(),
89 other.len(),
90 "Vectors must be of the same length for division."
91 );
92 let data = self
93 .iter()
94 .zip(other.iter())
95 .map(|(&a, &b)| a / b)
96 .collect();
97 Vector::new(data)
98 }
99
100 fn rem_vec(&self, other: &Vector<T>) -> Vector<T> {
101 assert_eq!(
102 self.len(),
103 other.len(),
104 "Vectors must be of the same length for modulus."
105 );
106 let data = self
107 .iter()
108 .zip(other.iter())
109 .map(|(&a, &b)| a % b)
110 .collect();
111 Vector::new(data)
112 }
113
114 fn add_scalar(&self, scalar: T) -> Vector<T> {
115 let data = self.iter().map(|&x| x + scalar).collect();
116 Vector::new(data)
117 }
118
119 fn sub_scalar(&self, scalar: T) -> Vector<T> {
120 let data = self.iter().map(|&x| x - scalar).collect();
121 Vector::new(data)
122 }
123
124 fn mul_scalar(&self, scalar: T) -> Vector<T> {
125 let data = self.iter().map(|&x| x * scalar).collect();
126 Vector::new(data)
127 }
128
129 fn div_scalar(&self, scalar: T) -> Vector<T> {
130 let data = self.iter().map(|&x| x / scalar).collect();
131 Vector::new(data)
132 }
133
134 fn rem_scalar(&self, scalar: T) -> Vector<T> {
135 let data = self.iter().map(|&x| x % scalar).collect();
136 Vector::new(data)
137 }
138}
139
140impl<T> Add for &Vector<T>
142where
143 T: Num + Copy + PartialOrd + Float,
144{
145 type Output = Vector<T>;
146
147 fn add(self, rhs: &Vector<T>) -> Vector<T> {
148 self.add_vec(rhs)
149 }
150}
151
152impl<T> Sub for &Vector<T>
154where
155 T: Num + Copy + PartialOrd + Float,
156{
157 type Output = Vector<T>;
158
159 fn sub(self, rhs: &Vector<T>) -> Vector<T> {
160 self.sub_vec(rhs)
161 }
162}
163
164impl<T> Mul for &Vector<T>
166where
167 T: Num + Copy + PartialOrd + Float,
168{
169 type Output = Vector<T>;
170
171 fn mul(self, rhs: &Vector<T>) -> Vector<T> {
172 self.mul_vec(rhs)
173 }
174}
175
176impl<T> Div for &Vector<T>
178where
179 T: Num + Copy + PartialOrd + Float,
180{
181 type Output = Vector<T>;
182
183 fn div(self, rhs: &Vector<T>) -> Vector<T> {
184 self.div_vec(rhs)
185 }
186}
187
188impl<T> Rem for &Vector<T>
190where
191 T: Num + Copy + PartialOrd + Float,
192{
193 type Output = Vector<T>;
194
195 fn rem(self, rhs: &Vector<T>) -> Vector<T> {
196 self.rem_vec(rhs)
197 }
198}