haskell-eigen-util (empty) → 0.1.0.0
raw patch · 5 files changed
+181/−0 lines, 5 filesdep +basedep +eigendep +haskell-eigen-utilsetup-changed
Dependencies added: base, eigen, haskell-eigen-util, vector
Files
- Data/Eigen/Util.hs +115/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- haskell-eigen-util.cabal +32/−0
- test/Spec.hs +2/−0
+ Data/Eigen/Util.hs view
@@ -0,0 +1,115 @@+{-# LANGUAGE FlexibleContexts #-}++module Data.Eigen.Util (+ -- Basic operations+ rowAdd + , colAdd + , scaleRow+ , scaleCol+ -- creation + , fromList'+ -- stacking functions + , hstack+ , vstack+ -- Function to manipulate matrices+ , delRow + , delRows+ , delCol+ , delCols+ -- Kronecker product of two matrix+ , kronecker+) where++import Data.Eigen.Matrix as E+import Data.Maybe (fromJust)+import Data.List as L+import Data.Vector.Storable as V++-- | to2DList turns a 1-d list to 2D list.+-- to2DList :: E.Elem a b => Int -> [ b ] -> [[ b ]]+to2DList _ [] = []+to2DList n es = row : to2DList n rest+ where+ ( row, rest ) = L.splitAt n es++-- | Alternative implementation of fromList. It accepts a flatten list of+-- elements and number of columns. +-- No tests are performed to check if number of elements in list are sufficient.+fromList' :: E.Elem a b => Int -> [ a ] -> E.Matrix a b+fromList' n elems = E.fromList $ to2DList n elems++-- | Stack matrices horizontallly+hstack :: E.Elem a b => [ E.Matrix a b ] -> E.Matrix a b+hstack mats = E.generate allrows allcols generateFunc+ where+ allcols = L.sum ncols+ allrows = E.rows $ L.head mats+ ncols = L.map E.cols mats+ whichMat c = fromJust $ L.findIndex (>c) $ L.scanl1 (+) ncols + generateFunc i j = (mats!!matId) E.! (i, j - (L.sum $ L.take matId ncols) )+ where + matId = whichMat j++-- | Stack given matrices vertically. It uses the following property+-- vstack [a, b, c ..] = ( hstack [a',b',c'.. ] )' where M' is transpose of+-- matrix M. +--+-- TODO: This is computationally inefficient than implementing is directly like+-- hstack.+vstack :: E.Elem a b => [ E.Matrix a b ] -> E.Matrix a b+vstack mats = E.transpose $ hstack $ L.map E.transpose mats++-- | Kronecker matric multiplication.+kronecker mat1 mat2 = vstack $ L.map hstack result+ where+ result = to2DList c1 $ E.fold' ( \c e -> ((E.map (*e) mat2):c) ) [] mat1 + [ (r1,c1), (r2,c2) ] = [ E.dims mat1, E.dims mat2 ]+ (r, c) = ( r1*r2, c1*c2 )++-- | rowAdd+-- row r1 = k1 * row r1 + k2 * row r2+rowAdd :: E.Elem a b => (a, Int) -> (a, Int) -> E.Matrix a b -> E.Matrix a b+rowAdd (k1,r1) (k2,r2) mat = E.imap ( + \i j v -> if i == r1 then k1 * v + k2 * ( mat E.! (r2,j) ) else v ) mat++-- | colAdd +-- col c1 = k1 * col c1 + k2 * col c2 +colAdd :: E.Elem a b => (a, Int) -> (a, Int ) -> E.Matrix a b -> E.Matrix a b+colAdd (k1, c1) (k2, c2) mat = E.imap ( + \i j v -> if j == c1 then k1 * v + k2 * ( mat E.! (i,c2) ) else v ) mat++-- | scale a row by a factor+scaleRow :: E.Elem a b => Int -> a -> E.Matrix a b -> E.Matrix a b+scaleRow row c mat = E.imap ( \i j v -> if i == row then c * v else v ) mat++-- | scale a column by a factor+scaleCol :: E.Elem a b => Int -> a -> E.Matrix a b -> E.Matrix a b+scaleCol col c mat = E.imap ( \i j v -> if j == col then c * v else v ) mat++-- TODO: Following two functions are inefficient since they don't manipulate+-- matrix in place.+-- | delete a row +delRow :: E.Elem a b => Int -> E.Matrix a b -> E.Matrix a b+delRow r mat = E.fromList $ deleteAt r $ E.toList mat++-- Utility function to delete an element from the at index n+deleteAt :: Int -> [a] -> [a]+deleteAt n ls = let (ys,zs) = L.splitAt n ls in ys L.++ (L.tail zs)++-- | delete a column+delCol :: E.Elem a b => Int -> E.Matrix a b -> E.Matrix a b+delCol c mat = E.fromList $ L.map (\row -> deleteAt c row) $ E.toList mat++-- | delete list of given rows+delRows :: E.Elem a b => [ Int ] -> E.Matrix a b -> E.Matrix a b+delRows rows mat = delRows' (L.sort rows) mat + where + delRows' [] mat = mat+ delRows' (r:rs) mat = delRows' (L.map (\e->e-1) rs) (delRow r mat) ++-- | delete list of given columns+delCols :: E.Elem a b => [ Int ] -> E.Matrix a b -> E.Matrix a b+delCols cols mat = delCols' (L.sort cols) mat + where + delCols' [] mat = mat+ delCols' (c:cs) mat = delCols' (L.map (\e->e-1) cs) (delCol c mat)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Dilawar Singh (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 Author name here 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
+ haskell-eigen-util.cabal view
@@ -0,0 +1,32 @@+name: haskell-eigen-util+version: 0.1.0.0+synopsis: Some utility functions for haskell-eigen library+description: Please see README.md+homepage: https://github.com/dilawar/haskell-eigen-util#Readme.md+license: BSD3+license-file: LICENSE+author: Dilawar Singh+maintainer: dilawars@ncbs.res.in+copyright: 2016 Dilawar Singh+category: Math+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ exposed-modules: Data.Eigen.Util+ build-depends: base >= 4.7 && < 5, eigen, vector+ default-language: Haskell2010++test-suite haskell-eigen-util-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ build-depends: base+ , haskell-eigen-util+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/dilawar/haskell-eigen-util
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"