module Utils_mmult where import Control.DeepSeq import Control.Parallel.Strategies import System.Random as Rand import Data.Vector as V hiding (MVector) import Data.Array.IArray as A import Prelude as P import Data.List as L type MMultType = Double type Matrix = Vector (Vector MMultType) type MVector = Vector MMultType randomRange :: (MMultType, MMultType) randomRange = (-100, 100) seedA :: Int seedA = 5 -- Más de 300x300 DPH usa demasiada memoria dimM = 600 dimN = 600 instance (NFData a) => NFData (Vector a) where rnf x = let eval = x `using` (evalTraversable rdeepseq) in seq eval () randomList :: [MMultType] randomList = Rand.randomRs randomRange (Rand.mkStdGen seedA) rMatrixAB :: (Matrix, Matrix) rMatrixAB = let (listA, listB) = L.splitAt (dimM*dimN) randomList in (rMatrixGen dimM dimN listA, rMatrixGen dimN dimM listB) rMatrixGen :: Int -> Int -> [MMultType] -> Matrix rMatrixGen m n rL = let (rL1, rL2) = L.splitAt n rL in if (m == 1) then V.singleton $ V.fromList rL1 else cons (V.fromList rL1) $ rMatrixGen (m-1) n rL2 matrixA :: Matrix matrixA = fst rMatrixAB matrixB :: Matrix matrixB = snd rMatrixAB dotp :: MVector -> MVector -> MMultType dotp row col = V.sum (V.zipWith (*) row col) transposeM :: Matrix -> Matrix transposeM m = let h = V.length m w = V.length (m V.! 0) in if h == 0 then V.empty else V.map (\y -> V.map (\x -> m V.! x V.! y) (V.generate h id)) (V.generate w id)