devela/num/alg/linear/vector/array/
ops.rs#![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};
impl<T: Clone + Add<Output = T>, const D: usize> Vector<T, D> {
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> {
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);
}
}
impl<T: Clone + Mul<Output = T>, const D: usize> Vector<T, D> {
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> {
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);
}
}