2 "Simantics/Python/Python"
3 "org.simantics.pythonlink.PythonContext"
5 class PythonCompatible a where
6 setPythonVariable :: String -> a -> <Python> ()
7 getPythonVariable :: String -> <Python> a
9 importJava "org.simantics.pythonlink.NDArray" where
13 ndarray :: Vector Double -> NDArray
15 ndarrayM :: Integer -> Integer -> Vector Double -> NDArray
17 ndarrayN :: Vector Integer -> Vector Double -> NDArray
20 ndarraySize :: NDArray -> Integer
22 ndarrayDims :: NDArray -> Vector Integer
24 ndarrayValues :: NDArray -> Vector Double
26 ndarrayElement :: NDArray -> Integer -> Double
28 ndarrayElementM :: NDArray -> Integer -> Integer -> Double
30 ndarrayElementN :: NDArray -> Vector Integer -> Double
33 ndarrayToString :: NDArray -> String
35 ndarrayEquals :: NDArray -> NDArray -> Boolean
37 class NDArrayInput a where
38 ndarrayInput :: a -> ([Integer], [Double])
40 instance NDArrayInput [Double] where
41 ndarrayInput v = ([length v], v)
43 instance (NDArrayInput [a]) => NDArrayInput [[a]] where
45 inp = map ndarrayInput v
47 vals = join $ map snd inp
49 if not (all (== (dims!0)) dims) then
50 fail "List of lists not of uniform dimension"
52 ([length v] + (dims!0), vals)
54 ndarrayFromList :: NDArrayInput a => a -> NDArray
55 ndarrayFromList v = ndarrayN (vector dims) (vector vals) where
56 (dims, vals) = ndarrayInput v
58 instance Show NDArray where
59 show = ndarrayToString
61 instance Eq NDArray where
64 importJava "org.simantics.pythonlink.PythonContext" where
68 closePythonContext :: PythonContext -> <Proc> ()
70 executePythonStatement :: String -> <Python> ()
72 setPythonBooleanVariable :: String -> Boolean -> <Python> ()
73 setPythonIntegerVariable :: String -> Integer -> <Python> ()
74 setPythonLongVariable :: String -> Long -> <Python> ()
75 setPythonDoubleVariable :: String -> Double -> <Python> ()
76 setPythonStringVariable :: String -> String -> <Python> ()
78 setPythonBooleanArrayVariable :: String -> Vector Boolean -> <Python> ()
79 setPythonIntegerArrayVariable :: String -> Vector Integer -> <Python> ()
80 setPythonLongArrayVariable :: String -> Vector Long -> <Python> ()
81 setPythonDoubleArrayVariable :: String -> Vector Double -> <Python> ()
82 setPythonStringArrayVariable :: String -> Vector String -> <Python> ()
83 setPythonNDArrayVariable :: String -> NDArray -> <Python> ()
85 getPythonBooleanVariable :: String -> <Python> Boolean
86 getPythonIntegerVariable :: String -> <Python> Integer
87 getPythonLongVariable :: String -> <Python> Long
88 getPythonDoubleVariable :: String -> <Python> Double
89 getPythonStringVariable :: String -> <Python> String
91 getPythonBooleanArrayVariable :: String -> <Python> Vector Boolean
92 getPythonIntegerArrayVariable :: String -> <Python> Vector Integer
93 getPythonLongArrayVariable :: String -> <Python> Vector Long
94 getPythonDoubleArrayVariable :: String -> <Python> Vector Double
95 getPythonStringArrayVariable :: String -> <Python> Vector String
96 getPythonNDArrayVariable :: String -> <Python> NDArray
98 setPythonVariantVariable :: String -> Variant -> <Python> ()
99 getPythonVariantVariable :: String -> <Python> Variant
101 importJava "org.simantics.pythonlink.Python" where
102 openPythonContext :: <Proc> PythonContext
104 runPythonF :: (() -> <Python, e> a) -> <Proc, e> a
105 runWithPythonContextF :: PythonContext -> (() -> <Python, e> a) -> <Proc, e> a
107 runPython :: (<Python, e> a) -> <Proc, e> a
108 runPython v = runPythonF (\_ -> v)
110 runWithPythonContext :: PythonContext -> (<Python, e> a) -> <Proc, e> a
111 runWithPythonContext python v = runWithPythonContextF python (\_ -> v)
113 instance (VecComp a, PythonCompatible (Vector a)) => PythonCompatible [a] where
114 setPythonVariable n v = setPythonVariable n (vector v)
115 getPythonVariable = vectorToList . getPythonVariable
117 instance PythonCompatible Boolean where
118 getPythonVariable = getPythonBooleanVariable
119 setPythonVariable = setPythonBooleanVariable
121 instance PythonCompatible (Vector Boolean) where
122 getPythonVariable = getPythonBooleanArrayVariable
123 setPythonVariable = setPythonBooleanArrayVariable
125 instance PythonCompatible Integer where
126 setPythonVariable = setPythonIntegerVariable
127 getPythonVariable = getPythonIntegerVariable
129 instance PythonCompatible (Vector Integer) where
130 setPythonVariable = setPythonIntegerArrayVariable
131 getPythonVariable = getPythonIntegerArrayVariable
133 instance PythonCompatible Long where
134 setPythonVariable = setPythonLongVariable
135 getPythonVariable = getPythonLongVariable
137 instance PythonCompatible (Vector Long) where
138 setPythonVariable = setPythonLongArrayVariable
139 getPythonVariable = getPythonLongArrayVariable
141 instance PythonCompatible Double where
142 setPythonVariable = setPythonDoubleVariable
143 getPythonVariable = getPythonDoubleVariable
145 instance PythonCompatible (Vector Double) where
146 setPythonVariable = setPythonDoubleArrayVariable
147 getPythonVariable = getPythonDoubleArrayVariable
149 instance PythonCompatible String where
150 setPythonVariable = setPythonStringVariable
151 getPythonVariable = getPythonStringVariable
153 instance PythonCompatible (Vector String) where
154 setPythonVariable = setPythonStringArrayVariable
155 getPythonVariable = getPythonStringArrayVariable
157 instance PythonCompatible NDArray where
158 setPythonVariable = setPythonNDArrayVariable
159 getPythonVariable = getPythonNDArrayVariable