hmatrix-vector-sized 0.1.1.3 → 0.1.2.0
raw patch · 4 files changed
+199/−46 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Numeric.LinearAlgebra.Static.Vector: cVec :: C n -> Vector n ℂ
+ Numeric.LinearAlgebra.Static.Vector: cVec :: KnownNat n => C n -> Vector n ℂ
- Numeric.LinearAlgebra.Static.Vector: colsL :: forall m n. () => Vector n (R m) -> L m n
+ Numeric.LinearAlgebra.Static.Vector: colsL :: forall m n. KnownNat m => Vector n (R m) -> L m n
- Numeric.LinearAlgebra.Static.Vector: colsM :: forall m n. () => Vector n (C m) -> M m n
+ Numeric.LinearAlgebra.Static.Vector: colsM :: forall m n. KnownNat m => Vector n (C m) -> M m n
- Numeric.LinearAlgebra.Static.Vector: lCols :: forall m n. () => L m n -> Vector n (R m)
+ Numeric.LinearAlgebra.Static.Vector: lCols :: forall m n. (KnownNat m, KnownNat n) => L m n -> Vector n (R m)
- Numeric.LinearAlgebra.Static.Vector: lRows :: forall m n. () => L m n -> Vector m (R n)
+ Numeric.LinearAlgebra.Static.Vector: lRows :: forall m n. (KnownNat m, KnownNat n) => L m n -> Vector m (R n)
- Numeric.LinearAlgebra.Static.Vector: lVec :: forall m n. () => L m n -> Vector (m * n) ℝ
+ Numeric.LinearAlgebra.Static.Vector: lVec :: forall m n. (KnownNat m, KnownNat n) => L m n -> Vector (m * n) ℝ
- Numeric.LinearAlgebra.Static.Vector: mCols :: forall m n. () => M m n -> Vector n (C m)
+ Numeric.LinearAlgebra.Static.Vector: mCols :: forall m n. (KnownNat m, KnownNat n) => M m n -> Vector n (C m)
- Numeric.LinearAlgebra.Static.Vector: mRows :: forall m n. () => M m n -> Vector m (C n)
+ Numeric.LinearAlgebra.Static.Vector: mRows :: forall m n. (KnownNat m, KnownNat n) => M m n -> Vector m (C n)
- Numeric.LinearAlgebra.Static.Vector: mVec :: forall m n. () => M m n -> Vector (m * n) ℂ
+ Numeric.LinearAlgebra.Static.Vector: mVec :: forall m n. (KnownNat m, KnownNat n) => M m n -> Vector (m * n) ℂ
- Numeric.LinearAlgebra.Static.Vector: rVec :: R n -> Vector n ℝ
+ Numeric.LinearAlgebra.Static.Vector: rVec :: KnownNat n => R n -> Vector n ℝ
- Numeric.LinearAlgebra.Static.Vector: rowsL :: forall m n. () => Vector m (R n) -> L m n
+ Numeric.LinearAlgebra.Static.Vector: rowsL :: forall m n. KnownNat n => Vector m (R n) -> L m n
- Numeric.LinearAlgebra.Static.Vector: rowsM :: forall m n. () => Vector m (C n) -> M m n
+ Numeric.LinearAlgebra.Static.Vector: rowsM :: forall m n. KnownNat n => Vector m (C n) -> M m n
Files
- CHANGELOG.md +12/−0
- hmatrix-vector-sized.cabal +3/−3
- src/Numeric/LinearAlgebra/Static/Vector.hs +24/−24
- test/Spec.hs +160/−19
CHANGELOG.md view
@@ -1,6 +1,18 @@ Changelog ========= +Version 0.1.2.0+---------------++*August 17, 2019*++<https://github.com/mstksg/hmatrix-vector-sized/releases/tag/v0.1.2.0>++* Fixed bugs that would occur if ever converting anything that contained an+ hmatrix vector or matrix that was created using `konst`. This does change+ the API slightly in a potentially breaking way, as some functions now+ require `KnownNat` constraints.+ Version 0.1.1.x ---------------
hmatrix-vector-sized.cabal view
@@ -1,13 +1,13 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.31.1.+-- This file has been generated from package.yaml by hpack version 0.31.2. -- -- see: https://github.com/sol/hpack ----- hash: 677d211149f0d40bd2024eacc652a3939c10f05a788fd1fc438aa4a7b14ca00c+-- hash: 333f0f10beb5c9bbb414d7f8ad6e7e23eb992e393a3e945cd300ec8b6e1be88b name: hmatrix-vector-sized-version: 0.1.1.3+version: 0.1.2.0 synopsis: Conversions between hmatrix and vector-sized types description: Conversions between statically sized types in hmatrix and vector-sized. .
src/Numeric/LinearAlgebra/Static/Vector.hs view
@@ -63,8 +63,8 @@ -- | Convert an /hmatrix/ vector (parameterized by its lenth) to -- a /vector-sized/ storable vector of 'Double's.-rVec :: H.R n -> VS.Vector n H.ℝ-rVec = unsafeCoerce+rVec :: KnownNat n => H.R n -> VS.Vector n H.ℝ+rVec = unsafeCoerce . H.extract -- | Convert a /vector-sized/ storable vector to an /hmatrix/ vector -- (parameterized by its lenth).@@ -74,8 +74,8 @@ -- | Convert an /hmatrix/ complex vector (parameterized by its lenth) to -- a /vector-sized/ storable vector of 'Complex Double's, preserving the -- length in the type.-cVec :: H.C n -> VS.Vector n H.ℂ-cVec = unsafeCoerce+cVec :: KnownNat n => H.C n -> VS.Vector n H.ℂ+cVec = unsafeCoerce . H.extract -- | Convert a /vector-sized/ storable vector to an /hmatrix/ complex -- vector (parameterized by its lenth), preserving the length in the type.@@ -85,91 +85,91 @@ -- | Split an /hmatrix/ matrix (parameterized by its dimensions) to -- a /vector-sized/ boxed vector of its rows (as /hmatrix/ vectors). lRows- :: forall m n. ()+ :: forall m n. (KnownNat m, KnownNat n) => H.L m n -> V.Vector m (H.R n) lRows = unsafeCoerce . UV.fromList . HU.toRows- . (unsafeCoerce :: H.L m n -> HU.Matrix Double)+ . H.extract -- | Join together a /vector-sized/ boxed vector of /hmatrix/ vectors to an -- /hmatrix/ matrix as its rows. rowsL- :: forall m n. ()+ :: forall m n. KnownNat n => V.Vector m (H.R n) -> H.L m n rowsL = (unsafeCoerce :: HU.Matrix Double -> H.L m n) . HU.fromRows- . (unsafeCoerce :: [H.R n] -> [HU.Vector Double])+ . map H.extract . toList -- | Split an /hmatrix/ matrix (parameterized by its dimensions) to -- a /vector-sized/ boxed vector of its columns (as /hmatrix/ vectors). lCols- :: forall m n. ()+ :: forall m n. (KnownNat m, KnownNat n) => H.L m n -> V.Vector n (H.R m) lCols = unsafeCoerce . UV.fromList . HU.toColumns- . (unsafeCoerce :: H.L m n -> HU.Matrix Double)+ . H.extract -- | Join together a /vector-sized/ boxed vector of /hmatrix/ vectors to an -- /hmatrix/ matrix as its columns. colsL- :: forall m n. ()+ :: forall m n. KnownNat m => V.Vector n (H.R m) -> H.L m n colsL = (unsafeCoerce :: HU.Matrix Double -> H.L m n) . HU.fromColumns- . (unsafeCoerce :: [H.R m] -> [HU.Vector Double])+ . map H.extract . toList -- | Split an /hmatrix/ complex matrix (parameterized by its dimensions) to -- a /vector-sized/ boxed vector of its rows (as /hmatrix/ complex -- vectors). mRows- :: forall m n. ()+ :: forall m n. (KnownNat m, KnownNat n) => H.M m n -> V.Vector m (H.C n) mRows = unsafeCoerce . UV.fromList . HU.toRows- . (unsafeCoerce :: H.M m n -> HU.Matrix H.ℂ)+ . H.extract -- | Join together a /vector-sized/ boxed vector of /hmatrix/ complex -- vectors to an /hmatrix/ complex matrix as its rows. rowsM- :: forall m n. ()+ :: forall m n. KnownNat n => V.Vector m (H.C n) -> H.M m n rowsM = (unsafeCoerce :: HU.Matrix H.ℂ -> H.M m n) . HU.fromRows- . (unsafeCoerce :: [H.C n] -> [HU.Vector H.ℂ])+ . map H.extract . toList -- | Split an /hmatrix/ complex matrix (parameterized by its dimensions) to -- a /vector-sized/ boxed vector of its columns (as /hmatrix/ complex -- vectors). mCols- :: forall m n. ()+ :: forall m n. (KnownNat m, KnownNat n) => H.M m n -> V.Vector n (H.C m) mCols = unsafeCoerce . UV.fromList . HU.toColumns- . (unsafeCoerce :: H.M m n -> HU.Matrix H.ℂ)+ . H.extract -- | Join together a /vector-sized/ boxed vector of /hmatrix/ complex -- vectors to an /hmatrix/ complex matrix as its columns. colsM- :: forall m n. ()+ :: forall m n. KnownNat m => V.Vector n (H.C m) -> H.M m n colsM = (unsafeCoerce :: HU.Matrix H.ℂ -> H.M m n) . HU.fromColumns- . (unsafeCoerce :: [H.C m] -> [HU.Vector H.ℂ])+ . map H.extract . toList -- | Shape a /vector-sized/ storable vector of elements into an /hmatrix/@@ -189,12 +189,12 @@ -- -- @since 0.1.1.0 lVec- :: forall m n. ()+ :: forall m n. (KnownNat m, KnownNat n) => H.L m n -> VS.Vector (m * n) H.ℝ lVec = unsafeCoerce . HU.flatten- . (unsafeCoerce :: H.L m n -> HU.Matrix H.ℝ)+ . H.extract -- | Shape a /vector-sized/ storable vector of elements into an /hmatrix/ -- complex matrix.@@ -213,10 +213,10 @@ -- -- @since 0.1.1.0 mVec- :: forall m n. ()+ :: forall m n. (KnownNat m, KnownNat n) => H.M m n -> VS.Vector (m * n) H.ℂ mVec = unsafeCoerce . HU.flatten- . (unsafeCoerce :: H.M m n -> HU.Matrix H.ℂ)+ . H.extract
test/Spec.hs view
@@ -18,8 +18,11 @@ import Data.Proxy import GHC.TypeLits import Hedgehog+import Numeric.Natural import System.Exit import System.IO+import qualified Data.Vector.Generic as UVG+import qualified Data.Vector.Generic.Sized as VG import qualified Data.Vector.Sized as V import qualified Data.Vector.Storable.Sized as VS import qualified Hedgehog.Gen as Gen@@ -40,54 +43,88 @@ instance (KnownNat m, KnownNat n) => Eq (H.M m n) where (==) = (==) `on` H.extract +someNatVal' :: Natural -> SomeNat+someNatVal' = fromJust . someNatVal . fromIntegral+ genDouble :: Gen H.ℝ genDouble = Gen.double (Range.linearFracFrom 0 (-10) 10) genComplex :: Gen H.ℂ genComplex = (:+) <$> genDouble <*> genDouble +sizeRange :: Num a => Range a+sizeRange = Range.constant 5 10++validVector+ :: (UVG.Vector v a, KnownNat n, MonadTest m)+ => VG.Vector v n a+ -> m ()+validVector v = VG.length v === UVG.length (VG.fromSized v)+ prop_rVec :: Property prop_rVec = property $ do- xs <- forAll $ Gen.list (Range.constant 5 10) genDouble- case fromJust $ someNatVal (fromIntegral (length xs)) of- SomeNat (Proxy :: Proxy n) ->- tripping (H.vector @n xs)- (VS.map (* 2) . H.rVec)- (Identity . (/ 2) . H.vecR)+ xs <- forAll $ Gen.list sizeRange genDouble+ SomeNat (Proxy :: Proxy n) <- pure $ someNatVal' (fromIntegral (length xs))+ tripping (H.vector @n xs)+ (VS.map (* 2) . H.rVec)+ (Identity . (/ 2) . H.vecR) +prop_rVec_konst :: Property+prop_rVec_konst = property $ do+ x <- forAll genDouble+ SomeNat (Proxy :: Proxy n) <- fmap someNatVal' . forAll+ $ Gen.integral sizeRange+ validVector . H.rVec @n $ H.konst x+ prop_vecR :: Property prop_vecR = property $ do- xs <- forAll $ Gen.list (Range.constant 5 10) genDouble+ xs <- forAll $ Gen.list sizeRange genDouble VS.withSizedList xs $ \v -> tripping v ((* 2) . H.vecR) (Identity . VS.map (/ 2) . H.rVec) prop_cVec :: Property prop_cVec = property $ do- xs <- forAll $ Gen.list (Range.constant 5 10) genComplex- case fromJust $ someNatVal (fromIntegral (length xs)) of- SomeNat (Proxy :: Proxy n) ->- tripping (fromJust . H.create $ HU.fromList xs)- (VS.map (* 2) . H.cVec @n)- (Identity . (/ 2) . H.vecC)+ xs <- forAll $ Gen.list sizeRange genComplex+ SomeNat (Proxy :: Proxy n) <- pure $ someNatVal' (fromIntegral (length xs))+ tripping (fromJust . H.create $ HU.fromList xs)+ (VS.map (* 2) . H.cVec @n)+ (Identity . (/ 2) . H.vecC) +prop_cVec_konst :: Property+prop_cVec_konst = property $ do+ x <- forAll genComplex+ SomeNat (Proxy :: Proxy n) <- fmap someNatVal' . forAll+ $ Gen.integral sizeRange+ validVector . H.cVec @n $ H.konst x+ prop_vecC :: Property prop_vecC = property $ do- xs <- forAll $ Gen.list (Range.constant 5 10) genComplex+ xs <- forAll $ Gen.list sizeRange genComplex VS.withSizedList xs $ \v -> tripping v ((* 2) . H.vecC) (Identity . VS.map (/ 2) . H.cVec) genMatList :: Gen a -> Gen (SomeNat, SomeNat, [[a]]) genMatList g = do- m <- Gen.int (Range.constant 5 10)- n <- Gen.int (Range.constant 5 10)- xs <- (replicateM m . replicateM n) g- return ( fromJust . someNatVal . fromIntegral $ m- , fromJust . someNatVal . fromIntegral $ n+ m <- Gen.integral sizeRange+ n <- Gen.integral sizeRange+ xs <- (replicateM (fromIntegral m) . replicateM (fromIntegral n)) g+ return ( someNatVal' m+ , someNatVal' n , xs ) +genMatKonst :: Gen a -> Gen (SomeNat, SomeNat, a)+genMatKonst g = do+ m <- Gen.integral sizeRange+ n <- Gen.integral sizeRange+ x <- g+ return ( someNatVal' m+ , someNatVal' n+ , x+ )+ prop_lRows :: Property prop_lRows = property $ do ( SomeNat (Proxy :: Proxy m)@@ -98,6 +135,14 @@ (V.map (* 2) . H.lRows @m @n) (Identity . (/ 2) . H.rowsL) +prop_lRows_konst :: Property+prop_lRows_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genDouble+ validVector . H.lRows @m @n $ H.konst x+ prop_rowsL :: Property prop_rowsL = property $ do (_, SomeNat (Proxy :: Proxy n), xs) <- forAll $ genMatList genDouble@@ -106,6 +151,18 @@ ((* 2) . H.rowsL @m @n) (Identity . V.map (/ 2) . H.lRows) +prop_rowsL_konst :: Property+prop_rowsL_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genDouble+ let rs = V.replicate $ H.konst x+ validVector . H.lVec . H.rowsL @m @n $ rs+ tripping rs+ ((* 2) . H.rowsL @m @n)+ (Identity . V.map (/ 2) . H.lRows)+ prop_lCols :: Property prop_lCols = property $ do ( SomeNat (Proxy :: Proxy m)@@ -116,6 +173,14 @@ (V.map (* 2) . H.lCols @m @n) (Identity . (/ 2) . H.colsL) +prop_lCols_konst :: Property+prop_lCols_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genDouble+ validVector . H.lCols @m @n $ H.konst x+ prop_colsL :: Property prop_colsL = property $ do (_, SomeNat (Proxy :: Proxy m), xs) <- forAll $ genMatList genDouble@@ -124,6 +189,18 @@ ((* 2) . H.colsL @m @n) (Identity . V.map (/ 2) . H.lCols) +prop_colsL_konst :: Property+prop_colsL_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genDouble+ let cs = V.replicate $ H.konst x+ validVector . H.lVec . H.colsL @m @n $ cs+ tripping cs+ ((* 2) . H.colsL @m @n)+ (Identity . V.map (/ 2) . H.lCols)+ prop_vecL :: Property prop_vecL = property $ do ( SomeNat (Proxy :: Proxy m)@@ -149,6 +226,18 @@ tripping m (VS.map (* 2) . H.lVec) (Identity . (/ 2) . H.vecL) +prop_lVec_konst :: Property+prop_lVec_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genDouble+ let xs = H.konst x+ validVector $ H.lVec @m @n xs+ tripping xs+ (VS.map (* 2) . H.lVec @m @n)+ (Identity . (/ 2) . H.vecL)+ prop_mRows :: Property prop_mRows = property $ do ( SomeNat (Proxy :: Proxy m)@@ -159,6 +248,14 @@ (V.map (* 2) . H.mRows @m @n) (Identity . (/ 2) . H.rowsM) +prop_mRows_konst :: Property+prop_mRows_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genComplex+ validVector . H.mRows @m @n $ H.konst x+ prop_rowsM :: Property prop_rowsM = property $ do (_, SomeNat (Proxy :: Proxy n), xs) <- forAll $ genMatList genComplex@@ -167,6 +264,18 @@ ((* 2) . H.rowsM @m @n) (Identity . V.map (/ 2) . H.mRows) +prop_rowsM_konst :: Property+prop_rowsM_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genComplex+ let rs = V.replicate $ H.konst x+ validVector . H.mVec . H.rowsM @m @n $ rs+ tripping rs+ ((* 2) . H.rowsM @m @n)+ (Identity . V.map (/ 2) . H.mRows)+ prop_mCols :: Property prop_mCols = property $ do ( SomeNat (Proxy :: Proxy m)@@ -177,6 +286,14 @@ (V.map (* 2) . H.mCols @m @n) (Identity . (/ 2) . H.colsM) +prop_mCols_konst :: Property+prop_mCols_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genComplex+ validVector . H.mCols @m @n $ H.konst x+ prop_colsM :: Property prop_colsM = property $ do (_, SomeNat (Proxy :: Proxy m), xs) <- forAll $ genMatList genComplex@@ -185,6 +302,18 @@ ((* 2) . H.colsM @m @n) (Identity . V.map (/ 2) . H.mCols) +prop_colsM_konst :: Property+prop_colsM_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genComplex+ let cs = V.replicate $ H.konst x+ validVector . H.mVec . H.colsM @m @n $ cs+ tripping cs+ ((* 2) . H.colsM @m @n)+ (Identity . V.map (/ 2) . H.mCols)+ prop_vecM :: Property prop_vecM = property $ do ( SomeNat (Proxy :: Proxy m)@@ -209,6 +338,18 @@ $ concat xs tripping m (VS.map (* 2) . H.mVec) (Identity . (/ 2) . H.vecM)++prop_mVec_konst :: Property+prop_mVec_konst = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , x+ ) <- forAll $ genMatKonst genComplex+ let xs = H.konst x+ validVector $ H.mVec @m @n xs+ tripping xs+ (VS.map (* 2) . H.mVec @m @n)+ (Identity . (/ 2) . H.vecM) main :: IO ()