Skip to content
This repository was archived by the owner on Nov 18, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions Data/Matrix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module Data.Matrix (
-- ** Determinants
, detLaplace
, detLU
, flatten
) where

-- Classes
Expand All @@ -71,13 +72,14 @@ import Control.Monad (forM_)
import Control.Loop (numLoop,numLoopFold)
import Data.Foldable (Foldable, foldMap)
import Data.Monoid
import Data.Traversable
import Data.Traversable()
-- Data
import Control.Monad.Primitive (PrimMonad, PrimState)
import Data.List (maximumBy,foldl1')
import Data.Ord (comparing)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV
import Data.Maybe

-------------------------------------------------------
-------------------------------------------------------
Expand Down Expand Up @@ -152,6 +154,41 @@ instance Functor Matrix where
-------------------------------------------------------
-------------------------------------------------------

-------------------------------------------------------
-------------------------------------------------------
---- MONOID INSTANCE

instance Monoid a => Monoid (Matrix a) where
mempty = fromList 1 1 [mempty]
mappend m m' = matrix (max (nrows m) (nrows m')) (max (ncols m) (ncols m')) $ uncurry zipTogether
where zipTogether row column = fromMaybe mempty $ safeGet row column m <> safeGet row column m'


-------------------------------------------------------
-------------------------------------------------------
-------------------------------------------------------
-------------------------------------------------------

-------------------------------------------------------
-------------------------------------------------------
---- APPLICATIVE INSTANCE
---- Works like tensor product but applies a function

instance Applicative Matrix where
pure x = fromList 1 1 [x]
m <*> m' = flatten $ ((\f -> f <$> m') <$> m)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this instance obey the Applicative class laws? (the same could be asked about the monoid instance above)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell yes. I worked some examples on paper but I couldn't get quickcheck to quite work for the applicative instances.


-------------------------------------------------------
-------------------------------------------------------



-- | Flatten a matrix of matrices. All sub matrices must have same dimensions
-- This criteria is not checked.
flatten:: (Matrix (Matrix a)) -> Matrix a
flatten m = foldl1 (<->) $ map (foldl1 (<|>) . (\i -> getRow i m)) [1..(nrows m)]

-- | /O(rows*cols)/. Map a function over a row.
-- Example:
--
Expand Down Expand Up @@ -1233,4 +1270,3 @@ detLU :: (Ord a, Fractional a) => Matrix a -> a
detLU m = case luDecomp m of
Just (u,_,_,d) -> d * diagProd u
Nothing -> 0

3 changes: 3 additions & 0 deletions matrix.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ Test-Suite matrix-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base == 4.*
, matrix
, tasty
, QuickCheck
, tasty-quickcheck
, hspec


Test-Suite matrix-examples
type: exitcode-stdio-1.0
Expand Down