Skip to content

Commit 730ad08

Browse files
author
sigurd4
committed
Improve magintude_squared
1 parent 29a47f8 commit 730ad08

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/ops/_1d/magnitude.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,38 @@ use super::ArrayPartialMulDot;
55
#[const_trait]
66
pub trait ArrayPartialMagnitude<T, const N: usize>: ArrayPartialMulDot<T, N>
77
{
8-
fn try_magnitude_squared(self) -> Option<<T as Mul<T>>::Output>
8+
fn try_magnitude_squared(&self) -> Option<<T as Mul<T>>::Output>
99
where
1010
T: Mul<T, Output: AddAssign> + Copy;
11-
async fn try_magnitude_squared_async(self) -> Option<<T as Mul<T>>::Output>
11+
async fn try_magnitude_squared_async(&self) -> Option<<T as Mul<T>>::Output>
1212
where
1313
T: Mul<T, Output: AddAssign> + Copy;
1414
}
1515

1616
impl<T, const N: usize> ArrayPartialMagnitude<T, N> for [T; N]
1717
{
18-
fn try_magnitude_squared(self) -> Option<<T as Mul<T>>::Output>
18+
fn try_magnitude_squared(&self) -> Option<<T as Mul<T>>::Output>
1919
where
2020
T: Mul<T, Output: AddAssign> + Copy
2121
{
22-
self.try_mul_dot(self)
22+
if N == 0
23+
{
24+
return None;
25+
}
26+
27+
let mut y = self[0]*self[0];
28+
let mut i = 1;
29+
while i < N
30+
{
31+
y += self[i]*self[i];
32+
i += 1
33+
}
34+
Some(y)
2335
}
24-
async fn try_magnitude_squared_async(self) -> Option<<T as Mul<T>>::Output>
36+
async fn try_magnitude_squared_async(&self) -> Option<<T as Mul<T>>::Output>
2537
where
2638
T: Mul<T, Output: AddAssign> + Copy
2739
{
28-
self.try_mul_dot_async(self).await
40+
self.try_mul_dot_async(*self).await
2941
}
3042
}

0 commit comments

Comments
 (0)