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
use super::super::iter::VectorIter;
use super::super::order::Order;
use super::super::shape::{IntoAxisShape, Shape};
use super::super::Matrix;
use crate::error::{Error, Result};

impl<L> Matrix<L> {
    /// Ensures that two matrices are conformable for multiplication-like
    /// operation.
    ///
    /// # Errors
    ///
    /// - [`Error::NotConformable`] if the matrices are not conformable.
    ///
    /// # Examples
    ///
    /// ```
    /// use matreex::{Error, Matrix};
    ///
    /// let lhs = Matrix::<i32>::new((2, 3));
    ///
    /// let rhs = Matrix::<i32>::new((3, 1));
    /// let result = lhs.ensure_multiplication_like_operation_conformable(&rhs);
    /// assert!(result.is_ok());
    ///
    /// let rhs = Matrix::<i32>::new((2, 3));
    /// let result = lhs.ensure_multiplication_like_operation_conformable(&rhs);
    /// assert_eq!(result, Err(Error::NotConformable));
    /// ```
    pub fn ensure_multiplication_like_operation_conformable<R>(
        &self,
        rhs: &Matrix<R>,
    ) -> Result<&Self> {
        if self.ncols() != rhs.nrows() {
            Err(Error::NotConformable)
        } else {
            Ok(self)
        }
    }

    /// Performs multiplication-like operation on two matrices.
    /// The operation can abort and fill the result with default
    /// values if `op` returns `None` at any point.
    ///
    /// # Errors
    ///
    /// - [`Error::NotConformable`] if the matrices are not conformable.
    ///
    /// # Notes
    ///
    /// The resulting matrix will always have the same order as `self`.
    ///
    /// # Examples
    ///
    /// ```
    /// use matreex::matrix;
    /// use matreex::matrix::arithmetic::vector_dot_product;
    ///
    /// let lhs = matrix![[0, 1, 2], [3, 4, 5]];
    /// let rhs = matrix![[0, 1], [2, 3], [4, 5]];
    ///
    /// let result = lhs.multiplication_like_operation(&rhs, vector_dot_product);
    /// assert_eq!(result, Ok(matrix![[10, 13], [28, 40]]));
    /// ```
    pub fn multiplication_like_operation<R, F, U>(
        &self,
        rhs: &Matrix<R>,
        mut op: F,
    ) -> Result<Matrix<U>>
    where
        F: FnMut(VectorIter<&L>, VectorIter<&R>) -> Option<U>,
        U: Default,
    {
        self.ensure_multiplication_like_operation_conformable(rhs)?;

        let nrows = self.nrows();
        let ncols = rhs.ncols();
        let order = self.order;
        let shape = Shape::new(nrows, ncols).try_into_axis_shape(order)?;
        let size = shape.size();
        let mut data = Vec::with_capacity(size);
        match order {
            Order::RowMajor => {
                'outer: for row in 0..nrows {
                    for col in 0..ncols {
                        let row_vector = unsafe { self.iter_nth_row_unchecked(row) };
                        let col_vector = unsafe { rhs.iter_nth_col_unchecked(col) };
                        match op(row_vector, col_vector) {
                            None => {
                                data.clear();
                                data.resize_with(size, U::default);
                                break 'outer;
                            }
                            Some(value) => data.push(value),
                        }
                    }
                }
            }
            Order::ColMajor => {
                'outer: for col in 0..ncols {
                    for row in 0..nrows {
                        let row_vector = unsafe { self.iter_nth_row_unchecked(row) };
                        let col_vector = unsafe { rhs.iter_nth_col_unchecked(col) };
                        match op(row_vector, col_vector) {
                            None => {
                                data.clear();
                                data.resize_with(size, U::default);
                                break 'outer;
                            }
                            Some(value) => data.push(value),
                        }
                    }
                }
            }
        }

        Ok(Matrix { data, order, shape })
    }
}