summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-09-14 11:12:50 +0000
committerAlberto Ruiz <aruiz@um.es>2010-09-14 11:12:50 +0000
commitb1c372da927651887a46b6c47880acd0814fa01a (patch)
tree2506b97cb3e2fb03ba68829ee61e19fd1126aff6
parent9d54e7b9543c22d398ef5097e3ba619ca974820b (diff)
more general pnorm
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs47
-rw-r--r--lib/Numeric/LinearAlgebra/Tests/Properties.hs4
2 files changed, 28 insertions, 23 deletions
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs
index 3cd200e..28cf88f 100644
--- a/lib/Numeric/LinearAlgebra/Algorithms.hs
+++ b/lib/Numeric/LinearAlgebra/Algorithms.hs
@@ -82,7 +82,12 @@ import Data.Array
82import Numeric.Container 82import Numeric.Container
83 83
84-- | Auxiliary typeclass used to define generic computations for both real and complex matrices. 84-- | Auxiliary typeclass used to define generic computations for both real and complex matrices.
85class (Product t, Convert t, Container Vector t, Container Matrix t) => Field t where 85class (Product t,
86 Convert t,
87 Container Vector t,
88 Container Matrix t,
89 Normed Matrix t,
90 Normed Vector t) => Field t where
86 svd' :: Matrix t -> (Matrix t, Vector Double, Matrix t) 91 svd' :: Matrix t -> (Matrix t, Vector Double, Matrix t)
87 thinSVD' :: Matrix t -> (Matrix t, Vector Double, Matrix t) 92 thinSVD' :: Matrix t -> (Matrix t, Vector Double, Matrix t)
88 sv' :: Matrix t -> Vector Double 93 sv' :: Matrix t -> Vector Double
@@ -566,7 +571,7 @@ expGolub m = iterate msq f !! j
566{- | Matrix exponential. It uses a direct translation of Algorithm 11.3.1 in Golub & Van Loan, 571{- | Matrix exponential. It uses a direct translation of Algorithm 11.3.1 in Golub & Van Loan,
567 based on a scaled Pade approximation. 572 based on a scaled Pade approximation.
568-} 573-}
569expm :: (Normed (Matrix t), Field t) => Matrix t -> Matrix t 574expm :: Field t => Matrix t -> Matrix t
570expm = expGolub 575expm = expGolub
571 576
572-------------------------------------------------------------- 577--------------------------------------------------------------
@@ -582,7 +587,7 @@ It only works with invertible matrices that have a real solution. For diagonaliz
582 [ 2.0, 2.25 587 [ 2.0, 2.25
583 , 0.0, 2.0 ]@ 588 , 0.0, 2.0 ]@
584-} 589-}
585sqrtm :: (Normed (Matrix t), Field t) => Matrix t -> Matrix t 590sqrtm :: Field t => Matrix t -> Matrix t
586sqrtm = sqrtmInv 591sqrtm = sqrtmInv
587 592
588sqrtmInv x = fst $ fixedPoint $ iterate f (x, ident (rows x)) 593sqrtmInv x = fst $ fixedPoint $ iterate f (x, ident (rows x))
@@ -631,54 +636,54 @@ luFact (l_u,perm) | r <= c = (l ,u ,p, s)
631 636
632data NormType = Infinity | PNorm1 | PNorm2 | Frobenius 637data NormType = Infinity | PNorm1 | PNorm2 | Frobenius
633 638
634class Normed t where 639class (RealFloat (RealOf t)) => Normed c t where
635 pnorm :: NormType -> t -> Double 640 pnorm :: NormType -> c t -> RealOf t
636 641
637instance Normed (Vector Double) where 642instance Normed Vector Double where
638 pnorm PNorm1 = norm1 643 pnorm PNorm1 = norm1
639 pnorm PNorm2 = norm2 644 pnorm PNorm2 = norm2
640 pnorm Infinity = normInf 645 pnorm Infinity = normInf
641 pnorm Frobenius = norm2 646 pnorm Frobenius = norm2
642 647
643instance Normed (Vector (Complex Double)) where 648instance Normed Vector (Complex Double) where
644 pnorm PNorm1 = norm1 649 pnorm PNorm1 = norm1
645 pnorm PNorm2 = norm2 650 pnorm PNorm2 = norm2
646 pnorm Infinity = normInf 651 pnorm Infinity = normInf
647 pnorm Frobenius = pnorm PNorm2 652 pnorm Frobenius = pnorm PNorm2
648 653
649instance Normed (Vector Float) where 654instance Normed Vector Float where
650 pnorm PNorm1 = realToFrac . norm1 655 pnorm PNorm1 = norm1
651 pnorm PNorm2 = realToFrac . norm2 656 pnorm PNorm2 = norm2
652 pnorm Infinity = realToFrac . normInf 657 pnorm Infinity = normInf
653 pnorm Frobenius = pnorm PNorm2 658 pnorm Frobenius = pnorm PNorm2
654 659
655instance Normed (Vector (Complex Float)) where 660instance Normed Vector (Complex Float) where
656 pnorm PNorm1 = realToFrac . norm1 661 pnorm PNorm1 = norm1
657 pnorm PNorm2 = realToFrac . norm2 662 pnorm PNorm2 = norm2
658 pnorm Infinity = realToFrac . normInf 663 pnorm Infinity = normInf
659 pnorm Frobenius = pnorm PNorm2 664 pnorm Frobenius = pnorm PNorm2
660 665
661 666
662instance Normed (Matrix Double) where 667instance Normed Matrix Double where
663 pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns 668 pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns
664 pnorm PNorm2 = (@>0) . singularValues 669 pnorm PNorm2 = (@>0) . singularValues
665 pnorm Infinity = pnorm PNorm1 . trans 670 pnorm Infinity = pnorm PNorm1 . trans
666 pnorm Frobenius = pnorm PNorm2 . flatten 671 pnorm Frobenius = pnorm PNorm2 . flatten
667 672
668instance Normed (Matrix (Complex Double)) where 673instance Normed Matrix (Complex Double) where
669 pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns 674 pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns
670 pnorm PNorm2 = (@>0) . singularValues 675 pnorm PNorm2 = (@>0) . singularValues
671 pnorm Infinity = pnorm PNorm1 . trans 676 pnorm Infinity = pnorm PNorm1 . trans
672 pnorm Frobenius = pnorm PNorm2 . flatten 677 pnorm Frobenius = pnorm PNorm2 . flatten
673 678
674instance Normed (Matrix Float) where 679instance Normed Matrix Float where
675 pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns 680 pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns
676 pnorm PNorm2 = (@>0) . singularValues . double 681 pnorm PNorm2 = realToFrac . (@>0) . singularValues . double
677 pnorm Infinity = pnorm PNorm1 . trans 682 pnorm Infinity = pnorm PNorm1 . trans
678 pnorm Frobenius = pnorm PNorm2 . flatten 683 pnorm Frobenius = pnorm PNorm2 . flatten
679 684
680instance Normed (Matrix (Complex Float)) where 685instance Normed Matrix (Complex Float) where
681 pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns 686 pnorm PNorm1 = maximum . map (pnorm PNorm1) . toColumns
682 pnorm PNorm2 = (@>0) . singularValues . double 687 pnorm PNorm2 = realToFrac . (@>0) . singularValues . double
683 pnorm Infinity = pnorm PNorm1 . trans 688 pnorm Infinity = pnorm PNorm1 . trans
684 pnorm Frobenius = pnorm PNorm2 . flatten 689 pnorm Frobenius = pnorm PNorm2 . flatten
diff --git a/lib/Numeric/LinearAlgebra/Tests/Properties.hs b/lib/Numeric/LinearAlgebra/Tests/Properties.hs
index e780c35..623b78c 100644
--- a/lib/Numeric/LinearAlgebra/Tests/Properties.hs
+++ b/lib/Numeric/LinearAlgebra/Tests/Properties.hs
@@ -54,8 +54,8 @@ import Debug.Trace
54debug x = trace (show x) x 54debug x = trace (show x) x
55 55
56-- relative error 56-- relative error
57dist :: (Normed t, Num t) => t -> t -> Double 57dist :: (Normed c t, Num (c t)) => c t -> c t -> Double
58dist a b = r 58dist a b = realToFrac r
59 where norm = pnorm Infinity 59 where norm = pnorm Infinity
60 na = norm a 60 na = norm a
61 nb = norm b 61 nb = norm b