summaryrefslogtreecommitdiff
path: root/packages/base/src/Numeric/LinearAlgebra/Util
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2015-06-01 18:51:17 +0200
committerAlberto Ruiz <aruiz@um.es>2015-06-01 18:51:17 +0200
commit60dc8245539263899c083b2760310a86b14d367b (patch)
tree0be5e63961bd16aa008dd45b8999932425fa9625 /packages/base/src/Numeric/LinearAlgebra/Util
parent221749b4ffd37acaa3e9a76ceaa4ea0909aadb5e (diff)
generic gaussElim, modularTest
Diffstat (limited to 'packages/base/src/Numeric/LinearAlgebra/Util')
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Util/Modular.hs26
1 files changed, 22 insertions, 4 deletions
diff --git a/packages/base/src/Numeric/LinearAlgebra/Util/Modular.hs b/packages/base/src/Numeric/LinearAlgebra/Util/Modular.hs
index 18b635d..ea4a668 100644
--- a/packages/base/src/Numeric/LinearAlgebra/Util/Modular.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Util/Modular.hs
@@ -26,7 +26,7 @@ module Numeric.LinearAlgebra.Util.Modular(
26) where 26) where
27 27
28import Data.Packed.Numeric 28import Data.Packed.Numeric
29import Numeric.LinearAlgebra.Util(Indexable(..),size) 29import Numeric.LinearAlgebra.Util(Indexable(..),gaussElim)
30import GHC.TypeLits 30import GHC.TypeLits
31import Data.Proxy(Proxy) 31import Data.Proxy(Proxy)
32import Foreign.ForeignPtr(castForeignPtr) 32import Foreign.ForeignPtr(castForeignPtr)
@@ -65,11 +65,14 @@ instance KnownNat m => Integral (F m)
65 where 65 where
66 (q,r) = quotRem (unMod a) (unMod b) 66 (q,r) = quotRem (unMod a) (unMod b)
67 67
68-- | this instance is only valid for prime m (not checked) 68-- | this instance is only valid for prime m
69instance KnownNat m => Fractional (F m) 69instance KnownNat m => Fractional (F m)
70 where 70 where
71 recip x = x^(m'-2) 71 recip x
72 | x*r == 1 = r
73 | otherwise = error $ show x ++" does not have a multiplicative inverse mod "++show m'
72 where 74 where
75 r = x^(m'-2)
73 m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int 76 m' = fromIntegral . natVal $ (undefined :: Proxy m) :: Int
74 fromRational x = fromInteger (numerator x) / fromInteger (denominator x) 77 fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
75 78
@@ -124,7 +127,7 @@ instance Element (F n)
124instance forall m . KnownNat m => Container Vector (F m) 127instance forall m . KnownNat m => Container Vector (F m)
125 where 128 where
126 conj' = id 129 conj' = id
127 size' = size 130 size' = dim
128 scale' s x = fromInt (scale (unMod s) (toInt x)) 131 scale' s x = fromInt (scale (unMod s) (toInt x))
129 addConstant c x = fromInt (addConstant (unMod c) (toInt x)) 132 addConstant c x = fromInt (addConstant (unMod c) (toInt x))
130 add a b = fromInt (add (toInt a) (toInt b)) 133 add a b = fromInt (add (toInt a) (toInt b))
@@ -216,6 +219,18 @@ test = (ok, info)
216 where 219 where
217 v = fromList [3,-5,75] :: V 11 220 v = fromList [3,-5,75] :: V 11
218 m = (3><3) [1..] :: M 11 221 m = (3><3) [1..] :: M 11
222
223 a = (3><3) [1,2 , 3
224 ,4,5 , 6
225 ,0,10,-3] :: Matrix I
226
227 b = (3><2) [0..] :: Matrix I
228
229 am = fromInt a :: Matrix (F 13)
230 bm = fromInt b :: Matrix (F 13)
231 ad = fromInt a :: Matrix Double
232 bd = fromInt b :: Matrix Double
233
219 info = do 234 info = do
220 print v 235 print v
221 print m 236 print m
@@ -225,9 +240,12 @@ test = (ok, info)
225 print $ m <> m 240 print $ m <> m
226 print $ m #> v 241 print $ m #> v
227 242
243 print $ am <> gaussElim am bm - bm
244 print $ ad <> gaussElim ad bd - bd
228 245
229 ok = and 246 ok = and
230 [ toInt (m #> v) == cmod 11 (toInt m #> toInt v ) 247 [ toInt (m #> v) == cmod 11 (toInt m #> toInt v )
248 , am <> gaussElim am bm == bm
231 ] 249 ]
232 250
233 251