bmsherman/haskell-matlab

Modeling integer types

Closed this issue · 2 comments

Oddly, this isn't something I'd thought of in MATLAB until working with haskell-matlab. But unlike JavaScript, which only has a double type, MATLAB actually does have (sized) integer types:

>> class(5)

ans =

    'double'

>> class(int8(5))

ans =

    'int8'

>> isinteger(5)

ans =

  logical

   0

>> isinteger(int8(5))

ans =

  logical

   1

>> foo = [int8(5) int8(3)]

foo =

  1×2 int8 row vector

   5   3

>> class(foo)

ans =

    'int8'

>> bar = [1:10]

bar =

     1     2     3     4     5     6     7     8     9    10

>> bar(foo)

ans =

     5     3

At the least, these do not currently have an MXArrayComponent instance, but there may be other missing instances as well.

It looks like it may actually be supported on haskell-matlab, though I'm having some issues understanding this code:

#let numarray t = "\
foreign import ccall unsafe mxIs%s :: MXArrayPtr -> IO CBool\n\
instance MXArrayComponent M%s where\n\
isMXArray a = boolC =.< withMXArray a mxIs%s\n\
createMXArray s = withNDims s (uncurry $ createNumericArray (mxClassOf (undefined :: M%s)) False) >>= mkMXArray\n\
\
mxArrayGetOffset = arrayDataGet ;\
mxArraySetOffset = arrayDataSet ;\
mxArrayGetOffsetList = arrayDataGetList ;\
mxArraySetOffsetList = arrayDataSetList\
\n\
instance MXArrayData MX%s M%s\
", #t, #t, #t, #t, #t, #t
foreign import ccall unsafe mxIsDouble :: MXArrayPtr -> IO CBool
foreign import ccall unsafe mxCreateDoubleScalar :: MXDouble -> IO MXArrayPtr
foreign import ccall unsafe mxGetScalar :: MXArrayPtr -> IO MXDouble
instance MXArrayComponent MDouble where
isMXArray a = boolC =.< withMXArray a mxIsDouble
createMXScalar = mxCreateDoubleScalar . hs2mx >=> mkMXArray
mxScalarGet a = withMXArray a mxGetScalar
createMXArray s = withNDims s (uncurry $ createNumericArray (mxClassOf (undefined :: Double)) False) >>= mkMXArray
#arrayDataComponent
instance MXArrayData MXDouble MDouble
#numarray Single
#numarray Int8
#numarray Int16
#numarray Int32
#numarray Int64
#numarray Uint8
#numarray Uint16
#numarray Uint32
#numarray Uint64

This appears to be working, though there are a few details of the hsc I still need to understand.