diff options
author | Alberto Ruiz <aruiz@um.es> | 2008-10-02 15:53:10 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2008-10-02 15:53:10 +0000 |
commit | 192ac5f4b98517862c37ecf161505396ad223cd8 (patch) | |
tree | 811312f28bca2bd18d282bc0be732a17cd8dbcd7 /lib/Data/Packed/Internal/Matrix.hs | |
parent | 9c6b2af0066f7608301ad685ea5e60753fc3b6ff (diff) |
alternative multiply versions
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 37 |
1 files changed, 6 insertions, 31 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index caf3699..45a3955 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -212,7 +212,6 @@ compat m1 m2 = rows m1 == rows m2 && cols m1 == cols m2 | |||
212 | class (Storable a, Floating a) => Element a where | 212 | class (Storable a, Floating a) => Element a where |
213 | constantD :: a -> Int -> Vector a | 213 | constantD :: a -> Int -> Vector a |
214 | transdata :: Int -> Vector a -> Int -> Vector a | 214 | transdata :: Int -> Vector a -> Int -> Vector a |
215 | multiplyD :: Matrix a -> Matrix a -> Matrix a | ||
216 | subMatrixD :: (Int,Int) -- ^ (r0,c0) starting position | 215 | subMatrixD :: (Int,Int) -- ^ (r0,c0) starting position |
217 | -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix | 216 | -> (Int,Int) -- ^ (rt,ct) dimensions of submatrix |
218 | -> Matrix a -> Matrix a | 217 | -> Matrix a -> Matrix a |
@@ -221,14 +220,12 @@ class (Storable a, Floating a) => Element a where | |||
221 | instance Element Double where | 220 | instance Element Double where |
222 | constantD = constantR | 221 | constantD = constantR |
223 | transdata = transdataR | 222 | transdata = transdataR |
224 | multiplyD = multiplyR | ||
225 | subMatrixD = subMatrixR | 223 | subMatrixD = subMatrixR |
226 | diagD = diagR | 224 | diagD = diagR |
227 | 225 | ||
228 | instance Element (Complex Double) where | 226 | instance Element (Complex Double) where |
229 | constantD = constantC | 227 | constantD = constantC |
230 | transdata = transdataC | 228 | transdata = transdataC |
231 | multiplyD = multiplyC | ||
232 | subMatrixD = subMatrixC | 229 | subMatrixD = subMatrixC |
233 | diagD = diagC | 230 | diagD = diagC |
234 | 231 | ||
@@ -266,33 +263,6 @@ transdataAux fun c1 d c2 = | |||
266 | foreign import ccall "auxi.h transR" ctransR :: TMM | 263 | foreign import ccall "auxi.h transR" ctransR :: TMM |
267 | foreign import ccall "auxi.h transC" ctransC :: TCMCM | 264 | foreign import ccall "auxi.h transC" ctransC :: TCMCM |
268 | 265 | ||
269 | ------------------------------------------------------------------ | ||
270 | |||
271 | gmatC MF { rows = r, cols = c } p f = f 1 (fi c) (fi r) p | ||
272 | gmatC MC { rows = r, cols = c } p f = f 0 (fi r) (fi c) p | ||
273 | |||
274 | dtt MC { cdat = d } = d | ||
275 | dtt MF { fdat = d } = d | ||
276 | |||
277 | multiplyAux fun a b = unsafePerformIO $ do | ||
278 | when (cols a /= rows b) $ error $ "inconsistent dimensions in contraction "++ | ||
279 | show (rows a,cols a) ++ " x " ++ show (rows b, cols b) | ||
280 | r <- createMatrix RowMajor (rows a) (cols b) | ||
281 | withForeignPtr (fptr (dtt a)) $ \pa -> withForeignPtr (fptr (dtt b)) $ \pb -> | ||
282 | withMatrix r $ \r' -> | ||
283 | fun // gmatC a pa // gmatC b pb // r' // check "multiplyAux" | ||
284 | return r | ||
285 | |||
286 | multiplyR = multiplyAux cmultiplyR | ||
287 | foreign import ccall "auxi.h multiplyR" cmultiplyR :: TauxMul Double | ||
288 | |||
289 | multiplyC = multiplyAux cmultiplyC | ||
290 | foreign import ccall "auxi.h multiplyC" cmultiplyC :: TauxMul (Complex Double) | ||
291 | |||
292 | -- | matrix product | ||
293 | multiply :: (Element a) => Matrix a -> Matrix a -> Matrix a | ||
294 | multiply = multiplyD | ||
295 | |||
296 | ---------------------------------------------------------------------- | 266 | ---------------------------------------------------------------------- |
297 | 267 | ||
298 | -- | extraction of a submatrix from a real matrix | 268 | -- | extraction of a submatrix from a real matrix |
@@ -370,7 +340,12 @@ constant = constantD | |||
370 | 340 | ||
371 | -- | obtains the complex conjugate of a complex vector | 341 | -- | obtains the complex conjugate of a complex vector |
372 | conj :: Vector (Complex Double) -> Vector (Complex Double) | 342 | conj :: Vector (Complex Double) -> Vector (Complex Double) |
373 | conj v = asComplex $ flatten $ reshape 2 (asReal v) `multiply` diag (fromList [1,-1]) | 343 | conj v = unsafePerformIO $ do |
344 | r <- createVector (dim v) | ||
345 | app2 cconjugate vec v vec r "cconjugate" | ||
346 | return r | ||
347 | foreign import ccall "auxi.h conjugate" cconjugate :: TCVCV | ||
348 | |||
374 | 349 | ||
375 | -- | creates a complex vector from vectors with real and imaginary parts | 350 | -- | creates a complex vector from vectors with real and imaginary parts |
376 | toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) | 351 | toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) |