samer@2
|
1 \documentclass[10pt]{article}
|
samer@2
|
2 \usepackage{pldoc}
|
samer@2
|
3 \sloppy
|
samer@2
|
4 \makeindex
|
samer@2
|
5
|
samer@2
|
6 \begin{document}
|
samer@2
|
7 % This LaTeX document was generated using the LaTeX backend of PlDoc,
|
samer@2
|
8 % The SWI-Prolog documentation system
|
samer@2
|
9
|
samer@2
|
10
|
samer@2
|
11
|
samer@2
|
12 \section{plml: Prolog-Matlab interface}
|
samer@2
|
13
|
samer@2
|
14 \label{sec:plml}
|
samer@2
|
15
|
samer@2
|
16 \begin{tags}
|
samer@2
|
17 \tag{To be done}
|
samer@2
|
18 Use mat(I) and tmp(I) as types to include engine Id.
|
samer@2
|
19
|
samer@2
|
20 Clarify relationship between return values and valid Matlab denotation.
|
samer@2
|
21
|
samer@2
|
22 Reshape/2 array representation: reshape([ ... ],Size)
|
samer@2
|
23 Expression language: arr(Vals,Shape,InnerFunctor) - allows efficient
|
samer@2
|
24 representation of arrays of arbitrary things. Will require more strict
|
samer@2
|
25 nested list form.
|
samer@2
|
26
|
samer@2
|
27 Deprecate old array(Vals::Type) and cell(Vals::Type) left-value syntax.
|
samer@2
|
28
|
samer@2
|
29 Remove I from \dcgref{ml_expr}{2} and add to mx type?
|
samer@2
|
30 \end{tags}
|
samer@2
|
31
|
samer@2
|
32 \paragraph{Types}
|
samer@2
|
33
|
samer@2
|
34 \textbf{ml_eng} - Any atom identifying a Matlab engine.
|
samer@2
|
35
|
samer@2
|
36 \textbf{ml_stmt} - A Matlab statement
|
samer@2
|
37
|
samer@2
|
38 \begin{code}
|
samer@2
|
39 X;Y :: ml_stmt :- X:ml_stmt, Y:ml_stmt.
|
samer@2
|
40 X,Y :: ml_stmt :- X:ml_stmt, Y:ml_stmt.
|
samer@2
|
41 X=Y :: ml_stmt :- X:ml_lval, Y:ml_expr.
|
samer@2
|
42 hide(X) :: ml_stmt :- X:ml_stmt.
|
samer@2
|
43 \end{code}
|
samer@2
|
44
|
samer@2
|
45 \begin{code}
|
samer@2
|
46 ml_expr(A) % A Matlab expression, possibly with multiple return values
|
samer@2
|
47 ml_loc ---> mat(atom,atom). % Matbase locator
|
samer@2
|
48 \end{code}
|
samer@2
|
49
|
samer@2
|
50 \paragraph{Matlab expression syntax}
|
samer@2
|
51
|
samer@2
|
52 The Matlab expression syntax adopted by this module allows Prolog terms to represent
|
samer@2
|
53 or denote Matlab expressions. Let T be the domain of recognised Prolog terms (corresponding to
|
samer@2
|
54 the type ml_expr), and M be the domain of Matlab expressions written in Matlab syntax.
|
samer@2
|
55 Then V : T\Sifthen{}M is the valuation function which maps Prolog term X to Matlab expression V[X].
|
samer@2
|
56 These are some of the constructs it recognises:
|
samer@2
|
57
|
samer@2
|
58 Constructs valid only in top level statements, not subexpressions:
|
samer@2
|
59
|
samer@2
|
60 \begin{code}
|
samer@2
|
61 X;Y % |--> V[X]; V[Y] (sequential evaluation hiding first result)
|
samer@2
|
62 X,Y % |--> V[X], V[Y] (sequential evaluation displaying first result)
|
samer@2
|
63 X=Y % |--> V[X]=V[Y] (assignment, X must denote a valid left-value)
|
samer@2
|
64 hide(X) % |--> V[X]; (execute X but hide return value)
|
samer@2
|
65 \end{code}
|
samer@2
|
66
|
samer@2
|
67 Things that look and work like Matlab syntax (more or less):
|
samer@2
|
68
|
samer@2
|
69 \begin{code}
|
samer@2
|
70 +X % |--> uplus(V[X])
|
samer@2
|
71 -X % |--> uminus(V[X])
|
samer@2
|
72 X+Y % |--> plus(V[X],V[Y])
|
samer@2
|
73 X-Y % |--> minus(V[X],V[Y])
|
samer@2
|
74 X^Y % |--> mpower(V[X],V[Y])
|
samer@2
|
75 X*Y % |--> mtimes(V[X],V[Y])
|
samer@2
|
76 X/Y % |--> mrdivide(V[X],V[Y])
|
samer@2
|
77 X\Y % |--> mldivide(V[X],V[Y])
|
samer@2
|
78 X.^Y % |--> power(V[X],V[Y])
|
samer@2
|
79 X.*Y % |--> times(V[X],V[Y])
|
samer@2
|
80 X./Y % |--> rdivide(V[X],V[Y])
|
samer@2
|
81 X.\Y % |--> ldivide(V[X],V[Y])
|
samer@2
|
82 X:Y:Z % |--> colon(V[X],V[Y],V[Z])
|
samer@2
|
83 X:Z % |--> colon(V[X],V[Z])
|
samer@2
|
84 X>Z % |--> gt(V[X],V[Y])
|
samer@2
|
85 X>=Z % |--> ge(V[X],V[Y])
|
samer@2
|
86 X<Z % |--> lt(V[X],V[Y])
|
samer@2
|
87 X=<Z % |--> le(V[X],V[Y])
|
samer@2
|
88 X==Z % |--> eq(V[X],V[Y])
|
samer@2
|
89 [X1,X2,...] % |--> [ V[X1], V[X2], ... ]
|
samer@2
|
90 [X1;X2;...] % |--> [ V[X1]; V[X2]; ... ]
|
samer@2
|
91 {X1,X2,...} % |--> { V[X1], V[X2], ... }
|
samer@2
|
92 {X1;X2;...} % |--> { V[X1]; V[X2]; ... }
|
samer@2
|
93 @X % |--> @V[X] (function handle)
|
samer@2
|
94 \end{code}
|
samer@2
|
95
|
samer@2
|
96 Things that do not look like Matlab syntax but provide standard Matlab features:
|
samer@2
|
97
|
samer@2
|
98 \begin{code}
|
samer@2
|
99 'Infinity' % |--> inf (positive infinity)
|
samer@2
|
100 'Nan' % |--> nan (not a number)
|
samer@2
|
101 X`` % |--> ctranpose(V[X]) (conjugate transpose, V[X]')
|
samer@2
|
102 X#Y % |--> getfield(V[X],V[q(Y)])
|
samer@2
|
103 X\\Y % |--> @(V[X])V[Y] (same as lambda(X,Y))
|
samer@2
|
104 \\Y % |--> @()V[Y] (same as thunk(Y))
|
samer@2
|
105 lambda(X,Y) % |--> @(V[X])V[Y] (anonymous function with arguments X)
|
samer@2
|
106 thunk(Y) % |--> @()V[Y] (anonymous function with no arguments)
|
samer@2
|
107 vector(X) % |--> horzcat(V[X1],V[X2], ...)
|
samer@2
|
108 atvector(X) % as vector but assumes elements of X are assumed all atomic
|
samer@2
|
109 cell(X) % construct 1xN cell array from elements of X
|
samer@2
|
110 `X % same as q(X)
|
samer@2
|
111 q(X) % wrap V[X] in single quotes (escaping internal quotes)
|
samer@2
|
112 qq(X) % wrap V[X] in double quotes (escaping internal double quotes)
|
samer@2
|
113 tq(X) % wrap TeX expression in single quotes (escape internal quotes)
|
samer@2
|
114 \end{code}
|
samer@2
|
115
|
samer@2
|
116 Referencing different value representations.
|
samer@2
|
117
|
samer@2
|
118 \begin{code}
|
samer@2
|
119 mat(X,Y) % denotes a value in the Matbase using a dbload expression
|
samer@2
|
120 mx(X:mx_blob) % denotes an MX Matlab array in SWI memory
|
samer@2
|
121 ws(X:ws_blob) % denotes a variable in a Matlab workspace
|
samer@2
|
122 wsseq(X:ws_blob) % workspace variable containing list as cell array.
|
samer@2
|
123 \end{code}
|
samer@2
|
124
|
samer@2
|
125 Tricky bits.
|
samer@2
|
126
|
samer@2
|
127 \begin{code}
|
samer@2
|
128 apply(X,AX) % X must denote a function or array, applied to list of arguments AX.
|
samer@2
|
129 cref(X,Y) % cell dereference, |--> V[X]{ V[Y1], V[Y2], ... }
|
samer@2
|
130 arr(Lists) % multidimensional array from nested lists.
|
samer@2
|
131 arr(Lists,Dims) % multidimensional array from nested lists.
|
samer@2
|
132 \end{code}
|
samer@2
|
133
|
samer@2
|
134 Things to bypass default formatting
|
samer@2
|
135
|
samer@2
|
136 \begin{code}
|
samer@2
|
137 noeval(_) % triggers a failure when processed
|
samer@2
|
138 atom(X) % write atom X as write/1
|
samer@2
|
139 term(X) % write term X as write/1
|
samer@2
|
140 \(P) % escape and call phrase P directly to generate Matlab string
|
samer@2
|
141 $(X) % calls pl2ml_hook/2, denotes V[Y] where plml_hook(X,Y).
|
samer@2
|
142 '$VAR'(N) % gets formatted as p_N where N is assumed to be atomic.
|
samer@2
|
143 \end{code}
|
samer@2
|
144
|
samer@2
|
145 All other Prolog atoms are written using \predref{write}{1}, while other Prolog terms
|
samer@2
|
146 are assumed to be calls to Matlab functions named according to the head functor.
|
samer@2
|
147 Thus V[ $<$head$>$( $<$arg1$>$, $<$arg2$>$, ...) ] = $<$head$>$(V[$<$arg1$>$, V[$<$arg2$>$], ...).
|
samer@2
|
148
|
samer@2
|
149 There are some incompatibilities between Matlab syntax and Prolog syntax,
|
samer@2
|
150 that is, syntactic structures that Prolog cannot parse correctly:
|
samer@2
|
151
|
samer@2
|
152 \begin{itemize}
|
samer@2
|
153 \item 'Command line' syntax, ie where a function of string arguments:
|
samer@2
|
154 "save('x','Y')" can be written as "save x Y" in Matlab,
|
samer@2
|
155 but in Prolog, you must use function call syntax with quoted arguments:
|
samer@2
|
156 save(`x,`'Y').
|
samer@2
|
157 \item Matlab's postfix transpose operator "x'" must be written using a different
|
samer@2
|
158 posfix operator "x``" or function call syntax "ctranspose(x)".
|
samer@2
|
159 \item Matlab cell referencing using braces, as in x\{1,2\} must be written
|
samer@2
|
160 as "cref(x,1,2)".
|
samer@2
|
161 \item Field referencing using dot (.), eg x.thing - currently resolved
|
samer@2
|
162 by using hash (\#) operator, eg x\#thing.
|
samer@2
|
163 \item Using variables as arrays and indexing them. The problem is that
|
samer@2
|
164 Prolog doesn't let you write a term with a variable as the head
|
samer@2
|
165 functor.
|
samer@2
|
166 \end{itemize}
|
samer@2
|
167
|
samer@2
|
168 \vspace{0.7cm}
|
samer@2
|
169
|
samer@2
|
170 \begin{description}
|
samer@2
|
171 \predicate[nondet,multifile]{matlab_init}{2}{-Key, -Cmd:ml_expr}
|
samer@2
|
172 Each user-defined clause of \predref{matlab_init}{2} causes \arg{Cmd} to be executed
|
samer@2
|
173 whenever a new Matlab session is started.
|
samer@2
|
174
|
samer@2
|
175 \predicate[nondet,multifile]{matlab_path}{2}{-Key, -Path:list(atom)}
|
samer@2
|
176 Each user-defined clause of \predref{matlab_path}{2} causes the directories in \arg{Path}
|
samer@2
|
177 to be added to the Matlab path of every new Matlab session. Directories
|
samer@2
|
178 are relative to the root directory as returned by Matlab function proot.
|
samer@2
|
179
|
samer@2
|
180 \predicate[nondet,multifile]{pl2ml_hook}{2}{+X:term, -Y:ml_expr}
|
samer@2
|
181 Clauses of \predref{pl2ml_hook}{2} allow for extensions to the Matlab expression
|
samer@2
|
182 language such that \verb|V[$X] = V[Y]| if \verb$pl2ml_hook(X,Y)$.
|
samer@2
|
183
|
samer@2
|
184 \predicate[det]{ml_open}{3}{+Id:ml_eng, +Host:atom, +Options:list(_)}
|
samer@2
|
185 \nodescription
|
samer@2
|
186 \predicate[det]{ml_open}{2}{+Id:ml_eng, +Host:atom}
|
samer@2
|
187 \nodescription
|
samer@2
|
188 \predicate[det]{ml_open}{1}{+Id:ml_eng}
|
samer@2
|
189 Start a Matlab session on the given host. If \arg{Host}=localhost
|
samer@2
|
190 or the name of the current current host as returned by \predref{hostname}{1},
|
samer@2
|
191 then a Matlab process is started directly. Otherwise, it is
|
samer@2
|
192 started remotely via SSH. \arg{Options} defaults to []. \arg{Host} defaults to
|
samer@2
|
193 localhost.
|
samer@2
|
194
|
samer@2
|
195 Start a Matlab session on the specified host using default options.
|
samer@2
|
196 If \arg{Host} is not given, it defaults to localhost. Session will be
|
samer@2
|
197 associated with the given \arg{Id}, which should be an atom. See \predref{ml_open}{3}.
|
samer@2
|
198
|
samer@2
|
199 Valid options are
|
samer@2
|
200
|
samer@2
|
201 \begin{description}
|
samer@2
|
202 \termitem{noinit}{}
|
samer@2
|
203 If present, do not run initialisation commands specified by
|
samer@2
|
204 \predref{matlab_path}{2} and \predref{matlab_init}{2} clauses. Otherwise, do run them.
|
samer@2
|
205 \end{description}
|
samer@2
|
206
|
samer@2
|
207 \begin{description}
|
samer@2
|
208 \termitem{debug}{In, Out}
|
samer@2
|
209 if present, Matlab is started in a script which captures standard
|
samer@2
|
210 input and output to files In and Out respectively.
|
samer@2
|
211
|
samer@2
|
212 [What if session is already open and attached to \arg{Id}?]
|
samer@2
|
213 \end{description}
|
samer@2
|
214
|
samer@2
|
215 \predicate[det]{ml_close}{1}{+Id:ml_eng}
|
samer@2
|
216 Close Matlab session associated with \arg{Id}.
|
samer@2
|
217
|
samer@2
|
218 \predicate[det]{ml_exec}{2}{+Id:ml_eng, +Expr:ml_expr}
|
samer@2
|
219 Execute Matlab expression without returning any values.
|
samer@2
|
220
|
samer@2
|
221 \predicate[det]{ml_eval}{4}{+Id:ml_eng, +Expr:ml_expr, +Types:list(type), -Res:list(ml_val)}
|
samer@2
|
222 Evaluate Matlab expression binding return values to results list \arg{Res}. This new
|
samer@2
|
223 form uses an explicit output types list, so \arg{Res} can be completely unbound on entry
|
samer@2
|
224 even when multiple values are required.
|
samer@2
|
225
|
samer@2
|
226 \predicate[semidet]{ml_test}{2}{+Id:ml_eng, +X:ml_expr(bool)}
|
samer@2
|
227 Succeeds if \arg{X} evaluates to true in Matlab session \arg{Id}.
|
samer@2
|
228
|
samer@2
|
229 \predicate[det]{===}{2}{Y:ml_vals(A), X:ml_expr(A)}
|
samer@2
|
230 Evaluate Matlab expression \arg{X} as in \predref{ml_eval}{4}, binding one or more return values
|
samer@2
|
231 to \arg{Y}. If \arg{Y} is unbound or a single ml_val(_), only the first return value is bound.
|
samer@2
|
232 If \arg{Y} is a list, multiple return values are processed.
|
samer@2
|
233
|
samer@2
|
234 \predicate[det]{??}{1}{X:ml_expr(_)}
|
samer@2
|
235 Execute Matlab expression \arg{X} as with \predref{ml_exec}{2}, without returning any values.
|
samer@2
|
236
|
samer@2
|
237 \predicate[semidet]{???}{1}{X:ml_expr(bool)}
|
samer@2
|
238 Evaluate Matlab boolean expression \arg{X} as with \predref{ml_test}{2}.
|
samer@2
|
239
|
samer@2
|
240 \predicate[det]{ml_debug}{1}{+Flag:boolean}
|
samer@2
|
241 Set or reset debug state. \verb$ml_debug(true)$ causes formatted Matlab
|
samer@2
|
242 statements to be printed before being sent to Matlab engine.
|
samer@2
|
243
|
samer@2
|
244 \predicate[det]{term_mlstring}{3}{+Id:ml_eng, +X:ml_expr, -Y:list(code)}
|
samer@2
|
245 Convert term representing Matlab expression to a list of character codes.
|
samer@2
|
246
|
samer@2
|
247 \predicate[det]{term_texatom}{2}{+X:tex_expr, -Y:atom}
|
samer@2
|
248 Convert term representing TeX expression to a string in atom form.
|
samer@2
|
249
|
samer@2
|
250 \predicate[semidet]{wsvar}{3}{+X:ws_blob(A), -Nm:atom, -Id:ml_eng}
|
samer@2
|
251 True if \arg{X} is a workspace variable in Matlab session \arg{Id}.
|
samer@2
|
252 Unifies \arg{Nm} with the name of the Matlab variable.
|
samer@2
|
253
|
samer@2
|
254 \predicate[det]{dropmat}{2}{+Id:ml_id, +Mat:ml_loc}
|
samer@2
|
255 Deleting MAT file from matbase.
|
samer@2
|
256
|
samer@2
|
257 \predicate[det]{exportmat}{3}{+Id:ml_id, +Mat:ml_loc, +Dir:atom}
|
samer@2
|
258 Export specified MAT file from matbase to given directory.
|
samer@2
|
259
|
samer@2
|
260 \predicate[nondet]{matbase_mat}{2}{+Id:ml_eng, -X:ml_loc}
|
samer@2
|
261 Listing mat files actually in matbase at given root directory.
|
samer@2
|
262
|
samer@2
|
263 \predicate[det]{persist_item}{2}{+X:ml_expr(A), -Y:ml_expr(A)}
|
samer@2
|
264 Convert Matlab expression to persistent form not dependent on
|
samer@2
|
265 current Matlab workspace or MX arrays in Prolog memory space.
|
samer@2
|
266 Large values like arrays and structures are saved in the matbase
|
samer@2
|
267 replaced with matbase locators. Scalar values are converted to
|
samer@2
|
268 literal numeric values. Character strings are converted to Prolog atoms.
|
samer@2
|
269 Cell arrays wrapped in the \predref{wsseq}{1} functor are converted to literal
|
samer@2
|
270 form.
|
samer@2
|
271
|
samer@2
|
272 NB. any side effects are undone on backtracking -- in particular, any
|
samer@2
|
273 files created in the matbase are deleted.
|
samer@2
|
274
|
samer@2
|
275 \predicate[det]{mhelp}{1}{+Name:atom}
|
samer@2
|
276 Lookup Matlab help on the given name. Equivalent to executing help(`X).
|
samer@2
|
277
|
samer@2
|
278 \predicate[det]{compileoptions}{2}{+Opts:list(ml_options), -Prefs:ml_expr(options)}
|
samer@2
|
279 Convert list of option specifiers into a Matlab expression representing
|
samer@2
|
280 options (ie a struct). Each specifier can be a Name:Value pair, a name
|
samer@2
|
281 to be looked up in the \predref{optionset}{2} predicate, a nested list of ml_options
|
samer@2
|
282 compileoptions :: list (optionset \Sbar{} atom:value \Sbar{} struct) \Sifthen{} struct.
|
samer@2
|
283 NB. option types are as follows:
|
samer@2
|
284
|
samer@2
|
285 \begin{code}
|
samer@2
|
286 X :: ml_options :- optionset(X,_).
|
samer@2
|
287 X :: ml_options :- X :: ml_option(_).
|
samer@2
|
288 X :: ml_options :- X :: list(ml_options).
|
samer@2
|
289 X :: ml_options :- X :: ml_expr(struct(_)).
|
samer@2
|
290
|
samer@2
|
291 ml_option(A) ---> atom:ml_expr(A).
|
samer@2
|
292 \end{code}
|
samer@2
|
293
|
samer@2
|
294 \predicate[det]{multiplot}{2}{+Type:ml_plot, +Cmds:list(ml_expr(_))}
|
samer@2
|
295 \nodescription
|
samer@2
|
296 \predicate[det]{multiplot}{3}{+Type:ml_plot, +Cmds:list(ml_expr(_)), -Axes:list(ml_val(handle))}
|
samer@2
|
297 Executes plotting commands in \arg{Cmds} in multiple figures or axes as determined
|
samer@2
|
298 by \arg{Type}. Valid types are:
|
samer@2
|
299
|
samer@2
|
300 \begin{description}
|
samer@2
|
301 \termitem{figs}{Range}
|
samer@2
|
302 Executes each plot in a separate figure, Range must be P..Q where P
|
samer@2
|
303 and Q are figure numbers.
|
samer@2
|
304 \termitem{vertical}{}
|
samer@2
|
305 Executes each plot in a subplot;
|
samer@2
|
306 subplots are arranged vertically top to bottom in the current figure.
|
samer@2
|
307 \termitem{horizontal}{}
|
samer@2
|
308 Executes each plot in a subplot;
|
samer@2
|
309 subplots are arranged horizontally left to right in the current figure.
|
samer@2
|
310 \termitem{\Sdot}{Type, [link(Axis)]}
|
samer@2
|
311 As for multplot type \arg{Type}, but link X or Y axis scales as determined by Axis,
|
samer@2
|
312 which can be `x, `y, or `xy.
|
samer@2
|
313 \end{description}
|
samer@2
|
314
|
samer@2
|
315 Three argument form returns a list containing the Matlab handles to axes objects,
|
samer@2
|
316 one for each plot.
|
samer@2
|
317
|
samer@2
|
318 \predicate[semidet,multifile]{optionset}{2}{+Key:term, -Opts:list(ml_options)}
|
samer@2
|
319 Extensible predicate for mapping arbitrary terms to a list of options
|
samer@2
|
320 to be processed by \predref{compileoptions}{2}.
|
samer@2
|
321 \end{description}
|
samer@2
|
322
|
samer@2
|
323
|
samer@2
|
324 \printindex
|
samer@2
|
325 \end{document}
|