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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2022 Stefan Zobel
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! SMatrix * HMatrix -> SMatrix
//!
//! HMatrix * HMatrix -> SMatrix
//!
//! HMatrix * SMatrix -> SMatrix

use crate::matrix::*;
use crate::matrix_mul::multiply;

impl<T: Numeric<T>, const ROWS_LEFT: usize, const COLS_LEFT: usize>
    SMatrix<T, ROWS_LEFT, COLS_LEFT>
{
    //noinspection ALL
    /// Multiply a stack matrix with a heap matrix and allocate the result on the stack.
    #[inline]
    pub fn mul_val_to_stack<const COLS_RIGHT: usize>(
        &self,
        rhs: HMatrix<T, COLS_LEFT, COLS_RIGHT>,
    ) -> SMatrix<T, ROWS_LEFT, COLS_RIGHT> {
        let mut c = MF::<T, ROWS_LEFT, COLS_RIGHT>::new_stack();
        multiply(self.array(), rhs.array(), c.array_mut());
        c
    }

    //noinspection ALL
    /// Multiply a stack matrix with a heap matrix and allocate the result on the stack.
    #[inline]
    pub fn mul_ref_to_stack<const COLS_RIGHT: usize>(
        &self,
        rhs: &HMatrix<T, COLS_LEFT, COLS_RIGHT>,
    ) -> SMatrix<T, ROWS_LEFT, COLS_RIGHT> {
        let mut c = MF::<T, ROWS_LEFT, COLS_RIGHT>::new_stack();
        multiply(self.array(), rhs.array(), c.array_mut());
        c
    }
}

impl<T: Numeric<T>, const ROWS_LEFT: usize, const COLS_LEFT: usize>
    HMatrix<T, ROWS_LEFT, COLS_LEFT>
{
    //noinspection ALL
    /// Multiply a heap matrix with a heap matrix and allocate the result on the stack.
    #[inline]
    pub fn mul_heapval_to_stack<const COLS_RIGHT: usize>(
        &self,
        rhs: HMatrix<T, COLS_LEFT, COLS_RIGHT>,
    ) -> SMatrix<T, ROWS_LEFT, COLS_RIGHT> {
        let mut c = MF::<T, ROWS_LEFT, COLS_RIGHT>::new_stack();
        multiply(self.array(), rhs.array(), c.array_mut());
        c
    }

    //noinspection ALL
    /// Multiply a heap matrix with a heap matrix and allocate the result on the stack.
    #[inline]
    pub fn mul_heapref_to_stack<const COLS_RIGHT: usize>(
        &self,
        rhs: &HMatrix<T, COLS_LEFT, COLS_RIGHT>,
    ) -> SMatrix<T, ROWS_LEFT, COLS_RIGHT> {
        let mut c = MF::<T, ROWS_LEFT, COLS_RIGHT>::new_stack();
        multiply(self.array(), rhs.array(), c.array_mut());
        c
    }

    /// Multiply a heap matrix with a stack matrix and allocate the result on the stack.
    #[inline]
    pub fn mul_stackval_to_stack<const COLS_RIGHT: usize>(
        &self,
        rhs: SMatrix<T, COLS_LEFT, COLS_RIGHT>,
    ) -> SMatrix<T, ROWS_LEFT, COLS_RIGHT> {
        let mut c = MF::<T, ROWS_LEFT, COLS_RIGHT>::new_stack();
        multiply(self.array(), rhs.array(), c.array_mut());
        c
    }

    /// Multiply a heap matrix with a stack matrix and allocate the result on the stack.
    #[inline]
    pub fn mul_stackref_to_stack<const COLS_RIGHT: usize>(
        &self,
        rhs: &SMatrix<T, COLS_LEFT, COLS_RIGHT>,
    ) -> SMatrix<T, ROWS_LEFT, COLS_RIGHT> {
        let mut c = MF::<T, ROWS_LEFT, COLS_RIGHT>::new_stack();
        multiply(self.array(), rhs.array(), c.array_mut());
        c
    }
}

#[cfg(test)]
mod mul_to_heap_tests {
    use super::*;

    #[test]
    fn test_smatrix_mul_to_stack() {
        let a = MF::<f64, 1, 100>::new_stack();
        let by_value = MF::<f64, 100, 1>::new_heap();
        let by_ref = &MF::<f64, 100, 1>::new_heap();
        let by_mut_ref = &mut MF::<f64, 100, 1>::new_heap();

        let _c = a.mul_val_to_stack(by_value);
        let _d = a.mul_ref_to_stack(by_ref);
        let _e = a.mul_ref_to_stack(by_mut_ref);
    }

    #[test]
    fn test_hmatrix_mul_hmatrix_to_stack() {
        let a = MF::<f64, 1, 100>::new_heap();
        let by_value = MF::<f64, 100, 1>::new_heap();
        let by_ref = &MF::<f64, 100, 1>::new_heap();
        let by_mut_ref = &mut MF::<f64, 100, 1>::new_heap();

        let _c = a.mul_heapval_to_stack(by_value);
        let _d = a.mul_heapref_to_stack(by_ref);
        let _e = a.mul_heapref_to_stack(by_mut_ref);
    }

    #[test]
    fn test_hmatrix_mul_smatrix_to_stack() {
        let a = MF::<f64, 1, 100>::new_heap();
        let by_value = MF::<f64, 100, 1>::new_stack();
        let by_ref = &MF::<f64, 100, 1>::new_stack();
        let by_mut_ref = &mut MF::<f64, 100, 1>::new_stack();

        let _c = a.mul_stackval_to_stack(by_value);
        let _d = a.mul_stackref_to_stack(by_ref);
        let _e = a.mul_stackref_to_stack(by_mut_ref);
    }
}