Skip to content

Commit e53d103

Browse files
committed
No changes from hackage v0.1
0 parents  commit e53d103

29 files changed

+5969
-0
lines changed

Foreign/Matlab.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{-# OPTIONS_GHC -fno-implicit-prelude #-}
2+
3+
{-|
4+
Bundles Matlab data structure and general-purpose routines.
5+
-}
6+
7+
module Foreign.Matlab
8+
( module Foreign.Matlab.Types
9+
, module Foreign.Matlab.Array
10+
, module Foreign.Matlab.Array.Auto
11+
--, module Foreign.Matlab.Array.MArray
12+
, module Foreign.Matlab.Array.IMX
13+
, module Foreign.Matlab.Array.Able
14+
, module Foreign.Matlab.MAT
15+
) where
16+
17+
import Foreign.Matlab.Types
18+
import Foreign.Matlab.Array
19+
import Foreign.Matlab.Array.Auto
20+
--import Foreign.Matlab.Array.MArray
21+
import Foreign.Matlab.Array.IMX
22+
import Foreign.Matlab.Array.Able
23+
import Foreign.Matlab.MAT

Foreign/Matlab/Array.hsc

Lines changed: 454 additions & 0 deletions
Large diffs are not rendered by default.

Foreign/Matlab/Array/Able.hs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{-|
2+
Direct conversion between Haskell data structures and 'IMXData'.
3+
4+
In general, scalars convert to the obvious, and lists to row vectors.
5+
-}
6+
module Foreign.Matlab.Array.Able (
7+
Matlabable,
8+
toMatlab, fromMatlab,
9+
withMatlabArray, fromMatlabArray
10+
) where
11+
12+
import Foreign.Matlab.Util
13+
import Foreign.Matlab.Types
14+
import Foreign.Matlab.Array.IMX
15+
16+
class Matlabable a where
17+
toMatlab :: a -> IMXData
18+
fromMatlab :: IMXData -> Maybe a
19+
20+
--instance (MScalar a, IMXArrayElem a) => Matlabable a where { toMatlab = scalarIMX ; fromMatlab = imxScalar }
21+
instance Matlabable Bool where { toMatlab = scalarIMX ; fromMatlab = imxScalar }
22+
instance Matlabable Char where { toMatlab = scalarIMX ; fromMatlab = imxScalar }
23+
instance Matlabable Float where { toMatlab = scalarIMX ; fromMatlab = imxScalar }
24+
instance Matlabable Double where { toMatlab = scalarIMX ; fromMatlab = imxScalar }
25+
26+
instance Matlabable Int where
27+
toMatlab = scalarIMX . (ii :: Int -> MInt32)
28+
fromMatlab = fmap (ii :: MInt32 -> Int) . imxScalar
29+
30+
instance Matlabable String where
31+
toMatlab = listIMX [1,-1]
32+
fromMatlab = imxList
33+
34+
instance Matlabable [Double] where
35+
toMatlab = listIMX [1,-1]
36+
fromMatlab = imxList
37+
38+
instance Matlabable [Int] where
39+
toMatlab = listIMX [1,-1] . map (ii :: Int -> MInt32)
40+
fromMatlab = fmap (map (ii :: MInt32 -> Int)) . imxList
41+
42+
instance Matlabable () where
43+
toMatlab () = IMXNull
44+
fromMatlab IMXNull = Just ()
45+
fromMatlab _ = Nothing
46+
47+
-- |Generate a temporary 'MXArray'
48+
withMatlabArray :: Matlabable a => a -> (MAnyArray -> IO a) -> IO a
49+
withMatlabArray = withIMXData . toMatlab
50+
51+
-- |Convert directly from 'MXArray' (without freeing the original array)
52+
fromMatlabArray :: Matlabable a => MAnyArray -> IO (Maybe a)
53+
fromMatlabArray = fromMatlab .=< imxData

Foreign/Matlab/Array/Auto.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{-|
2+
'MXArray' 'ForeignPtr' wrapper.
3+
4+
A 'MXArray' that is automatically freed. These arrays must never be put inside other arrays or used as function results.
5+
-}
6+
module Foreign.Matlab.Array.Auto (
7+
MXAuto,
8+
mxAuto,
9+
withMXAuto,
10+
MAnyAuto
11+
) where
12+
13+
import Foreign
14+
import Foreign.Matlab.Util
15+
import Foreign.Matlab.Internal
16+
import Foreign.Matlab.Types
17+
18+
-- |A 'MXArray' that is automatically freed with 'Foreign.Matlab.Array.freeMXArray'
19+
newtype MXAuto a = MXAuto (ForeignPtr MXArrayType)
20+
21+
foreign import ccall unsafe "&mxDestroyArray" mxDestroyArray_ptr :: FunPtr (MXArrayPtr -> IO ())
22+
23+
-- |Turn an 'MXArray' into an 'MXAuto'. The original 'MXArray' should not be used after this operation.
24+
mxAuto :: MXArray a -> MIO (MXAuto a)
25+
mxAuto (MXArray a)
26+
| a == nullPtr = MXAuto =.< newForeignPtr_ a
27+
| otherwise = MXAuto =.< newForeignPtr mxDestroyArray_ptr a
28+
29+
-- |Use a 'MXAuto'
30+
withMXAuto :: MXAuto a -> (MXArray a -> IO b) -> IO b
31+
withMXAuto (MXAuto a) f = withForeignPtr a (f . MXArray)
32+
33+
type MAnyAuto = MXAuto MAny

0 commit comments

Comments
 (0)