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 135 136 137 138 139 140 141 142 143 144 145 146 147
use crate::matrix::Matrix; use crate::number::{c64, Number}; use blas::{dgemm, zgemm}; use rayon::prelude::*; use std::mem::transmute; use std::ops::Mul; fn mul<T>(slf: T, rhs: Matrix<T>) -> Matrix<T> where T: Number, { let mut rhs = rhs; rhs.elements .par_iter_mut() .map(|r| { *r *= slf; }) .collect::<Vec<_>>(); rhs } fn mul_f64(lhs: &Matrix, rhs: &Matrix) -> Matrix { if lhs.columns != rhs.rows { panic!("dimension mismatch") } let m = lhs.rows as i32; let n = lhs.columns as i32; let k = rhs.columns as i32; let mut new_matrix = Matrix::zeros(lhs.rows, rhs.columns); unsafe { dgemm( 'N' as u8, 'N' as u8, k, n, m, 1.0, rhs.elements.as_slice(), k, lhs.elements.as_slice(), m, 0.0, &mut new_matrix.elements, k, ); } new_matrix } fn mul_c64(lhs: &Matrix<c64>, rhs: &Matrix<c64>) -> Matrix<c64> { if lhs.columns != rhs.rows { panic!("dimension mismatch") } let m = lhs.rows as i32; let n = lhs.columns as i32; let k = rhs.columns as i32; let mut new_matrix = Matrix::<c64>::zeros(lhs.rows, rhs.columns); unsafe { zgemm( 'N' as u8, 'N' as u8, k, n, m, blas::c64::new(1.0, 0.0), transmute::<&[c64], &[blas::c64]>(&rhs.elements), k, transmute::<&[c64], &[blas::c64]>(&lhs.elements), m, blas::c64::new(0.0, 0.0), transmute::<&mut [c64], &mut [blas::c64]>(&mut new_matrix.elements), k, ); } new_matrix } macro_rules! impl_mul_scalar { {$t: ty} => { impl Mul<Matrix<$t>> for $t { type Output = Matrix<$t>; fn mul(self, rhs: Matrix<$t>) -> Self::Output { mul(self, rhs) } } impl Mul<$t> for Matrix<$t> { type Output = Matrix<$t>; fn mul(self, rhs: $t) -> Self::Output { mul(rhs, self) } } }; } impl_mul_scalar! {f64} impl_mul_scalar! {c64} macro_rules! impl_mul { {$t: ty, $e: expr} => { impl Mul<Matrix<$t>> for Matrix<$t> { type Output = Matrix<$t>; fn mul(self, rhs: Matrix<$t>) -> Self::Output { $e(&self, &rhs) } } impl Mul<&Matrix<$t>> for Matrix<$t> { type Output = Matrix<$t>; fn mul(self, rhs: &Matrix<$t>) -> Self::Output { $e(&self, rhs) } } impl Mul<Matrix<$t>> for &Matrix<$t> { type Output = Matrix<$t>; fn mul(self, rhs: Matrix<$t>) -> Self::Output { $e(self, &rhs) } } impl Mul<&Matrix<$t>> for &Matrix<$t> { type Output = Matrix<$t>; fn mul(self, rhs: &Matrix<$t>) -> Self::Output { $e(self, rhs) } } }; } impl_mul! {f64, mul_f64} impl_mul! {c64, mul_c64}