hmatrix-vector-sized (empty) → 0.1.0.0
raw patch · 7 files changed
+440/−0 lines, 7 filesdep +basedep +hedgehogdep +hmatrixsetup-changed
Dependencies added: base, hedgehog, hmatrix, hmatrix-vector-sized, vector, vector-sized
Files
- CHANGELOG.md +11/−0
- LICENSE +30/−0
- README.md +11/−0
- Setup.hs +2/−0
- hmatrix-vector-sized.cabal +62/−0
- src/Numeric/LinearAlgebra/Static/Vector.hs +160/−0
- test/Spec.hs +164/−0
+ CHANGELOG.md view
@@ -0,0 +1,11 @@+Changelog+=========++Version 0.1.0.0+---------------++*Feb 10, 2018*++<https://github.com/mstksg/hmatrix-vector-sized/releases/tag/v0.1.0.0>++* Initial release
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Justin Le (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Justin Le nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,11 @@+# hmatrix-vector-sized++[](https://hackage.haskell.org/package/hmatrix-vector-sized)+[](https://travis-ci.org/mstksg/hmatrix-vector-sized)++Conversion between statically sized types in *[hmatrix][]* (in the+*[Numeric.LinearAlgebra.Static][static]* module) and *[vector-sized][]*.++[hmatrix]: http://hackage.haskell.org/package/hmatrix+[vector-sized]: http://hackage.haskell.org/package/vector-sized+[static]: https://hackage.haskell.org/package/hmatrix-0.18.2.0/docs/Numeric-LinearAlgebra-Static.html
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hmatrix-vector-sized.cabal view
@@ -0,0 +1,62 @@+-- This file has been generated from package.yaml by hpack version 0.20.0.+--+-- see: https://github.com/sol/hpack+--+-- hash: 70aa6c0a47ec2f24e7aa0c898db148bf6e298635849cef213bfdd866941396e3++name: hmatrix-vector-sized+version: 0.1.0.0+synopsis: Conversions between hmatrix and vector-sized types+description: Conversions between statically sized types in hmatrix and vector-sized.+ .+ See README on Github <https://github.com/mstksg/hmatrix-vector-sized#readme>+category: Math+homepage: https://github.com/mstksg/hmatrix-vector-sized#readme+bug-reports: https://github.com/mstksg/hmatrix-vector-sized/issues+author: Justin Le+maintainer: justin@jle.im+copyright: (c) Justin Le 2018+license: BSD3+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10++extra-source-files:+ CHANGELOG.md+ README.md++source-repository head+ type: git+ location: https://github.com/mstksg/hmatrix-vector-sized++library+ hs-source-dirs:+ src+ ghc-options: -Wall -Wcompat -Wincomplete-record-updates -Wredundant-constraints+ build-depends:+ base >=4.7 && <5+ , hmatrix >=0.18+ , vector+ , vector-sized >=0.4.1+ exposed-modules:+ Numeric.LinearAlgebra.Static.Vector+ other-modules:+ Paths_hmatrix_vector_sized+ default-language: Haskell2010++test-suite hmatrix-vector-sized-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ hs-source-dirs:+ test+ ghc-options: -Wall -Wcompat -Wincomplete-record-updates -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ base >=4.7 && <5+ , hedgehog+ , hmatrix >=0.18+ , hmatrix-vector-sized+ , vector+ , vector-sized >=0.4.1+ other-modules:+ Paths_hmatrix_vector_sized+ default-language: Haskell2010
+ src/Numeric/LinearAlgebra/Static/Vector.hs view
@@ -0,0 +1,160 @@+{-# LANGUAGE ScopedTypeVariables #-}++-- |+-- Module : Numeric.LinearAlgebra.Static.Vector+-- Copyright : (c) Justin Le 2018+-- License : BSD3+--+-- Maintainer : justin@jle.im+-- Stability : experimental+-- Portability : non-portable+--+-- Conversions between statically sized types in+-- "Numeric.LinearAlgebra.Static" from /hmatrix/ and /vector-sized/.+--+-- This module is intentionally minimal, exporting only functions that+-- cannot be written without "unsafe" operations. With these, however, you+-- can easily write other useful combinators by using type-safe operations+-- like 'fmap', 'VS.map', 'liftA2', 'Data.Vector.Generic.Sized.convert',+-- etc.+--++module Numeric.LinearAlgebra.Static.Vector (+ -- * Vector+ -- ** Real+ rVec+ , vecR+ -- ** Complex+ , cVec+ , vecC+ -- * Matrix+ -- ** Real+ , lRows+ , rowsL+ , lCols+ , colsL+ -- ** Complex+ , mRows+ , rowsM+ , mCols+ , colsM+ ) where++import Data.Foldable+import Unsafe.Coerce+import qualified Data.Vector as UV+import qualified Data.Vector.Sized as V+import qualified Data.Vector.Storable.Sized as VS+import qualified Numeric.LinearAlgebra as HU+import qualified Numeric.LinearAlgebra.Static as H++-- | 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++-- | Convert a /vector-sized/ storable vector to an /hmatrix/ vector+-- (parameterized by its lenth).+vecR :: VS.Vector n H.ℝ -> H.R n+vecR = unsafeCoerce++-- | 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++-- | Convert a /vector-sized/ storable vector to an /hmatrix/ complex+-- vector (parameterized by its lenth), preserving the length in the type.+vecC :: VS.Vector n H.ℂ -> H.C n+vecC = unsafeCoerce++-- | Split an /hmatrix/ matrix (parameterized by its dimensions) to+-- a /vector-sized/ boxed vector of its rows (as /hmatrix/ vectors).+lRows+ :: forall m 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)++-- | Join together a /vector-sized/ boxed vector of /hmatrix/ vectors to an+-- /hmatrix/ matrix as its rows.+rowsL+ :: forall m n. ()+ => V.Vector m (H.R n)+ -> H.L m n+rowsL = (unsafeCoerce :: HU.Matrix Double -> H.L m n)+ . HU.fromRows+ . map (unsafeCoerce :: H.R n -> HU.Vector Double)+ . 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. ()+ => H.L m n+ -> V.Vector n (H.R m)+lCols = unsafeCoerce+ . UV.fromList+ . HU.toColumns+ . (unsafeCoerce :: H.L m n -> HU.Matrix Double)++-- | Join together a /vector-sized/ boxed vector of /hmatrix/ vectors to an+-- /hmatrix/ matrix as its columns.+colsL+ :: forall m n. ()+ => V.Vector n (H.R m)+ -> H.L m n+colsL = (unsafeCoerce :: HU.Matrix Double -> H.L m n)+ . HU.fromColumns+ . map (unsafeCoerce :: H.R m -> HU.Vector Double)+ . 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. ()+ => H.M m n+ -> V.Vector m (H.C n)+mRows = unsafeCoerce+ . UV.fromList+ . HU.toRows+ . (unsafeCoerce :: H.M m n -> HU.Matrix H.ℂ)++-- | Join together a /vector-sized/ boxed vector of /hmatrix/ complex+-- vectors to an /hmatrix/ complex matrix as its rows.+rowsM+ :: forall m n. ()+ => V.Vector m (H.C n)+ -> H.M m n+rowsM = (unsafeCoerce :: HU.Matrix H.ℂ -> H.M m n)+ . HU.fromRows+ . map (unsafeCoerce :: H.C n -> HU.Vector H.ℂ)+ . 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. ()+ => H.M m n+ -> V.Vector n (H.C m)+mCols = unsafeCoerce+ . UV.fromList+ . HU.toColumns+ . (unsafeCoerce :: H.M m n -> HU.Matrix H.ℂ)++-- | Join together a /vector-sized/ boxed vector of /hmatrix/ complex+-- vectors to an /hmatrix/ complex matrix as its columns.+colsM+ :: forall m n. ()+ => V.Vector n (H.C m)+ -> H.M m n+colsM = (unsafeCoerce :: HU.Matrix H.ℂ -> H.M m n)+ . HU.fromColumns+ . map (unsafeCoerce :: H.C m -> HU.Vector H.ℂ)+ . toList
+ test/Spec.hs view
@@ -0,0 +1,164 @@+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE TypeApplications #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++import Control.Monad+import Data.Complex+import Data.Function+import Data.Functor.Identity+import Data.Maybe+import Data.Proxy+import GHC.TypeLits+import Hedgehog+import System.Exit+import System.IO+import qualified Data.Vector.Sized as V+import qualified Data.Vector.Storable.Sized as VS+import qualified Hedgehog.Gen as Gen+import qualified Hedgehog.Range as Range+import qualified Numeric.LinearAlgebra as HU+import qualified Numeric.LinearAlgebra.Static as H+import qualified Numeric.LinearAlgebra.Static.Vector as H++instance KnownNat n => Eq (H.R n) where+ (==) = (==) `on` H.extract++instance KnownNat n => Eq (H.C n) where+ (==) = (==) `on` H.extract++instance (KnownNat m, KnownNat n) => Eq (H.L m n) where+ (==) = (==) `on` H.extract++instance (KnownNat m, KnownNat n) => Eq (H.M m n) where+ (==) = (==) `on` H.extract++genDouble :: Gen H.ℝ+genDouble = Gen.double (Range.linearFracFrom 0 (-10) 10)++genComplex :: Gen H.ℂ+genComplex = (:+) <$> genDouble <*> genDouble++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)++prop_vecR :: Property+prop_vecR = property $ do+ xs <- forAll $ Gen.list (Range.constant 5 10) 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)++prop_vecC :: Property+prop_vecC = property $ do+ xs <- forAll $ Gen.list (Range.constant 5 10) 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+ , xs+ )++prop_lRows :: Property+prop_lRows = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , xs+ ) <- forAll $ genMatList genDouble+ tripping (fromJust . H.create $ HU.fromLists xs)+ (V.map (* 2) . H.lRows @m @n)+ (Identity . (/ 2) . H.rowsL)++prop_rowsL :: Property+prop_rowsL = property $ do+ (_, SomeNat (Proxy :: Proxy n), xs) <- forAll $ genMatList genDouble+ V.withSizedList xs $ \(v :: V.Vector m [Double]) ->+ tripping (V.map (fromJust . H.create . HU.fromList) v)+ ((* 2) . H.rowsL @m @n)+ (Identity . V.map (/ 2) . H.lRows)++prop_lCols :: Property+prop_lCols = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , xs+ ) <- forAll $ genMatList genDouble+ tripping (fromJust . H.create $ HU.fromLists xs)+ (V.map (* 2) . H.lCols @m @n)+ (Identity . (/ 2) . H.colsL)++prop_colsL :: Property+prop_colsL = property $ do+ (_, SomeNat (Proxy :: Proxy m), xs) <- forAll $ genMatList genDouble+ V.withSizedList xs $ \(v :: V.Vector n [Double]) ->+ tripping (V.map (fromJust . H.create . HU.fromList) v)+ ((* 2) . H.colsL @m @n)+ (Identity . V.map (/ 2) . H.lCols)++prop_mRows :: Property+prop_mRows = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , xs+ ) <- forAll $ genMatList genComplex+ tripping (fromJust . H.create $ HU.fromLists xs)+ (V.map (* 2) . H.mRows @m @n)+ (Identity . (/ 2) . H.rowsM)++prop_rowsM :: Property+prop_rowsM = property $ do+ (_, SomeNat (Proxy :: Proxy n), xs) <- forAll $ genMatList genComplex+ V.withSizedList xs $ \(v :: V.Vector m [Complex Double]) ->+ tripping (V.map (fromJust . H.create . HU.fromList) v)+ ((* 2) . H.rowsM @m @n)+ (Identity . V.map (/ 2) . H.mRows)++prop_mCols :: Property+prop_mCols = property $ do+ ( SomeNat (Proxy :: Proxy m)+ , SomeNat (Proxy :: Proxy n)+ , xs+ ) <- forAll $ genMatList genComplex+ tripping (fromJust . H.create $ HU.fromLists xs)+ (V.map (* 2) . H.mCols @m @n)+ (Identity . (/ 2) . H.colsM)++prop_colsM :: Property+prop_colsM = property $ do+ (_, SomeNat (Proxy :: Proxy m), xs) <- forAll $ genMatList genComplex+ V.withSizedList xs $ \(v :: V.Vector n [Complex Double]) ->+ tripping (V.map (fromJust . H.create . HU.fromList) v)+ ((* 2) . H.colsM @m @n)+ (Identity . V.map (/ 2) . H.mCols)++main :: IO ()+main = do+ hSetBuffering stdout LineBuffering+ hSetBuffering stderr LineBuffering++ results <- checkParallel $$(discover)++ unless results exitFailure+