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