From dcc03a4a764cb8683b80758af97fcbcc9aadba73 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Thu, 8 Jan 2015 16:15:29 +0100 Subject: wip on tests --- packages/tests/hmatrix-tests.cabal | 6 +- packages/tests/src/Numeric/GSL/Tests.hs | 2 +- packages/tests/src/Numeric/LinearAlgebra/Tests.hs | 186 ++++++++++----------- .../src/Numeric/LinearAlgebra/Tests/Instances.hs | 19 +-- .../src/Numeric/LinearAlgebra/Tests/Properties.hs | 147 ++++++++-------- 5 files changed, 185 insertions(+), 175 deletions(-) (limited to 'packages/tests') diff --git a/packages/tests/hmatrix-tests.cabal b/packages/tests/hmatrix-tests.cabal index 0514843..de796e8 100644 --- a/packages/tests/hmatrix-tests.cabal +++ b/packages/tests/hmatrix-tests.cabal @@ -1,5 +1,5 @@ Name: hmatrix-tests -Version: 0.4.1.0 +Version: 0.5.0.0 License: BSD3 License-file: LICENSE Author: Alberto Ruiz @@ -28,9 +28,9 @@ library Build-Depends: base >= 4 && < 5, QuickCheck >= 2, HUnit, random, - hmatrix >= 0.16 + hmatrix >= 0.17 if flag(gsl) - Build-Depends: hmatrix-gsl >= 0.16 + Build-Depends: hmatrix-gsl >= 0.17 hs-source-dirs: src diff --git a/packages/tests/src/Numeric/GSL/Tests.hs b/packages/tests/src/Numeric/GSL/Tests.hs index 9dff6f5..2e225b6 100644 --- a/packages/tests/src/Numeric/GSL/Tests.hs +++ b/packages/tests/src/Numeric/GSL/Tests.hs @@ -19,7 +19,7 @@ import System.Exit (exitFailure) import Test.HUnit (runTestTT, failures, Test(..), errors) -import Numeric.LinearAlgebra +import Numeric.LinearAlgebra.HMatrix import Numeric.GSL import Numeric.LinearAlgebra.Tests (qCheck, utest) import Numeric.LinearAlgebra.Tests.Properties ((|~|), (~~)) diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs index 8587561..14d02e4 100644 --- a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs +++ b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs @@ -1,6 +1,9 @@ {-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-incomplete-patterns #-} {-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE GADTs #-} + ----------------------------------------------------------------------------- {- | @@ -25,12 +28,10 @@ module Numeric.LinearAlgebra.Tests( --, runBigTests ) where -import Numeric.LinearAlgebra -import Numeric.LinearAlgebra.HMatrix hiding ((<>),linearSolve) +import Numeric.LinearAlgebra.HMatrix +import Numeric.LinearAlgebra.Devel hiding (vec) +import Numeric.LinearAlgebra.Util hiding (ones) import Numeric.LinearAlgebra.Static(L) -import Numeric.LinearAlgebra.Util(col,row) -import Data.Packed -import Numeric.LinearAlgebra.LAPACK import Numeric.LinearAlgebra.Tests.Instances import Numeric.LinearAlgebra.Tests.Properties import Test.HUnit hiding ((~:),test,Testable,State) @@ -41,16 +42,13 @@ import qualified Prelude import System.CPUTime import System.Exit import Text.Printf -import Data.Packed.Development(unsafeFromForeignPtr,unsafeToForeignPtr) +import Numeric.LinearAlgebra.Devel(unsafeFromForeignPtr,unsafeToForeignPtr) import Control.Arrow((***)) import Debug.Trace import Control.Monad(when) -import Numeric.LinearAlgebra.Util hiding (ones,row,col) import Control.Applicative import Control.Monad(ap) -import Data.Packed.ST - import Test.QuickCheck(Arbitrary,arbitrary,coarbitrary,choose,vector ,sized,classify,Testable,Property ,quickCheckWithResult,maxSize,stdArgs,shrink) @@ -85,7 +83,7 @@ detTest1 = det m == 26 mc = (3><3) [ 1, 2, 3 , 4, 5, 7 - , 2, 8, i + , 2, 8, iC ] detTest2 = inv1 |~| inv2 && [det1] ~~ [det2] @@ -136,7 +134,7 @@ randomTestGaussian = c :~1~: snd (meanCov dat) where 2,4,0, -2,2,1] m = 3 |> [1,2,3] - c = a <> trans a + c = a <> tr a dat = gaussianSample 7 (10^6) m c randomTestUniform = c :~1~: snd (meanCov dat) where @@ -170,51 +168,51 @@ offsetTest = y == y' where normsVTest = TestList [ utest "normv2CD" $ norm2PropC v - , utest "normv2CF" $ norm2PropC (single v) +-- , utest "normv2CF" $ norm2PropC (single v) #ifndef NONORMVTEST , utest "normv2D" $ norm2PropR x - , utest "normv2F" $ norm2PropR (single x) +-- , utest "normv2F" $ norm2PropR (single x) #endif - , utest "normv1CD" $ norm1 v == 8 - , utest "normv1CF" $ norm1 (single v) == 8 - , utest "normv1D" $ norm1 x == 6 - , utest "normv1F" $ norm1 (single x) == 6 + , utest "normv1CD" $ norm_1 v == 8 +-- , utest "normv1CF" $ norm_1 (single v) == 8 + , utest "normv1D" $ norm_1 x == 6 +-- , utest "normv1F" $ norm_1 (single x) == 6 - , utest "normvInfCD" $ normInf v == 5 - , utest "normvInfCF" $ normInf (single v) == 5 - , utest "normvInfD" $ normInf x == 3 - , utest "normvInfF" $ normInf (single x) == 3 + , utest "normvInfCD" $ norm_Inf v == 5 +-- , utest "normvInfCF" $ norm_Inf (single v) == 5 + , utest "normvInfD" $ norm_Inf x == 3 +-- , utest "normvInfF" $ norm_Inf (single x) == 3 ] where v = fromList [1,-2,3:+4] :: Vector (Complex Double) x = fromList [1,2,-3] :: Vector Double #ifndef NONORMVTEST - norm2PropR a = norm2 a =~= sqrt (udot a a) + norm2PropR a = norm_2 a =~= sqrt (udot a a) #endif - norm2PropC a = norm2 a =~= realPart (sqrt (a <.> a)) + norm2PropC a = norm_2 a =~= realPart (sqrt (a `dot` a)) a =~= b = fromList [a] |~| fromList [b] normsMTest = TestList [ - utest "norm2mCD" $ pnorm PNorm2 v =~= 8.86164970498005 - , utest "norm2mCF" $ pnorm PNorm2 (single v) =~= 8.86164970498005 - , utest "norm2mD" $ pnorm PNorm2 x =~= 5.96667765076216 - , utest "norm2mF" $ pnorm PNorm2 (single x) =~= 5.96667765076216 - - , utest "norm1mCD" $ pnorm PNorm1 v == 9 - , utest "norm1mCF" $ pnorm PNorm1 (single v) == 9 - , utest "norm1mD" $ pnorm PNorm1 x == 7 - , utest "norm1mF" $ pnorm PNorm1 (single x) == 7 - - , utest "normmInfCD" $ pnorm Infinity v == 12 - , utest "normmInfCF" $ pnorm Infinity (single v) == 12 - , utest "normmInfD" $ pnorm Infinity x == 8 - , utest "normmInfF" $ pnorm Infinity (single x) == 8 - - , utest "normmFroCD" $ pnorm Frobenius v =~= 8.88819441731559 - , utest "normmFroCF" $ pnorm Frobenius (single v) =~~= 8.88819441731559 - , utest "normmFroD" $ pnorm Frobenius x =~= 6.24499799839840 - , utest "normmFroF" $ pnorm Frobenius (single x) =~~= 6.24499799839840 - - ] where v = (2><2) [1,-2*i,3:+4,7] :: Matrix (Complex Double) + utest "norm2mCD" $ norm_2 v =~= 8.86164970498005 +-- , utest "norm2mCF" $ norm_2 (single v) =~= 8.86164970498005 + , utest "norm2mD" $ norm_2 x =~= 5.96667765076216 +-- , utest "norm2mF" $ norm_2 (single x) =~= 5.96667765076216 + + , utest "norm1mCD" $ norm_1 v == 9 +-- , utest "norm1mCF" $ norm_1 (single v) == 9 + , utest "norm1mD" $ norm_1 x == 7 +-- , utest "norm1mF" $ norm_1 (single x) == 7 + + , utest "normmInfCD" $ norm_Inf v == 12 +-- , utest "normmInfCF" $ norm_Inf (single v) == 12 + , utest "normmInfD" $ norm_Inf x == 8 +-- , utest "normmInfF" $ norm_Inf (single x) == 8 + + , utest "normmFroCD" $ norm_Frob v =~= 8.88819441731559 +-- , utest "normmFroCF" $ norm_Frob (single v) =~~= 8.88819441731559 + , utest "normmFroD" $ norm_Frob x =~= 6.24499799839840 +-- , utest "normmFroF" $ norm_Frob (single x) =~~= 6.24499799839840 + + ] where v = (2><2) [1,-2*iC,3:+4,7] :: Matrix (Complex Double) x = (2><2) [1,2,-3,5] :: Matrix Double a =~= b = fromList [a] :~10~: fromList [b] a =~~= b = fromList [a] :~5~: fromList [b] @@ -232,7 +230,7 @@ sumprodTest = TestList [ , utest "prodD" $ prodProp v , utest "prodF" $ prodProp (single v) ] where v = fromList [1,2,3] :: Vector Double - z = fromList [1,2-i,3+i] + z = fromList [1,2-iC,3+iC] prodProp x = prodElements x == product (toList x) --------------------------------------------------------------------- @@ -246,7 +244,7 @@ chainTest = utest "chain" $ foldl1' (<>) ms |~| optimiseMult ms where --------------------------------------------------------------------- -conjuTest m = mapVector conjugate (flatten (trans m)) == flatten (ctrans m) +conjuTest m = cmap conjugate (flatten (conj (tr m))) == flatten (tr m) --------------------------------------------------------------------- @@ -302,7 +300,7 @@ lift_maybe m = MaybeT $ do -- apply a test to successive elements of a vector, evaluates to true iff test passes for all pairs --successive_ :: Storable a => (a -> a -> Bool) -> Vector a -> Bool -successive_ t v = maybe False (\_ -> True) $ evalState (runMaybeT (mapVectorM_ stp (subVector 1 (dim v - 1) v))) (v @> 0) +successive_ t v = maybe False (\_ -> True) $ evalState (runMaybeT (mapVectorM_ stp (subVector 1 (size v - 1) v))) (v ! 0) where stp e = do ep <- lift_maybe $ state_get if t e ep @@ -311,7 +309,7 @@ successive_ t v = maybe False (\_ -> True) $ evalState (runMaybeT (mapVectorM_ s -- operate on successive elements of a vector and return the resulting vector, whose length 1 less than that of the input --successive :: (Storable a, Storable b) => (a -> a -> b) -> Vector a -> Vector b -successive f v = evalState (mapVectorM stp (subVector 1 (dim v - 1) v)) (v @> 0) +successive f v = evalState (mapVectorM stp (subVector 1 (size v - 1) v)) (v ! 0) where stp e = do ep <- state_get state_put e @@ -380,12 +378,12 @@ kroneckerTest = utest "kronecker" ok x = (4><2) [3,5..] b = (2><5) [0,5..] v1 = vec (a <> x <> b) - v2 = (trans b `kronecker` a) <> vec x - s = trans b <> b + v2 = (tr b `kronecker` a) #> vec x + s = tr b <> b v3 = vec s - v4 = (dup 5 :: Matrix Double) <> vech s + v4 = (dup 5 :: Matrix Double) #> vech s ok = v1 == v2 && v3 == v4 - && vtrans 1 a == trans a + && vtrans 1 a == tr a && vtrans (rows a) a == asColumn (vec a) -------------------------------------------------------------------------------- @@ -430,11 +428,11 @@ runTests n = do test (multProp1 10 . cConsist) test (multProp2 10 . rConsist) test (multProp2 10 . cConsist) - putStrLn "------ mult Float" - test (multProp1 6 . (single *** single) . rConsist) - test (multProp1 6 . (single *** single) . cConsist) - test (multProp2 6 . (single *** single) . rConsist) - test (multProp2 6 . (single *** single) . cConsist) +-- putStrLn "------ mult Float" +-- test (multProp1 6 . (single *** single) . rConsist) +-- test (multProp1 6 . (single *** single) . cConsist) +-- test (multProp2 6 . (single *** single) . rConsist) +-- test (multProp2 6 . (single *** single) . cConsist) putStrLn "------ sub-trans" test (subProp . rM) test (subProp . cM) @@ -467,16 +465,16 @@ runTests n = do putStrLn "------ svd" test (svdProp1 . rM) test (svdProp1 . cM) - test (svdProp1a svdR) - test (svdProp1a svdC) - test (svdProp1a svdRd) - test (svdProp1b svdR) - test (svdProp1b svdC) - test (svdProp1b svdRd) - test (svdProp2 thinSVDR) - test (svdProp2 thinSVDC) - test (svdProp2 thinSVDRd) - test (svdProp2 thinSVDCd) + test (svdProp1a svd . rM) + test (svdProp1a svd . cM) +-- test (svdProp1a svdRd) + test (svdProp1b svd . rM) + test (svdProp1b svd . cM) +-- test (svdProp1b svdRd) + test (svdProp2 thinSVD . rM) + test (svdProp2 thinSVD . cM) +-- test (svdProp2 thinSVDRd) +-- test (svdProp2 thinSVDCd) test (svdProp3 . rM) test (svdProp3 . cM) test (svdProp4 . rM) @@ -489,10 +487,10 @@ runTests n = do test (svdProp7 . cM) putStrLn "------ svdCd" #ifdef NOZGESDD - putStrLn "Omitted" +-- putStrLn "Omitted" #else - test (svdProp1a svdCd) - test (svdProp1b svdCd) +-- test (svdProp1a svdCd) +-- test (svdProp1b svdCd) #endif putStrLn "------ eig" test (eigSHProp . rHer) @@ -510,10 +508,10 @@ runTests n = do test (qrProp . rM) test (qrProp . cM) test (rqProp . rM) - test (rqProp . cM) +-- test (rqProp . cM) test (rqProp1 . cM) test (rqProp2 . cM) - test (rqProp3 . cM) +-- test (rqProp3 . cM) putStrLn "------ hess" test (hessProp . rSq) test (hessProp . cSq) @@ -535,11 +533,11 @@ runTests n = do test (\u -> cos u * tan u |~| sin (u::RM)) test $ (\u -> cos u * tan u |~| sin (u::CM)) . liftMatrix makeUnitary putStrLn "------ vector operations - Float" - test (\u -> sin u ^ 2 + cos u ^ 2 |~~| (1::FM)) - test $ (\u -> sin u ^ 2 + cos u ^ 2 |~~| (1::ZM)) . liftMatrix makeUnitary - test (\u -> sin u ** 2 + cos u ** 2 |~~| (1::FM)) - test (\u -> cos u * tan u |~~| sin (u::FM)) - test $ (\u -> cos u * tan u |~~| sin (u::ZM)) . liftMatrix makeUnitary +-- test (\u -> sin u ^ 2 + cos u ^ 2 |~~| (1::FM)) +-- test $ (\u -> sin u ^ 2 + cos u ^ 2 |~~| (1::ZM)) . liftMatrix makeUnitary +-- test (\u -> sin u ** 2 + cos u ** 2 |~~| (1::FM)) +-- test (\u -> cos u * tan u |~~| sin (u::FM)) +-- test $ (\u -> cos u * tan u |~~| sin (u::ZM)) . liftMatrix makeUnitary putStrLn "------ read . show" test (\m -> (m::RM) == read (show m)) test (\m -> (m::CM) == read (show m)) @@ -557,8 +555,8 @@ runTests n = do , utest "expm1" (expmTest1) , utest "expm2" (expmTest2) , utest "arith1" $ ((ones (100,100) * 5 + 2)/0.5 - 7)**2 |~| (49 :: RM) - , utest "arith2" $ ((scalar (1+i) * ones (100,100) * 5 + 2)/0.5 - 7)**2 |~| ( scalar (140*i-51) :: CM) - , utest "arith3" $ exp (scalar i * ones(10,10)*pi) + 1 |~| 0 + , utest "arith2" $ ((scalar (1+iC) * ones (100,100) * 5 + 2)/0.5 - 7)**2 |~| ( scalar (140*iC-51) :: CM) + , utest "arith3" $ exp (scalar iC * ones(10,10)*pi) + 1 |~| 0 , utest "<\\>" $ (3><2) [2,0,0,3,1,1::Double] <\> 3|>[4,9,5] |~| 2|>[2,3] -- , utest "gamma" (gamma 5 == 24.0) -- , besselTest @@ -566,10 +564,10 @@ runTests n = do , utest "randomGaussian" randomTestGaussian , utest "randomUniform" randomTestUniform , utest "buildVector/Matrix" $ - complex (10 |> [0::Double ..]) == buildVector 10 fromIntegral - && ident 5 == buildMatrix 5 5 (\(r,c) -> if r==c then 1::Double else 0) - , utest "rank" $ rank ((2><3)[1,0,0,1,5*eps,0]) == 1 - && rank ((2><3)[1,0,0,1,7*eps,0]) == 2 + complex (10 |> [0::Double ..]) == build 10 id + && ident 5 == build (5,5) (\r c -> if r==c then 1::Double else 0) + , utest "rank" $ rank ((2><3)[1,0,0,1,5*peps,0::Double]) == 1 + && rank ((2><3)[1,0,0,1,7*peps,0::Double]) == 2 , utest "block" $ fromBlocks [[ident 3,0],[0,ident 4]] == (ident 7 :: CM) , mbCholTest , utest "offset" offsetTest @@ -597,7 +595,7 @@ a |~~| b = a :~6~: b makeUnitary v | realPart n > 1 = v / scalar n | otherwise = v - where n = sqrt (v <.> v) + where n = sqrt (v `dot` v) -- -- | Some additional tests on big matrices. They take a few minutes. -- runBigTests :: IO () @@ -663,9 +661,9 @@ manyvec5 xs = sumElements $ fromRows $ map (\x -> vec3 x (x**2) (x**3)) xs manyvec2 xs = sum $ map (\x -> sqrt(x^2 + (x**2)^2 +(x**3)^2)) xs -manyvec3 xs = sum $ map (pnorm PNorm2 . (\x -> fromList [x,x**2,x**3])) xs +manyvec3 xs = sum $ map (norm_2 . (\x -> fromList [x,x**2,x**3])) xs -manyvec4 xs = sum $ map (pnorm PNorm2 . (\x -> vec3 x (x**2) (x**3))) xs +manyvec4 xs = sum $ map (norm_2 . (\x -> vec3 x (x**2) (x**3))) xs vec3 :: Double -> Double -> Double -> Vector Double vec3 a b c = runSTVector $ do @@ -690,11 +688,11 @@ mkVecBench = do subBench = do putStrLn "" - let g = foldl1' (.) (replicate (10^5) (\v -> subVector 1 (dim v -1) v)) - time "0.1M subVector " (g (konst 1 (1+10^5) :: Vector Double) @> 0) + let g = foldl1' (.) (replicate (10^5) (\v -> subVector 1 (size v -1) v)) + time "0.1M subVector " (g (konst 1 (1+10^5) :: Vector Double) ! 0) let f = foldl1' (.) (replicate (10^5) (fromRows.toRows)) - time "subVector-join 3" (f (ident 3 :: Matrix Double) @@>(0,0)) - time "subVector-join 10" (f (ident 10 :: Matrix Double) @@>(0,0)) + time "subVector-join 3" (f (ident 3 :: Matrix Double) `atIndex` (0,0)) + time "subVector-join 10" (f (ident 10 :: Matrix Double) `atIndex` (0,0)) -------------------------------- @@ -719,7 +717,7 @@ multBench = do eigBench = do let m = reshape 1000 (randomVector 777 Uniform (1000*1000)) - s = m + trans m + s = m + tr m m `seq` s `seq` putStrLn "" time "eigenvalues symmetric 1000x1000" (eigenvaluesSH' m) time "eigenvectors symmetric 1000x1000" (snd $ eigSH' m) @@ -731,7 +729,7 @@ eigBench = do svdBench = do let a = reshape 500 (randomVector 777 Uniform (3000*500)) b = reshape 1000 (randomVector 777 Uniform (1000*1000)) - fv (_,_,v) = v@@>(0,0) + fv (_,_,v) = v `atIndex` (0,0) a `seq` b `seq` putStrLn "" time "singular values 3000x500" (singularValues a) time "thin svd 3000x500" (fv $ thinSVD a) @@ -743,7 +741,7 @@ svdBench = do solveBenchN n = do let x = uniformSample 777 (2*n) (replicate n (-1,1)) - a = trans x <> x + a = tr x <> x b = asColumn $ randomVector 666 Uniform n a `seq` b `seq` putStrLn "" time ("svd solve " ++ show n) (linearSolveSVD a b) @@ -760,7 +758,7 @@ solveBench = do cholBenchN n = do let x = uniformSample 777 (2*n) (replicate n (-1,1)) - a = trans x <> x + a = tr x <> x a `seq` putStr "" time ("chol " ++ show n) (chol a) diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs index 53fc4d2..e2c3840 100644 --- a/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs +++ b/packages/tests/src/Numeric/LinearAlgebra/Tests/Instances.hs @@ -26,9 +26,8 @@ module Numeric.LinearAlgebra.Tests.Instances( import System.Random -import Numeric.LinearAlgebra +import Numeric.LinearAlgebra.HMatrix hiding (vector) import Numeric.LinearAlgebra.Devel -import Numeric.Container import Control.Monad(replicateM) import Test.QuickCheck(Arbitrary,arbitrary,coarbitrary,choose,vector ,sized,classify,Testable,Property @@ -130,7 +129,7 @@ instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Her a) where arbitrary = do Sq m <- arbitrary let m' = m/2 - return $ Her (m' + ctrans m') + return $ Her (m' + tr m') #if MIN_VERSION_QuickCheck(2,0,0) #else @@ -144,7 +143,7 @@ instance ArbitraryField (Complex Double) -- a well-conditioned general matrix (the singular values are between 1 and 100) newtype (WC a) = WC (Matrix a) deriving Show -instance (ArbitraryField a) => Arbitrary (WC a) where +instance (Numeric a, ArbitraryField a) => Arbitrary (WC a) where arbitrary = do m <- arbitrary let (u,_,v) = svd m @@ -153,7 +152,7 @@ instance (ArbitraryField a) => Arbitrary (WC a) where n = min r c sv' <- replicateM n (choose (1,100)) let s = diagRect 0 (fromList sv') r c - return $ WC (u `mXm` real s `mXm` trans v) + return $ WC (u <> real s <> tr v) #if MIN_VERSION_QuickCheck(2,0,0) #else @@ -163,14 +162,14 @@ instance (ArbitraryField a) => Arbitrary (WC a) where -- a well-conditioned square matrix (the singular values are between 1 and 100) newtype (SqWC a) = SqWC (Matrix a) deriving Show -instance (ArbitraryField a) => Arbitrary (SqWC a) where +instance (ArbitraryField a, Numeric a) => Arbitrary (SqWC a) where arbitrary = do Sq m <- arbitrary let (u,_,v) = svd m n = rows m sv' <- replicateM n (choose (1,100)) let s = diag (fromList sv') - return $ SqWC (u `mXm` real s `mXm` trans v) + return $ SqWC (u <> real s <> tr v) #if MIN_VERSION_QuickCheck(2,0,0) #else @@ -180,7 +179,7 @@ instance (ArbitraryField a) => Arbitrary (SqWC a) where -- a positive definite square matrix (the eigenvalues are between 0 and 100) newtype (PosDef a) = PosDef (Matrix a) deriving Show -instance (ArbitraryField a, Num (Vector a)) +instance (Numeric a, ArbitraryField a, Num (Vector a)) => Arbitrary (PosDef a) where arbitrary = do Her m <- arbitrary @@ -188,8 +187,8 @@ instance (ArbitraryField a, Num (Vector a)) n = rows m l <- replicateM n (choose (0,100)) let s = diag (fromList l) - p = v `mXm` real s `mXm` ctrans v - return $ PosDef (0.5 * p + 0.5 * ctrans p) + p = v <> real s <> tr v + return $ PosDef (0.5 * p + 0.5 * tr p) #if MIN_VERSION_QuickCheck(2,0,0) #else diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs index 9bdf897..d9645c3 100644 --- a/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs +++ b/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs @@ -1,5 +1,7 @@ {-# LANGUAGE CPP, FlexibleContexts #-} {-# OPTIONS_GHC -fno-warn-unused-imports #-} +{-# LANGUAGE GADTs #-} + ----------------------------------------------------------------------------- {- | Module : Numeric.LinearAlgebra.Tests.Properties @@ -27,7 +29,7 @@ module Numeric.LinearAlgebra.Tests.Properties ( pinvProp, detProp, nullspaceProp, - bugProp, +-- bugProp, svdProp1, svdProp1a, svdProp1b, svdProp2, svdProp3, svdProp4, svdProp5a, svdProp5b, svdProp6a, svdProp6b, svdProp7, eigProp, eigSHProp, eigProp2, eigSHProp2, @@ -41,9 +43,7 @@ module Numeric.LinearAlgebra.Tests.Properties ( linearSolveProp, linearSolveProp2 ) where -import Numeric.Container -import Numeric.LinearAlgebra --hiding (real,complex) -import Numeric.LinearAlgebra.LAPACK +import Numeric.LinearAlgebra.HMatrix hiding (Testable)--hiding (real,complex) import Debug.Trace import Test.QuickCheck(Arbitrary,arbitrary,coarbitrary,choose,vector ,sized,classify,Testable,Property @@ -53,8 +53,8 @@ trivial :: Testable a => Bool -> a -> Property trivial = (`classify` "trivial") -- relative error -dist :: (Normed c t, Num (c t)) => c t -> c t -> Double -dist = relativeError Infinity +dist :: (Num a, Normed a) => a -> a -> Double +dist = relativeError norm_Inf infixl 4 |~| a |~| b = a :~10~: b @@ -71,11 +71,11 @@ a :~n~: b = dist a b < 10^^(-n) square m = rows m == cols m -- orthonormal columns -orthonormal m = ctrans m <> m |~| ident (cols m) +orthonormal m = tr m <> m |~| ident (cols m) unitary m = square m && orthonormal m -hermitian m = square m && m |~| ctrans m +hermitian m = square m && m |~| tr m wellCond m = rcond m > 1/100 @@ -83,12 +83,12 @@ positiveDefinite m = minimum (toList e) > 0 where (e,_v) = eigSH m upperTriang m = rows m == 1 || down == z - where down = fromList $ concat $ zipWith drop [1..] (toLists (ctrans m)) - z = konst 0 (dim down) + where down = fromList $ concat $ zipWith drop [1..] (toLists (tr m)) + z = konst 0 (size down) upperHessenberg m = rows m < 3 || down == z - where down = fromList $ concat $ zipWith drop [2..] (toLists (ctrans m)) - z = konst 0 (dim down) + where down = fromList $ concat $ zipWith drop [2..] (toLists (tr m)) + z = konst 0 (size down) zeros (r,c) = reshape c (konst 0 (r*c)) @@ -116,81 +116,94 @@ detProp m = s d1 |~| s d2 s x = fromList [x] nullspaceProp m = null nl `trivial` (null nl || m <> n |~| zeros (r,c) - && orthonormal (fromColumns nl)) - where nl = nullspacePrec 1 m - n = fromColumns nl + && orthonormal n) + where n = nullspaceSVD (Left (1*peps)) m (rightSV m) + nl = toColumns n r = rows m c = cols m - rank m ------------------------------------------------------------------ - +{- -- testcase for nonempty fpu stack -- uncommenting unitary' signature eliminates the problem -bugProp m = m |~| u <> real d <> trans v && unitary' u && unitary' v - where (u,d,v) = fullSVD m +bugProp m = m |~| u <> real d <> tr v && unitary' u && unitary' v + where (u,d,v) = svd m -- unitary' :: (Num (Vector t), Field t) => Matrix t -> Bool unitary' a = unitary a - +-} ------------------------------------------------------------------ -- fullSVD -svdProp1 m = m |~| u <> real d <> trans v && unitary u && unitary v - where (u,d,v) = fullSVD m +svdProp1 m = m |~| u <> real d <> tr v && unitary u && unitary v + where + (u,s,v) = svd m + d = diagRect 0 s (rows m) (cols m) -svdProp1a svdfun m = m |~| u <> real d <> trans v && unitary u && unitary v where +svdProp1a svdfun m = m |~| u <> real d <> tr v && unitary u && unitary v + where (u,s,v) = svdfun m d = diagRect 0 s (rows m) (cols m) -svdProp1b svdfun m = unitary u && unitary v where +svdProp1b svdfun m = unitary u && unitary v + where (u,_,v) = svdfun m -- thinSVD -svdProp2 thinSVDfun m = m |~| u <> diag (real s) <> trans v && orthonormal u && orthonormal v && dim s == min (rows m) (cols m) - where (u,s,v) = thinSVDfun m +svdProp2 thinSVDfun m + = m |~| u <> diag (real s) <> tr v + && orthonormal u && orthonormal v + && size s == min (rows m) (cols m) + where + (u,s,v) = thinSVDfun m -- compactSVD -svdProp3 m = (m |~| u <> real (diag s) <> trans v +svdProp3 m = (m |~| u <> real (diag s) <> tr v && orthonormal u && orthonormal v) - where (u,s,v) = compactSVD m + where + (u,s,v) = compactSVD m -svdProp4 m' = m |~| u <> real (diag s) <> trans v +svdProp4 m' = m |~| u <> real (diag s) <> tr v && orthonormal u && orthonormal v - && (dim s == r || r == 0 && dim s == 1) - where (u,s,v) = compactSVD m - m = fromBlocks [[m'],[m']] - r = rank m' - -svdProp5a m = all (s1|~|) [s2,s3,s4,s5,s6] where - s1 = svR m - s2 = svRd m - (_,s3,_) = svdR m - (_,s4,_) = svdRd m - (_,s5,_) = thinSVDR m - (_,s6,_) = thinSVDRd m - -svdProp5b m = all (s1|~|) [s2,s3,s4,s5,s6] where - s1 = svC m - s2 = svCd m - (_,s3,_) = svdC m - (_,s4,_) = svdCd m - (_,s5,_) = thinSVDC m - (_,s6,_) = thinSVDCd m + && (size s == r || r == 0 && size s == 1) + where + (u,s,v) = compactSVD m + m = fromBlocks [[m'],[m']] + r = rank m' + +svdProp5a m = all (s1|~|) [s3,s5] where + s1 = singularValues (m :: Matrix Double) +-- s2 = svRd m + (_,s3,_) = svd m +-- (_,s4,_) = svdRd m + (_,s5,_) = thinSVD m +-- (_,s6,_) = thinSVDRd m + +svdProp5b m = all (s1|~|) [s3,s5] where + s1 = singularValues (m :: Matrix (Complex Double)) +-- s2 = svCd m + (_,s3,_) = svd m +-- (_,s4,_) = svdCd m + (_,s5,_) = thinSVD m +-- (_,s6,_) = thinSVDCd m svdProp6a m = s |~| s' && v |~| v' && s |~| s'' && u |~| u' - where (u,s,v) = svdR m - (s',v') = rightSVR m - (u',s'') = leftSVR m + where + (u,s,v) = svd (m :: Matrix Double) + (s',v') = rightSV m + (u',s'') = leftSV m svdProp6b m = s |~| s' && v |~| v' && s |~| s'' && u |~| u' - where (u,s,v) = svdC m - (s',v') = rightSVC m - (u',s'') = leftSVC m + where + (u,s,v) = svd (m :: Matrix (Complex Double)) + (s',v') = rightSV m + (u',s'') = leftSV m svdProp7 m = s |~| s' && u |~| u' && v |~| v' && s |~| s''' - where (u,s,v) = svd m - (s',v') = rightSV m - (u',_s'') = leftSV m - s''' = singularValues m + where + (u,s,v) = svd m + (s',v') = rightSV m + (u',_s'') = leftSV m + s''' = singularValues m ------------------------------------------------------------------ @@ -199,7 +212,7 @@ eigProp m = complex m <> v |~| v <> diag s eigSHProp m = m <> v |~| v <> real (diag s) && unitary v - && m |~| v <> real (diag s) <> ctrans v + && m |~| v <> real (diag s) <> tr v where (s, v) = eigSH m eigProp2 m = fst (eig m) |~| eigenvalues m @@ -224,19 +237,19 @@ rqProp3 m = upperTriang' r where (r,_q) = rq m upperTriang' r = upptr (rows r) (cols r) * r |~| r - where upptr f c = buildMatrix f c $ \(r',c') -> if r'-t > c' then 0 else 1 - where t = f-c + where upptr f c = build (f,c) $ \r' c' -> if r'-t > c' then 0 else 1 + where t = fromIntegral (f-c) -hessProp m = m |~| p <> h <> ctrans p && unitary p && upperHessenberg h +hessProp m = m |~| p <> h <> tr p && unitary p && upperHessenberg h where (p,h) = hess m -schurProp1 m = m |~| u <> s <> ctrans u && unitary u && upperTriang s +schurProp1 m = m |~| u <> s <> tr u && unitary u && upperTriang s where (u,s) = schur m -schurProp2 m = m |~| u <> s <> ctrans u && unitary u && upperHessenberg s -- fixme +schurProp2 m = m |~| u <> s <> tr u && unitary u && upperHessenberg s -- fixme where (u,s) = schur m -cholProp m = m |~| ctrans c <> c && upperTriang c +cholProp m = m |~| tr c <> c && upperTriang c where c = chol m exactProp m = chol m == chol (m+0) @@ -250,7 +263,7 @@ mulH a b = fromLists [[ doth ai bj | bj <- toColumns b] | ai <- toRows a ] multProp1 p (a,b) = (a <> b) :~p~: (mulH a b) -multProp2 p (a,b) = (ctrans (a <> b)) :~p~: (ctrans b <> ctrans a) +multProp2 p (a,b) = (tr (a <> b)) :~p~: (tr b <> tr a) linearSolveProp f m = f m m |~| ident (rows m) @@ -259,5 +272,5 @@ linearSolveProp2 f (a,x) = not wc `trivial` (not wc || a <> f a b |~| b) b = a <> x wc = rank a == q -subProp m = m == (trans . fromColumns . toRows) m +subProp m = m == (tr . fromColumns . toRows) m -- cgit v1.2.3