summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Internal/Matrix.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data/Packed/Internal/Matrix.hs')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs37
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
212class (Storable a, Floating a) => Element a where 212class (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
221instance Element Double where 220instance 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
228instance Element (Complex Double) where 226instance 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 =
266foreign import ccall "auxi.h transR" ctransR :: TMM 263foreign import ccall "auxi.h transR" ctransR :: TMM
267foreign import ccall "auxi.h transC" ctransC :: TCMCM 264foreign import ccall "auxi.h transC" ctransC :: TCMCM
268 265
269------------------------------------------------------------------
270
271gmatC MF { rows = r, cols = c } p f = f 1 (fi c) (fi r) p
272gmatC MC { rows = r, cols = c } p f = f 0 (fi r) (fi c) p
273
274dtt MC { cdat = d } = d
275dtt MF { fdat = d } = d
276
277multiplyAux 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
286multiplyR = multiplyAux cmultiplyR
287foreign import ccall "auxi.h multiplyR" cmultiplyR :: TauxMul Double
288
289multiplyC = multiplyAux cmultiplyC
290foreign import ccall "auxi.h multiplyC" cmultiplyC :: TauxMul (Complex Double)
291
292-- | matrix product
293multiply :: (Element a) => Matrix a -> Matrix a -> Matrix a
294multiply = 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
372conj :: Vector (Complex Double) -> Vector (Complex Double) 342conj :: Vector (Complex Double) -> Vector (Complex Double)
373conj v = asComplex $ flatten $ reshape 2 (asReal v) `multiply` diag (fromList [1,-1]) 343conj v = unsafePerformIO $ do
344 r <- createVector (dim v)
345 app2 cconjugate vec v vec r "cconjugate"
346 return r
347foreign 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
376toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) 351toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double)