diff options
author | Justin Le <justin@jle.im> | 2016-01-07 00:59:48 -0800 |
---|---|---|
committer | Justin Le <justin@jle.im> | 2016-01-07 01:00:15 -0800 |
commit | cde00d2c7a660cb8f6edf7b4b308b66232b9b230 (patch) | |
tree | ff4fdc08ad53b3b25b32e19b3fd2c7572fba64b4 | |
parent | 69e0f0c19251cd2abe08cc5f0711f6ac7e42a2ce (diff) |
added extactLength and exactDims, which are useful for constraining multiple dependently typed vectors or matrices to be the same size when they were previously unknown at compile time
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Static.hs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/packages/base/src/Numeric/LinearAlgebra/Static.hs b/packages/base/src/Numeric/LinearAlgebra/Static.hs index b551cd9..7790aef 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Static.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Static.hs | |||
@@ -55,7 +55,7 @@ module Numeric.LinearAlgebra.Static( | |||
55 | -- * Misc | 55 | -- * Misc |
56 | mean, | 56 | mean, |
57 | Disp(..), Domain(..), | 57 | Disp(..), Domain(..), |
58 | withVector, withMatrix, | 58 | withVector, exactLength, withMatrix, exactDims, |
59 | toRows, toColumns, | 59 | toRows, toColumns, |
60 | Sized(..), Diag(..), Sym, sym, mTm, unSym, (<·>) | 60 | Sized(..), Diag(..), Sym, sym, mTm, unSym, (<·>) |
61 | ) where | 61 | ) where |
@@ -70,7 +70,7 @@ import Numeric.LinearAlgebra hiding ( | |||
70 | qr,size,dot,chol,range,R,C,sym,mTm,unSym) | 70 | qr,size,dot,chol,range,R,C,sym,mTm,unSym) |
71 | import qualified Numeric.LinearAlgebra as LA | 71 | import qualified Numeric.LinearAlgebra as LA |
72 | import qualified Numeric.LinearAlgebra.Devel as LA | 72 | import qualified Numeric.LinearAlgebra.Devel as LA |
73 | import Data.Proxy(Proxy) | 73 | import Data.Proxy(Proxy(..)) |
74 | import Internal.Static | 74 | import Internal.Static |
75 | import Control.Arrow((***)) | 75 | import Control.Arrow((***)) |
76 | 76 | ||
@@ -395,6 +395,18 @@ withVector v f = | |||
395 | Nothing -> error "static/dynamic mismatch" | 395 | Nothing -> error "static/dynamic mismatch" |
396 | Just (SomeNat (_ :: Proxy m)) -> f (mkR v :: R m) | 396 | Just (SomeNat (_ :: Proxy m)) -> f (mkR v :: R m) |
397 | 397 | ||
398 | -- | Useful for constraining two dependently typed vectors to match each | ||
399 | -- other in length when they are unknown at compile-time. | ||
400 | exactLength | ||
401 | :: forall n m. (KnownNat n, KnownNat m) | ||
402 | => R m | ||
403 | -> Maybe (R n) | ||
404 | exactLength v | ||
405 | | natVal (Proxy :: Proxy n) == natVal (Proxy :: Proxy m) | ||
406 | = Just (mkR (unwrap v)) | ||
407 | | otherwise | ||
408 | = Nothing | ||
409 | |||
398 | withMatrix | 410 | withMatrix |
399 | :: forall z | 411 | :: forall z |
400 | . Matrix ℝ | 412 | . Matrix ℝ |
@@ -409,6 +421,20 @@ withMatrix a f = | |||
409 | Just (SomeNat (_ :: Proxy n)) -> | 421 | Just (SomeNat (_ :: Proxy n)) -> |
410 | f (mkL a :: L m n) | 422 | f (mkL a :: L m n) |
411 | 423 | ||
424 | -- | Useful for constraining two dependently typed matrices to match each | ||
425 | -- other in dimensions when they are unknown at compile-time. | ||
426 | exactDims | ||
427 | :: forall n m j k. (KnownNat n, KnownNat m, KnownNat j, KnownNat k) | ||
428 | => L m n | ||
429 | -> Maybe (L j k) | ||
430 | exactDims m | ||
431 | | natVal (Proxy :: Proxy m) == natVal (Proxy :: Proxy j) | ||
432 | && natVal (Proxy :: Proxy n) == natVal (Proxy :: Proxy k) | ||
433 | = Just (mkL (unwrap m)) | ||
434 | | otherwise | ||
435 | = Nothing | ||
436 | |||
437 | |||
412 | -------------------------------------------------------------------------------- | 438 | -------------------------------------------------------------------------------- |
413 | 439 | ||
414 | class Domain field vec mat | mat -> vec field, vec -> mat field, field -> mat vec | 440 | class Domain field vec mat | mat -> vec field, vec -> mat field, field -> mat vec |