packages feed

haskell-eigen-util 0.1.0.0 → 0.1.0.1

raw patch · 2 files changed

+42/−27 lines, 2 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

+ Data.Eigen.Util: colsAdd :: Elem a b => [Int] -> [a] -> Matrix a b -> Matrix a b
+ Data.Eigen.Util: rowsAdd :: Elem a b => [Int] -> [a] -> Matrix a b -> Matrix a b
- Data.Eigen.Util: colAdd :: Elem a b => (a, Int) -> (a, Int) -> Matrix a b -> Matrix a b
+ Data.Eigen.Util: colAdd :: Elem a b => Int -> (a, Int) -> Matrix a b -> Matrix a b
- Data.Eigen.Util: rowAdd :: Elem a b => (a, Int) -> (a, Int) -> Matrix a b -> Matrix a b
+ Data.Eigen.Util: rowAdd :: Elem a b => Int -> (a, Int) -> Matrix a b -> Matrix a b

Files

Data/Eigen/Util.hs view
@@ -1,22 +1,25 @@ {-# LANGUAGE FlexibleContexts #-}  module Data.Eigen.Util (-    -- Basic operations+    -- | Basic operations on rows and columns on rows and columns on rows and+    -- columns on rows and columns     rowAdd +    , rowsAdd     , colAdd +    , colsAdd      , scaleRow     , scaleCol-    -- creation +    -- | Matrix creation from list      , fromList'-    -- stacking functions +    -- | stacking functions      , hstack     , vstack-    -- Function to manipulate matrices+    -- | Function to manipulate matrices     , delRow      , delRows     , delCol     , delCols-    -- Kronecker product of two matrix+    -- | Kronecker product of two matrix     , kronecker ) where @@ -26,15 +29,15 @@ 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+{- | 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 @@ -50,12 +53,13 @@       where          matId = whichMat j --- | Stack given matrices vertically. It uses the following property+{- | 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 @@ -66,18 +70,30 @@     [ (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+-- | rowAdd r1 = r1 + k * r2+rowAdd :: E.Elem a b => Int -> (a,  Int) -> E.Matrix a b -> E.Matrix a b+rowAdd r1 (k,r2) mat = E.imap ( +    \i j v -> if i == r1 then v + k * ( 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+-- | colAdd c1 = c1 + k * c2+colAdd :: E.Elem a b => Int -> (a, Int ) -> E.Matrix a b -> E.Matrix a b+colAdd c1 (k, c2) mat = E.imap ( +    \i j v -> if j == c1 then v + k * ( mat E.! (i,c2) ) else v ) mat +{- | Adds a list of given columns with a list weights to the first column in the list. + - Note that first value in the list of weights is ignored + -}+colsAdd :: E.Elem a b => [ Int ] -> [ a ] -> E.Matrix a b -> E.Matrix a b+colsAdd (c:[]) _ m = m+colsAdd (c:c1:cols) (w:w1:ws) m = colsAdd (c:cols) (w:ws) $ colAdd c (w1,c1) m++{- | Adds a list of given rows with a list weights to the first row in the list. + - Note that first value in the list of weights is ignored + -}+rowsAdd :: E.Elem a b => [ Int ] -> [ a ] -> E.Matrix a b -> E.Matrix a b+rowsAdd (r:[]) _ m = m+rowsAdd (r:r1:rows) (w:w1:ws) m = rowsAdd (r:rows) (w:ws) $ rowAdd r (w1,r1) m+ -- | 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@@ -86,15 +102,14 @@ 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.+-- Utility function to delete given element from the list+deleteAt :: Int -> [a] -> [a]+deleteAt n ls = let (ys,zs) = L.splitAt n ls in ys L.++ (L.tail zs)++ -- | 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
haskell-eigen-util.cabal view
@@ -1,14 +1,14 @@ name:                haskell-eigen-util-version:             0.1.0.0+version:             0.1.0.1 synopsis:            Some utility functions for haskell-eigen library description:         Please see README.md-homepage:            https://github.com/dilawar/haskell-eigen-util#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+category:            Math, Algebra build-type:          Simple -- extra-source-files: cabal-version:       >=1.10