# HG changeset patch # User dbateman # Date 1177661299 0 # Node ID 3da1f4a4145520b533ae4d156dfb3a1086786179 # Parent 8899e24ae362bedf70ca957be6e1cd43aba86119 [project @ 2007-04-27 08:08:19 by dbateman] diff -r 8899e24ae362 -r 3da1f4a41455 ChangeLog --- a/ChangeLog Fri Apr 27 04:11:48 2007 +0000 +++ b/ChangeLog Fri Apr 27 08:08:19 2007 +0000 @@ -1,3 +1,8 @@ +2007-04-27 David Bateman + + * examples/mycell.c, examples/mypow2.c, examples/mystring.c: New + example mex files. + 2007-04-26 Alex Zvoleff * configure.in: Don't report ARPACK libraries in summary. diff -r 8899e24ae362 -r 3da1f4a41455 doc/ChangeLog --- a/doc/ChangeLog Fri Apr 27 04:11:48 2007 +0000 +++ b/doc/ChangeLog Fri Apr 27 08:08:19 2007 +0000 @@ -1,3 +1,8 @@ +2007-04-27 David Bateman + + * interpreter.txi/dynamic.txi: Complete all but the section on + the mex- and oct-file APIs. + 2007-04-26 David Bateman * interpreter/stmt.txi: Document for loops over matrices, arrays diff -r 8899e24ae362 -r 3da1f4a41455 doc/interpreter/dynamic.txi --- a/doc/interpreter/dynamic.txi Fri Apr 27 04:11:48 2007 +0000 +++ b/doc/interpreter/dynamic.txi Fri Apr 27 08:08:19 2007 +0000 @@ -11,6 +11,12 @@ @end example @end macro +@macro longexamplefile{file} +@example +@verbatiminclude @value{abs_top_srcdir}/examples/\file\ +@end example +@end macro + @node Dynamically Linked Functions @appendix Dynamically Linked Functions @cindex dynamic-linking @@ -79,7 +85,7 @@ * Input Parameter Checking in Oct-Files:: * Exception and Error Handling in Oct-Files:: * Documentation and Test of Oct-Files:: -* Application Programming Interface for Oct-Files:: +@c * Application Programming Interface for Oct-Files:: @end menu @node Getting Started with Oct-Files @@ -446,7 +452,9 @@ octave_value tmp = arg0.contents (p1) (0); @end example -where the trailing (0) is the () operator on the @code{Cell} object. +where the trailing (0) is the () operator on the @code{Cell} object. We +can equally iterate of the elements of the Cell array to address the +elements of the structure array. @node Sparse Matrices in Oct-Files @subsection Sparse Matrices in Oct-Files @@ -983,15 +991,15 @@ The minimum requirement, as previously discussed, is to check the number of input arguments before using them to avoid referencing a non existent argument. However, it some case this might not be sufficient as the -underlying code imposes further constraints. For example an external +underlying code imposes further constraints. For example an external function call might be undefined if the input arguments are not -integers, or if one of the arguments is zero. Therefore, oct-files often +integers, or if one of the arguments is zero. Therefore, oct-files often need additional input parameter checking. There are several functions within Octave that might be useful for the -purposes of parameter checking. These include the methods of the +purposes of parameter checking. These include the methods of the octave_value class like @code{is_real_matrix}, etc, but equally include -more specialized functions. Some of the more common ones are +more specialized functions. Some of the more common ones are demonstrated in the following example @examplefile{paramdemo.cc} @@ -1073,7 +1081,7 @@ @result{} Inf 1 / 0 @result{} warning: division by zero - Inf + Inf @end group @end example @@ -1091,7 +1099,7 @@ The major issue is that the help string will typically be longer than a single line of text, and so the formatting of long help strings need to -be taken into account. There are several manner in which to happen this +be taken into account. There are several manner in which to happen this issue, but the most common is illustrated in the following example @example @@ -1124,7 +1132,7 @@ in a comment block of the compiled code to avoid it being interpreted by the compiler. Finally, he Octave test and demonstration code must have access to the source code of the oct-file and not just the compiled code -as th nlhs ? nlhs : n); + + for (i = 0; i < n; i++) + plhs[i] = mxDuplicateArray (mxGetCell (prhs[0], i)); +} diff -r 8899e24ae362 -r 3da1f4a41455 examples/mypow2.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/mypow2.c Fri Apr 27 08:08:19 2007 +0000 @@ -0,0 +1,37 @@ +#include "mex.h" + +void +mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) +{ + int i, n; + double *vri, *vro; + + if (nrhs != 1 || ! mxIsNumeric (prhs[0])) + mexErrMsgTxt ("expects matrix"); + + n = mxGetNumberOfElements (prhs[0]); + plhs[0] = (mxArray *) mxCreateNumericArray + (mxGetNumberOfDimensions (prhs[0]), + mxGetDimensions (prhs[0]), mxGetClassID (prhs[0]), + mxIsComplex (prhs[0])); + vri = mxGetPr (prhs[0]); + vro = mxGetPr (plhs[0]); + + if (mxIsComplex (prhs[0])) + { + double *vii, *vio; + vii = mxGetPi (prhs[0]); + vio = mxGetPi (plhs[0]); + + for (i = 0; i < n; i++) + { + vro [i] = vri [i] * vri [i] - vii [i] * vii [i]; + vio [i] = 2 * vri [i] * vii [i]; + } + } + else + { + for (i = 0; i < n; i++) + vro [i] = vri [i] * vri [i]; + } +} diff -r 8899e24ae362 -r 3da1f4a41455 examples/mystring.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/mystring.c Fri Apr 27 08:08:19 2007 +0000 @@ -0,0 +1,23 @@ +#include +#include "mex.h" + +void +mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) +{ + int i, j, m, n; + mxChar *pi, *po; + + if (nrhs != 1 || ! mxIsChar (prhs[0]) || + mxGetNumberOfDimensions (prhs[0]) > 2) + mexErrMsgTxt ("expecting char matrix"); + + m = mxGetM (prhs[0]); + n = mxGetN (prhs[0]); + pi = mxGetChars (prhs[0]); + plhs[0] = mxCreateNumericMatrix (m, n, mxCHAR_CLASS, mxREAL); + po = mxGetChars (plhs[0]); + + for (j = 0; j < n; j++) + for (i = 0; i < m; i++) + po [j*m + m - 1 - i] = pi [j*m + i]; +}