Mercurial > octave-nkf
comparison doc/interpreter/dynamic.txi @ 6593:3da1f4a41455
[project @ 2007-04-27 08:08:19 by dbateman]
author | dbateman |
---|---|
date | Fri, 27 Apr 2007 08:08:19 +0000 |
parents | e4ef75fe0bf2 |
children | 2c19eaa2c6f7 |
comparison
equal
deleted
inserted
replaced
6592:8899e24ae362 | 6593:3da1f4a41455 |
---|---|
6 @macro examplefile{file} | 6 @macro examplefile{file} |
7 @example | 7 @example |
8 @group | 8 @group |
9 @verbatiminclude @value{abs_top_srcdir}/examples/\file\ | 9 @verbatiminclude @value{abs_top_srcdir}/examples/\file\ |
10 @end group | 10 @end group |
11 @end example | |
12 @end macro | |
13 | |
14 @macro longexamplefile{file} | |
15 @example | |
16 @verbatiminclude @value{abs_top_srcdir}/examples/\file\ | |
11 @end example | 17 @end example |
12 @end macro | 18 @end macro |
13 | 19 |
14 @node Dynamically Linked Functions | 20 @node Dynamically Linked Functions |
15 @appendix Dynamically Linked Functions | 21 @appendix Dynamically Linked Functions |
77 * Calling External Code from Oct-Files:: | 83 * Calling External Code from Oct-Files:: |
78 * Allocating Local Memory in Oct-Files:: | 84 * Allocating Local Memory in Oct-Files:: |
79 * Input Parameter Checking in Oct-Files:: | 85 * Input Parameter Checking in Oct-Files:: |
80 * Exception and Error Handling in Oct-Files:: | 86 * Exception and Error Handling in Oct-Files:: |
81 * Documentation and Test of Oct-Files:: | 87 * Documentation and Test of Oct-Files:: |
82 * Application Programming Interface for Oct-Files:: | 88 @c * Application Programming Interface for Oct-Files:: |
83 @end menu | 89 @end menu |
84 | 90 |
85 @node Getting Started with Oct-Files | 91 @node Getting Started with Oct-Files |
86 @subsection Getting Started with Oct-Files | 92 @subsection Getting Started with Oct-Files |
87 | 93 |
444 | 450 |
445 @example | 451 @example |
446 octave_value tmp = arg0.contents (p1) (0); | 452 octave_value tmp = arg0.contents (p1) (0); |
447 @end example | 453 @end example |
448 | 454 |
449 where the trailing (0) is the () operator on the @code{Cell} object. | 455 where the trailing (0) is the () operator on the @code{Cell} object. We |
456 can equally iterate of the elements of the Cell array to address the | |
457 elements of the structure array. | |
450 | 458 |
451 @node Sparse Matrices in Oct-Files | 459 @node Sparse Matrices in Oct-Files |
452 @subsection Sparse Matrices in Oct-Files | 460 @subsection Sparse Matrices in Oct-Files |
453 | 461 |
454 There are three classes of sparse objects that are of interest to the | 462 There are three classes of sparse objects that are of interest to the |
981 checking needed to ensure that Octave behaves well. | 989 checking needed to ensure that Octave behaves well. |
982 | 990 |
983 The minimum requirement, as previously discussed, is to check the number | 991 The minimum requirement, as previously discussed, is to check the number |
984 of input arguments before using them to avoid referencing a non existent | 992 of input arguments before using them to avoid referencing a non existent |
985 argument. However, it some case this might not be sufficient as the | 993 argument. However, it some case this might not be sufficient as the |
986 underlying code imposes further constraints. For example an external | 994 underlying code imposes further constraints. For example an external |
987 function call might be undefined if the input arguments are not | 995 function call might be undefined if the input arguments are not |
988 integers, or if one of the arguments is zero. Therefore, oct-files often | 996 integers, or if one of the arguments is zero. Therefore, oct-files often |
989 need additional input parameter checking. | 997 need additional input parameter checking. |
990 | 998 |
991 There are several functions within Octave that might be useful for the | 999 There are several functions within Octave that might be useful for the |
992 purposes of parameter checking. These include the methods of the | 1000 purposes of parameter checking. These include the methods of the |
993 octave_value class like @code{is_real_matrix}, etc, but equally include | 1001 octave_value class like @code{is_real_matrix}, etc, but equally include |
994 more specialized functions. Some of the more common ones are | 1002 more specialized functions. Some of the more common ones are |
995 demonstrated in the following example | 1003 demonstrated in the following example |
996 | 1004 |
997 @examplefile{paramdemo.cc} | 1005 @examplefile{paramdemo.cc} |
998 | 1006 |
999 @noindent | 1007 @noindent |
1071 @group | 1079 @group |
1072 unwinddemo (1, 0) | 1080 unwinddemo (1, 0) |
1073 @result{} Inf | 1081 @result{} Inf |
1074 1 / 0 | 1082 1 / 0 |
1075 @result{} warning: division by zero | 1083 @result{} warning: division by zero |
1076 Inf | 1084 Inf |
1077 @end group | 1085 @end group |
1078 @end example | 1086 @end example |
1079 | 1087 |
1080 The division by zero (and in fact all warnings) is disabled in the | 1088 The division by zero (and in fact all warnings) is disabled in the |
1081 @code{unwinddemo} function. | 1089 @code{unwinddemo} function. |
1089 however there are some issue that are particular to the formatting of | 1097 however there are some issue that are particular to the formatting of |
1090 help strings within oct-files. | 1098 help strings within oct-files. |
1091 | 1099 |
1092 The major issue is that the help string will typically be longer than a | 1100 The major issue is that the help string will typically be longer than a |
1093 single line of text, and so the formatting of long help strings need to | 1101 single line of text, and so the formatting of long help strings need to |
1094 be taken into account. There are several manner in which to happen this | 1102 be taken into account. There are several manner in which to happen this |
1095 issue, but the most common is illustrated in the following example | 1103 issue, but the most common is illustrated in the following example |
1096 | 1104 |
1097 @example | 1105 @example |
1098 @group | 1106 @group |
1099 DEFUN_DLD (do_what_i_want, args, nargout, | 1107 DEFUN_DLD (do_what_i_want, args, nargout, |
1122 and demonstration code. This is equally a requirement for | 1130 and demonstration code. This is equally a requirement for |
1123 oct-files. Furthermore the test and demonstration code must be included | 1131 oct-files. Furthermore the test and demonstration code must be included |
1124 in a comment block of the compiled code to avoid it being interpreted by | 1132 in a comment block of the compiled code to avoid it being interpreted by |
1125 the compiler. Finally, he Octave test and demonstration code must have | 1133 the compiler. Finally, he Octave test and demonstration code must have |
1126 access to the source code of the oct-file and not just the compiled code | 1134 access to the source code of the oct-file and not just the compiled code |
1127 as th<e tests are stripped from the compiled code. An example in an | 1135 as th<e tests are stripped from the compiled code. An example in an |
1128 oct-file might be | 1136 oct-file might be |
1129 | 1137 |
1130 @example | 1138 @example |
1131 @group | 1139 @group |
1132 /* | 1140 /* |
1137 | 1145 |
1138 */ | 1146 */ |
1139 @end group | 1147 @end group |
1140 @end example | 1148 @end example |
1141 | 1149 |
1142 @node Application Programming Interface for Oct-Files | 1150 @c @node Application Programming Interface for Oct-Files |
1143 @subsection Application Programming Interface for Oct-Files | 1151 @c @subsection Application Programming Interface for Oct-Files |
1144 | 1152 @c |
1145 WRITE ME, using Coda section 1.3 as a starting point. | 1153 @c WRITE ME, using Coda section 1.3 as a starting point. |
1146 | 1154 |
1147 @node Mex-Files | 1155 @node Mex-Files |
1148 @section Mex-Files | 1156 @section Mex-Files |
1149 @cindex mex-files | 1157 @cindex mex-files |
1150 @cindex mex | 1158 @cindex mex |
1151 | 1159 |
1152 Octave includes an interface to allow legacy mex-files to be compiled | 1160 Octave includes an interface to allow legacy mex-files to be compiled |
1153 and used with Octave. This interface can also be used to share code | 1161 and used with Octave. This interface can also be used to share code |
1154 between Octave and non Octave users. However, as mex-files expose the | 1162 between Octave and non Octave users. However, as mex-files expose the |
1155 intern API of a product alternative to Octave, and the internal | 1163 internal API of an alternative product to Octave, and the internal |
1156 structure of Octave is different to this product, a mex-file can never | 1164 structure of Octave is different to this product, a mex-file can never |
1157 have the same performance in Octave as the equivalent oct-file. In | 1165 have the same performance in Octave as the equivalent oct-file. In |
1158 particular to support the manner in which mex-files access the variables | 1166 particular to support the manner in which mex-files access the variables |
1159 passed to mex functions, there are a significant number of additional | 1167 passed to mex functions, there are a significant number of additional |
1160 copies of memory when calling or returning from a mex function. For this | 1168 copies of memory when calling or returning from a mex function. For |
1161 reason, new code should be written using the oct-file interface | 1169 this reason, new code should be written using the oct-file interface |
1162 discussed above if possible. | 1170 discussed above if possible. |
1163 | 1171 |
1164 @menu | 1172 @menu |
1165 * Getting Started with Mex-Files:: | 1173 * Getting Started with Mex-Files:: |
1166 * Working with Matrices and Arrays in Mex-Files:: | 1174 * Working with Matrices and Arrays in Mex-Files:: |
1167 * Character Strings in Mex-Files:: | 1175 * Character Strings in Mex-Files:: |
1168 * Cell Arrays with Mex-Files:: | 1176 * Cell Arrays with Mex-Files:: |
1169 * Structures with Mex-Files:: | 1177 * Structures with Mex-Files:: |
1170 * Sparse Matrices with Mex-Files:: | 1178 * Sparse Matrices with Mex-Files:: |
1171 * Calling Other Functions in Mex-Files:: | 1179 * Calling Other Functions in Mex-Files:: |
1172 * Application Programming Interface for Mex-Files:: | 1180 @c * Application Programming Interface for Mex-Files:: |
1173 @end menu | 1181 @end menu |
1174 | 1182 |
1175 @node Getting Started with Mex-Files | 1183 @node Getting Started with Mex-Files |
1176 @subsection Getting Started with Mex-Files | 1184 @subsection Getting Started with Mex-Files |
1177 | 1185 |
1192 | 1200 |
1193 Consider the short example | 1201 Consider the short example |
1194 | 1202 |
1195 @examplefile{firstmexdemo.c} | 1203 @examplefile{firstmexdemo.c} |
1196 | 1204 |
1197 This simple example demonstrates the basics of writing a mex-file. The | 1205 This simple example demonstrates the basics of writing a mex-file. The |
1198 entry point into the mex-file is defined by @code{mexFunction}. Note | 1206 entry point into the mex-file is defined by @code{mexFunction}. Note |
1199 that the function name is not explicitly included in the | 1207 that the function name is not explicitly included in the |
1200 @code{mexFunction} and so there can only be a single @code{mexFunction} | 1208 @code{mexFunction} and so there can only be a single @code{mexFunction} |
1201 entry point per-file. Also the name of the function is determined by the | 1209 entry point per-file. Also the name of the function is determined by the |
1202 name of the mex-file itself. Therefore if the above function is in the | 1210 name of the mex-file itself. Therefore if the above function is in the |
1203 file @file{firstmexdemo.c}, it can be compiled with | 1211 file @file{firstmexdemo.c}, it can be compiled with |
1216 @result{} 1.2346 | 1224 @result{} 1.2346 |
1217 @end group | 1225 @end group |
1218 @end example | 1226 @end example |
1219 | 1227 |
1220 It should be noted that the mex-file contains no help string for the | 1228 It should be noted that the mex-file contains no help string for the |
1221 functions it contains. To document mex-files, there should exist an | 1229 functions it contains. To document mex-files, there should exist an |
1222 m-file in the same directory as the mex-file itself. Taking the above as | 1230 m-file in the same directory as the mex-file itself. Taking the above as |
1223 an example, we would therefore have a file @file{firstmexdemo.m} that might | 1231 an example, we would therefore have a file @file{firstmexdemo.m} that might |
1224 contain the text | 1232 contain the text |
1225 | 1233 |
1226 @example | 1234 @example |
1227 %FIRSTMEXDEMO Simple test of the functionality of a mex-file. | 1235 %FIRSTMEXDEMO Simple test of the functionality of a mex-file. |
1228 @end example | 1236 @end example |
1229 | 1237 |
1230 In this case, the function that will be executed within Octave will be | 1238 In this case, the function that will be executed within Octave will be |
1231 given by the mex-file, while the help string will come from the | 1239 given by the mex-file, while the help string will come from the |
1232 m-file. This can also be useful to allow a sample implementation of the | 1240 m-file. This can also be useful to allow a sample implementation of the |
1233 mex-file within the Octave language itself for testing purposes. | 1241 mex-file within the Octave language itself for testing purposes. |
1234 | 1242 |
1235 Although we can not have multiple entry points into a single mex-file, | 1243 Although we can not have multiple entry points into a single mex-file, |
1236 we can use the @code{mexFunctionName} function to determine what name | 1244 we can use the @code{mexFunctionName} function to determine what name |
1237 the mex-file was called with. This can be used to alter the behavior of | 1245 the mex-file was called with. This can be used to alter the behavior of |
1238 the mex-file based on the function name. For example if | 1246 the mex-file based on the function name. For example if |
1239 | 1247 |
1240 @examplefile{myfunc.c} | 1248 @examplefile{myfunc.c} |
1241 | 1249 |
1242 @noindent | 1250 @noindent |
1243 is in file @file{myfunc.c}, and it is compiled with | 1251 is in file @file{myfunc.c}, and it is compiled with |
1263 | 1271 |
1264 @noindent | 1272 @noindent |
1265 the behavior of the mex-file can be altered depending on the functions | 1273 the behavior of the mex-file can be altered depending on the functions |
1266 name. | 1274 name. |
1267 | 1275 |
1276 Allow the user should only include @code{mex.h} in their code, Octave | |
1277 declares additional functions, typedefs, etc, available to the user to | |
1278 write mex-files in the headers @code{mexproto.h} and @code{mxarray.h}. | |
1279 | |
1268 @node Working with Matrices and Arrays in Mex-Files | 1280 @node Working with Matrices and Arrays in Mex-Files |
1269 @subsection Working with Matrices and Arrays in Mex-Files | 1281 @subsection Working with Matrices and Arrays in Mex-Files |
1270 | 1282 |
1271 The basic mex type of all variables is @code{mxArray}. All variables, | 1283 The basic mex type of all variables is @code{mxArray}. All variables, |
1272 such as Matrices, cell arrays or structures are all stored in this basic | 1284 such as matrices, cell arrays or structures are all stored in this basic |
1273 type, and this type serves basically the same purpose as the | 1285 type, and this type serves basically the same purpose as the |
1274 octave_value class in oct-files. That is it acts as a container for the | 1286 octave_value class in oct-files. That is it acts as a container for the |
1275 more specialized types. | 1287 more specialized types. |
1276 | 1288 |
1277 WRITE ME | 1289 The @code{mxArray} structure contains at a minimum, the variable it |
1290 represents name, its dimensions, its type and whether the variable is | |
1291 real or complex. It can however contain a number of additional fields | |
1292 depending on the type of the @code{mxArray}. There are a number of | |
1293 functions to create @code{mxArray} structures, including | |
1294 @code{mxCreateCellArray}, @code{mxCreateSparse} and the generic | |
1295 @code{mxCreateNumericArray}. | |
1296 | |
1297 The basic functions to access the data contained in an array is | |
1298 @code{mxGetPr}. As the mex interface assumes that the real and imaginary | |
1299 parts of a complex array are stored seperately, there is an equivalent | |
1300 function @code{mxGetPi} that get the imaginary part. Both of these | |
1301 functions are for use only with double precision matrices. There also | |
1302 exists the generic function @code{mxGetData} and @code{mxGetImagData} | |
1303 that perform the same operation on all matrix types. For example | |
1304 | |
1305 @example | |
1306 @group | |
1307 mxArray *m; | |
1308 int *dims; | |
1309 UINT32_T *pr; | |
1310 | |
1311 dims = (int *) mxMalloc (2 * sizeof(int)); | |
1312 dims[0] = 2; | |
1313 dims[1] = 2; | |
1314 m = mxCreateNumericArray (2, dims, mxUINT32_CLASS, mxREAL); | |
1315 pr = = (UINT32_T *) mxGetData (m); | |
1316 @end group | |
1317 @end example | |
1318 | |
1319 There are also the functions @code{mxSetPr}, etc, that perform the | |
1320 inverse, and set the data of an Array to use the block of memory pointed | |
1321 to by the argument of @code{mxSetPr}. | |
1322 | |
1323 An example that demonstration how to work with arbitrary real or complex | |
1324 double precision arrays is given by the file @file{mypow2.c} as given | |
1325 below. | |
1326 | |
1327 @examplefile{mypow2.c} | |
1328 | |
1329 @noindent | |
1330 with an example of its use | |
1331 | |
1332 @example | |
1333 @group | |
1334 b = randn(4,1) + 1i * randn(4,1); | |
1335 all(b.^2 == mypow2(b)) | |
1336 @result{} 1 | |
1337 @end group | |
1338 @end example | |
1339 | |
1340 | |
1341 The example above uses the @code{mxGetNumberOfElements}, | |
1342 @code{mxGetNumberOfDimensions} and @code{mxGetDimensions}, to work with | |
1343 the dimensional parameters of multi-dimensional arrays. The also exists | |
1344 the functions @code{mxGetM}, and @code{mxGetN} that probe the number of | |
1345 rows and columns in a matrix. | |
1278 | 1346 |
1279 @node Character Strings in Mex-Files | 1347 @node Character Strings in Mex-Files |
1280 @subsection Character Strings in Mex-Files | 1348 @subsection Character Strings in Mex-Files |
1281 | 1349 |
1282 WRITE ME | 1350 As mex-files do not make the distinction between single and double |
1351 quoted strings within Octave, there is perhaps less complexity in the | |
1352 use of strings and character matrices in mex-files. An example of their | |
1353 use, that parallels the demo in @file{stringdemo.cc}, is given in the | |
1354 file @file{mystring.c}, as seen below. | |
1355 | |
1356 @examplefile{mystring.c} | |
1357 | |
1358 @noindent | |
1359 An example of its expected output is | |
1360 | |
1361 @example | |
1362 @group | |
1363 mystring(["First String"; "Second String"]) | |
1364 @result{} s1 = Second String | |
1365 First String | |
1366 @end group | |
1367 @end example | |
1368 | |
1369 There are a couple of additional functions available in mex-files of | |
1370 interest in the treatment of strings. These are @code{mxCreateString}, | |
1371 @code{mxArrayToString} and @code{mxCreateCharMatrixFromStrings}. A | |
1372 string in a mex-file is considered to be a vector rather than a | |
1373 matrix. This is perhaps an arbitrary distinction as the data in the | |
1374 mxArray for the matrix is consequetive in any case. | |
1283 | 1375 |
1284 @node Cell Arrays with Mex-Files | 1376 @node Cell Arrays with Mex-Files |
1285 @subsection Cell Arrays with Mex-Files | 1377 @subsection Cell Arrays with Mex-Files |
1286 | 1378 |
1287 WRITE ME | 1379 We can perform exactly the same operations in Cell arrays in mex-files |
1380 as we can in oct-files. An example that reduplicates the functional of | |
1381 the @file{celldemo.cc} oct-file in a mex-file is given by | |
1382 @file{mycell.c} as below | |
1383 | |
1384 @examplefile{mycell.c} | |
1385 | |
1386 @noindent | |
1387 which as can be seen below has exactly the same behavior as the oct-file | |
1388 version. | |
1389 | |
1390 @example | |
1391 @group | |
1392 [b1, b2, b3] = mycell (@{1, [1, 2], "test"@}) | |
1393 @result{} | |
1394 b1 = 1 | |
1395 b2 = | |
1396 | |
1397 1 2 | |
1398 | |
1399 b3 = test | |
1400 @end group | |
1401 @end example | |
1402 | |
1403 Note in the example the use of the @code{mxDuplicateArry} function. This | |
1404 is needed as the @code{mxArray} pointer returned by @code{mxGetCell} | |
1405 might be deallocated. The inverse function to @code{mxGetCell} is | |
1406 @code{mcSetCell} and is defined as | |
1407 | |
1408 @example | |
1409 void mxSetCell (mxArray *ptr, int idx, mxArray *val); | |
1410 @end example | |
1411 | |
1412 Finally, to create a cell array or matrix, the appropraiate functions are | |
1413 | |
1414 @example | |
1415 @group | |
1416 mxArray *mxCreateCellArray (int ndims, const int *dims); | |
1417 mxArray *mxCreateCellMatrix (int m, int n); | |
1418 @end group | |
1419 @end example | |
1288 | 1420 |
1289 @node Structures with Mex-Files | 1421 @node Structures with Mex-Files |
1290 @subsection Structures with Mex-Files | 1422 @subsection Structures with Mex-Files |
1291 | 1423 |
1292 See the file @file{mystruct.c} | 1424 The basic function to create a structure in a mex-file is |
1425 @code{mxCreateStructMatrix}, which creates a structure array with a two | |
1426 dimensional matrix, or @code{mxCreateStructArray}. | |
1427 | |
1428 @example | |
1429 @group | |
1430 mxArray *mxCreateStructArray (int ndims, int *dims, int num_keys, | |
1431 const char **keys); | |
1432 mxArray *mxCreateStructMatrix (int rows, int cols, int num_keys, | |
1433 const char **keys); | |
1434 @end group | |
1435 @end example | |
1436 | |
1437 Accessing the fields of the structure can then be performed with the | |
1438 @code{mxGetField} and @code{mxSetField} or alternatively with the | |
1439 @code{mxGetFieldByNumber} and @code{mxSetFieldByNumber} functions. | |
1440 | |
1441 @example | |
1442 @group | |
1443 mxArray *mxGetField (const mxArray *ptr, int index, const char *key); | |
1444 mxArray *mxGetFieldByNumber (const mxArray *ptr, | |
1445 int index, int key_num); | |
1446 void mxSetField (mxArray *ptr, int index, | |
1447 const char *key, mxArray *val); | |
1448 void mxSetFieldByNumber (mxArray *ptr, int index, | |
1449 int key_num, mxArray *val); | |
1450 @end group | |
1451 @end example | |
1452 | |
1453 A difference between the oct-file interface to structures and the | |
1454 mex-file version is that the functions to operate on structures in | |
1455 mex-files directly include an @code{index} over the elements of the | |
1456 arrays of elements per @code{field}. Whereas the oct-file structure | |
1457 includes a Cell Array per field of the structure. | |
1458 | |
1459 An example that demonstrates the use of structures in mex-file can be | |
1460 found in the file @file{mystruct.c}, as seen below | |
1293 | 1461 |
1294 @examplefile{mystruct.c} | 1462 @examplefile{mystruct.c} |
1295 | 1463 |
1296 WRITE ME | 1464 An example of the behavior of this function within Octave is then |
1465 | |
1466 @example | |
1467 @group | |
1468 a(1).f1 = "f11"; a(1).f2 = "f12"; a(2).f1 = "f21"; a(2).f2 = "f22"; | |
1469 b = mystruct(a) | |
1470 @result{} field f1(0) = f11 | |
1471 field f1(1) = f21 | |
1472 field f2(0) = f12 | |
1473 field f2(1) = f22 | |
1474 b = | |
1475 @{ | |
1476 this = | |
1477 | |
1478 (, | |
1479 [1] = this1 | |
1480 [2] = this2 | |
1481 [3] = this3 | |
1482 [4] = this4 | |
1483 ,) | |
1484 | |
1485 that = | |
1486 | |
1487 (, | |
1488 [1] = that1 | |
1489 [2] = that2 | |
1490 [3] = that3 | |
1491 [4] = that4 | |
1492 ,) | |
1493 | |
1494 @} | |
1495 @end group | |
1496 @end example | |
1297 | 1497 |
1298 @node Sparse Matrices with Mex-Files | 1498 @node Sparse Matrices with Mex-Files |
1299 @subsection Sparse Matrices with Mex-Files | 1499 @subsection Sparse Matrices with Mex-Files |
1300 | 1500 |
1301 See the file @file{mysparse.c} | 1501 The Octave format for sparse matrices is identical to the mex format in |
1302 | 1502 that it is a compressed colument sparse format. Also in both, sparse |
1303 @examplefile{mysparse.c} | 1503 matrices are required to be two dimensional. The only difference is that |
1304 | 1504 the real and imaginary parts of the matrix are stored seperately. |
1305 WRITE ME | 1505 |
1506 The mex-file interface, as well as using @code{mxGetM}, @code{mxGetN}, | |
1507 @code{mxSetM}, @code{mxSetN}, @code{mxGetPr}, @code{mxGetPi}, | |
1508 @code{mxSetPr} and @code{mxSetPi}, the mex-file interface supplies the | |
1509 functions | |
1510 | |
1511 @example | |
1512 @group | |
1513 int *mxGetIr (const mxArray *ptr); | |
1514 int *mxGetJc (const mxArray *ptr); | |
1515 int mxGetNzmax (const mxArray *ptr); | |
1516 | |
1517 void mxSetIr (mxArray *ptr, int *ir); | |
1518 void mxSetJc (mxArray *ptr, int *jc); | |
1519 void mxSetNzmax (mxArray *ptr, int nzmax); | |
1520 @end group | |
1521 @end example | |
1522 | |
1523 @noindent | |
1524 @code{mxGetNzmax} gets the maximum number of elements that can be stored | |
1525 in the sparse matrix. This is not necessarily the number of non-zero | |
1526 elements in the sparse matrix. @code{mxGetJc} returns an array with one | |
1527 additional value than the number of columns in the sparse matrix. The | |
1528 difference between consecutive values of the array returned by | |
1529 @code{mxGetJc} define the number of non-zero elements in each column of | |
1530 the sparse matrix. Therefore | |
1531 | |
1532 @example | |
1533 @group | |
1534 int nz, n; | |
1535 int *Jc; | |
1536 mxArray *m; | |
1537 @dots{} | |
1538 n = mxGetN (m); | |
1539 Jc = mxGetJc (m); | |
1540 nz = Jc[n]; | |
1541 @end group | |
1542 @end example | |
1543 | |
1544 @noindent | |
1545 returns the actual number of non-zero elements stored in the matrix in | |
1546 @code{nz}. As the arrays returned by @code{mxGetPr} and @code{mxGetPi} | |
1547 only contain the non-zero values of the matrix, we also need a pointer | |
1548 to the rows of the non-zero elements, and this is given by | |
1549 @code{mxGetIr}. A complete example of the use of sparse matrices in | |
1550 mex-files is given by the file @file{mysparse.c} as seen below | |
1551 | |
1552 @longexamplefile{mysparse.c} | |
1306 | 1553 |
1307 @node Calling Other Functions in Mex-Files | 1554 @node Calling Other Functions in Mex-Files |
1308 @subsection Calling Other Functions in Mex-Files | 1555 @subsection Calling Other Functions in Mex-Files |
1309 | 1556 |
1310 It is also possible call other Octave functions from within a mex-file | 1557 It is also possible call other Octave functions from within a mex-file |
1311 using @code{mexCallMATLAB}. An example of the use of | 1558 using @code{mexCallMATLAB}. An example of the use of |
1312 @code{mexCallMATLAB} can be see in the example below | 1559 @code{mexCallMATLAB} can be see in the example below |
1313 | 1560 |
1314 @examplefile{myfeval.c} | 1561 @examplefile{myfeval.c} |
1315 | 1562 |
1316 If this code is in the file @file{myfeval.c}, and is compiled to | 1563 If this code is in the file @file{myfeval.c}, and is compiled to |
1328 @end example | 1575 @end example |
1329 | 1576 |
1330 Note that it is not possible to use function handles or inline functions | 1577 Note that it is not possible to use function handles or inline functions |
1331 within a mex-file. | 1578 within a mex-file. |
1332 | 1579 |
1333 @node Application Programming Interface for Mex-Files | 1580 @c @node Application Programming Interface for Mex-Files |
1334 @subsection Application Programming Interface for Mex-Files | 1581 @c @subsection Application Programming Interface for Mex-Files |
1335 | 1582 @c |
1336 WRITE ME, refer to mex.h and mexproto.h | 1583 @c WRITE ME, refer to mex.h and mexproto.h |
1337 | 1584 |
1338 @node Standalone Programs | 1585 @node Standalone Programs |
1339 @section Standalone Programs | 1586 @section Standalone Programs |
1340 | 1587 |
1341 The libraries Octave itself uses, can be utilized in standalone | 1588 The libraries Octave itself uses, can be utilized in standalone |