diff options
author | Alberto Ruiz <aruiz@um.es> | 2015-06-30 12:04:21 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2015-06-30 12:04:21 +0200 |
commit | b9329d636d19f6a26da1cf1fd7e8d7cbd0b04cce (patch) | |
tree | c0beb22b3b394ed9d18a6a98d5cf1ca6d4ea8960 /packages/tests/src/Numeric/LinearAlgebra/Tests.hs | |
parent | 9c05df0cd663bafaf0b69eafee53fce45569dc95 (diff) |
support slice in multiply
Diffstat (limited to 'packages/tests/src/Numeric/LinearAlgebra/Tests.hs')
-rw-r--r-- | packages/tests/src/Numeric/LinearAlgebra/Tests.hs | 151 |
1 files changed, 149 insertions, 2 deletions
diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs index b226c9f..79cb769 100644 --- a/packages/tests/src/Numeric/LinearAlgebra/Tests.hs +++ b/packages/tests/src/Numeric/LinearAlgebra/Tests.hs | |||
@@ -4,6 +4,8 @@ | |||
4 | {-# LANGUAGE TypeFamilies #-} | 4 | {-# LANGUAGE TypeFamilies #-} |
5 | {-# LANGUAGE FlexibleContexts #-} | 5 | {-# LANGUAGE FlexibleContexts #-} |
6 | {-# LANGUAGE RankNTypes #-} | 6 | {-# LANGUAGE RankNTypes #-} |
7 | {-# LANGUAGE TypeOperators #-} | ||
8 | {-# LANGUAGE ViewPatterns #-} | ||
7 | 9 | ||
8 | ----------------------------------------------------------------------------- | 10 | ----------------------------------------------------------------------------- |
9 | {- | | 11 | {- | |
@@ -76,7 +78,7 @@ detTest1 = det m == 26 | |||
76 | && det mc == 38 :+ (-3) | 78 | && det mc == 38 :+ (-3) |
77 | && det (feye 2) == -1 | 79 | && det (feye 2) == -1 |
78 | where | 80 | where |
79 | m = (3><3) | 81 | m = (3><3) |
80 | [ 1, 2, 3 | 82 | [ 1, 2, 3 |
81 | , 4, 5, 7 | 83 | , 4, 5, 7 |
82 | , 2, 8, 4 :: Double | 84 | , 2, 8, 4 :: Double |
@@ -357,7 +359,7 @@ accumTest = utest "accum" ok | |||
357 | ,0,1,7 | 359 | ,0,1,7 |
358 | ,0,0,4] | 360 | ,0,0,4] |
359 | && | 361 | && |
360 | toList (flatten x) == [1,0,0,0,1,0,0,0,1] | 362 | toList (flatten x) == [1,0,0,0,1,0,0,0,1] |
361 | 363 | ||
362 | -------------------------------------------------------------------------------- | 364 | -------------------------------------------------------------------------------- |
363 | 365 | ||
@@ -400,6 +402,150 @@ indexProp g f x = a1 == g a2 && a2 == a3 && b1 == g b2 && b2 == b3 | |||
400 | 402 | ||
401 | -------------------------------------------------------------------------------- | 403 | -------------------------------------------------------------------------------- |
402 | 404 | ||
405 | sliceTest = utest "slice test" $ and | ||
406 | [ testSlice chol (gen 5 :: Matrix R) | ||
407 | , testSlice chol (gen 5 :: Matrix C) | ||
408 | , testSlice qr (rec :: Matrix R) | ||
409 | , testSlice qr (rec :: Matrix C) | ||
410 | , testSlice hess (agen 5 :: Matrix R) | ||
411 | , testSlice hess (agen 5 :: Matrix C) | ||
412 | , testSlice schur (agen 5 :: Matrix R) | ||
413 | , testSlice schur (agen 5 :: Matrix C) | ||
414 | , testSlice lu (agen 5 :: Matrix R) | ||
415 | , testSlice lu (agen 5 :: Matrix C) | ||
416 | , testSlice (luSolve (luPacked (agen 5 :: Matrix R))) (agen 5) | ||
417 | , testSlice (luSolve (luPacked (agen 5 :: Matrix C))) (agen 5) | ||
418 | , test_lus (agen 5 :: Matrix R) | ||
419 | , test_lus (agen 5 :: Matrix C) | ||
420 | |||
421 | , testSlice eig (agen 5 :: Matrix R) | ||
422 | , testSlice eig (agen 5 :: Matrix C) | ||
423 | , testSlice eigSH (gen 5 :: Matrix R) | ||
424 | , testSlice eigSH (gen 5 :: Matrix C) | ||
425 | , testSlice eigenvalues (agen 5 :: Matrix R) | ||
426 | , testSlice eigenvalues (agen 5 :: Matrix C) | ||
427 | , testSlice eigenvaluesSH (gen 5 :: Matrix R) | ||
428 | , testSlice eigenvaluesSH (gen 5 :: Matrix C) | ||
429 | |||
430 | , testSlice svd (rec :: Matrix R) | ||
431 | , testSlice thinSVD (rec :: Matrix R) | ||
432 | , testSlice compactSVD (rec :: Matrix R) | ||
433 | , testSlice leftSV (rec :: Matrix R) | ||
434 | , testSlice rightSV (rec :: Matrix R) | ||
435 | , testSlice singularValues (rec :: Matrix R) | ||
436 | |||
437 | , testSlice svd (rec :: Matrix C) | ||
438 | , testSlice thinSVD (rec :: Matrix C) | ||
439 | , testSlice compactSVD (rec :: Matrix C) | ||
440 | , testSlice leftSV (rec :: Matrix C) | ||
441 | , testSlice rightSV (rec :: Matrix C) | ||
442 | , testSlice singularValues (rec :: Matrix C) | ||
443 | |||
444 | , testSlice (linearSolve (agen 5:: Matrix R)) (agen 5) | ||
445 | , testSlice (flip linearSolve (agen 5:: Matrix R)) (agen 5) | ||
446 | |||
447 | , testSlice (linearSolve (agen 5:: Matrix C)) (agen 5) | ||
448 | , testSlice (flip linearSolve (agen 5:: Matrix C)) (agen 5) | ||
449 | |||
450 | , testSlice (linearSolveLS (ogen 5:: Matrix R)) (ogen 5) | ||
451 | , testSlice (flip linearSolveLS (ogen 5:: Matrix R)) (ogen 5) | ||
452 | |||
453 | , testSlice (linearSolveLS (ogen 5:: Matrix C)) (ogen 5) | ||
454 | , testSlice (flip linearSolveLS (ogen 5:: Matrix C)) (ogen 5) | ||
455 | |||
456 | , testSlice (linearSolveSVD (ogen 5:: Matrix R)) (ogen 5) | ||
457 | , testSlice (flip linearSolveSVD (ogen 5:: Matrix R)) (ogen 5) | ||
458 | |||
459 | , testSlice (linearSolveSVD (ogen 5:: Matrix C)) (ogen 5) | ||
460 | , testSlice (flip linearSolveSVD (ogen 5:: Matrix C)) (ogen 5) | ||
461 | |||
462 | , testSlice (linearSolveLS (ugen 5:: Matrix R)) (ugen 5) | ||
463 | , testSlice (flip linearSolveLS (ugen 5:: Matrix R)) (ugen 5) | ||
464 | |||
465 | , testSlice (linearSolveLS (ugen 5:: Matrix C)) (ugen 5) | ||
466 | , testSlice (flip linearSolveLS (ugen 5:: Matrix C)) (ugen 5) | ||
467 | |||
468 | , testSlice (linearSolveSVD (ugen 5:: Matrix R)) (ugen 5) | ||
469 | , testSlice (flip linearSolveSVD (ugen 5:: Matrix R)) (ugen 5) | ||
470 | |||
471 | , testSlice (linearSolveSVD (ugen 5:: Matrix C)) (ugen 5) | ||
472 | , testSlice (flip linearSolveSVD (ugen 5:: Matrix C)) (ugen 5) | ||
473 | |||
474 | , testSlice ((<>) (ogen 5:: Matrix R)) (gen 5) | ||
475 | , testSlice (flip (<>) (gen 5:: Matrix R)) (ogen 5) | ||
476 | , testSlice ((<>) (ogen 5:: Matrix C)) (gen 5) | ||
477 | , testSlice (flip (<>) (gen 5:: Matrix C)) (ogen 5) | ||
478 | , testSlice ((<>) (ogen 5:: Matrix Float)) (gen 5) | ||
479 | , testSlice (flip (<>) (gen 5:: Matrix Float)) (ogen 5) | ||
480 | , testSlice ((<>) (ogen 5:: Matrix (Complex Float))) (gen 5) | ||
481 | , testSlice (flip (<>) (gen 5:: Matrix (Complex Float))) (ogen 5) | ||
482 | , testSlice ((<>) (ogen 5:: Matrix I)) (gen 5) | ||
483 | , testSlice (flip (<>) (gen 5:: Matrix I)) (ogen 5) | ||
484 | , testSlice ((<>) (ogen 5:: Matrix Z)) (gen 5) | ||
485 | , testSlice (flip (<>) (gen 5:: Matrix Z)) (ogen 5) | ||
486 | |||
487 | , testSlice ((<>) (ogen 5:: Matrix (I ./. 7))) (gen 5) | ||
488 | , testSlice (flip (<>) (gen 5:: Matrix (I ./. 7))) (ogen 5) | ||
489 | , testSlice ((<>) (ogen 5:: Matrix (Z ./. 7))) (gen 5) | ||
490 | , testSlice (flip (<>) (gen 5:: Matrix (Z ./. 7))) (ogen 5) | ||
491 | |||
492 | , testSlice (flip cholSolve (agen 5:: Matrix R)) (chol $ gen 5) | ||
493 | , testSlice (flip cholSolve (agen 5:: Matrix C)) (chol $ gen 5) | ||
494 | , testSlice (cholSolve (chol $ gen 5:: Matrix R)) (agen 5) | ||
495 | , testSlice (cholSolve (chol $ gen 5:: Matrix C)) (agen 5) | ||
496 | |||
497 | , ok_qrgr (rec :: Matrix R) | ||
498 | , ok_qrgr (rec :: Matrix C) | ||
499 | , testSlice (test_qrgr 4 tau1) qrr1 | ||
500 | , testSlice (test_qrgr 4 tau2) qrr2 | ||
501 | ] | ||
502 | where | ||
503 | (qrr1,tau1) = qrRaw (rec :: Matrix R) | ||
504 | (qrr2,tau2) = qrRaw (rec :: Matrix C) | ||
505 | |||
506 | test_qrgr n t x = qrgr n (x,t) | ||
507 | |||
508 | ok_qrgr x = simeq 1E-15 q q' | ||
509 | where | ||
510 | (q,_) = qr x | ||
511 | atau = qrRaw x | ||
512 | q' = qrgr (rows q) atau | ||
513 | |||
514 | simeq eps a b = not $ magnit eps (norm_1 $ flatten (a-b)) | ||
515 | |||
516 | test_lus m = testSlice f lup | ||
517 | where | ||
518 | f x = luSolve (x,p) m | ||
519 | (lup,p) = luPacked m | ||
520 | |||
521 | gen :: Numeric t => Int -> Matrix t | ||
522 | gen n = diagRect 1 (konst 5 n) n n | ||
523 | |||
524 | agen :: (Numeric t, Num (Vector t))=> Int -> Matrix t | ||
525 | agen n = gen n + fromInt ((n><n)[0..]) | ||
526 | |||
527 | ogen :: (Numeric t, Num (Vector t))=> Int -> Matrix t | ||
528 | ogen n = gen n === gen n | ||
529 | |||
530 | ugen :: (Numeric t, Num (Vector t))=> Int -> Matrix t | ||
531 | ugen n = takeRows 3 (gen n) | ||
532 | |||
533 | |||
534 | rec :: Numeric t => Matrix t | ||
535 | rec = subMatrix (0,0) (4,5) (gen 5) | ||
536 | |||
537 | testSlice f x@(size->sz@(r,c)) = all (==f x) (map f (g y1 ++ g y2)) | ||
538 | where | ||
539 | subm = sliceMatrix | ||
540 | g y = [ subm (a*r,b*c) sz y | a <-[0..2], b <- [0..2]] | ||
541 | h z = fromBlocks (replicate 3 (replicate 3 z)) | ||
542 | y1 = h x | ||
543 | y2 = (tr . h . tr) x | ||
544 | |||
545 | |||
546 | |||
547 | -------------------------------------------------------------------------------- | ||
548 | |||
403 | -- | All tests must pass with a maximum dimension of about 20 | 549 | -- | All tests must pass with a maximum dimension of about 20 |
404 | -- (some tests may fail with bigger sizes due to precision loss). | 550 | -- (some tests may fail with bigger sizes due to precision loss). |
405 | runTests :: Int -- ^ maximum dimension | 551 | runTests :: Int -- ^ maximum dimension |
@@ -578,6 +724,7 @@ runTests n = do | |||
578 | , staticTest | 724 | , staticTest |
579 | , intTest | 725 | , intTest |
580 | , modularTest | 726 | , modularTest |
727 | , sliceTest | ||
581 | ] | 728 | ] |
582 | when (errors c + failures c > 0) exitFailure | 729 | when (errors c + failures c > 0) exitFailure |
583 | return () | 730 | return () |