diff options
author | Alberto Ruiz <aruiz@um.es> | 2016-03-15 20:33:32 +0100 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2016-03-15 20:33:32 +0100 |
commit | f5e235bbdb4bc342b623676b07245d781a9fb994 (patch) | |
tree | 9c3b7331f40b3fa773de7ce1f09460c58c8e272f /packages/base | |
parent | 6a0bf038091e453115a3451c040cbe790e770b89 (diff) | |
parent | 80e88bbb1fef8b904e5e01d3ca6cc35a97339cda (diff) |
Merge pull request #178 from sid-kap/matrix_binary
Add binary instances for static matrix and vector
Diffstat (limited to 'packages/base')
-rw-r--r-- | packages/base/src/Internal/Matrix.hs | 1 | ||||
-rw-r--r-- | packages/base/src/Internal/Static.hs | 33 |
2 files changed, 29 insertions, 5 deletions
diff --git a/packages/base/src/Internal/Matrix.hs b/packages/base/src/Internal/Matrix.hs index 6efbe5f..c47c625 100644 --- a/packages/base/src/Internal/Matrix.hs +++ b/packages/base/src/Internal/Matrix.hs | |||
@@ -5,6 +5,7 @@ | |||
5 | {-# LANGUAGE TypeOperators #-} | 5 | {-# LANGUAGE TypeOperators #-} |
6 | {-# LANGUAGE TypeFamilies #-} | 6 | {-# LANGUAGE TypeFamilies #-} |
7 | {-# LANGUAGE ViewPatterns #-} | 7 | {-# LANGUAGE ViewPatterns #-} |
8 | {-# LANGUAGE DeriveGeneric #-} | ||
8 | {-# LANGUAGE ConstrainedClassMethods #-} | 9 | {-# LANGUAGE ConstrainedClassMethods #-} |
9 | 10 | ||
10 | -- | | 11 | -- | |
diff --git a/packages/base/src/Internal/Static.hs b/packages/base/src/Internal/Static.hs index 2c31097..a0af085 100644 --- a/packages/base/src/Internal/Static.hs +++ b/packages/base/src/Internal/Static.hs | |||
@@ -13,6 +13,7 @@ | |||
13 | {-# LANGUAGE TypeOperators #-} | 13 | {-# LANGUAGE TypeOperators #-} |
14 | {-# LANGUAGE ViewPatterns #-} | 14 | {-# LANGUAGE ViewPatterns #-} |
15 | {-# LANGUAGE BangPatterns #-} | 15 | {-# LANGUAGE BangPatterns #-} |
16 | {-# LANGUAGE DeriveGeneric #-} | ||
16 | 17 | ||
17 | {- | | 18 | {- | |
18 | Module : Internal.Static | 19 | Module : Internal.Static |
@@ -35,13 +36,34 @@ import Data.Proxy(Proxy) | |||
35 | import Foreign.Storable(Storable) | 36 | import Foreign.Storable(Storable) |
36 | import Text.Printf | 37 | import Text.Printf |
37 | 38 | ||
39 | import Data.Binary | ||
40 | import GHC.Generics (Generic) | ||
41 | import Data.Proxy (Proxy(..)) | ||
42 | |||
38 | -------------------------------------------------------------------------------- | 43 | -------------------------------------------------------------------------------- |
39 | 44 | ||
40 | type ℝ = Double | 45 | type ℝ = Double |
41 | type ℂ = Complex Double | 46 | type ℂ = Complex Double |
42 | 47 | ||
43 | newtype Dim (n :: Nat) t = Dim t | 48 | newtype Dim (n :: Nat) t = Dim t |
44 | deriving Show | 49 | deriving (Show, Generic) |
50 | |||
51 | instance Binary a => Binary (Complex a) | ||
52 | where | ||
53 | put (r :+ i) = put (r, i) | ||
54 | get = (\(r,i) -> r :+ i) <$> get | ||
55 | |||
56 | instance (KnownNat n, Binary a) => Binary (Dim n a) where | ||
57 | get = do | ||
58 | k <- get | ||
59 | let n = natVal (Proxy :: Proxy n) | ||
60 | if n == k | ||
61 | then Dim <$> get | ||
62 | else fail ("Expected dimension " ++ (show n) ++ ", but found dimension " ++ (show k)) | ||
63 | |||
64 | put (Dim x) = do | ||
65 | put (natVal (Proxy :: Proxy n)) | ||
66 | put x | ||
45 | 67 | ||
46 | lift1F | 68 | lift1F |
47 | :: (c t -> c t) | 69 | :: (c t -> c t) |
@@ -59,15 +81,16 @@ instance NFData t => NFData (Dim n t) where | |||
59 | -------------------------------------------------------------------------------- | 81 | -------------------------------------------------------------------------------- |
60 | 82 | ||
61 | newtype R n = R (Dim n (Vector ℝ)) | 83 | newtype R n = R (Dim n (Vector ℝ)) |
62 | deriving (Num,Fractional,Floating) | 84 | deriving (Num,Fractional,Floating,Generic,Binary) |
63 | 85 | ||
64 | newtype C n = C (Dim n (Vector ℂ)) | 86 | newtype C n = C (Dim n (Vector ℂ)) |
65 | deriving (Num,Fractional,Floating) | 87 | deriving (Num,Fractional,Floating,Generic,Binary) |
66 | 88 | ||
67 | newtype L m n = L (Dim m (Dim n (Matrix ℝ))) | 89 | newtype L m n = L (Dim m (Dim n (Matrix ℝ))) |
90 | deriving (Generic, Binary) | ||
68 | 91 | ||
69 | newtype M m n = M (Dim m (Dim n (Matrix ℂ))) | 92 | newtype M m n = M (Dim m (Dim n (Matrix ℂ))) |
70 | 93 | deriving (Generic, Binary) | |
71 | 94 | ||
72 | mkR :: Vector ℝ -> R n | 95 | mkR :: Vector ℝ -> R n |
73 | mkR = R . Dim | 96 | mkR = R . Dim |