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
|
{-# LANGUAGE DeriveFunctor #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Tests.Matrix.Types
(
Mat(..)
, fromMat
, toMat
, arbMat
, arbMatWith
) where
import Control.Monad (join)
import Control.Applicative ((<$>), (<*>))
import Statistics.Matrix (Matrix(..), fromList)
import Test.QuickCheck
import Tests.Helpers (shrinkFixedList, small)
import qualified Data.Vector.Unboxed as U
data Mat a = Mat { mrows :: Int , mcols :: Int
, asList :: [[a]] }
deriving (Eq, Ord, Show, Functor)
fromMat :: Mat Double -> Matrix
fromMat (Mat r c xs) = fromList r c (concat xs)
toMat :: Matrix -> Mat Double
toMat (Matrix r c v) = Mat r c . split . U.toList $ v
where split xs@(_:_) = let (h,t) = splitAt c xs
in h : split t
split [] = []
instance (Arbitrary a) => Arbitrary (Mat a) where
arbitrary = small $ join (arbMat <$> arbitrary <*> arbitrary)
shrink (Mat r c xs) = Mat r c <$> shrinkFixedList (shrinkFixedList shrink) xs
arbMat
:: (Arbitrary a)
=> Positive (Small Int)
-> Positive (Small Int)
-> Gen (Mat a)
arbMat r c = arbMatWith r c arbitrary
arbMatWith
:: (Arbitrary a)
=> Positive (Small Int)
-> Positive (Small Int)
-> Gen a
-> Gen (Mat a)
arbMatWith (Positive (Small r)) (Positive (Small c)) genA =
Mat r c <$> vectorOf r (vectorOf c genA)
instance Arbitrary Matrix where
arbitrary = fromMat <$> arbitrary
-- shrink = map fromMat . shrink . toMat
|