module Numeric.Morpheus.MatrixReduce
(
columnPredicate
, rowPredicate
, columnSum
, rowSum
, columnMaxIndex
, columnMinIndex
, rowMaxIndex
, rowMinIndex
)
where
import Numeric.LinearAlgebra
import Numeric.LinearAlgebra.Devel
import System.IO.Unsafe(unsafePerformIO)
import Foreign
import Foreign.C.Types
import Foreign.Ptr(Ptr)
morpheusLayout :: CInt -> CInt -> CInt
morpheusLayout xCol cols = if xCol == 1 || cols == 1 then 21 else 22
type Predicate = R -> R -> R
foreign import ccall "wrapper" mkPredicate :: Predicate -> IO (FunPtr Predicate)
foreign import ccall safe "morpheus_column_predicate"
c_morpheus_column_predicate :: FunPtr Predicate -> CInt -> CInt -> CInt
-> Ptr R -> Ptr R -> IO ()
call_morpheus_column_predicate :: FunPtr Predicate
-> CInt -> CInt -> CInt -> CInt -> Ptr R
-> CInt -> Ptr R
-> IO ()
call_morpheus_column_predicate f rows cols xRow xCol matPtr _ vecPtr = do
let layout = morpheusLayout xCol cols
c_morpheus_column_predicate f layout rows cols matPtr vecPtr
columnPredicate :: (R -> R -> R) -> Matrix R -> Vector R
columnPredicate f m = unsafePerformIO $ do
v <- createVector (cols m)
fpred <- mkPredicate f
apply m (apply v id) (call_morpheus_column_predicate fpred)
return v
foreign import ccall safe "morpheus_row_predicate"
c_morpheus_row_predicate :: FunPtr Predicate -> CInt -> CInt -> CInt
-> Ptr R -> Ptr R -> IO ()
call_morpheus_row_predicate :: FunPtr Predicate
-> CInt -> CInt -> CInt -> CInt -> Ptr R
-> CInt -> Ptr R
-> IO ()
call_morpheus_row_predicate f rows cols xRow xCol matPtr _ vecPtr = do
let layout = morpheusLayout xCol cols
c_morpheus_row_predicate f layout rows cols matPtr vecPtr
rowPredicate :: (R -> R -> R) -> Matrix R -> Vector R
rowPredicate f m = unsafePerformIO $ do
v <- createVector (rows m)
fpred <- mkPredicate f
apply m (apply v id) (call_morpheus_row_predicate fpred)
return v
foreign import ccall unsafe "morpheus_column_sum"
c_morpheus_column_sum :: CInt -> CInt -> CInt -> Ptr Double -> Ptr Double -> IO ()
call_morpheus_column_sum :: CInt -> CInt -> CInt -> CInt -> Ptr Double
-> CInt -> Ptr Double
-> IO ()
call_morpheus_column_sum rows cols xRow xCol matPtr _ vecPtr = do
let layout = morpheusLayout xCol cols
c_morpheus_column_sum layout rows cols matPtr vecPtr
columnSum :: Matrix Double -> Vector Double
columnSum m = unsafePerformIO $ do
v <- createVector (cols m)
apply m (apply v id) call_morpheus_column_sum
return v
foreign import ccall unsafe "morpheus_row_sum"
c_morpheus_row_sum :: CInt -> CInt -> CInt -> Ptr Double -> Ptr Double -> IO ()
call_morpheus_row_sum :: CInt -> CInt -> CInt -> CInt -> Ptr Double
-> CInt -> Ptr Double
-> IO ()
call_morpheus_row_sum rows cols xRow xCol matPtr _ vecPtr = do
let layout = morpheusLayout xCol cols
c_morpheus_row_sum layout rows cols matPtr vecPtr
rowSum :: Matrix Double -> Vector Double
rowSum m = unsafePerformIO $ do
v <- createVector (rows m)
apply m (apply v id) call_morpheus_row_sum
return v
foreign import ccall unsafe "morpheus_column_max_index"
c_morpheus_column_max_index :: CInt -> CInt -> CInt -> Ptr Double -> Ptr Double -> Ptr CInt -> IO ()
call_morpheus_column_max_index :: CInt -> CInt -> CInt -> CInt -> Ptr Double
-> CInt -> Ptr Double
-> CInt -> Ptr CInt
-> IO ()
call_morpheus_column_max_index rows cols xRow xCol matPtr _ vecPtr _ idxPtr = do
let layout = morpheusLayout xCol cols
c_morpheus_column_max_index layout rows cols matPtr vecPtr idxPtr
columnMaxIndex :: Matrix Double -> (Vector R, Vector I)
columnMaxIndex m = unsafePerformIO $ do
v <- createVector (cols m)
i <- createVector (cols m)
apply m (apply v (apply i id)) call_morpheus_column_max_index
return (v, i)
foreign import ccall unsafe "morpheus_column_min_index"
c_morpheus_column_min_index :: CInt -> CInt -> CInt -> Ptr Double -> Ptr Double -> Ptr CInt -> IO ()
call_morpheus_column_min_index :: CInt -> CInt -> CInt -> CInt -> Ptr Double
-> CInt -> Ptr Double
-> CInt -> Ptr CInt
-> IO ()
call_morpheus_column_min_index rows cols xRow xCol matPtr _ vecPtr _ idxPtr = do
let layout = morpheusLayout xCol cols
c_morpheus_column_min_index layout rows cols matPtr vecPtr idxPtr
columnMinIndex :: Matrix Double -> (Vector R, Vector I)
columnMinIndex m = unsafePerformIO $ do
v <- createVector (cols m)
i <- createVector (cols m)
apply m (apply v (apply i id)) call_morpheus_column_min_index
return (v, i)
foreign import ccall unsafe "morpheus_row_max_index"
c_morpheus_row_max_index :: CInt -> CInt -> CInt -> Ptr Double -> Ptr Double -> Ptr CInt -> IO ()
call_morpheus_row_max_index :: CInt -> CInt -> CInt -> CInt -> Ptr Double
-> CInt -> Ptr Double
-> CInt -> Ptr CInt
-> IO ()
call_morpheus_row_max_index rows cols xRow xCol matPtr _ vecPtr _ idxPtr = do
let layout = morpheusLayout xCol cols
c_morpheus_row_max_index layout rows cols matPtr vecPtr idxPtr
rowMaxIndex :: Matrix Double -> (Vector R, Vector I)
rowMaxIndex m = unsafePerformIO $ do
v <- createVector (rows m)
i <- createVector (rows m)
apply m (apply v (apply i id)) call_morpheus_row_max_index
return (v, i)
foreign import ccall unsafe "morpheus_row_min_index"
c_morpheus_row_min_index :: CInt -> CInt -> CInt -> Ptr Double -> Ptr Double -> Ptr CInt -> IO ()
call_morpheus_row_min_index :: CInt -> CInt -> CInt -> CInt -> Ptr Double
-> CInt -> Ptr Double
-> CInt -> Ptr CInt
-> IO ()
call_morpheus_row_min_index rows cols xRow xCol matPtr _ vecPtr _ idxPtr = do
let layout = morpheusLayout xCol cols
c_morpheus_row_min_index layout rows cols matPtr vecPtr idxPtr
rowMinIndex :: Matrix Double -> (Vector R, Vector I)
rowMinIndex m = unsafePerformIO $ do
v <- createVector (rows m)
i <- createVector (rows m)
apply m (apply v (apply i id)) call_morpheus_row_min_index
return (v, i)