packages feed

LATS (empty) → 0.4.0

raw patch · 10 files changed

+322/−0 lines, 10 filesdep +basedep +constraint-classesdep +hmatrixsetup-changed

Dependencies added: base, constraint-classes, hmatrix, semigroups, vector

Files

+ LATS.cabal view
@@ -0,0 +1,36 @@+name:                LATS+version:             0.4.0+synopsis:            Linear Algebra on Typed Spaces+description:         Please see README.org+homepage:            http://github.com/guaraqe/lats#readme+license:             BSD3+license-file:        LICENSE+author:              guaraqe+maintainer:          guaraqe@openmailbox.org+copyright:           Copyright 2016 guaraqe+category:            Numeric+build-type:          Simple+cabal-version:       >=1.10++library+  hs-source-dirs:      src++  exposed-modules:     LinearAlgebra.TypedSpaces+                       LinearAlgebra.TypedSpaces.Classes+                       LinearAlgebra.TypedSpaces.Matrix+                       LinearAlgebra.TypedSpaces.Matrix.Raw+                       LinearAlgebra.TypedSpaces.Vector+                       LinearAlgebra.TypedSpaces.Vector.Raw+                       LinearAlgebra.TypedSpaces.Vector.Mutable++  build-depends:       base >= 4.7 && < 5+                     , constraint-classes+                     , hmatrix+                     , semigroups+                     , vector++  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/guaraqe/lats
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright guaraqe (c) 2016++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 guaraqe 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/LinearAlgebra/TypedSpaces.hs view
@@ -0,0 +1,6 @@+module LinearAlgebra.TypedSpaces+       ( module Export ) where++import LinearAlgebra.TypedSpaces.Classes as Export+import LinearAlgebra.TypedSpaces.Vector  as Export+import LinearAlgebra.TypedSpaces.Matrix  as Export
+ src/LinearAlgebra/TypedSpaces/Classes.hs view
@@ -0,0 +1,41 @@+{-# LANGUAGE ConstraintKinds        #-}+{-# LANGUAGE FunctionalDependencies #-}+{-# LANGUAGE MultiParamTypeClasses  #-}+{-# LANGUAGE TypeFamilies           #-}++module LinearAlgebra.TypedSpaces.Classes+       ( Isomorphism  (..)+       , CZippable    (..)+       , CIndexed     (..)+       , IsList       (..)+       , module CC ) where++import GHC.Exts (IsList (..))+import Control.ConstraintClasses as CC++----------------------------------------------------------------------++class Isomorphism a b where+  fw :: a -> b+  bw :: b -> a++----------------------------------------------------------------------++class CFunctor f => CZippable f where++  czipWith :: (CFun f a, CFun f b, CFun f c)+          => (a -> b -> c) -> f a -> f b -> f c++  czipWith3 :: (CFun f a, CFun f b, CFun f c, CFun f d)+            => (a -> b -> c -> d) -> f a -> f b -> f c -> f d++  czipWith4 :: (CFun f a, CFun f b, CFun f c, CFun f d, CFun f e)+            => (a -> b -> c -> d -> e) -> f a -> f b -> f c -> f d -> f e++----------------------------------------------------------------------++class CFunctor f => CIndexed f i | f -> i where++  (!) :: CFun f a => f a -> i -> a++----------------------------------------------------------------------
+ src/LinearAlgebra/TypedSpaces/Matrix.hs view
@@ -0,0 +1,119 @@+{-# LANGUAGE ConstraintKinds       #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TypeFamilies          #-}+{-# LANGUAGE FlexibleInstances     #-}+{-# LANGUAGE FlexibleContexts      #-}++module LinearAlgebra.TypedSpaces.Matrix+        ( Matrix (..)+        , L.Storable+        , L.Element+        , L.Numeric+        , L.Field+        , rows+        , cols+        , fromRows+        , toRows+        , fromColumns+        , toColumns+        , takeRows+        , takeColumns+        , tr+        , Sparse (..)+        , toDense+        , addToSparse+        , (<.>)+        , (#>)+        , (<#)+        , (#)+        , inv+        , pinv ) where++import qualified Foreign.Storable as L+import qualified Numeric.LinearAlgebra as L+import LinearAlgebra.TypedSpaces.Classes+import LinearAlgebra.TypedSpaces.Vector++newtype Matrix i j a = Matrix { matrix :: L.Matrix a }+  deriving (Show)++instance (Isomorphism Int i, Isomorphism Int j)+      => CIndexed (Matrix i j) (i,j) where++  (Matrix v) ! (m,n) = v `L.atIndex` (bw m, bw n)++instance CFunctor (Matrix i j) where++  type CFun (Matrix i j) a = ( L.Container L.Matrix a+                             , L.Storable a+                             , Num a )++  cmap f (Matrix m) = Matrix (L.cmap f m)++----------------------------------------------------------------------++rows :: Matrix i j a -> Int+rows = L.rows . matrix++cols :: Matrix i j a -> Int+cols = L.cols . matrix++fromRows :: L.Element a => [Vector j a] -> Matrix i j a+fromRows = Matrix . L.fromRows . map vector++toRows :: L.Element a => Matrix i j a -> [Vector j a]+toRows = map Vector . L.toRows . matrix++fromColumns :: L.Element a => [Vector i a] -> Matrix i j a+fromColumns = Matrix . L.fromColumns . map vector++toColumns :: L.Element a => Matrix i j a -> [Vector i a]+toColumns = map Vector . L.toColumns . matrix++takeRows :: L.Element a => Int -> Matrix i j a -> Matrix i j a+takeRows n (Matrix m) = Matrix (L.takeRows n m)++takeColumns :: L.Element a => Int -> Matrix i j a -> Matrix i j a+takeColumns n (Matrix m)= Matrix (L.takeColumns n m)++tr :: (L.Transposable (L.Matrix a) (L.Matrix a))+   => Matrix i j a -> Matrix j i a+tr (Matrix m) = Matrix (L.tr' m)++----------------------------------------------------------------------++newtype Sparse i j a = Sparse { sparse :: [((Int,Int),a)] }+  deriving (Show)++toDense :: Sparse i j Double -> Matrix i j Double+toDense = Matrix . L.toDense . sparse++addToSparse :: (Isomorphism Int i, Isomorphism Int j)+            => ((i,j),a) -> Sparse i j a -> Sparse i j a+addToSparse ((i,j),a) (Sparse l) = Sparse (((i',j'),a):l)+  where i' = bw i+        j' = bw j++----------------------------------------------------------------------++infixr 8 <.>+(<.>) :: (L.Numeric a) => Vector i a -> Vector i a -> a+(Vector v1) <.> (Vector v2) = v1 L.<.> v2++infixr 8 #>+(#>) :: (L.Numeric a) => Matrix i j a -> Vector j a -> Vector i a+(Matrix m) #> (Vector v) = Vector (m L.#> v)++infixl 8 <#+(<#) :: (L.Numeric a) => Vector i a -> Matrix i j a -> Vector j a+(Vector v) <# (Matrix m) = Vector (v L.<# m)++infixr 8 #+(#) :: (L.Numeric a) => Matrix i j a -> Matrix j k a -> Matrix i k a+(Matrix m1) # (Matrix m2) = Matrix (m1 L.<> m2)++inv :: (L.Field a) => Matrix i j a -> Matrix j i a+inv (Matrix m) = Matrix (L.inv m)++pinv :: (L.Field a) => Matrix i j a -> Matrix j i a+pinv (Matrix m) = Matrix (L.pinv m)
+ src/LinearAlgebra/TypedSpaces/Matrix/Raw.hs view
@@ -0,0 +1,4 @@+module LinearAlgebra.TypedSpaces.Matrix.Raw+       ( module HMatrix ) where++import Numeric.LinearAlgebra as HMatrix
+ src/LinearAlgebra/TypedSpaces/Vector.hs view
@@ -0,0 +1,76 @@+{-# LANGUAGE ConstraintKinds       #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TypeFamilies          #-}+{-# LANGUAGE FlexibleInstances     #-}+{-# LANGUAGE FlexibleContexts      #-}++module LinearAlgebra.TypedSpaces.Vector+       ( Vector (..) ) where++import qualified Data.Vector.Storable as L+import LinearAlgebra.TypedSpaces.Classes+import Data.Semigroup++newtype Vector i a = Vector { vector :: L.Vector a }+                   deriving (Show, Eq)++----------------------------------------------------------------------++instance (Isomorphism Int i) => CIndexed (Vector i) i where+  (Vector v) ! n = v L.! (bw n)++----------------------------------------------------------------------++instance CFunctor (Vector i) where++  type CFun (Vector i) a = L.Storable a+  cmap f (Vector v) = Vector (L.map f v)++----------------------------------------------------------------------++instance CFoldable (Vector i) where++  type CFol (Vector i) a = L.Storable a++  cfoldr f x = L.foldr f x . vector+  cfoldr' f x = L.foldr' f x . vector+  cfoldl f x = L.foldl f x . vector+  cfoldl' f x = L.foldl' f x . vector+  clength = L.length . vector+  cmapM f (Vector v) = Vector <$> L.mapM f v+  cmapM_ f = L.mapM_ f . vector++----------------------------------------------------------------------++instance (L.Storable a) => Semigroup (Vector i a) where++  (Vector v1) <> (Vector v2) = Vector (v1 L.++ v2)+++----------------------------------------------------------------------++instance (L.Storable a) => Monoid (Vector i a) where++  mempty = Vector L.empty+  mappend = (<>)++----------------------------------------------------------------------++instance CZippable (Vector i) where++  czipWith f (Vector v1) (Vector v2) =+    Vector (L.zipWith f v1 v2)++  czipWith3 f (Vector v1) (Vector v2) (Vector v3) =+    Vector (L.zipWith3 f v1 v2 v3)++  czipWith4 f (Vector v1) (Vector v2) (Vector v3) (Vector v4)=+    Vector (L.zipWith4 f v1 v2 v3 v4)+++instance (L.Storable a) => IsList (Vector i a) where++  type Item (Vector i a) = a++  fromList = Vector . L.fromList+  toList = L.toList . vector
+ src/LinearAlgebra/TypedSpaces/Vector/Mutable.hs view
@@ -0,0 +1,4 @@+module LinearAlgebra.TypedSpaces.Vector.Mutable+       ( module Mutable ) where++import Data.Vector.Storable.Mutable as Mutable
+ src/LinearAlgebra/TypedSpaces/Vector/Raw.hs view
@@ -0,0 +1,4 @@+module LinearAlgebra.TypedSpaces.Vector.Raw+       ( module Raw ) where++import Data.Vector.Storable as Raw