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