diff options
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Algorithms.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Algorithms.hs | 47 |
1 files changed, 26 insertions, 21 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 | |||
82 | import Numeric.Container | 82 | import 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. |
85 | class (Product t, Convert t, Container Vector t, Container Matrix t) => Field t where | 85 | class (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 | -} |
569 | expm :: (Normed (Matrix t), Field t) => Matrix t -> Matrix t | 574 | expm :: Field t => Matrix t -> Matrix t |
570 | expm = expGolub | 575 | expm = 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 | -} |
585 | sqrtm :: (Normed (Matrix t), Field t) => Matrix t -> Matrix t | 590 | sqrtm :: Field t => Matrix t -> Matrix t |
586 | sqrtm = sqrtmInv | 591 | sqrtm = sqrtmInv |
587 | 592 | ||
588 | sqrtmInv x = fst $ fixedPoint $ iterate f (x, ident (rows x)) | 593 | sqrtmInv 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 | ||
632 | data NormType = Infinity | PNorm1 | PNorm2 | Frobenius | 637 | data NormType = Infinity | PNorm1 | PNorm2 | Frobenius |
633 | 638 | ||
634 | class Normed t where | 639 | class (RealFloat (RealOf t)) => Normed c t where |
635 | pnorm :: NormType -> t -> Double | 640 | pnorm :: NormType -> c t -> RealOf t |
636 | 641 | ||
637 | instance Normed (Vector Double) where | 642 | instance 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 | ||
643 | instance Normed (Vector (Complex Double)) where | 648 | instance 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 | ||
649 | instance Normed (Vector Float) where | 654 | instance 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 | ||
655 | instance Normed (Vector (Complex Float)) where | 660 | instance 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 | ||
662 | instance Normed (Matrix Double) where | 667 | instance 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 | ||
668 | instance Normed (Matrix (Complex Double)) where | 673 | instance 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 | ||
674 | instance Normed (Matrix Float) where | 679 | instance 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 | ||
680 | instance Normed (Matrix (Complex Float)) where | 685 | instance 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 |