From 2747c2ba403dafcc52bac09d2fd51b6d66fa5e9e Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Sun, 3 Feb 2008 12:52:17 +0000 Subject: test modules reorganized --- lib/Numeric/LinearAlgebra/Testing.hs | 98 --------------------------- lib/Numeric/LinearAlgebra/Tests/Instances.hs | 70 +++++++++++++++++++ lib/Numeric/LinearAlgebra/Tests/Properties.hs | 50 ++++++++++++++ 3 files changed, 120 insertions(+), 98 deletions(-) delete mode 100644 lib/Numeric/LinearAlgebra/Testing.hs create mode 100644 lib/Numeric/LinearAlgebra/Tests/Instances.hs create mode 100644 lib/Numeric/LinearAlgebra/Tests/Properties.hs (limited to 'lib/Numeric') diff --git a/lib/Numeric/LinearAlgebra/Testing.hs b/lib/Numeric/LinearAlgebra/Testing.hs deleted file mode 100644 index dcf1d8e..0000000 --- a/lib/Numeric/LinearAlgebra/Testing.hs +++ /dev/null @@ -1,98 +0,0 @@ -{-# OPTIONS_GHC -XPatternSignatures #-} ------------------------------------------------------------------------------ -{- | -Module : Numeric.LinearAlgebra.Testing -Copyright : (c) Alberto Ruiz 2007 -License : GPL-style - -Maintainer : Alberto Ruiz (aruiz at um dot es) -Stability : provisional -Portability : portable - -Some consistency tests. - --} - -module Numeric.LinearAlgebra.Testing( - runTests, runBigTests -) where - -import Numeric.LinearAlgebra -import Test.QuickCheck -import Debug.Trace - -qCheck n = check defaultConfig {configSize = const n} - -debug x = trace (show x) x - -type RM = Matrix Double -type CM = Matrix (Complex Double) - -instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where - arbitrary = do - r <- arbitrary - i <- arbitrary - return (r:+i) - coarbitrary = undefined - -chooseDim = sized $ \m -> choose (1,max 1 m) - -instance (Field a, Arbitrary a) => Arbitrary (Vector a) where - arbitrary = do m <- chooseDim - l <- vector m - return $ fromList l - coarbitrary = undefined - -instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where - arbitrary = do - m <- chooseDim - n <- chooseDim - l <- vector (m*n) - return $ (m> Arbitrary (Sq a) where - arbitrary = do - n <- chooseDim - l <- vector (n*n) - return $ Sq $ (n> Arbitrary (Rot a) where - arbitrary = do - Sq m <- arbitrary - let (q,_) = qr m - return (Rot q) - coarbitrary = undefined - -newtype (Her a) = Her (Matrix a) deriving Show -her (Her a) = a - -instance (Field a, Arbitrary a) => Arbitrary (Her a) where - arbitrary = do - Sq m <- arbitrary - let m' = m/2 - return $ Her (m' + ctrans m') - coarbitrary = undefined - -------------------------------------------------------------------- - -herR x = her x :: RM - --- | It runs all the tests. -runTests :: Int -- ^ maximum dimension - -> IO () -runTests n = do - qCheck n (\(Her (m::CM))-> m == ctrans m) - qCheck n $ (\m->m==ctrans m) . herR - --- | Some additional tests on big matrices. They take a few minutes. -runBigTests :: IO () -runBigTests = undefined diff --git a/lib/Numeric/LinearAlgebra/Tests/Instances.hs b/lib/Numeric/LinearAlgebra/Tests/Instances.hs new file mode 100644 index 0000000..fececdb --- /dev/null +++ b/lib/Numeric/LinearAlgebra/Tests/Instances.hs @@ -0,0 +1,70 @@ +{-# OPTIONS #-} +----------------------------------------------------------------------------- +{- | +Module : Numeric.LinearAlgebra.Tests.Instances +Copyright : (c) Alberto Ruiz 2008 +License : GPL-style + +Maintainer : Alberto Ruiz (aruiz at um dot es) +Stability : provisional +Portability : portable + +Arbitrary instances for vectors, matrices. + +-} + +module Numeric.LinearAlgebra.Tests.Instances( + Sq(..), + Rot(..), + Her(..) +) where + +import Numeric.LinearAlgebra +import Test.QuickCheck + +instance (Arbitrary a, RealFloat a) => Arbitrary (Complex a) where + arbitrary = do + r <- arbitrary + i <- arbitrary + return (r:+i) + coarbitrary = undefined + +chooseDim = sized $ \m -> choose (1,max 1 m) + +instance (Field a, Arbitrary a) => Arbitrary (Vector a) where + arbitrary = do m <- chooseDim + l <- vector m + return $ fromList l + coarbitrary = undefined + +instance (Element a, Arbitrary a) => Arbitrary (Matrix a) where + arbitrary = do + m <- chooseDim + n <- chooseDim + l <- vector (m*n) + return $ (m> Arbitrary (Sq a) where + arbitrary = do + n <- chooseDim + l <- vector (n*n) + return $ Sq $ (n> Arbitrary (Rot a) where + arbitrary = do + Sq m <- arbitrary + let (q,_) = qr m + return (Rot q) + coarbitrary = undefined + +newtype (Her a) = Her (Matrix a) deriving Show +instance (Field a, Arbitrary a) => Arbitrary (Her a) where + arbitrary = do + Sq m <- arbitrary + let m' = m/2 + return $ Her (m' + ctrans m') + coarbitrary = undefined diff --git a/lib/Numeric/LinearAlgebra/Tests/Properties.hs b/lib/Numeric/LinearAlgebra/Tests/Properties.hs new file mode 100644 index 0000000..ad31454 --- /dev/null +++ b/lib/Numeric/LinearAlgebra/Tests/Properties.hs @@ -0,0 +1,50 @@ +{-# OPTIONS #-} +----------------------------------------------------------------------------- +{- | +Module : Numeric.LinearAlgebra.Tests.Properties +Copyright : (c) Alberto Ruiz 2008 +License : GPL-style + +Maintainer : Alberto Ruiz (aruiz at um dot es) +Stability : provisional +Portability : portable + +Arbitrary instances for vectors, matrices. + +-} + +module Numeric.LinearAlgebra.Tests.Properties + +where + +import Numeric.LinearAlgebra +import Numeric.LinearAlgebra.Tests.Instances(Sq(..),Her(..),Rot(..)) + +-- relative error +dist :: (Normed t, Num t) => t -> t -> Double +dist a b = r + where norm = pnorm Infinity + na = norm a + nb = norm b + nab = norm (a-b) + mx = max na nb + mn = min na nb + r = if mn < eps + then mx + else nab/mx + +infixl 4 |~| +--a |~| b = a :~10~: b +a |~| b = dist a b < 10^^(-10) + +--data Aprox a = (:~) a Int +--(~:) :: (Normed a, Num a) => Aprox a -> a -> Bool +--a :~n~: b = dist a b < 10^^(-n) + +------------------------------------------------------ + +square m = rows m == cols m + +unitary m = square m && m <> ctrans m |~| ident (rows m) + +hermitian m = square m && m |~| ctrans m -- cgit v1.2.3