import "Map" as Map\r
import "Vector"\r
import "File"\r
+import "JavaBuiltin" as Java\r
\r
effect Matlab\r
"Simantics/Matlab/Matlab"\r
"org.simantics.matlablink.Engine"\r
\r
-importJava "org.simantics.matlablink.MatlabArray"\r
+importJava "org.simantics.matlablink.MatlabArray" where\r
data MatlabArray\r
-importJava "org.simantics.matlablink.DoubleArray"\r
- data DoubleArray\r
-importJava "org.simantics.matlablink.CharacterArray"\r
- data CharacterArray\r
-importJava "org.simantics.matlablink.CellArray"\r
- data CellArray\r
-importJava "org.simantics.matlablink.StructArray"\r
- data StructArray\r
+ getDoubleValue :: MatlabArray -> Vector Double\r
+ \r
+ @JavaName getDoubleValue\r
+ getDoubleItem :: MatlabArray -> Integer -> Double\r
+ \r
+ getStringValue :: MatlabArray -> String\r
+ getCellValue :: MatlabArray -> Vector MatlabArray\r
+ getCellItem :: MatlabArray -> Integer -> MatlabArray\r
+ getField :: MatlabArray -> Integer -> String -> MatlabArray\r
+ getStructItem :: MatlabArray -> Integer -> Map.T String MatlabArray\r
+ \r
+ isDouble :: MatlabArray -> Boolean\r
+ isCharacter :: MatlabArray -> Boolean\r
+ isCell :: MatlabArray -> Boolean\r
+ isStruct :: MatlabArray -> Boolean\r
+ \r
+ size :: MatlabArray -> Integer\r
+ dims :: MatlabArray -> Vector Integer\r
+ \r
+ @private\r
+ @JavaName toString\r
+ matlabArrayToString :: MatlabArray -> String\r
+ \r
+importJava "org.simantics.matlablink.DoubleArray" where\r
+ data MatlabDoubleArray\r
+ @JavaName "<init>"\r
+ doubleArray :: Vector Double -> <Proc> MatlabArray\r
+ \r
+importJava "org.simantics.matlablink.CharacterArray" where\r
+ data MatlabCharacterArray\r
+ @JavaName "<init>"\r
+ characterArray :: String -> <Proc> MatlabArray\r
+ \r
+importJava "org.simantics.matlablink.CellArray" where\r
+ data MatlabCellArray\r
+ @JavaName "<init>"\r
+ cellArray :: Integer -> <Proc> MatlabCellArray\r
+ @JavaName "<init>"\r
+ ndimCellArray :: Vector Integer -> <Proc> MatlabCellArray \r
+ \r
+ setCellItem :: MatlabCellArray -> Integer -> MatlabArray -> <Proc> ()\r
+ \r
+importJava "org.simantics.matlablink.StructArray" where\r
+ data MatlabStructArray\r
+ @JavaName "<init>"\r
+ structArray :: Integer -> [String] -> <Proc> MatlabStructArray\r
+ @JavaName "<init>"\r
+ ndimStructArray :: Vector Integer -> [String] -> <Proc> MatlabStructArray\r
+ \r
+ setField :: MatlabStructArray -> Integer -> String -> MatlabArray -> <Proc> ()\r
\r
importJava "org.simantics.matlablink.Engine" where\r
data Engine\r
closeEngine :: Engine -> <Proc> ()\r
\r
@JavaName evaluateMatlabExpression \r
- executeMatlabExpression :: String -> <Matlab, Proc> ()\r
+ executeMatlabStatement :: String -> <Matlab, Proc> ()\r
\r
getMatlabArray :: String -> <Matlab, Proc> MatlabArray\r
- getMatlabDoubleArray :: String -> <Matlab, Proc> DoubleArray\r
- getMatlabCharacterArray :: String -> <Matlab, Proc> ChracterArray\r
- getMatlabCellArray :: String -> <Matlab, Proc> CellArray\r
- getMatlabStructArray :: String -> <Matlab, Proc> StructArray\r
+ getMatlabDoubleArray :: String -> <Matlab, Proc> MatlabArray\r
+ getMatlabCharacterArray :: String -> <Matlab, Proc> MatlabArray\r
+ getMatlabCellArray :: String -> <Matlab, Proc> MatlabArray\r
+ getMatlabStructArray :: String -> <Matlab, Proc> MatlabArray\r
+\r
+ @private \r
+ @JavaName setMatlabDoubleArray\r
+ setMatlabDoubleArray' :: String -> MatlabDoubleArray -> <Matlab, Proc> ()\r
+ @private \r
+ @JavaName setMatlabCharacterArray\r
+ setMatlabCharacterArray' :: String -> MatlabCharacterArray -> <Matlab, Proc> ()\r
+ @private \r
+ @JavaName setMatlabCellArray\r
+ setMatlabCellArray' :: String -> MatlabCellArray -> <Matlab, Proc> ()\r
+ @private \r
+ @JavaName setMatlabStructArray\r
+ setMatlabStructArray' :: String -> MatlabStructArray -> <Matlab, Proc> ()\r
+\r
+@private \r
+setMatlabDoubleArray :: String -> MatlabArray -> <Matlab, Proc> ()\r
+setMatlabDoubleArray name value = setMatlabDoubleArray' name (Java.unsafeCoerce value)\r
+\r
+@private \r
+setMatlabCharacterArray :: String -> MatlabArray -> <Matlab, Proc> ()\r
+setMatlabCharacterArray name value = setMatlabCharacterArray' name (Java.unsafeCoerce value)\r
+\r
+@private \r
+setMatlabCellArray :: String -> MatlabArray -> <Matlab, Proc> ()\r
+setMatlabCellArray name value = setMatlabCellArray' name (Java.unsafeCoerce value)\r
+\r
+@private \r
+setMatlabStructArray :: String -> MatlabArray -> <Matlab, Proc> ()\r
+setMatlabStructArray name value = setMatlabStructArray' name (Java.unsafeCoerce value)\r
+\r
+class MatlabArrayType a where\r
+ asArray :: a -> MatlabArray\r
+ asArray = Java.unsafeCoerce\r
\r
- setMatlabArray :: String -> MatlabArray -> <Matlab, Proc> ()\r
- setMatlabDoubleArray :: String -> DoubleArray -> <Matlab, Proc> ()\r
- setMatlabCharacterArray :: String -> CharacterArray -> <Matlab, Proc> ()\r
- setMatlabCellArray :: String -> CellArray -> <Matlab, Proc> ()\r
- setMatlabStructArray :: String -> StructArray -> <Matlab, Proc> ()\r
+instance MatlabArrayType MatlabArray\r
+instance MatlabArrayType MatlabDoubleArray\r
+instance MatlabArrayType MatlabCharacterArray\r
+instance MatlabArrayType MatlabCellArray\r
+instance MatlabArrayType MatlabStructArray\r
+\r
+instance (MatlabArrayType a) => Show a where\r
+ show = matlabArrayToString . asArray\r
+\r
+setMatlabArray :: MatlabArrayType a => String -> a -> <Matlab, Proc> ()\r
+setMatlabArray name value = matchMatlabArray (asArray value) (setMatlabDoubleArray name) (setMatlabCharacterArray name) (setMatlabCellArray name) (setMatlabStructArray name)\r
\r
importJava "org.simantics.matlablink.Matlab" where\r
@JavaName runWithEngine\r
\r
@JavaName runMatlab\r
runMatlabF :: File -> (() -> <Matlab, Proc> a) -> <Proc> a\r
- \r
- matchMatlabArray :: a -> (Vector Double -> b) -> (String -> b) -> (Vector Dynamic -> b) -> (Map.T String (Vector Dynamic) -> b) -> b\r
+\r
+ "Apply type-specific processing to a Matlab array: `matchMatlabArray array doubleFun charFun cellFun structFun`"\r
+ matchMatlabArray :: MatlabArray -> (MatlabArray -> <e> b) -> (MatlabArray -> <e> b) -> (MatlabArray -> <e> b) -> (MatlabArray -> <e> b) -> <e> b\r
\r
openEngine :: File -> <Proc> Engine\r
\r
\r
evaluateMatlabExpression :: MatlabCompatible a => String -> <Matlab, Proc> a\r
evaluateMatlabExpression expression = do\r
- executeMatlabExpression expression\r
+ executeMatlabStatement expression\r
getMatlabVariable "ans"\r
\r
class (Typeable a) => MatlabCompatible a where\r
setMatlabVariable :: String -> a -> <Matlab, Proc> ()\r
\r
instance MatlabCompatible (Vector Double) where\r
- getMatlabVariable = getMatlabDoubleArray\r
- setMatlabVariable name value = setMatlabDoubleArray name False value\r
+ getMatlabVariable = getDoubleValue . getMatlabDoubleArray\r
+ setMatlabVariable name value = setMatlabDoubleArray name (doubleArray value)\r
\r
instance MatlabCompatible [Double] where\r
- getMatlabVariable = vectorToList . getMatlabDoubleArray\r
- setMatlabVariable name value = setMatlabDoubleArray name False (vector value)\r
+ getMatlabVariable = vectorToList . getDoubleValue . getMatlabDoubleArray\r
+ setMatlabVariable name value = setMatlabDoubleArray name (doubleArray (vector value))\r
\r
instance MatlabCompatible Double where\r
- getMatlabVariable name = (getMatlabDoubleArray name)!0\r
- setMatlabVariable name value = setMatlabDoubleArray name False (vector [value])\r
+ getMatlabVariable name = getDoubleItem (getMatlabDoubleArray name) 0\r
+ setMatlabVariable name value = setMatlabDoubleArray name (doubleArray (vector [value]))\r
\r
instance MatlabCompatible String where\r
- getMatlabVariable = getMatlabCharacterArray\r
- setMatlabVariable = setMatlabCharacterArray\r
+ getMatlabVariable = getStringValue . getMatlabCharacterArray\r
+ setMatlabVariable name value = setMatlabCharacterArray name (characterArray value)\r
\r
-instance MatlabCompatible (Vector Dynamic) where\r
- getMatlabVariable = getMatlabCellArray\r
- setMatlabVariable name value = setMatlabCellArray name False value\r
+instance MatlabCompatible (Vector MatlabArray) where\r
+ getMatlabVariable = getCellValue . getMatlabCellArray\r
+ setMatlabVariable name value = do\r
+ n = length value\r
+ array = cellArray n\r
+ forN n \i -> setCellItem array i (value!i)\r
+ setMatlabCellArray' name array\r
\r
-instance MatlabCompatible Dynamic where\r
+instance MatlabCompatible [MatlabArray] where\r
+ getMatlabVariable = vectorToList . getCellValue . getMatlabCellArray\r
+ setMatlabVariable name value = setMatlabVariable name (vector value)\r
+\r
+instance MatlabCompatible MatlabArray where\r
getMatlabVariable = getMatlabArray\r
setMatlabVariable = setMatlabArray \r
-\r
-instance MatlabCompatible (Dynamic, Dynamic) where\r
- getMatlabVariable name = let\r
- v = getMatlabCellArray name\r
- in\r
- (v!0, v!1)\r
- \r
- setMatlabVariable name (a, b) = setMatlabCellArray name False (vector [toDynamic a, toDynamic b])\r