summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal/Matrix.hs
diff options
context:
space:
mode:
authorDon Stewart <dons@galois.com>2008-06-10 00:53:03 +0000
committerDon Stewart <dons@galois.com>2008-06-10 00:53:03 +0000
commit9cb72979acc7bcd9df2fa8eab05169d9c5ca84f5 (patch)
tree52cb0810dc6fbcd199d40146df1f7afebb2171ef /lib/Data/Packed/Internal/Matrix.hs
parent8fdd2158ab7a122e9c72a7f41c8bac1a794cf53c (diff)
Unpack Matrix type, and add -funsafe flag
-funsafe optionally compiles out the bounds checks on indexing matrices and vectors. Yields good speedups on tight loops. Not enabled by default.
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs25
1 files changed, 17 insertions, 8 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index c6129c6..7a35a61 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -1,4 +1,5 @@
1{-# OPTIONS_GHC -fglasgow-exts #-} 1{-# OPTIONS_GHC -fglasgow-exts #-}
2{-# LANGUAGE CPP #-}
2----------------------------------------------------------------------------- 3-----------------------------------------------------------------------------
3-- | 4-- |
4-- Module : Data.Packed.Internal.Matrix 5-- Module : Data.Packed.Internal.Matrix
@@ -61,8 +62,13 @@ import Data.List(transpose)
61data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq) 62data MatrixOrder = RowMajor | ColumnMajor deriving (Show,Eq)
62 63
63-- | Matrix representation suitable for GSL and LAPACK computations. 64-- | Matrix representation suitable for GSL and LAPACK computations.
64data Matrix t = MC { rows :: Int, cols :: Int, cdat :: Vector t } 65data Matrix t = MC { rows :: {-# UNPACK #-} !Int
65 | MF { rows :: Int, cols :: Int, fdat :: Vector t } 66 , cols :: {-# UNPACK #-} !Int
67 , cdat :: {-# UNPACK #-} !(Vector t) }
68
69 | MF { rows :: {-# UNPACK #-} !Int
70 , cols :: {-# UNPACK #-} !Int
71 , fdat :: {-# UNPACK #-} !(Vector t) }
66 72
67-- MC: preferred by C, fdat may require a transposition 73-- MC: preferred by C, fdat may require a transposition
68-- MF: preferred by LAPACK, cdat may require a transposition 74-- MF: preferred by LAPACK, cdat may require a transposition
@@ -100,7 +106,6 @@ withMatrix MF {rows = r, cols = c, fdat = d } f =
100flatten :: Element t => Matrix t -> Vector t 106flatten :: Element t => Matrix t -> Vector t
101flatten = cdat . cmat 107flatten = cdat . cmat
102 108
103
104type Mt t s = Int -> Int -> Ptr t -> s 109type Mt t s = Int -> Int -> Ptr t -> s
105-- not yet admitted by my haddock version 110-- not yet admitted by my haddock version
106-- infixr 6 ::> 111-- infixr 6 ::>
@@ -133,7 +138,6 @@ fromColumns m = trans . fromRows $ m
133toColumns :: Element t => Matrix t -> [Vector t] 138toColumns :: Element t => Matrix t -> [Vector t]
134toColumns m = toRows . trans $ m 139toColumns m = toRows . trans $ m
135 140
136
137-- | Reads a matrix position. 141-- | Reads a matrix position.
138(@@>) :: Storable t => Matrix t -> (Int,Int) -> t 142(@@>) :: Storable t => Matrix t -> (Int,Int) -> t
139infixl 9 @@> 143infixl 9 @@>
@@ -142,12 +146,17 @@ infixl 9 @@>
142-- | otherwise = cdat m `at` (i*c+j) 146-- | otherwise = cdat m `at` (i*c+j)
143 147
144MC {rows = r, cols = c, cdat = v} @@> (i,j) 148MC {rows = r, cols = c, cdat = v} @@> (i,j)
145 | i<0 || i>=r || j<0 || j>=c = error "matrix indexing out of range" 149 | safe = if i<0 || i>=r || j<0 || j>=c
146 | otherwise = v `at` (i*c+j) 150 then error "matrix indexing out of range"
151 else v `at` (i*c+j)
152 | otherwise = v `at` (i*c+j)
147 153
148MF {rows = r, cols = c, fdat = v} @@> (i,j) 154MF {rows = r, cols = c, fdat = v} @@> (i,j)
149 | i<0 || i>=r || j<0 || j>=c = error "matrix indexing out of range" 155 | safe = if i<0 || i>=r || j<0 || j>=c
150 | otherwise = v `at` (j*r+i) 156 then error "matrix indexing out of range"
157 else v `at` (j*r+i)
158 | otherwise = v `at` (j*r+i)
159{-# INLINE (@@>) #-}
151 160
152------------------------------------------------------------------ 161------------------------------------------------------------------
153 162