diff options
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index b1e7670..13ffc34 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -221,13 +221,13 @@ class (Storable a, Floating a) => Element a where | |||
221 | 221 | ||
222 | instance Element Double where | 222 | instance Element Double where |
223 | subMatrixD = subMatrix' | 223 | subMatrixD = subMatrix' |
224 | transdata = transdata' | 224 | transdata = transdataAux ctransR -- transdata' |
225 | constantD = constant' | 225 | constantD = constantAux cconstantR -- constant' |
226 | 226 | ||
227 | instance Element (Complex Double) where | 227 | instance Element (Complex Double) where |
228 | subMatrixD = subMatrix' | 228 | subMatrixD = subMatrix' |
229 | transdata = transdata' | 229 | transdata = transdataAux ctransC -- transdata' |
230 | constantD = constant' | 230 | constantD = constantAux cconstantC -- constant' |
231 | 231 | ||
232 | ------------------------------------------------------------------- | 232 | ------------------------------------------------------------------- |
233 | 233 | ||
@@ -257,6 +257,23 @@ transdata' c1 v c2 = | |||
257 | -- The above pragmas only seem to work on top level defs | 257 | -- The above pragmas only seem to work on top level defs |
258 | -- Fortunately everything seems to work using the above class | 258 | -- Fortunately everything seems to work using the above class |
259 | 259 | ||
260 | -- C versions, still a little faster: | ||
261 | |||
262 | transdataAux fun c1 d c2 = | ||
263 | if noneed | ||
264 | then d | ||
265 | else unsafePerformIO $ do | ||
266 | v <- createVector (dim d) | ||
267 | withForeignPtr (fptr d) $ \pd -> | ||
268 | withForeignPtr (fptr v) $ \pv -> | ||
269 | fun (fi r1) (fi c1) pd (fi r2) (fi c2) pv // check "transdataAux" | ||
270 | return v | ||
271 | where r1 = dim d `div` c1 | ||
272 | r2 = dim d `div` c2 | ||
273 | noneed = r1 == 1 || c1 == 1 | ||
274 | |||
275 | foreign import ccall "transR" ctransR :: TMM | ||
276 | foreign import ccall "transC" ctransC :: TCMCM | ||
260 | ---------------------------------------------------------------------- | 277 | ---------------------------------------------------------------------- |
261 | 278 | ||
262 | constant' v n = unsafePerformIO $ do | 279 | constant' v n = unsafePerformIO $ do |
@@ -267,6 +284,22 @@ constant' v n = unsafePerformIO $ do | |||
267 | go (n-1) | 284 | go (n-1) |
268 | return w | 285 | return w |
269 | 286 | ||
287 | -- C versions | ||
288 | |||
289 | constantAux fun x n = unsafePerformIO $ do | ||
290 | v <- createVector n | ||
291 | px <- newArray [x] | ||
292 | app1 (fun px) vec v "constantAux" | ||
293 | free px | ||
294 | return v | ||
295 | |||
296 | constantR :: Double -> Int -> Vector Double | ||
297 | constantR = constantAux cconstantR | ||
298 | foreign import ccall "constantR" cconstantR :: Ptr Double -> TV | ||
299 | |||
300 | constantC :: Complex Double -> Int -> Vector (Complex Double) | ||
301 | constantC = constantAux cconstantC | ||
302 | foreign import ccall "constantC" cconstantC :: Ptr (Complex Double) -> TCV | ||
270 | ---------------------------------------------------------------------- | 303 | ---------------------------------------------------------------------- |
271 | 304 | ||
272 | -- | Extracts a submatrix from a matrix. | 305 | -- | Extracts a submatrix from a matrix. |