Mercurial > hg > octave-nkf
comparison doc/interpreter/dynamic.txi @ 10828:322f43e0e170
Grammarcheck .txi documentation files.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 28 Jul 2010 12:45:04 -0700 |
parents | 3140cb7a05a1 |
children | a4f482e66b65 |
comparison
equal
deleted
inserted
replaced
10827:228cd18455a6 | 10828:322f43e0e170 |
---|---|
37 address ask yourself | 37 address ask yourself |
38 | 38 |
39 @itemize @bullet | 39 @itemize @bullet |
40 @item | 40 @item |
41 Can I get the same functionality using the Octave scripting language only? | 41 Can I get the same functionality using the Octave scripting language only? |
42 | |
42 @item | 43 @item |
43 Is it thoroughly optimized Octave code? Vectorization of Octave code, | 44 Is it thoroughly optimized Octave code? Vectorization of Octave code, |
44 doesn't just make it concise, it generally significantly improves its | 45 doesn't just make it concise, it generally significantly improves its |
45 performance. Above all, if loops must be used, make sure that the | 46 performance. Above all, if loops must be used, make sure that the |
46 allocation of space for variables takes place outside the loops using an | 47 allocation of space for variables takes place outside the loops using an |
47 assignment to a matrix of the right size, or zeros. | 48 assignment to a matrix of the right size, or zeros. |
49 | |
48 @item | 50 @item |
49 Does it make as much use as possible of existing built-in library | 51 Does it make as much use as possible of existing built-in library |
50 routines? These are highly optimized and many do not carry the overhead | 52 routines? These are highly optimized and many do not carry the overhead |
51 of being interpreted. | 53 of being interpreted. |
54 | |
52 @item | 55 @item |
53 Does writing a dynamically linked function represent useful investment | 56 Does writing a dynamically linked function represent useful investment |
54 of your time, relative to staying in Octave? | 57 of your time, relative to staying in Octave? |
55 @end itemize | 58 @end itemize |
56 | 59 |
94 The basic command to build oct-files is @code{mkoctfile} and it can be | 97 The basic command to build oct-files is @code{mkoctfile} and it can be |
95 call from within octave or from the command line. | 98 call from within octave or from the command line. |
96 | 99 |
97 @DOCSTRING(mkoctfile) | 100 @DOCSTRING(mkoctfile) |
98 | 101 |
99 Consider the short example | 102 Consider the short example: |
100 | 103 |
101 @example | 104 @example |
102 @group | 105 @group |
103 @EXAMPLEFILE(helloworld.cc) | 106 @EXAMPLEFILE(helloworld.cc) |
104 @end group | 107 @end group |
113 The macro that defines the entry point into the dynamically loaded | 116 The macro that defines the entry point into the dynamically loaded |
114 function is @w{@code{DEFUN_DLD}}. This macro takes four arguments, these being | 117 function is @w{@code{DEFUN_DLD}}. This macro takes four arguments, these being |
115 | 118 |
116 @enumerate 1 | 119 @enumerate 1 |
117 @item The function name as it will be seen in Octave, | 120 @item The function name as it will be seen in Octave, |
121 | |
118 @item The list of arguments to the function of type @code{octave_value_list}, | 122 @item The list of arguments to the function of type @code{octave_value_list}, |
123 | |
119 @item The number of output arguments, which can and often is omitted if | 124 @item The number of output arguments, which can and often is omitted if |
120 not used, and | 125 not used, and |
126 | |
121 @item The string that will be seen as the help text of the function. | 127 @item The string that will be seen as the help text of the function. |
122 @end enumerate | 128 @end enumerate |
123 | 129 |
124 The return type of functions defined with @w{@code{DEFUN_DLD}} is always | 130 The return type of functions defined with @w{@code{DEFUN_DLD}} is always |
125 @code{octave_value_list}. | 131 @code{octave_value_list}. |
126 | 132 |
127 There are a couple of important considerations in the choice of function | 133 There are a couple of important considerations in the choice of function |
128 name. Firstly, it must be a valid Octave function name and so must be a | 134 name. Firstly, it must be a valid Octave function name and so must be a |
129 sequence of letters, digits and underscores, not starting with a | 135 sequence of letters, digits and underscores, not starting with a |
130 digit. Secondly, as Octave uses the function name to define the filename | 136 digit. Secondly, as Octave uses the function name to define the filename |
131 it attempts to find the function in, the function name in the @w{@code{DEFUN_DLD}} | 137 it attempts to find the function in, the function name in the |
132 macro must match the filename of the oct-file. Therefore, the above | 138 @w{@code{DEFUN_DLD}} macro must match the filename of the oct-file. Therefore, |
133 function should be in a file @file{helloworld.cc}, and it should be | 139 the above function should be in a file @file{helloworld.cc}, and it should be |
134 compiled to an oct-file using the command | 140 compiled to an oct-file using the command |
135 | 141 |
136 @example | 142 @example |
137 mkoctfile helloworld.cc | 143 mkoctfile helloworld.cc |
138 @end example | 144 @end example |
165 matrix types | 171 matrix types |
166 | 172 |
167 @table @code | 173 @table @code |
168 @item Matrix | 174 @item Matrix |
169 A double precision matrix class defined in dMatrix.h, | 175 A double precision matrix class defined in dMatrix.h, |
176 | |
170 @item ComplexMatrix | 177 @item ComplexMatrix |
171 A complex matrix class defined in CMatrix.h, and | 178 A complex matrix class defined in CMatrix.h, and |
179 | |
172 @item BoolMatrix | 180 @item BoolMatrix |
173 A boolean matrix class defined in boolMatrix.h. | 181 A boolean matrix class defined in boolMatrix.h. |
174 @end table | 182 @end table |
175 | 183 |
176 These are the basic two-dimensional matrix types of octave. In | 184 These are the basic two-dimensional matrix types of octave. In |
178 being | 186 being |
179 | 187 |
180 @table @code | 188 @table @code |
181 @item NDArray | 189 @item NDArray |
182 A double precision array class defined in @file{dNDArray.h} | 190 A double precision array class defined in @file{dNDArray.h} |
191 | |
183 @item ComplexNDarray | 192 @item ComplexNDarray |
184 A complex array class defined in @file{CNDArray.h} | 193 A complex array class defined in @file{CNDArray.h} |
194 | |
185 @item boolNDArray | 195 @item boolNDArray |
186 A boolean array class defined in @file{boolNDArray.h} | 196 A boolean array class defined in @file{boolNDArray.h} |
197 | |
187 @item int8NDArray | 198 @item int8NDArray |
188 @itemx int16NDArray | 199 @itemx int16NDArray |
189 @itemx int32NDArray | 200 @itemx int32NDArray |
190 @itemx int64NDArray | 201 @itemx int64NDArray |
191 8, 16, 32 and 64-bit signed array classes defined in | 202 8, 16, 32 and 64-bit signed array classes defined in |
192 @file{int8NDArray.h}, @file{int16NDArray.h}, etc. | 203 @file{int8NDArray.h}, @file{int16NDArray.h}, etc. |
204 | |
193 @item uint8NDArray | 205 @item uint8NDArray |
194 @itemx uint16NDArray | 206 @itemx uint16NDArray |
195 @itemx uint32NDArray | 207 @itemx uint32NDArray |
196 @itemx uint64NDArray | 208 @itemx uint64NDArray |
197 8, 16, 32 and 64-bit unsigned array classes defined in | 209 8, 16, 32 and 64-bit unsigned array classes defined in |
210 @example | 222 @example |
211 Matrix a; | 223 Matrix a; |
212 @end example | 224 @end example |
213 | 225 |
214 This can be used on all matrix and array types | 226 This can be used on all matrix and array types |
227 | |
215 @item | 228 @item |
216 Define the dimensions of the matrix or array with a dim_vector. For | 229 Define the dimensions of the matrix or array with a dim_vector. For |
217 example | 230 example |
218 | 231 |
219 @example | 232 @example |
223 Matrix a (dv); | 236 Matrix a (dv); |
224 @end group | 237 @end group |
225 @end example | 238 @end example |
226 | 239 |
227 This can be used on all matrix and array types | 240 This can be used on all matrix and array types |
241 | |
228 @item | 242 @item |
229 Define the number of rows and columns in the matrix. For example | 243 Define the number of rows and columns in the matrix. For example: |
230 | 244 |
231 @example | 245 @example |
232 Matrix a (2, 2) | 246 Matrix a (2, 2) |
233 @end example | 247 @end example |
234 | 248 |
236 @end itemize | 250 @end itemize |
237 | 251 |
238 These types all share a number of basic methods and operators, a | 252 These types all share a number of basic methods and operators, a |
239 selection of which include | 253 selection of which include |
240 | 254 |
241 @deftypefn Method T& {operator ()} (octave_idx_type) | 255 @deftypefn Method T& {operator ()} (octave_idx_type) |
242 @deftypefnx Method T& elem (octave_idx_type) | 256 @deftypefnx Method T& elem (octave_idx_type) |
243 The @code{()} operator or @code{elem} method allow the values of the | 257 The @code{()} operator or @code{elem} method allow the values of the |
244 matrix or array to be read or set. These can take a single argument, | 258 matrix or array to be read or set. These can take a single argument, |
245 which is of type @code{octave_idx_type}, that is the index into the matrix or | 259 which is of type @code{octave_idx_type}, that is the index into the matrix or |
246 array. Additionally, the matrix type allows two argument versions of the | 260 array. Additionally, the matrix type allows two argument versions of the |
343 | 357 |
344 @node Character Strings in Oct-Files | 358 @node Character Strings in Oct-Files |
345 @subsection Character Strings in Oct-Files | 359 @subsection Character Strings in Oct-Files |
346 | 360 |
347 In Octave a character string is just a special @code{Array} class. | 361 In Octave a character string is just a special @code{Array} class. |
348 Consider the example | 362 Consider the example: |
349 | 363 |
350 @example | 364 @example |
351 @EXAMPLEFILE(stringdemo.cc) | 365 @EXAMPLEFILE(stringdemo.cc) |
352 @end example | 366 @end example |
353 | 367 |
385 @end group | 399 @end group |
386 @end example | 400 @end example |
387 | 401 |
388 Note however, that both types of strings are represented by the | 402 Note however, that both types of strings are represented by the |
389 @code{charNDArray} type, and so when assigning to an | 403 @code{charNDArray} type, and so when assigning to an |
390 @code{octave_value}, the type of string should be specified. For example | 404 @code{octave_value}, the type of string should be specified. For example: |
391 | 405 |
392 @example | 406 @example |
393 @group | 407 @group |
394 octave_value_list retval; | 408 octave_value_list retval; |
395 charNDArray c; | 409 charNDArray c; |
481 user. | 495 user. |
482 | 496 |
483 @table @code | 497 @table @code |
484 @item SparseMatrix | 498 @item SparseMatrix |
485 A double precision sparse matrix class | 499 A double precision sparse matrix class |
500 | |
486 @item SparseComplexMatrix | 501 @item SparseComplexMatrix |
487 A complex sparse matrix class | 502 A complex sparse matrix class |
503 | |
488 @item SparseBoolMatrix | 504 @item SparseBoolMatrix |
489 A boolean sparse matrix class | 505 A boolean sparse matrix class |
490 @end table | 506 @end table |
491 | 507 |
492 All of these classes inherit from the @code{Sparse<T>} template class, | 508 All of these classes inherit from the @code{Sparse<T>} template class, |
790 | 806 |
791 Most of the same operators and functions on sparse matrices that are | 807 Most of the same operators and functions on sparse matrices that are |
792 available from the Octave are equally available with oct-files. | 808 available from the Octave are equally available with oct-files. |
793 The basic means of extracting a sparse matrix from an @code{octave_value} | 809 The basic means of extracting a sparse matrix from an @code{octave_value} |
794 and returning them as an @code{octave_value}, can be seen in the | 810 and returning them as an @code{octave_value}, can be seen in the |
795 following example | 811 following example. |
796 | 812 |
797 @example | 813 @example |
798 @group | 814 @group |
799 octave_value_list retval; | 815 octave_value_list retval; |
800 | 816 |
855 There are also many ways in which a function might be passed. It might | 871 There are also many ways in which a function might be passed. It might |
856 be passed as one of | 872 be passed as one of |
857 | 873 |
858 @enumerate 1 | 874 @enumerate 1 |
859 @item Function Handle | 875 @item Function Handle |
876 | |
860 @item Anonymous Function Handle | 877 @item Anonymous Function Handle |
878 | |
861 @item Inline Function | 879 @item Inline Function |
880 | |
862 @item String | 881 @item String |
863 @end enumerate | 882 @end enumerate |
864 | 883 |
865 The example below demonstrates an example that accepts all four means of | 884 The example below demonstrates an example that accepts all four means of |
866 passing a function to an oct-file. | 885 passing a function to an oct-file. |
1024 | 1043 |
1025 There are several functions within Octave that might be useful for the | 1044 There are several functions within Octave that might be useful for the |
1026 purposes of parameter checking. These include the methods of the | 1045 purposes of parameter checking. These include the methods of the |
1027 octave_value class like @code{is_real_matrix}, etc., but equally include | 1046 octave_value class like @code{is_real_matrix}, etc., but equally include |
1028 more specialized functions. Some of the more common ones are | 1047 more specialized functions. Some of the more common ones are |
1029 demonstrated in the following example | 1048 demonstrated in the following example. |
1030 | 1049 |
1031 @example | 1050 @example |
1032 @EXAMPLEFILE(paramdemo.cc) | 1051 @EXAMPLEFILE(paramdemo.cc) |
1033 @end example | 1052 @end example |
1034 | 1053 |
1035 @noindent | 1054 @noindent |
1036 and an example of its use is | 1055 An example of its use is: |
1037 | 1056 |
1038 @example | 1057 @example |
1039 @group | 1058 @group |
1040 paramdemo ([1, 2, NaN, Inf]) | 1059 paramdemo ([1, 2, NaN, Inf]) |
1041 @result{} Properties of input array: | 1060 @result{} Properties of input array: |
1051 Another important feature of Octave is its ability to react to the user | 1070 Another important feature of Octave is its ability to react to the user |
1052 typing @kbd{Control-C} even during calculations. This ability is based on the | 1071 typing @kbd{Control-C} even during calculations. This ability is based on the |
1053 C++ exception handler, where memory allocated by the C++ new/delete | 1072 C++ exception handler, where memory allocated by the C++ new/delete |
1054 methods are automatically released when the exception is treated. When | 1073 methods are automatically released when the exception is treated. When |
1055 writing an oct-file, to allow Octave to treat the user typing @kbd{Control-C}, | 1074 writing an oct-file, to allow Octave to treat the user typing @kbd{Control-C}, |
1056 the @w{@code{OCTAVE_QUIT}} macro is supplied. For example | 1075 the @w{@code{OCTAVE_QUIT}} macro is supplied. For example: |
1057 | 1076 |
1058 @example | 1077 @example |
1059 @group | 1078 @group |
1060 for (octave_idx_type i = 0; i < a.nelem (); i++) | 1079 for (octave_idx_type i = 0; i < a.nelem (); i++) |
1061 @{ | 1080 @{ |
1063 b.elem(i) = 2. * a.elem(i); | 1082 b.elem(i) = 2. * a.elem(i); |
1064 @} | 1083 @} |
1065 @end group | 1084 @end group |
1066 @end example | 1085 @end example |
1067 | 1086 |
1068 The presence of the @w{@code{OCTAVE_QUIT}} macro in the inner loop allows Octave to | 1087 The presence of the @w{@code{OCTAVE_QUIT}} macro in the inner loop allows |
1069 treat the user request with the @kbd{Control-C}. Without this macro, the user | 1088 Octave to treat the user request with the @kbd{Control-C}. Without this macro, |
1070 must either wait for the function to return before the interrupt is | 1089 the user must either wait for the function to return before the interrupt is |
1071 processed, or press @kbd{Control-C} three times to force Octave to exit. | 1090 processed, or press @kbd{Control-C} three times to force Octave to exit. |
1072 | 1091 |
1073 The @w{@code{OCTAVE_QUIT}} macro does impose a very small speed penalty, and so for | 1092 The @w{@code{OCTAVE_QUIT}} macro does impose a very small speed penalty, and so |
1074 loops that are known to be small it might not make sense to include | 1093 for loops that are known to be small it might not make sense to include |
1075 @w{@code{OCTAVE_QUIT}}. | 1094 @w{@code{OCTAVE_QUIT}}. |
1076 | 1095 |
1077 When creating an oct-file that uses an external libraries, the function | 1096 When creating an oct-file that uses an external libraries, the function |
1078 might spend a significant portion of its time in the external | 1097 might spend a significant portion of its time in the external |
1079 library. It is not generally possible to use the @w{@code{OCTAVE_QUIT}} macro in | 1098 library. It is not generally possible to use the @w{@code{OCTAVE_QUIT}} macro |
1080 this case. The alternative in this case is | 1099 in this case. The alternative in this case is |
1081 | 1100 |
1082 @example | 1101 @example |
1083 @group | 1102 @group |
1084 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | 1103 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
1085 @dots{} some code that calls a "foreign" function @dots{} | 1104 @dots{} some code that calls a "foreign" function @dots{} |
1101 | 1120 |
1102 @example | 1121 @example |
1103 @EXAMPLEFILE(unwinddemo.cc) | 1122 @EXAMPLEFILE(unwinddemo.cc) |
1104 @end example | 1123 @end example |
1105 | 1124 |
1106 As can be seen in the example | 1125 As can be seen in the example: |
1107 | 1126 |
1108 @example | 1127 @example |
1109 @group | 1128 @group |
1110 unwinddemo (1, 0) | 1129 unwinddemo (1, 0) |
1111 @result{} Inf | 1130 @result{} Inf |
1128 help strings within oct-files. | 1147 help strings within oct-files. |
1129 | 1148 |
1130 The major issue is that the help string will typically be longer than a | 1149 The major issue is that the help string will typically be longer than a |
1131 single line of text, and so the formatting of long help strings need to | 1150 single line of text, and so the formatting of long help strings need to |
1132 be taken into account. There are several manners in which to treat this | 1151 be taken into account. There are several manners in which to treat this |
1133 issue, but the most common is illustrated in the following example | 1152 issue, but the most common is illustrated in the following example, |
1134 | 1153 |
1135 @example | 1154 @example |
1136 @group | 1155 @group |
1137 DEFUN_DLD (do_what_i_want, args, nargout, | 1156 DEFUN_DLD (do_what_i_want, args, nargout, |
1138 "-*- texinfo -*-\n\ | 1157 "-*- texinfo -*-\n\ |
1226 with Octave is that the header file "matrix.h" is implicitly included | 1245 with Octave is that the header file "matrix.h" is implicitly included |
1227 through the inclusion of "mex.h". This is to avoid a conflict with the | 1246 through the inclusion of "mex.h". This is to avoid a conflict with the |
1228 Octave file "Matrix.h" with operating systems and compilers that don't | 1247 Octave file "Matrix.h" with operating systems and compilers that don't |
1229 distinguish between filenames in upper and lower case | 1248 distinguish between filenames in upper and lower case |
1230 | 1249 |
1231 Consider the short example | 1250 Consider the short example: |
1232 | 1251 |
1233 @example | 1252 @example |
1234 @group | 1253 @group |
1235 @EXAMPLEFILE(firstmexdemo.c) | 1254 @EXAMPLEFILE(firstmexdemo.c) |
1236 @end group | 1255 @end group |
1336 @code{mxGetPr}. As the mex interface assumes that the real and imaginary | 1355 @code{mxGetPr}. As the mex interface assumes that the real and imaginary |
1337 parts of a complex array are stored separately, there is an equivalent | 1356 parts of a complex array are stored separately, there is an equivalent |
1338 function @code{mxGetPi} that get the imaginary part. Both of these | 1357 function @code{mxGetPi} that get the imaginary part. Both of these |
1339 functions are for use only with double precision matrices. There also | 1358 functions are for use only with double precision matrices. There also |
1340 exists the generic function @code{mxGetData} and @code{mxGetImagData} | 1359 exists the generic function @code{mxGetData} and @code{mxGetImagData} |
1341 that perform the same operation on all matrix types. For example | 1360 that perform the same operation on all matrix types. For example: |
1342 | 1361 |
1343 @example | 1362 @example |
1344 @group | 1363 @group |
1345 mxArray *m; | 1364 mxArray *m; |
1346 mwSize *dims; | 1365 mwSize *dims; |