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
use crate::algebra::{
    abstr::{Field, Scalar},
    linear::{matrix::General, vector::Vector},
};
use std::ops::Mul;

impl<T> Mul<General<T>> for Vector<T>
where
    T: Field + Scalar,
{
    type Output = Vector<T>;

    fn mul(self, rhs: General<T>) -> Self::Output {
        &self * &rhs
    }
}

impl<'a, 'b, T> Mul<&'b General<T>> for &'a Vector<T>
where
    T: Field + Scalar,
{
    type Output = Vector<T>;

    fn mul(self, rhs: &'b General<T>) -> Self::Output {
        let (rhs_m, rhs_n): (usize, usize) = rhs.dim();
        let (_m, n): (usize, usize) = self.dim();

        if n != rhs_m {
            panic!("Vector and matrix dimensions do not match");
        }

        let mut res: Vec<T> = Vec::with_capacity(rhs_n);

        for i in 0..rhs_n {
            let mut sum: T = T::zero();
            for k in 0..rhs_m {
                sum += self[k] * rhs[[k, i]];
            }
            res.push(sum);
        }

        Vector::new_row(res)
    }
}

impl<T> Mul<T> for Vector<T>
where
    T: Field + Scalar,
{
    type Output = Vector<T>;

    /// Multiplies the vector elements with a scalar value
    ///
    /// # Example
    ///
    /// ```
    /// use mathru::algebra::linear::vector::Vector;
    ///
    /// let a: Vector<f64> = Vector::new_column(vec![1.0, 2.0, 3.0, 4.0]);
    /// let res_ref: Vector<f64> = Vector::new_column(vec![-5.0, -10.0, -15.0, -20.0]);
    ///
    /// assert_eq!(res_ref, a * -5.0)
    /// ```
    fn mul(self, rhs: T) -> Self::Output {
        Vector {
            data: &self.data * (&rhs),
        }
    }
}

impl<'a, T> Mul<&T> for &'a Vector<T>
where
    T: Field + Scalar,
{
    type Output = Vector<T>;

    /// Multiplies the vector elements with the scalar value
    ///
    /// # Example
    ///
    /// ```
    /// use mathru::algebra::linear::vector::Vector;
    ///
    /// let a: Vector<f64> = Vector::new_column(vec![1.0, 2.0, 3.0, 4.0]);
    /// let res_ref: Vector<f64> = Vector::new_column(vec![5.0, 10.0, 15.0, 20.0]);
    ///
    /// assert_eq!(res_ref, &a * &5.0)
    /// ```
    fn mul(self, rhs: &T) -> Self::Output {
        Vector {
            data: (&self.data).mul(rhs),
        }
    }
}

impl<'a, 'b, T> Mul<&'b T> for &'a mut Vector<T>
where
    T: Field + Scalar,
{
    type Output = Self;

    /// Multiplies the vector elements with the scalar value
    ///
    /// # Example
    ///
    /// ```
    /// use mathru::algebra::linear::vector::Vector;
    ///
    /// let mut a: Vector<f64> = Vector::new_column(vec![1.0, 2.0, 3.0, 4.0]);
    /// let res_ref: Vector<f64> = Vector::new_column(vec![5.0, 10.0, 15.0, 20.0]);
    ///
    /// assert_eq!(res_ref, *(&mut a * &5.0))
    /// ```
    fn mul(self, rhs: &'b T) -> Self::Output {
        let _ = &mut self.data * rhs;
        self
    }
}