diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-09-04 11:48:33 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-09-04 11:48:33 +0000 |
commit | badf588ae2940df6f4ce144f31e90e8973f144a4 (patch) | |
tree | 0d82b431823c51daf285213e3aeb0190d90db02b | |
parent | 7aa4500e6c71964094374d553faad1b1754cbc65 (diff) |
unit tests for norms
-rw-r--r-- | lib/Numeric/LinearAlgebra/Algorithms.hs | 32 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests.hs | 55 |
2 files changed, 71 insertions, 16 deletions
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs index 28063a9..7d2f84d 100644 --- a/lib/Numeric/LinearAlgebra/Algorithms.hs +++ b/lib/Numeric/LinearAlgebra/Algorithms.hs | |||
@@ -639,47 +639,47 @@ instance Normed (Vector Double) where | |||
639 | pnorm PNorm1 = norm1 | 639 | pnorm PNorm1 = norm1 |
640 | pnorm PNorm2 = norm2 | 640 | pnorm PNorm2 = norm2 |
641 | pnorm Infinity = normInf | 641 | pnorm Infinity = normInf |
642 | pnorm Frobenius = normInf | 642 | pnorm Frobenius = norm2 |
643 | 643 | ||
644 | instance Normed (Vector (Complex Double)) where | 644 | instance Normed (Vector (Complex Double)) where |
645 | pnorm PNorm1 = realPart . norm1 | 645 | pnorm PNorm1 = realPart . norm1 |
646 | pnorm PNorm2 = realPart . norm2 | 646 | pnorm PNorm2 = realPart . norm2 |
647 | pnorm Infinity = realPart . normInf | 647 | pnorm Infinity = realPart . normInf |
648 | pnorm Frobenius = realPart . normInf | 648 | pnorm Frobenius = pnorm PNorm2 |
649 | 649 | ||
650 | instance Normed (Vector Float) where | 650 | instance Normed (Vector Float) where |
651 | pnorm PNorm1 = realToFrac . norm1 | 651 | pnorm PNorm1 = realToFrac . norm1 |
652 | pnorm PNorm2 = realToFrac . norm2 | 652 | pnorm PNorm2 = realToFrac . norm2 |
653 | pnorm Infinity = realToFrac . normInf | 653 | pnorm Infinity = realToFrac . normInf |
654 | pnorm Frobenius = realToFrac . normInf | 654 | pnorm Frobenius = pnorm PNorm2 |
655 | 655 | ||
656 | instance Normed (Vector (Complex Float)) where | 656 | instance Normed (Vector (Complex Float)) where |
657 | pnorm PNorm1 = realToFrac . realPart . norm1 | 657 | pnorm PNorm1 = realToFrac . realPart . norm1 |
658 | pnorm PNorm2 = realToFrac . realPart . norm2 | 658 | pnorm PNorm2 = realToFrac . realPart . norm2 |
659 | pnorm Infinity = realToFrac . realPart . normInf | 659 | pnorm Infinity = realToFrac . realPart . normInf |
660 | pnorm Frobenius = realToFrac . realPart . normInf | 660 | pnorm Frobenius = pnorm PNorm2 |
661 | 661 | ||
662 | 662 | ||
663 | instance Normed (Matrix Double) where | 663 | instance Normed (Matrix Double) where |
664 | pnorm PNorm1 = maximum . map norm1 . toColumns | 664 | pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns |
665 | pnorm PNorm2 = (@>0) . singularValues | 665 | pnorm PNorm2 = (@>0) . singularValues |
666 | pnorm Infinity = pnorm PNorm1 . trans | 666 | pnorm Infinity = pnorm PNorm1 . trans |
667 | pnorm Frobenius = norm2 . flatten | 667 | pnorm Frobenius = pnorm PNorm2 . flatten |
668 | 668 | ||
669 | instance Normed (Matrix (Complex Double)) where | 669 | instance Normed (Matrix (Complex Double)) where |
670 | pnorm PNorm1 = maximum . map (realPart.norm1) . toColumns | 670 | pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns |
671 | pnorm PNorm2 = (@>0) . singularValues | 671 | pnorm PNorm2 = (@>0) . singularValues |
672 | pnorm Infinity = pnorm PNorm1 . trans | 672 | pnorm Infinity = pnorm PNorm1 . trans |
673 | pnorm Frobenius = realPart . norm2 . flatten | 673 | pnorm Frobenius = pnorm PNorm2 . flatten |
674 | 674 | ||
675 | instance Normed (Matrix Float) where | 675 | instance Normed (Matrix Float) where |
676 | pnorm PNorm1 = realToFrac . maximum . map norm1 . toColumns | 676 | pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns |
677 | pnorm PNorm2 = realToFrac . (@>0) . singularValues . double | 677 | pnorm PNorm2 = (@>0) . singularValues . double |
678 | pnorm Infinity = realToFrac . pnorm PNorm1 . trans | 678 | pnorm Infinity = pnorm PNorm1 . trans |
679 | pnorm Frobenius = realToFrac . norm2 . flatten | 679 | pnorm Frobenius = pnorm PNorm2 . flatten |
680 | 680 | ||
681 | instance Normed (Matrix (Complex Float)) where | 681 | instance Normed (Matrix (Complex Float)) where |
682 | pnorm PNorm1 = realToFrac . maximum . map (realPart.norm1) . toColumns | 682 | pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns |
683 | pnorm PNorm2 = realToFrac . (@>0) . singularValues . double | 683 | pnorm PNorm2 = (@>0) . singularValues . double |
684 | pnorm Infinity = realToFrac . pnorm PNorm1 . trans | 684 | pnorm Infinity = pnorm PNorm1 . trans |
685 | pnorm Frobenius = realToFrac . realPart . norm2 . flatten | 685 | pnorm Frobenius = pnorm PNorm2 . flatten |
diff --git a/lib/Numeric/LinearAlgebra/Tests.hs b/lib/Numeric/LinearAlgebra/Tests.hs index 77bb2f7..43a62e5 100644 --- a/lib/Numeric/LinearAlgebra/Tests.hs +++ b/lib/Numeric/LinearAlgebra/Tests.hs | |||
@@ -35,9 +35,12 @@ import System.CPUTime | |||
35 | import Text.Printf | 35 | import Text.Printf |
36 | import Data.Packed.Development(unsafeFromForeignPtr,unsafeToForeignPtr) | 36 | import Data.Packed.Development(unsafeFromForeignPtr,unsafeToForeignPtr) |
37 | import Control.Arrow((***)) | 37 | import Control.Arrow((***)) |
38 | import Debug.Trace | ||
38 | 39 | ||
39 | #include "Tests/quickCheckCompat.h" | 40 | #include "Tests/quickCheckCompat.h" |
40 | 41 | ||
42 | debug x = trace (show x) x | ||
43 | |||
41 | a ^ b = a Prelude.^ (b :: Int) | 44 | a ^ b = a Prelude.^ (b :: Int) |
42 | 45 | ||
43 | utest str b = TestCase $ assertBool str b | 46 | utest str b = TestCase $ assertBool str b |
@@ -217,6 +220,56 @@ offsetTest = y == y' where | |||
217 | 220 | ||
218 | --------------------------------------------------------------------- | 221 | --------------------------------------------------------------------- |
219 | 222 | ||
223 | normsVTest = TestList [ | ||
224 | utest "normv2CD" $ norm2PropC v | ||
225 | , utest "normv2CF" $ norm2PropC (single v) | ||
226 | , utest "normv2D" $ norm2PropR x | ||
227 | , utest "normv2F" $ norm2PropR (single x) | ||
228 | |||
229 | , utest "normv1CD" $ norm1 v == 8 | ||
230 | , utest "normv1CF" $ norm1 (single v) == 8 | ||
231 | , utest "normv1D" $ norm1 x == 6 | ||
232 | , utest "normv1F" $ norm1 (single x) == 6 | ||
233 | |||
234 | , utest "normvInfCD" $ normInf v == 5 | ||
235 | , utest "normvInfCF" $ normInf (single v) == 5 | ||
236 | , utest "normvInfD" $ normInf x == 3 | ||
237 | , utest "normvInfF" $ normInf (single x) == 3 | ||
238 | |||
239 | ] where v = fromList [1,-2,3:+4] :: Vector (Complex Double) | ||
240 | x = fromList [1,2,-3] :: Vector Double | ||
241 | norm2PropR a = norm2 a =~= sqrt (dot a a) | ||
242 | norm2PropC a = norm2 a =~= sqrt (dot a (conj a)) | ||
243 | a =~= b = fromList [a] |~| fromList [b] | ||
244 | |||
245 | normsMTest = TestList [ | ||
246 | utest "norm2mCD" $ pnorm PNorm2 v =~= 8.86164970498005 | ||
247 | , utest "norm2mCF" $ pnorm PNorm2 (single v) =~= 8.86164970498005 | ||
248 | , utest "norm2mD" $ pnorm PNorm2 x =~= 5.96667765076216 | ||
249 | , utest "norm2mF" $ pnorm PNorm2 (single x) =~= 5.96667765076216 | ||
250 | |||
251 | , utest "norm1mCD" $ pnorm PNorm1 v == 9 | ||
252 | , utest "norm1mCF" $ pnorm PNorm1 (single v) == 9 | ||
253 | , utest "norm1mD" $ pnorm PNorm1 x == 7 | ||
254 | , utest "norm1mF" $ pnorm PNorm1 (single x) == 7 | ||
255 | |||
256 | , utest "normmInfCD" $ pnorm Infinity v == 12 | ||
257 | , utest "normmInfCF" $ pnorm Infinity (single v) == 12 | ||
258 | , utest "normmInfD" $ pnorm Infinity x == 8 | ||
259 | , utest "normmInfF" $ pnorm Infinity (single x) == 8 | ||
260 | |||
261 | , utest "normmFroCD" $ pnorm Frobenius v =~= 8.88819441731559 | ||
262 | , utest "normmFroCF" $ pnorm Frobenius (single v) =~~= 8.88819441731559 | ||
263 | , utest "normmFroD" $ pnorm Frobenius x =~= 6.24499799839840 | ||
264 | , utest "normmFroF" $ pnorm Frobenius (single x) =~~= 6.24499799839840 | ||
265 | |||
266 | ] where v = (2><2) [1,-2*i,3:+4,7] :: Matrix (Complex Double) | ||
267 | x = (2><2) [1,2,-3,5] :: Matrix Double | ||
268 | a =~= b = fromList [a] :~10~: fromList [b] | ||
269 | a =~~= b = fromList [a] :~5~: fromList [b] | ||
270 | |||
271 | --------------------------------------------------------------------- | ||
272 | |||
220 | 273 | ||
221 | -- | All tests must pass with a maximum dimension of about 20 | 274 | -- | All tests must pass with a maximum dimension of about 20 |
222 | -- (some tests may fail with bigger sizes due to precision loss). | 275 | -- (some tests may fail with bigger sizes due to precision loss). |
@@ -373,6 +426,8 @@ runTests n = do | |||
373 | , fittingTest | 426 | , fittingTest |
374 | , mbCholTest | 427 | , mbCholTest |
375 | , utest "offset" offsetTest | 428 | , utest "offset" offsetTest |
429 | , normsVTest | ||
430 | , normsMTest | ||
376 | ] | 431 | ] |
377 | return () | 432 | return () |
378 | 433 | ||