6578
|
1 @c Copyright (C) 2007 John W. Eaton, David Bateman |
6580
|
2 @c Copyright (C) 2007 Paul Thomas, Christoph Spiel |
6578
|
3 @c This is part of the Octave manual. |
|
4 @c For copying conditions, see the file gpl.texi. |
|
5 |
|
6 @macro examplefile{file} |
|
7 @example |
|
8 @group |
|
9 @verbatiminclude @value{top_srcdir}/examples/\file\ |
|
10 @end group |
|
11 @end example |
|
12 @end macro |
|
13 |
6569
|
14 @node Dynamically Linked Functions |
|
15 @appendix Dynamically Linked Functions |
|
16 @cindex dynamic-linking |
|
17 |
|
18 Octave has the possibility of including compiled code as dynamically |
|
19 linked extensions and then using these extensions as if they were part |
6571
|
20 of Octave itself. Octave has the option of directly calling C++ code |
6569
|
21 through its native oct-file interface or C code through its mex |
6571
|
22 interface. It can also indirectly call functions written in any other |
|
23 language through a simple wrapper. The reasons to write code in a |
6569
|
24 compiled language might be either to link to an existing piece of code |
|
25 and allow it to be used within Octave, or to allow improved performance |
|
26 for key pieces of code. |
|
27 |
|
28 Before going further, you should first determine if you really need to |
6571
|
29 use dynamically linked functions at all. Before proceeding with writing |
6569
|
30 any dynamically linked function to improve performance you should |
|
31 address ask yourself |
|
32 |
|
33 @itemize @bullet |
|
34 @item |
|
35 Can I get the same functionality using the Octave scripting language only. |
|
36 @item |
6572
|
37 Is it thoroughly optimized Octave code? Vectorization of Octave code, |
6569
|
38 doesn't just make it concise, it generally significantly improves its |
6571
|
39 performance. Above all, if loops must be used, make sure that the |
6569
|
40 allocation of space for variables takes place outside the loops using an |
|
41 assignment to a like matrix or zeros. |
|
42 @item |
|
43 Does it make as much use as possible of existing built-in library |
6572
|
44 routines? These are highly optimized and many do not carry the overhead |
6569
|
45 of being interpreted. |
|
46 @item |
|
47 Does writing a dynamically linked function represent useful investment |
|
48 of your time, relative to staying in Octave? |
|
49 @end itemize |
|
50 |
|
51 Also, as oct- and mex-files are dynamically linked to octave, they |
|
52 introduce to possibility of having Octave abort due to coding errors in |
6571
|
53 the user code. For example a segmentation violation in the users code |
6569
|
54 will cause Octave to abort. |
|
55 |
|
56 @menu |
6572
|
57 * Oct-Files:: |
|
58 * Mex-Files:: |
|
59 * Standalone Programs:: |
6569
|
60 @end menu |
|
61 |
|
62 @node Oct-Files |
|
63 @section Oct-Files |
|
64 @cindex oct-files |
|
65 @cindex mkoctfile |
|
66 @cindex oct |
|
67 |
|
68 @menu |
6572
|
69 * Getting Started with Oct-Files:: |
|
70 * Matrices and Arrays in Oct-Files:: |
|
71 * Character Strings in Oct-Files:: |
|
72 * Cell Arrays in Oct-Files:: |
|
73 * Structures in Oct-Files:: |
|
74 * Sparse Matrices in Oct-Files:: |
|
75 * Accessing Global Variables in Oct-Files:: |
|
76 * Calling Octave Functions from Oct-Files:: |
|
77 * Calling External Code from Oct-Files:: |
|
78 * Allocating Local Memory in Oct-Files:: |
|
79 * Input Parameter Checking in Oct-Files:: |
|
80 * Exception and Error Handling in Oct-Files:: |
|
81 * Documentation and Test of Oct-Files:: |
|
82 * Application Programming Interface for Oct-Files:: |
6569
|
83 @end menu |
|
84 |
|
85 @node Getting Started with Oct-Files |
|
86 @subsection Getting Started with Oct-Files |
|
87 |
|
88 The basic command to build oct-files is @code{mkoctfile} and it can be |
|
89 call from within octave or from the command line. |
|
90 |
|
91 @DOCSTRING(mkoctfile) |
|
92 |
|
93 Consider the short example |
|
94 |
6578
|
95 @examplefile{helloworld.cc} |
6569
|
96 |
|
97 This example although short introduces the basics of writing a C++ |
6571
|
98 function that can be dynamically linked to Octave. The easiest way to |
6569
|
99 make available most of the definitions that might be necessary for an |
|
100 oct-file in Octave is to use the @code{#include <octave/oct.h>} |
6571
|
101 header. |
6569
|
102 |
|
103 The macro that defines the entry point into the dynamically loaded |
6572
|
104 function is @code{DEFUN_DLD}. This macro takes four arguments, these being |
6569
|
105 |
|
106 @enumerate 1 |
6571
|
107 @item The function name as it will be seen in Octave, |
6572
|
108 @item The list of arguments to the function of type @code{octave_value_list}, |
6569
|
109 @item The number of output arguments, which can and often is omitted if |
|
110 not used, and |
|
111 @item The string that will be seen as the help text of the function. |
|
112 @end enumerate |
|
113 |
6572
|
114 The return type of functions defined with @code{DEFUN_DLD} is always |
|
115 @code{octave_value_list}. |
6569
|
116 |
|
117 There are a couple of important considerations in the choice of function |
6571
|
118 name. Firstly, it must be a valid Octave function name and so must be a |
6569
|
119 sequence of letters, digits and underscores, not starting with a |
6571
|
120 digit. Secondly, as Octave uses the function name to define the filename |
6572
|
121 it attempts to find the function in, the function name in the @code{DEFUN_DLD} |
6571
|
122 macro must match the filename of the oct-file. Therefore, the above |
6572
|
123 function should be in a file @file{helloworld.cc}, and it should be |
|
124 compiled to an oct-file using the command |
6569
|
125 |
|
126 @example |
|
127 mkoctfile helloworld.cc |
|
128 @end example |
|
129 |
|
130 This will create a file call helloworld.oct, that is the compiled |
6571
|
131 version of the function. It should be noted that it is perfectly |
6572
|
132 acceptable to have more than one @code{DEFUN_DLD} function in a source |
6571
|
133 file. However, there must either be a symbolic link to the oct-file for |
6572
|
134 each of the functions defined in the source code with the @code{DEFUN_DLD} |
6569
|
135 macro or the autoload (@ref{Function Files}) function should be used. |
|
136 |
|
137 The rest of this function then shows how to find the number of input |
|
138 arguments, how to print through the octave pager, and return from the |
6571
|
139 function. After compiling this function as above, an example of its use |
6569
|
140 is |
|
141 |
|
142 @example |
|
143 @group |
6572
|
144 helloworld (1, 2, 3) |
|
145 @print{} Hello World has 3 input arguments and 0 output arguments. |
6569
|
146 @end group |
|
147 @end example |
|
148 |
|
149 @node Matrices and Arrays in Oct-Files |
|
150 @subsection Matrices and Arrays in Oct-Files |
|
151 |
|
152 Octave supports a number of different array and matrix classes, the |
6571
|
153 majority of which are based on the Array class. The exception is the |
|
154 sparse matrix types discussed separately below. There are three basic |
|
155 matrix types |
6569
|
156 |
6572
|
157 @table @code |
6569
|
158 @item Matrix |
|
159 A double precision matrix class defined in dMatrix.h, |
|
160 @item ComplexMatrix |
|
161 A complex matrix class defined in CMatrix.h, and |
|
162 @item BoolMatrix |
|
163 A boolean matrix class defined in boolMatrix.h. |
|
164 @end table |
|
165 |
6571
|
166 These are the basic two-dimensional matrix types of octave. In |
6569
|
167 additional there are a number of multi-dimensional array types, these |
|
168 being |
|
169 |
6572
|
170 @table @code |
6569
|
171 @item NDArray |
6572
|
172 A double precision array class defined in @file{dNDArray.h} |
6569
|
173 @item ComplexNDarray |
6572
|
174 A complex array class defined in @file{CNDArray.h} |
6569
|
175 @item boolNDArray |
6572
|
176 A boolean array class defined in @file{boolNDArray.h} |
|
177 @item int8NDArray |
|
178 @itemx int16NDArray |
|
179 @itemx int32NDArray |
|
180 @itemx int64NDArray |
|
181 8, 16, 32 and 64-bit signed array classes defined in |
|
182 @file{int8NDArray.h}, @file{int16NDArray.h}, etc. |
|
183 @item uint8NDArray |
|
184 @itemx uint16NDArray |
|
185 @itemx uint32NDArray |
|
186 @itemx uint64NDArray |
|
187 8, 16, 32 and 64-bit unsigned array classes defined in |
|
188 @file{uint8NDArray.h}, @file{uint16NDArray.h}, etc. |
6569
|
189 @end table |
|
190 |
|
191 There are several basic means of constructing matrices of |
6572
|
192 multi-dimensional arrays. Considering the @code{Matrix} type as an |
|
193 example |
6569
|
194 |
|
195 @itemize @bullet |
6571
|
196 @item |
|
197 We can create an empty matrix or array with the empty constructor. For |
6569
|
198 example |
|
199 |
|
200 @example |
|
201 Matrix a; |
|
202 @end example |
|
203 |
|
204 This can be used on all matrix and array types |
6571
|
205 @item |
|
206 Define the dimensions of the matrix or array with a dim_vector. For |
6569
|
207 example |
|
208 |
|
209 @example |
|
210 @group |
6572
|
211 dim_vector dv (2); |
6569
|
212 dv(0) = 2; dv(1) = 2; |
6572
|
213 Matrix a (dv); |
6569
|
214 @end group |
|
215 @end example |
|
216 |
|
217 This can be used on all matrix and array types |
|
218 @item |
6572
|
219 Define the number of rows and columns in the matrix. For example |
6569
|
220 |
|
221 @example |
6572
|
222 Matrix a (2, 2) |
6569
|
223 @end example |
|
224 |
|
225 However, this constructor can only be used with the matrix types. |
|
226 @end itemize |
|
227 |
|
228 These types all share a number of basic methods and operators, a |
|
229 selection of which include |
|
230 |
6572
|
231 @deftypefn Method T& {operator ()} (octave_idx_type) |
|
232 @deftypefnx Method T& elem (octave_idx_type) |
|
233 The @code{()} operator or @code{elem} method allow the values of the |
|
234 matrix or array to be read or set. These can take a single argument, |
|
235 which is of type @code{octave_idx_type}, that is the index into the matrix or |
6571
|
236 array. Additionally, the matrix type allows two argument versions of the |
6572
|
237 @code{()} operator and elem method, giving the row and column index of the |
6569
|
238 value to obtain or set. |
6572
|
239 @end deftypefn |
6569
|
240 |
|
241 Note that these function do significant error checking and so in some |
|
242 circumstances the user might prefer the access the data of the array or |
|
243 matrix directly through the fortran_vec method discussed below. |
6572
|
244 |
|
245 @deftypefn Method octave_idx_type nelem (void) const |
6569
|
246 The total number of elements in the matrix or array. |
6572
|
247 @end deftypefn |
|
248 |
|
249 @deftypefn Method size_t byte_size (void) const |
6569
|
250 The number of bytes used to store the matrix or array. |
6572
|
251 @end deftypefn |
|
252 |
|
253 @deftypefn Method dim_vector dims (void) const |
6569
|
254 The dimensions of the matrix or array in value of type dim_vector. |
6572
|
255 @end deftypefn |
|
256 |
|
257 @deftypefn Method void resize (const dim_vector&) |
|
258 A method taking either an argument of type @code{dim_vector}, or in the |
|
259 case of a matrix two arguments of type @code{octave_idx_type} defining |
|
260 the number of rows and columns in the matrix. |
|
261 @end deftypefn |
|
262 |
|
263 @deftypefn Method T* fortran_vec (void) |
6569
|
264 This method returns a pointer to the underlying data of the matrix or a |
|
265 array so that it can be manipulated directly, either within Octave or by |
|
266 an external library. |
6572
|
267 @end deftypefn |
6569
|
268 |
6572
|
269 Operators such an @code{+}, @code{-}, or @code{*} can be used on the |
|
270 majority of the above types. In addition there are a number of methods |
|
271 that are of interest only for matrices such as @code{transpose}, |
|
272 @code{hermitian}, @code{solve}, etc. |
6569
|
273 |
|
274 The typical way to extract a matrix or array from the input arguments of |
6572
|
275 @code{DEFUN_DLD} function is as follows |
6569
|
276 |
6578
|
277 @examplefile{addtwomatrices.cc} |
6569
|
278 |
|
279 To avoid segmentation faults causing Octave to abort, this function |
|
280 explicitly checks that there are sufficient arguments available before |
6571
|
281 accessing these arguments. It then obtains two multi-dimensional arrays |
6572
|
282 of type @code{NDArray} and adds these together. Note that the array_value |
|
283 method is called without using the @code{is_matrix_type} type, and instead the |
6571
|
284 error_state is checked before returning @code{A + B}. The reason to |
6569
|
285 prefer this is that the arguments might be a type that is not an |
6572
|
286 @code{NDArray}, but it would make sense to convert it to one. The |
|
287 @code{array_value} method allows this conversion to be performed |
|
288 transparently if possible, and sets @code{error_state} if it is not. |
6569
|
289 |
6572
|
290 @code{A + B}, operating on two @code{NDArray}'s returns an |
|
291 @code{NDArray}, which is cast to an @code{octave_value} on the return |
|
292 from the function. An example of the use of this demonstration function |
|
293 is |
6569
|
294 |
|
295 @example |
|
296 @group |
6572
|
297 addtwomatrices (ones (2, 2), ones (2, 2)) |
6569
|
298 @result{} 2 2 |
|
299 2 2 |
|
300 @end group |
|
301 @end example |
|
302 |
6572
|
303 A list of the basic @code{Matrix} and @code{Array} types, the methods to |
|
304 extract these from an @code{octave_value} and the associated header is |
|
305 listed below. |
6569
|
306 |
|
307 @multitable @columnfractions .3 .4 .3 |
6572
|
308 @item @code{RowVector} @tab @code{row_vector_value} @tab @file{dRowVector.h} |
|
309 @item @code{ComplexRowVector} @tab @code{complex_row_vector_value} @tab @file{CRowVector.h} |
|
310 @item @code{ColumnVector} @tab @code{column_vector_value} @tab @file{dColVector.h} |
|
311 @item @code{ComplexColumnVector} @tab @code{complex_column_vector_value} @tab @file{CColVector.h} |
|
312 @item @code{Matrix} @tab @code{matrix_value} @tab @file{dMatrix.h} |
|
313 @item @code{ComplexMatrix} @tab @code{complex_matrix_value} @tab @file{CMatrix.h} |
|
314 @item @code{boolMatrix} @tab @code{bool_matrix_value} @tab @file{boolMatrix.h} |
|
315 @item @code{charMatrix} @tab @code{char_matrix_value} @tab @file{chMatrix.h} |
|
316 @item @code{NDArray} @tab @code{array_value} @tab @file{dNDArray.h} |
|
317 @item @code{ComplexNDArray} @tab @code{complex_array_value} @tab @file{CNDArray.h} |
|
318 @item @code{boolNDArray} @tab @code{bool_array_value} @tab @file{boolNDArray.h} |
|
319 @item @code{charNDArray} @tab @code{char_array_value} @tab @file{charNDArray.h} |
|
320 @item @code{int8NDArray} @tab @code{int8_array_value} @tab @file{int8NDArray.h} |
|
321 @item @code{int16NDArray} @tab @code{int16_array_value} @tab @file{int16NDArray.h} |
|
322 @item @code{int32NDArray} @tab @code{int32_array_value} @tab @file{int32NDArray.h} |
|
323 @item @code{int64NDArray} @tab @code{int64_array_value} @tab @file{int64NDArray.h} |
|
324 @item @code{uint8NDArray} @tab @code{uint8_array_value} @tab @file{uint8NDArray.h} |
|
325 @item @code{uint16NDArray} @tab @code{uint16_array_value} @tab @file{uint16NDArray.h} |
|
326 @item @code{uint32NDArray} @tab @code{uint32_array_value} @tab @file{uint32NDArray.h} |
|
327 @item @code{uint64NDArray} @tab @code{uint64_array_value} @tab @file{uint64NDArray.h} |
6569
|
328 @end multitable |
|
329 |
6572
|
330 @node Character Strings in Oct-Files |
|
331 @subsection Character Strings in Oct-Files |
|
332 |
|
333 In Octave a character string is just a special @code{Array} class. |
|
334 Consider the example |
|
335 |
6578
|
336 @examplefile{stringdemo.cc} |
6572
|
337 |
|
338 An example of the of the use of this function is |
|
339 |
|
340 @example |
|
341 @group |
|
342 s0 = ["First String"; "Second String"]; |
|
343 [s1,s2] = stringdemo (s0) |
|
344 @result{} s1 = Second String |
|
345 First String |
|
346 |
|
347 @result{} s2 = First String |
|
348 Second String |
|
349 |
|
350 typeinfo (s2) |
|
351 @result{} sq_string |
|
352 typeinfo (s1) |
|
353 @result{} string |
|
354 @end group |
|
355 @end example |
|
356 |
|
357 One additional complication of strings in Octave is the difference |
|
358 between single quoted and double quoted strings. To find out if an |
|
359 @code{octave_value} contains a single or double quoted string an example is |
|
360 |
|
361 @example |
|
362 @group |
|
363 if (args(0).is_sq_string ()) |
|
364 octave_stdout << "First argument is a singularly quoted string\n"; |
|
365 else if (args(0).is_dq_string ()) |
|
366 octave_stdout << "First argument is a doubly quoted string\n"; |
|
367 @end group |
|
368 @end example |
|
369 |
|
370 Note however, that both types of strings are represented by the |
|
371 @code{charNDArray} type, and so when assigning to an |
|
372 @code{octave_value}, the type of string should be specified. For example |
|
373 |
|
374 @example |
|
375 @group |
|
376 octave_value_list retval; |
|
377 charNDArray c; |
|
378 @dots{} |
6577
|
379 // Create single quoted string |
|
380 retval(1) = octave_value (ch, true, '\''); |
|
381 |
|
382 // Create a double quoted string |
|
383 retval(0) = octave_value (ch, true); |
6572
|
384 @end group |
|
385 @end example |
|
386 |
|
387 @node Cell Arrays in Oct-Files |
|
388 @subsection Cell Arrays in Oct-Files |
|
389 |
|
390 Octave's cell type is equally accessible within an oct-files. A cell |
|
391 array is just an array of @code{octave_value}s, and so each element of the cell |
|
392 array can then be treated just like any other @code{octave_value}. A simple |
|
393 example is |
|
394 |
6578
|
395 @examplefile{celldemo.cc} |
6572
|
396 |
|
397 Note that cell arrays are used less often in standard oct-files and so |
|
398 the @file{Cell.h} header file must be explicitly included. The rest of this |
|
399 example extracts the @code{octave_value}s one by one from the cell array and |
|
400 returns be as individual return arguments. For example consider |
|
401 |
|
402 @example |
|
403 @group |
|
404 [b1, b2, b3] = celldemo (@{1, [1, 2], "test"@}) |
|
405 @result{} |
|
406 b1 = 1 |
|
407 b2 = |
|
408 |
|
409 1 2 |
|
410 |
|
411 b3 = test |
|
412 @end group |
|
413 @end example |
|
414 |
|
415 @node Structures in Oct-Files |
|
416 @subsection Structures in Oct-Files |
|
417 |
|
418 A structure in Octave is map between a number of fields represented and |
|
419 their values. The Standard Template Library @code{map} class is used, |
|
420 with the pair consisting of a @code{std::string} and an octave |
|
421 @code{Cell} variable. |
|
422 |
|
423 A simple example demonstrating the use of structures within oct-files is |
|
424 |
6578
|
425 @examplefile{structdemo.cc} |
6572
|
426 |
|
427 An example of its use is |
|
428 |
|
429 @example |
|
430 @group |
|
431 x.a = 1; x.b = "test"; x.c = [1, 2]; |
|
432 structdemo (x, "b") |
|
433 @result{} selected = test |
|
434 @end group |
|
435 @end example |
|
436 |
|
437 The commented code above demonstrates how to iterated over all of the |
|
438 fields of the structure, where as the following code demonstrates finding |
|
439 a particular field in a more concise manner. |
|
440 |
|
441 As can be seen the @code{contents} method of the @code{Octave_map} class |
|
442 returns a @code{Cell} which allows structure arrays to be represented. |
|
443 Therefore, to obtain the underlying @code{octave_value} we write |
|
444 |
|
445 @example |
|
446 octave_value tmp = arg0.contents (p1) (0); |
|
447 @end example |
|
448 |
|
449 where the trailing (0) is the () operator on the @code{Cell} object. |
|
450 |
|
451 @node Sparse Matrices in Oct-Files |
|
452 @subsection Sparse Matrices in Oct-Files |
6569
|
453 |
|
454 There are three classes of sparse objects that are of interest to the |
|
455 user. |
|
456 |
6572
|
457 @table @code |
6569
|
458 @item SparseMatrix |
|
459 A double precision sparse matrix class |
|
460 @item SparseComplexMatrix |
|
461 A complex sparse matrix class |
|
462 @item SparseBoolMatrix |
|
463 A boolean sparse matrix class |
|
464 @end table |
|
465 |
|
466 All of these classes inherit from the @code{Sparse<T>} template class, |
6571
|
467 and so all have similar capabilities and usage. The @code{Sparse<T>} |
6569
|
468 class was based on Octave @code{Array<T>} class, and so users familiar |
6572
|
469 with Octave's @code{Array} classes will be comfortable with the use of |
6569
|
470 the sparse classes. |
|
471 |
|
472 The sparse classes will not be entirely described in this section, due |
6572
|
473 to their similarity with the existing @code{Array} classes. However, |
|
474 there are a few differences due the different nature of sparse objects, |
|
475 and these will be described. Firstly, although it is fundamentally |
|
476 possible to have N-dimensional sparse objects, the Octave sparse classes do |
6571
|
477 not allow them at this time. So all operations of the sparse classes |
6569
|
478 must be 2-dimensional. This means that in fact @code{SparseMatrix} is |
|
479 similar to Octave's @code{Matrix} class rather than its |
|
480 @code{NDArray} class. |
|
481 |
|
482 @menu |
6572
|
483 * Array and Sparse Differences:: |
|
484 * Creating Sparse Matrices in Oct-Files:: |
|
485 * Using Sparse Matrices in Oct-Files:: |
6569
|
486 @end menu |
|
487 |
6572
|
488 @node Array and Sparse Differences |
6569
|
489 @subsubsection The Differences between the Array and Sparse Classes |
|
490 |
|
491 The number of elements in a sparse matrix is considered to be the number |
6571
|
492 of non-zero elements rather than the product of the dimensions. Therefore |
6569
|
493 |
|
494 @example |
6577
|
495 @group |
|
496 SparseMatrix sm; |
|
497 @dots{} |
|
498 int nel = sm.nelem (); |
|
499 @end group |
6569
|
500 @end example |
|
501 |
6571
|
502 returns the number of non-zero elements. If the user really requires the |
6569
|
503 number of elements in the matrix, including the non-zero elements, they |
6571
|
504 should use @code{numel} rather than @code{nelem}. Note that for very |
6569
|
505 large matrices, where the product of the two dimensions is large that |
|
506 the representation of the an unsigned int, then @code{numel} can overflow. |
|
507 An example is @code{speye(1e6)} which will create a matrix with a million |
6571
|
508 rows and columns, but only a million non-zero elements. Therefore the |
6569
|
509 number of rows by the number of columns in this case is more than two |
|
510 hundred times the maximum value that can be represented by an unsigned int. |
|
511 The use of @code{numel} should therefore be avoided useless it is known |
|
512 it won't overflow. |
|
513 |
|
514 Extreme care must be take with the elem method and the "()" operator, |
6571
|
515 which perform basically the same function. The reason is that if a |
6569
|
516 sparse object is non-const, then Octave will assume that a |
6571
|
517 request for a zero element in a sparse matrix is in fact a request |
|
518 to create this element so it can be filled. Therefore a piece of |
6569
|
519 code like |
|
520 |
|
521 @example |
6577
|
522 @group |
|
523 SparseMatrix sm; |
|
524 @dots{} |
|
525 for (int j = 0; j < nc; j++) |
|
526 for (int i = 0; i < nr; i++) |
|
527 std::cerr << " (" << i << "," << j << "): " << sm(i,j) |
|
528 << std::endl; |
|
529 @end group |
6569
|
530 @end example |
|
531 |
|
532 is a great way of turning the sparse matrix into a dense one, and a |
|
533 very slow way at that since it reallocates the sparse object at each |
|
534 zero element in the matrix. |
|
535 |
|
536 An easy way of preventing the above from happening is to create a temporary |
6571
|
537 constant version of the sparse matrix. Note that only the container for |
6569
|
538 the sparse matrix will be copied, while the actual representation of the |
6571
|
539 data will be shared between the two versions of the sparse matrix. So this |
|
540 is not a costly operation. For example, the above would become |
6569
|
541 |
|
542 @example |
6577
|
543 @group |
|
544 SparseMatrix sm; |
|
545 @dots{} |
|
546 const SparseMatrix tmp (sm); |
|
547 for (int j = 0; j < nc; j++) |
|
548 for (int i = 0; i < nr; i++) |
|
549 std::cerr << " (" << i << "," << j << "): " << tmp(i,j) |
|
550 << std::endl; |
|
551 @end group |
6569
|
552 @end example |
|
553 |
|
554 Finally, as the sparse types aren't just represented as a contiguous |
|
555 block of memory, the @code{fortran_vec} method of the @code{Array<T>} |
6571
|
556 is not available. It is however replaced by three separate methods |
6569
|
557 @code{ridx}, @code{cidx} and @code{data}, that access the raw compressed |
|
558 column format that the Octave sparse matrices are stored in. |
|
559 Additionally, these methods can be used in a manner similar to @code{elem}, |
6571
|
560 to allow the matrix to be accessed or filled. However, in that case it is |
6569
|
561 up to the user to respect the sparse matrix compressed column format |
|
562 discussed previous. |
|
563 |
6572
|
564 @node Creating Sparse Matrices in Oct-Files |
|
565 @subsubsection Creating Sparse Matrices in Oct-Files |
6569
|
566 |
6572
|
567 You have several alternatives for creating a sparse matrix. |
|
568 You can first create the data as three vectors representing the |
6569
|
569 row and column indexes and the data, and from those create the matrix. |
6572
|
570 Or alternatively, you can create a sparse matrix with the appropriate |
6571
|
571 amount of space and then fill in the values. Both techniques have their |
6569
|
572 advantages and disadvantages. |
|
573 |
6572
|
574 Here is an example of how to create a small sparse matrix with the first |
|
575 technique |
6569
|
576 |
|
577 @example |
6577
|
578 @group |
|
579 int nz = 4, nr = 3, nc = 4; |
|
580 |
|
581 ColumnVector ridx (nz); |
|
582 ColumnVector cidx (nz); |
|
583 ColumnVector data (nz); |
6569
|
584 |
6577
|
585 ridx(0) = 0; ridx(1) = 0; ridx(2) = 1; ridx(3) = 2; |
|
586 cidx(0) = 0; cidx(1) = 1; cidx(2) = 3; cidx(3) = 3; |
|
587 data(0) = 1; data(1) = 2; data(2) = 3; data(3) = 4; |
6569
|
588 |
6577
|
589 SparseMatrix sm (data, ridx, cidx, nr, nc); |
|
590 @end group |
6569
|
591 @end example |
|
592 |
6572
|
593 @noindent |
6571
|
594 which creates the matrix given in section @ref{Storage}. Note that |
6569
|
595 the compressed matrix format is not used at the time of the creation |
6571
|
596 of the matrix itself, however it is used internally. |
6569
|
597 |
|
598 As previously mentioned, the values of the sparse matrix are stored |
6571
|
599 in increasing column-major ordering. Although the data passed by the |
6569
|
600 user does not need to respect this requirement, the pre-sorting the |
|
601 data significantly speeds up the creation of the sparse matrix. |
|
602 |
|
603 The disadvantage of this technique of creating a sparse matrix is |
6571
|
604 that there is a brief time where two copies of the data exists. Therefore |
6569
|
605 for extremely memory constrained problems this might not be the right |
|
606 technique to create the sparse matrix. |
|
607 |
|
608 The alternative is to first create the sparse matrix with the desired |
6571
|
609 number of non-zero elements and then later fill those elements in. The |
|
610 easiest way to do this is |
6569
|
611 |
6571
|
612 @example |
6577
|
613 @group |
|
614 int nz = 4, nr = 3, nc = 4; |
|
615 SparseMatrix sm (nr, nc, nz); |
|
616 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4; |
|
617 @end group |
6569
|
618 @end example |
|
619 |
6571
|
620 That creates the same matrix as previously. Again, although it is not |
6569
|
621 strictly necessary, it is significantly faster if the sparse matrix is |
|
622 created in this manner that the elements are added in column-major |
6571
|
623 ordering. The reason for this is that if the elements are inserted |
6569
|
624 at the end of the current list of known elements then no element |
|
625 in the matrix needs to be moved to allow the new element to be |
6571
|
626 inserted. Only the column indexes need to be updated. |
6569
|
627 |
|
628 There are a few further points to note about this technique of creating |
6572
|
629 a sparse matrix. Firstly, it is possible to create a sparse matrix |
6571
|
630 with fewer elements than are actually inserted in the matrix. Therefore |
6569
|
631 |
6571
|
632 @example |
6577
|
633 @group |
|
634 int nz = 4, nr = 3, nc = 4; |
|
635 SparseMatrix sm (nr, nc, 0); |
|
636 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4; |
|
637 @end group |
6569
|
638 @end example |
|
639 |
6572
|
640 @noindent |
|
641 is perfectly valid. However it is a very bad idea. The reason is that |
6569
|
642 as each new element is added to the sparse matrix the space allocated |
6571
|
643 to it is increased by reallocating the memory. This is an expensive |
6569
|
644 operation, that will significantly slow this means of creating a sparse |
6572
|
645 matrix. Furthermore, it is possible to create a sparse matrix with |
|
646 too much storage, so having @var{nz} above equaling 6 is also valid. |
6569
|
647 The disadvantage is that the matrix occupies more memory than strictly |
|
648 needed. |
|
649 |
6572
|
650 It is not always easy to know the number of non-zero elements prior |
6571
|
651 to filling a matrix. For this reason the additional storage for the |
6569
|
652 sparse matrix can be removed after its creation with the |
6571
|
653 @dfn{maybe_compress} function. Furthermore, the maybe_compress can |
6569
|
654 deallocate the unused storage, but it can equally remove zero elements |
|
655 from the matrix. The removal of zero elements from the matrix is |
|
656 controlled by setting the argument of the @dfn{maybe_compress} function |
6572
|
657 to be @samp{true}. However, the cost of removing the zeros is high because it |
6571
|
658 implies resorting the elements. Therefore, if possible it is better |
|
659 is the user doesn't add the zeros in the first place. An example of |
6569
|
660 the use of @dfn{maybe_compress} is |
|
661 |
|
662 @example |
6577
|
663 @group |
6569
|
664 int nz = 6, nr = 3, nc = 4; |
6577
|
665 |
6569
|
666 SparseMatrix sm1 (nr, nc, nz); |
|
667 sm1(0,0) = 1; sm1(0,1) = 2; sm1(1,3) = 3; sm1(2,3) = 4; |
|
668 sm1.maybe_compress (); // No zero elements were added |
|
669 |
|
670 SparseMatrix sm2 (nr, nc, nz); |
6571
|
671 sm2(0,0) = 1; sm2(0,1) = 2; sm(0,2) = 0; sm(1,2) = 0; |
6569
|
672 sm1(1,3) = 3; sm1(2,3) = 4; |
|
673 sm2.maybe_compress (true); // Zero elements were added |
6577
|
674 @end group |
6569
|
675 @end example |
|
676 |
|
677 The use of the @dfn{maybe_compress} function should be avoided if |
|
678 possible, as it will slow the creation of the matrices. |
|
679 |
|
680 A third means of creating a sparse matrix is to work directly with |
6571
|
681 the data in compressed row format. An example of this technique might |
6569
|
682 be |
|
683 |
|
684 @c Note the @verbatim environment is a relatively new addition to texinfo. |
6571
|
685 @c Therefore use the @example environment and replace @, with @@, |
6569
|
686 @c { with @{, etc |
|
687 |
|
688 @example |
6577
|
689 @group |
|
690 octave_value arg; |
|
691 @dots{} |
|
692 int nz = 6, nr = 3, nc = 4; // Assume we know the max no nz |
|
693 SparseMatrix sm (nr, nc, nz); |
|
694 Matrix m = arg.matrix_value (); |
6569
|
695 |
6577
|
696 int ii = 0; |
|
697 sm.cidx (0) = 0; |
|
698 for (int j = 1; j < nc; j++) |
|
699 @{ |
|
700 for (int i = 0; i < nr; i++) |
|
701 @{ |
|
702 double tmp = foo (m(i,j)); |
|
703 if (tmp != 0.) |
|
704 @{ |
|
705 sm.data(ii) = tmp; |
|
706 sm.ridx(ii) = i; |
|
707 ii++; |
|
708 @} |
|
709 @} |
|
710 sm.cidx(j+1) = ii; |
|
711 @} |
|
712 sm.maybe_compress (); // If don't know a-priori the final no of nz. |
|
713 @end group |
6569
|
714 @end example |
|
715 |
6572
|
716 @noindent |
6569
|
717 which is probably the most efficient means of creating the sparse matrix. |
|
718 |
|
719 Finally, it might sometimes arise that the amount of storage initially |
6571
|
720 created is insufficient to completely store the sparse matrix. Therefore, |
6569
|
721 the method @code{change_capacity} exists to reallocate the sparse memory. |
6571
|
722 The above example would then be modified as |
6569
|
723 |
|
724 @example |
6577
|
725 @group |
|
726 octave_value arg; |
|
727 @dots{} |
|
728 int nz = 6, nr = 3, nc = 4; // Assume we know the max no nz |
|
729 SparseMatrix sm (nr, nc, nz); |
|
730 Matrix m = arg.matrix_value (); |
6569
|
731 |
6577
|
732 int ii = 0; |
|
733 sm.cidx (0) = 0; |
|
734 for (int j = 1; j < nc; j++) |
|
735 @{ |
|
736 for (int i = 0; i < nr; i++) |
|
737 @{ |
|
738 double tmp = foo (m(i,j)); |
|
739 if (tmp != 0.) |
|
740 @{ |
|
741 if (ii == nz) |
|
742 @{ |
|
743 nz += 2; // Add 2 more elements |
|
744 sm.change_capacity (nz); |
|
745 @} |
|
746 sm.data(ii) = tmp; |
|
747 sm.ridx(ii) = i; |
|
748 ii++; |
|
749 @} |
|
750 @} |
|
751 sm.cidx(j+1) = ii; |
|
752 @} |
|
753 sm.maybe_mutate (); // If don't know a-priori the final no of nz. |
|
754 @end group |
6569
|
755 @end example |
|
756 |
|
757 Note that both increasing and decreasing the number of non-zero elements in |
6571
|
758 a sparse matrix is expensive, as it involves memory reallocation. Also as |
6569
|
759 parts of the matrix, though not its entirety, exist as the old and new copy |
6571
|
760 at the same time, additional memory is needed. Therefore if possible this |
6569
|
761 should be avoided. |
|
762 |
6572
|
763 @node Using Sparse Matrices in Oct-Files |
6569
|
764 @subsubsection Using Sparse Matrices in Oct-Files |
|
765 |
|
766 Most of the same operators and functions on sparse matrices that are |
|
767 available from the Octave are equally available with oct-files. |
|
768 The basic means of extracting a sparse matrix from an @code{octave_value} |
|
769 and returning them as an @code{octave_value}, can be seen in the |
|
770 following example |
|
771 |
|
772 @example |
6577
|
773 @group |
|
774 octave_value_list retval; |
6569
|
775 |
6577
|
776 SparseMatrix sm = args(0).sparse_matrix_value (); |
|
777 SparseComplexMatrix scm = args(1).sparse_complex_matrix_value (); |
|
778 SparseBoolMatrix sbm = args(2).sparse_bool_matrix_value (); |
|
779 @dots{} |
|
780 retval(2) = sbm; |
|
781 retval(1) = scm; |
|
782 retval(0) = sm; |
|
783 @end group |
6569
|
784 @end example |
|
785 |
|
786 The conversion to an octave-value is handled by the sparse |
|
787 @code{octave_value} constructors, and so no special care is needed. |
|
788 |
|
789 @node Accessing Global Variables in Oct-Files |
|
790 @subsection Accessing Global Variables in Oct-Files |
|
791 |
|
792 Global variables allow variables in the global scope to be |
6571
|
793 accessed. Global variables can easily be accessed with oct-files using |
6569
|
794 the support functions @code{get_global_value} and |
6571
|
795 @code{set_global_value}. @code{get_global_value} takes two arguments, |
|
796 the first is a string representing the variable name to obtain. The |
6569
|
797 second argument is a boolean argument specifying what to do in the case |
6571
|
798 that no global variable of the desired name is found. An example of the |
6569
|
799 use of these two functions is |
|
800 |
6578
|
801 @examplefile{globaldemo.cc} |
6569
|
802 |
|
803 An example of its use is |
|
804 |
|
805 @example |
|
806 @group |
|
807 global a b |
|
808 b = 10; |
|
809 globaldemo ("b") |
|
810 @result{} 10 |
|
811 globaldemo ("c") |
|
812 @result{} "Global variable not found" |
|
813 num2str (a) |
|
814 @result{} 42 |
|
815 @end group |
|
816 @end example |
|
817 |
|
818 @node Calling Octave Functions from Oct-Files |
|
819 @subsection Calling Octave Functions from Oct-Files |
|
820 |
|
821 There is often a need to be able to call another octave function from |
|
822 within an oct-file, and there are many examples of such within octave |
6571
|
823 itself. For example the @code{quad} function is an oct-file that |
6569
|
824 calculates the definite integral by quadrature over a user supplied |
|
825 function. |
|
826 |
6571
|
827 There are also many ways in which a function might be passed. It might |
|
828 be passed as one of |
6569
|
829 |
|
830 @enumerate 1 |
|
831 @item Function Handle |
|
832 @item Anonymous Function Handle |
|
833 @item Inline Function |
|
834 @item String |
|
835 @end enumerate |
|
836 |
|
837 The example below demonstrates an example that accepts all four means of |
6571
|
838 passing a function to an oct-file. |
6569
|
839 |
6578
|
840 @examplefile{funcdemo.cc} |
6569
|
841 |
|
842 The first argument to this demonstration is the user supplied function |
|
843 and the following arguments are all passed to the user function. |
|
844 |
|
845 @example |
|
846 @group |
6572
|
847 funcdemo (@@sin,1) |
6569
|
848 @result{} 0.84147 |
6572
|
849 funcdemo (@@(x) sin(x), 1) |
6569
|
850 @result{} 0.84147 |
6572
|
851 funcdemo (inline ("sin(x)"), 1) |
6569
|
852 @result{} 0.84147 |
6572
|
853 funcdemo ("sin",1) |
6569
|
854 @result{} 0.84147 |
|
855 funcdemo (@@atan2, 1, 1) |
|
856 @result{} 0.78540 |
|
857 @end group |
|
858 @end example |
|
859 |
|
860 When the user function is passed as a string, the treatment of the |
6571
|
861 function is different. In some cases it is necessary to always have the |
6572
|
862 user supplied function as an @code{octave_function} object. In that |
|
863 case the string argument can be used to create a temporary function like |
6569
|
864 |
|
865 @example |
|
866 @group |
6577
|
867 std::octave fcn_name = unique_symbol_name ("__fcn__"); |
|
868 std::string fname = "function y = "; |
|
869 fname.append (fcn_name); |
|
870 fname.append ("(x) y = "); |
|
871 fcn = extract_function (args(0), "funcdemo", fcn_name, |
|
872 fname, "; endfunction"); |
|
873 @dots{} |
|
874 if (fcn_name.length ()) |
|
875 clear_function (fcn_name); |
6569
|
876 @end group |
|
877 @end example |
|
878 |
6571
|
879 There are two important things to know in this case. The number of input |
6569
|
880 arguments to the user function is fixed, and in the above is a single |
|
881 argument, and secondly to avoid leaving the temporary function in the |
|
882 Octave symbol table it should be cleared after use. |
|
883 |
|
884 @node Calling External Code from Oct-Files |
|
885 @subsection Calling External Code from Oct-Files |
|
886 |
|
887 Linking external C code to Octave is relatively simple, as the C |
6571
|
888 functions can easily be called directly from C++. One possible issue is |
6569
|
889 the declarations of the external C functions might need to be explicitly |
6571
|
890 defined as C functions to the compiler. If the declarations of the |
6569
|
891 external C functions are in the header @code{foo.h}, then the manner in |
|
892 which to ensure that the C++ compiler treats these declarations as C |
|
893 code is |
|
894 |
|
895 @example |
|
896 @group |
|
897 #ifdef __cplusplus |
6571
|
898 extern "C" |
6569
|
899 @{ |
|
900 #endif |
|
901 #include "foo.h" |
|
902 #ifdef __cplusplus |
|
903 @} /* end extern "C" */ |
|
904 #endif |
|
905 @end group |
|
906 @end example |
|
907 |
6571
|
908 Calling Fortran code however can pose some difficulties. This is due to |
6569
|
909 differences in the manner in compilers treat the linking of Fortran code |
6571
|
910 with C or C++ code. Octave supplies a number of macros that allow |
6569
|
911 consistent behavior across a number of compilers. |
|
912 |
|
913 The underlying Fortran code should use the @code{XSTOPX} function to |
6571
|
914 replace the Fortran @code{STOP} function. @code{XSTOPX} uses the Octave |
6569
|
915 exception handler to treat failing cases in the fortran code |
6571
|
916 explicitly. Note that Octave supplies its own replacement blas |
6569
|
917 @code{XERBLA} function, which uses @code{XSTOPX}. |
|
918 |
6572
|
919 If the underlying code calls @code{XSTOPX}, then the @code{F77_XFCN} |
6571
|
920 macro should be used to call the underlying fortran function. The Fortran |
6569
|
921 exception state can then be checked with the global variable |
6572
|
922 @code{f77_exception_encountered}. If @code{XSTOPX} will not be called, |
6569
|
923 then the @code{F77_FCN} macro should be used instead to call the Fortran |
|
924 code. |
|
925 |
|
926 There is no harm in using @code{F77_XFCN} in all cases, except that for |
|
927 Fortran code that is short running and executes a large number of times, |
6571
|
928 there is potentially an overhead in doing so. However, if @code{F77_FCN} |
6569
|
929 is used with code that calls @code{XSTOP}, Octave can generate a |
|
930 segmentation fault. |
|
931 |
|
932 An example of the inclusion of a Fortran function in an oct-file is |
|
933 given in the following example, where the C++ wrapper is |
|
934 |
6578
|
935 @examplefile{fortdemo.cc} |
6569
|
936 |
6572
|
937 @noindent |
6569
|
938 and the fortran function is |
|
939 |
6578
|
940 @examplefile{fortsub.f} |
6569
|
941 |
|
942 This example demonstrates most of the features needed to link to an |
|
943 external Fortran function, including passing arrays and strings, as well |
6571
|
944 as exception handling. An example of the behavior of this function is |
6569
|
945 |
|
946 @example |
|
947 @group |
6572
|
948 [b, s] = fortdemo (1:3) |
6569
|
949 @result{} |
|
950 b = 1.00000 0.50000 0.33333 |
|
951 s = There are 3 values in the input vector |
|
952 [b, s] = fortdemo(0:3) |
|
953 error: fortsub:divide by zero |
|
954 error: exception encountered in Fortran subroutine fortsub_ |
|
955 error: fortdemo: error in fortran |
|
956 @end group |
|
957 @end example |
|
958 |
|
959 @node Allocating Local Memory in Oct-Files |
|
960 @subsection Allocating Local Memory in Oct-Files |
|
961 |
|
962 Allocating memory within an oct-file might seem easy as the C++ |
6571
|
963 new/delete operators can be used. However, in that case care must be |
|
964 taken to avoid memory leaks. The preferred manner in which to allocate |
6572
|
965 memory for use locally is to use the @code{OCTAVE_LOCAL_BUFFER} macro. |
|
966 An example of its use is |
6569
|
967 |
|
968 @example |
|
969 OCTAVE_LOCAL_BUFFER (double, tmp, len) |
|
970 @end example |
|
971 |
|
972 that returns a pointer @code{tmp} of type @code{double *} of length |
|
973 @code{len}. |
|
974 |
|
975 @node Input Parameter Checking in Oct-Files |
|
976 @subsection Input Parameter Checking in Oct-Files |
|
977 |
6580
|
978 As oct-files are compiled functions they have the possibility of causing |
|
979 Octave to abort abnormally. It is therefore inportant that the input |
|
980 parameters to each and every function has the minimum of parameter |
|
981 checking needed to ensure that Octave behaves well. |
|
982 |
|
983 The minimum requirement, as previously discussed, is to check the number |
|
984 of input arguments before using them to avoid referencing a non existent |
|
985 argument. However, it some case this might not be sufficient as the |
|
986 underlying code imposes further constraints. For example an external |
|
987 function call might be undefined if the input arguments are not |
|
988 integers, or if one of the arguments is zero. Therefore, oct-files often |
|
989 need additional input parameter checking. |
|
990 |
|
991 There are several functions within Octave that might be useful for the |
|
992 purposes of parameter checking. These include the methods of the |
|
993 octave_value class like @code{is_real_matrix}, etc, but equally include |
|
994 more specialized functions. Some of the more common ones are |
|
995 demonstrated in the following example |
|
996 |
|
997 @examplefile{paramdemo.cc} |
|
998 |
|
999 @noindent |
|
1000 and an example of its use is |
|
1001 |
|
1002 @example |
|
1003 @group |
|
1004 paramdemo ([1, 2, NaN, Inf]) |
|
1005 @result{} Properties of input array: |
|
1006 includes Inf or NaN values |
|
1007 includes other values than 1 and 0 |
|
1008 includes only int, Inf or NaN values |
|
1009 @end group |
|
1010 @end example |
6569
|
1011 |
|
1012 @node Exception and Error Handling in Oct-Files |
|
1013 @subsection Exception and Error Handling in Oct-Files |
|
1014 |
|
1015 Another important feature of Octave is its ability to react to the user |
6571
|
1016 typing @kbd{Control-C} even during calculations. This ability is based on the |
6569
|
1017 C++ exception handler, where memory allocated by the C++ new/delete |
6571
|
1018 methods are automatically released when the exception is treated. When |
6569
|
1019 writing an oct-file, to allow Octave to treat the user typing @kbd{Control-C}, |
6571
|
1020 the @code{OCTAVE_QUIT} macro is supplied. For example |
6569
|
1021 |
|
1022 @example |
|
1023 @group |
6577
|
1024 for (octave_idx_type i = 0; i < a.nelem (); i++) |
|
1025 @{ |
|
1026 OCTAVE_QUIT; |
|
1027 b.elem(i) = 2. * a.elem(i); |
|
1028 @} |
6569
|
1029 @end group |
|
1030 @end example |
|
1031 |
6572
|
1032 The presence of the @code{OCTAVE_QUIT} macro in the inner loop allows Octave to |
6571
|
1033 treat the user request with the @kbd{Control-C}. Without this macro, the user |
6569
|
1034 must either wait for the function to return before the interrupt is |
|
1035 processed, or press @kbd{Control-C} three times to force Octave to exit. |
|
1036 |
6572
|
1037 The @code{OCTAVE_QUIT} macro does impose a very small speed penalty, and so for |
6569
|
1038 loops that are known to be small it might not make sense to include |
6572
|
1039 @code{OCTAVE_QUIT}. |
6569
|
1040 |
|
1041 When creating an oct-file that uses an external libraries, the function |
|
1042 might spend a significant portion of its time in the external |
6572
|
1043 library. It is not generally possible to use the @code{OCTAVE_QUIT} macro in |
6571
|
1044 this case. The alternative in this case is |
6569
|
1045 |
|
1046 @example |
|
1047 @group |
6577
|
1048 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
1049 @dots{} some code that calls a "foreign" function @dots{} |
|
1050 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
6569
|
1051 @end group |
|
1052 @end example |
|
1053 |
|
1054 The disadvantage of this is that if the foreign code allocates any |
|
1055 memory internally, then this memory might be lost during an interrupt, |
6571
|
1056 without being deallocated. Therefore, ideally Octave itself should |
6569
|
1057 allocate any memory that is needed by the foreign code, with either the |
6572
|
1058 fortran_vec method or the @code{OCTAVE_LOCAL_BUFFER} macro. |
6569
|
1059 |
6571
|
1060 The Octave unwind_protect mechanism (@ref{The unwind_protect Statement}) |
|
1061 can also be used in oct-files. In conjunction with the exception |
6569
|
1062 handling of Octave, it is important to enforce that certain code is run |
6571
|
1063 to allow variables, etc to be restored even if an exception occurs. An |
6569
|
1064 example of the use of this mechanism is |
|
1065 |
6578
|
1066 @examplefile{unwinddemo.cc} |
6569
|
1067 |
|
1068 As can be seen in the example |
|
1069 |
|
1070 @example |
|
1071 @group |
6572
|
1072 unwinddemo (1, 0) |
6569
|
1073 @result{} Inf |
|
1074 1 / 0 |
|
1075 @result{} warning: division by zero |
|
1076 Inf |
|
1077 @end group |
|
1078 @end example |
|
1079 |
|
1080 The division by zero (and in fact all warnings) is disabled in the |
|
1081 @code{unwinddemo} function. |
|
1082 |
|
1083 @node Documentation and Test of Oct-Files |
|
1084 @subsection Documentation and Test of Oct-Files |
|
1085 |
6580
|
1086 The documentation of an oct-file is the fourth string parameter of the |
|
1087 @code{DEFUN_DLD} macro. This string can be formatted in the same manner |
|
1088 as the help strings for user functions (@ref{Documentation Tips}), |
|
1089 however there are some issue that are particular to the formatting of |
|
1090 help strings within oct-files. |
|
1091 |
|
1092 The major issue is that the help string will typically be longer than a |
|
1093 single line of text, and so the formatting of long help strings need to |
|
1094 be taken into account. There are several manner in which to happen this |
|
1095 issue, but the most common is illustrated in the following example |
|
1096 |
|
1097 @example |
|
1098 @group |
|
1099 DEFUN_DLD (do_what_i_want, args, nargout, |
|
1100 "-*- texinfo -*-\n\ |
|
1101 @@deftypefn @{Function File@} @{@} do_what_i_say (@@var@{n@})\n\ |
|
1102 A function that does what the user actually wants rather than what\n\ |
|
1103 they requested.\n\ |
|
1104 @@end deftypefn") |
|
1105 @{ |
|
1106 @dots{} |
|
1107 @} |
|
1108 @end group |
|
1109 @end example |
|
1110 |
|
1111 @noindent |
|
1112 where, as can be seen, end line of text within the help string is |
|
1113 terminated by @code{\n\} which is an an embedded new-line in the string |
|
1114 together with a C++ string continuation character. Note that the final |
|
1115 @code{\} must be the last character on the line. |
|
1116 |
|
1117 Octave also includes the ability to embed the test and demonstration |
|
1118 code for a function within the code itself (@ref{Test and Demo Functions}). |
|
1119 This can be used from within oct-files (or in fact any file) with |
|
1120 certain provisos. Firstly, the test and demo functions of Octave look |
|
1121 for a @code{%!} as the first characters on a new-line to identify test |
|
1122 and demonstration code. This is equally a requirement for |
|
1123 oct-files. Furthermore the test and demonstration code must be included |
|
1124 in a comment block of the compiled code to avoid it being interpreted by |
|
1125 the compiler. Finally, he Octave test and demonstration code must have |
|
1126 access to the source code of the oct-file and not just the compiled code |
|
1127 as th<e tests are stripped from the compiled code. An example in an |
|
1128 oct-file might be |
|
1129 |
|
1130 @example |
|
1131 @group |
|
1132 /* |
|
1133 |
|
1134 %!error (sin()) |
|
1135 %!error (sin(1,1)) |
|
1136 %!assert (sin([1,2]),[sin(1),sin(2)]) |
|
1137 |
|
1138 */ |
|
1139 @end group |
|
1140 @end example |
6569
|
1141 |
|
1142 @node Application Programming Interface for Oct-Files |
|
1143 @subsection Application Programming Interface for Oct-Files |
|
1144 |
|
1145 WRITE ME, using Coda section 1.3 as a starting point. |
|
1146 |
|
1147 @node Mex-Files |
|
1148 @section Mex-Files |
|
1149 @cindex mex-files |
|
1150 @cindex mex |
|
1151 |
|
1152 Octave includes an interface to allow legacy mex-files to be compiled |
6571
|
1153 and used with Octave. This interface can also be used to share code |
|
1154 between Octave and non Octave users. However, as mex-files expose the |
6569
|
1155 intern API of a product alternative to Octave, and the internal |
|
1156 structure of Octave is different to this product, a mex-file can never |
6571
|
1157 have the same performance in Octave as the equivalent oct-file. In |
6569
|
1158 particular to support the manner in which mex-files access the variables |
|
1159 passed to mex functions, there are a significant number of additional |
6571
|
1160 copies of memory when calling or returning from a mex function. For this |
6569
|
1161 reason, new code should be written using the oct-file interface |
|
1162 discussed above if possible. |
|
1163 |
|
1164 @menu |
6572
|
1165 * Getting Started with Mex-Files:: |
6580
|
1166 * Working with Matrices and Arrays in Mex-Files:: |
|
1167 * Character Strings in Mex-Files:: |
|
1168 * Cell Arrays with Mex-Files:: |
6572
|
1169 * Structures with Mex-Files:: |
|
1170 * Sparse Matrices with Mex-Files:: |
6580
|
1171 * Calling Other Functions in Mex-Files:: |
|
1172 * Application Programming Interface for Mex-Files:: |
6569
|
1173 @end menu |
|
1174 |
|
1175 @node Getting Started with Mex-Files |
|
1176 @subsection Getting Started with Mex-Files |
|
1177 |
|
1178 The basic command to build a mex-file is either @code{mkoctfile --mex} or |
6571
|
1179 @code{mex}. The first can either be used from within Octave or from the |
|
1180 commandline. However, to avoid issues with the installation of other |
6569
|
1181 products, the use of the command @code{mex} is limited to within Octave. |
|
1182 |
|
1183 @DOCSTRING(mex) |
|
1184 |
|
1185 @DOCSTRING(mexext) |
|
1186 |
|
1187 One important difference between the use of mex with other products and |
|
1188 with Octave is that the header file "matrix.h" is implicitly included |
6571
|
1189 through the inclusion of "mex.h". This is to avoid a conflict with the |
6569
|
1190 Octave file "Matrix.h" with operating systems and compilers that don't |
|
1191 distinguish between filenames in upper and lower case |
|
1192 |
|
1193 Consider the short example |
|
1194 |
6578
|
1195 @examplefile{firstmexdemo.c} |
6569
|
1196 |
6580
|
1197 This simple example demonstrates the basics of writing a mex-file. The |
|
1198 entry point into the mex-file is defined by @code{mexFunction}. Note |
|
1199 that the function name is not explicitly included in the |
|
1200 @code{mexFunction} and so there can only be a single @code{mexFunction} |
|
1201 entry point per-file. Also the name of the function is determined by the |
|
1202 name of the mex-file itself. Therefore if the above function is in the |
|
1203 file @file{firstmexdemo.c}, it can be compiled with |
|
1204 |
|
1205 @example |
|
1206 mkoctfile --mex firstmexdemo.c |
|
1207 @end example |
|
1208 |
|
1209 @noindent |
|
1210 which creates a file @file{firstmexdemo.mex}. The function can then be run |
|
1211 from Octave as |
|
1212 |
|
1213 @example |
|
1214 @group |
|
1215 firstmexdemo() |
|
1216 @result{} 1.2346 |
|
1217 @end group |
|
1218 @end example |
|
1219 |
|
1220 It should be noted that the mex-file contains no help string for the |
|
1221 functions it contains. To document mex-files, there should exist an |
|
1222 m-file in the same directory as the mex-file itself. Taking the above as |
|
1223 an example, we would therefore have a file @file{firstmexdemo.m} that might |
|
1224 contain the text |
|
1225 |
|
1226 @example |
|
1227 %FIRSTMEXDEMO Simple test of the functionality of a mex-file. |
|
1228 @end example |
|
1229 |
|
1230 In this case, the function that will be executed within Octave will be |
|
1231 given by the mex-file, while the help string will come from the |
|
1232 m-file. This can also be useful to allow a sample implementation of the |
|
1233 mex-file within the Octave language itself for testing purposes. |
|
1234 |
|
1235 Although we can not have multiple entry points into a single mex-file, |
|
1236 we can use the @code{mexFunctionName} function to determine what name |
|
1237 the mex-file was called with. This can be used to alter the behavior of |
|
1238 the mex-file based on the function name. For example if |
|
1239 |
|
1240 @examplefile{myfunc.c} |
|
1241 |
|
1242 @noindent |
|
1243 is in file @file{myfunc.c}, and it is compiled with |
|
1244 |
|
1245 @example |
|
1246 @group |
|
1247 mkoctfile --mex myfunc.c |
|
1248 ln -s myfunc.mex myfunc2.mex |
|
1249 @end group |
|
1250 @end example |
|
1251 |
|
1252 Then as can be seen by |
|
1253 |
|
1254 @example |
|
1255 @group |
|
1256 myfunc() |
|
1257 @result{} You called function: myfunc |
|
1258 This is the principal function |
|
1259 myfunc2() |
|
1260 @result{} You called function: myfunc2 |
|
1261 @end group |
|
1262 @end example |
|
1263 |
|
1264 @noindent |
|
1265 the behavior of the mex-file can be altered depending on the functions |
|
1266 name. |
|
1267 |
|
1268 @node Working with Matrices and Arrays in Mex-Files |
|
1269 @subsection Working with Matrices and Arrays in Mex-Files |
|
1270 |
|
1271 The basic mex type of all variables is @code{mxArray}. All variables, |
|
1272 such as Matrices, cell arrays or structures are all stored in this basic |
|
1273 type, and this type serves basically the same purpose as the |
|
1274 octave_value class in oct-files. That is it acts as a container for the |
|
1275 more specialized types. |
|
1276 |
|
1277 WRITE ME |
|
1278 |
|
1279 @node Character Strings in Mex-Files |
|
1280 @subsection Character Strings in Mex-Files |
|
1281 |
|
1282 WRITE ME |
|
1283 |
|
1284 @node Cell Arrays with Mex-Files |
|
1285 @subsection Cell Arrays with Mex-Files |
6569
|
1286 |
|
1287 WRITE ME |
|
1288 |
6572
|
1289 @node Structures with Mex-Files |
|
1290 @subsection Structures with Mex-Files |
6569
|
1291 |
6580
|
1292 See the file @file{mystruct.c} |
|
1293 |
|
1294 @examplefile{mystruct.c} |
|
1295 |
6569
|
1296 WRITE ME |
|
1297 |
|
1298 @node Sparse Matrices with Mex-Files |
|
1299 @subsection Sparse Matrices with Mex-Files |
|
1300 |
6580
|
1301 See the file @file{mysparse.c} |
|
1302 |
|
1303 @examplefile{mysparse.c} |
|
1304 |
6569
|
1305 WRITE ME |
|
1306 |
6580
|
1307 @node Calling Other Functions in Mex-Files |
|
1308 @subsection Calling Other Functions in Mex-Files |
|
1309 |
|
1310 It is also possible call other Octave functions from within a mex-file |
|
1311 using @code{mexCallMATLAB}. An example of the use of |
|
1312 @code{mexCallMATLAB} can be see in the example below |
|
1313 |
|
1314 @examplefile{myfeval.c} |
|
1315 |
|
1316 If this code is in the file @file{myfeval.c}, and is compiled to |
|
1317 @file{myfeval.mex}, then an example of its use is |
6569
|
1318 |
6580
|
1319 @example |
|
1320 @group |
|
1321 myfeval("sin", 1) |
|
1322 a = myfeval("sin", 1) |
|
1323 @result{} Hello, World! |
|
1324 I have 2 inputs and 1 outputs |
|
1325 I'm going to call the interpreter function sin |
|
1326 a = 0.84147 |
|
1327 @end group |
|
1328 @end example |
|
1329 |
|
1330 Note that it is not possible to use function handles or inline functions |
|
1331 within a mex-file. |
|
1332 |
|
1333 @node Application Programming Interface for Mex-Files |
|
1334 @subsection Application Programming Interface for Mex-Files |
|
1335 |
|
1336 WRITE ME, refer to mex.h and mexproto.h |
6569
|
1337 |
|
1338 @node Standalone Programs |
|
1339 @section Standalone Programs |
|
1340 |
|
1341 The libraries Octave itself uses, can be utilized in standalone |
6571
|
1342 applications. These applications then have access, for example, to the |
6569
|
1343 array and matrix classes as well as to all the Octave algorithms. The |
|
1344 following C++ program, uses class Matrix from liboctave.a or |
|
1345 liboctave.so. |
|
1346 |
|
1347 @example |
|
1348 @group |
|
1349 #include <iostream> |
|
1350 #include <octave/oct.h> |
|
1351 int |
|
1352 main (void) |
|
1353 @{ |
|
1354 std::cout << "Hello Octave world!\n"; |
6572
|
1355 int n = 2; |
|
1356 Matrix a_matrix = Matrix (n, n); |
|
1357 for (octave_idx_type i = 0; i < n; i++) |
6569
|
1358 @{ |
6572
|
1359 for (octave_idx_type j = 0; j < n; j++) |
6569
|
1360 @{ |
6572
|
1361 a_matrix(row,column) = (i+1)*10 + (j+1); |
6569
|
1362 @} |
|
1363 @} |
|
1364 std::cout << a_matrix; |
|
1365 return 0; |
|
1366 @} |
|
1367 @end group |
|
1368 @end example |
|
1369 |
6580
|
1370 @noindent |
6569
|
1371 mkoctfile can then be used to build a standalone application with a |
|
1372 command like |
|
1373 |
|
1374 @example |
|
1375 @group |
|
1376 $ mkoctfile --link-stand-alone hello.cc -o hello |
|
1377 $ ./hello |
|
1378 Hello Octave world! |
|
1379 11 12 |
|
1380 21 22 |
|
1381 $ |
|
1382 @end group |
|
1383 @end example |
|
1384 |
|
1385 Note that the application @code{hello} will be dynamically linked |
|
1386 against the octave libraries and any octave support libraries. |