Mercurial > octave
annotate doc/interpreter/external.txi @ 31548:c8ad083a5802 stable
maint: Clean up m-files before Octave 8.1 release.
* external.txi, oop.txi, Table.h, documentation.cc, gui-preferences-ed.h,
lo-specfun.cc, range.tst : Eliminate triple newlines.
* Map.m, MemoizedFunction.m, delaunayn.m, inputParser.m,
__publish_latex_output__.m, publish.m, unpack.m, fminbnd.m,
__add_default_menu__.m, gammainc.m, gallery.m, hadamard.m, weboptions.m:
Add newline after keyword "function" or before keyword "endfunction" for
readability.
* getaudiodata.m, pkg.m : Add semicolon to end of line for error() statement.
* movegui.m: Combine mutliple calls to set() into one for performance.
* __unimplemented__.m (missing_functions): Remove missing functions that have
been implemented.
* __vectorize__.m, check_default_input.m, betaincinv.m, gammaincinv.m:
Remove semicolon at end of line with "function" declaration.
* weboptions.m: Remove semicolon at end of line with "if" keyword.
* integrate_adaptive.m, factor.m: Use keyword "endif" rather than bare "end".
author | Rik <rik@octave.org> |
---|---|
date | Fri, 25 Nov 2022 21:23:54 -0800 |
parents | 796f54d4ddbf |
children | 39b06d766f2a |
rev | line source |
---|---|
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
28713
diff
changeset
|
1 @c Copyright (C) 2007-2022 The Octave Project Developers |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23567
diff
changeset
|
5 @c Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22482
diff
changeset
|
6 @c under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23567
diff
changeset
|
7 @c the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22482
diff
changeset
|
8 @c (at your option) any later version. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
9 @c |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22482
diff
changeset
|
10 @c Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22482
diff
changeset
|
11 @c WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22482
diff
changeset
|
12 @c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22482
diff
changeset
|
13 @c GNU General Public License for more details. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
14 @c |
7018 | 15 @c You should have received a copy of the GNU General Public License |
16 @c along with Octave; see the file COPYING. If not, see | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23567
diff
changeset
|
17 @c <https://www.gnu.org/licenses/>. |
6578 | 18 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
19 @node External Code Interface |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
20 @appendix External Code Interface |
6569 | 21 @cindex dynamic-linking |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
22 @cindex Dynamically Linked Functions |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
23 @cindex Octave API |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
24 |
18953
bcbd309bf272
doc: Fix quote character at beginning of External Code Interfaces chapter.
Rik <rik@octave.org>
parents:
18797
diff
changeset
|
25 "The sum of human wisdom is not contained in any one language" |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
26 |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
27 --- Ezra Pound |
6569 | 28 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
29 Octave is a fantastic language for solving many problems in science and |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
30 engineering. However, it is not the only computer language and there are times |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
31 when you may want to use code written in other languages. Good reasons for |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
32 doing so include: 1) not re-inventing the wheel; existing function libraries |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
33 which have been thoroughly tested and debugged or large scale simulation |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
34 codebases are a good example, 2) accessing unique capabilities of a different |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
35 language; for example the well-known regular expression functions of Perl (but |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
36 don't do that because @code{regexp} already exists in Octave). |
6569 | 37 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
38 Performance should generally @strong{not} be a reason for using compiled |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
39 extensions. Although compiled extensions can run faster, particularly if they |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
40 replace a loop in Octave code, this is almost never the best path to take. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
41 First, there are many techniques to speed up Octave performance while remaining |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
42 within the language. Second, Octave is a high-level language that makes it |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
43 easy to perform common mathematical tasks. Giving that up means shifting the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
44 focus from solving the real problem to solving a computer programming problem. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
45 It means returning to low-level constructs such as pointers, memory management, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
46 mathematical overflow/underflow, etc. Because of the low level nature, and the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
47 fact that the compiled code is executed outside of Octave, there is the very |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
48 real possibility of crashing the interpreter and losing work. |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
49 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
50 Before going further, you should first determine if you really need to bother |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
51 writing code outside of Octave. |
6569 | 52 |
53 @itemize @bullet | |
54 @item | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
55 Can I get the same functionality using the Octave scripting language alone? |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
56 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
57 Even when a function already exists outside the language, it may be better to |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
58 simply reproduce the behavior in an m-file rather than attempt to interface to |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
59 the outside code. |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
60 |
6569 | 61 @item |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
62 Is the code thoroughly optimized for Octave? |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
63 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
64 If performance is an issue you should always start with the in-language |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
65 techniques for getting better performance. Chief among these is vectorization |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
66 (@pxref{Vectorization and Faster Code Execution}) which not only makes the code |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
67 concise and more understandable but improves performance (10X-100X). If loops |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
68 must be used, make sure that the allocation of space for variables takes place |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
69 outside the loops using an assignment to a matrix of the right size, or zeros. |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
70 |
6569 | 71 @item |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
72 Does the code make as much use as possible of existing built-in library |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
73 routines? |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
74 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
75 These routines are highly optimized and many do not carry the overhead of being |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
76 interpreted. |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
77 |
6569 | 78 @item |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
79 Does writing a dynamically linked function represent a useful investment of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
80 your time, relative to staying in Octave? |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
81 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
82 It will take time to learn Octave's interface for external code and there will |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
83 inevitably be issues with tools such as compilers. |
6569 | 84 @end itemize |
85 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
86 With that said, Octave offers a versatile interface for including chunks of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
87 compiled code as dynamically linked extensions. These dynamically linked |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
88 functions can be called from the interpreter in the same manner as any ordinary |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
89 function. The interface is bi-directional and external code can call Octave |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
90 functions (like @code{plot}) which otherwise might be very difficult to |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
91 develop. |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
92 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
93 The interface is centered around supporting the languages C++, C, and Fortran. |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
94 Octave itself is written in C++ and can call external C++/C code through its |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
95 native oct-file interface. The C language is also supported through the |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
96 mex-file interface for compatibility with @sc{matlab}. Fortran code is easiest |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
97 to reach through the oct-file interface. |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
98 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
99 Because many other languages provide C or C++ APIs it is relatively simple to |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
100 build bridges between Octave and other languages. This is also a way to bridge |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
101 to hardware resources which often have device drivers written in C. |
6569 | 102 |
103 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
104 * Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
105 * Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
106 * Standalone Programs:: |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
107 * Java Interface:: |
6569 | 108 @end menu |
109 | |
110 @node Oct-Files | |
111 @section Oct-Files | |
112 @cindex oct-files | |
113 @cindex mkoctfile | |
114 @cindex oct | |
115 | |
116 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
117 * Getting Started with Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
118 * Matrices and Arrays in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
119 * Character Strings in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
120 * Cell Arrays in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
121 * Structures in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
122 * Sparse Matrices in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
123 * Accessing Global Variables in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
124 * Calling Octave Functions from Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
125 * Calling External Code from Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
126 * Allocating Local Memory in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
127 * Input Parameter Checking in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
128 * Exception and Error Handling in Oct-Files:: |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
129 * Documentation and Testing of Oct-Files:: |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
130 @c * Application Programming Interface for Oct-Files:: |
6569 | 131 @end menu |
132 | |
133 @node Getting Started with Oct-Files | |
134 @subsection Getting Started with Oct-Files | |
135 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
136 Oct-files are pieces of C++ code that have been compiled with the Octave API |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
137 into a dynamically loadable object. They take their name from the file which |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
138 contains the object which has the extension @file{.oct}. |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
139 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
140 Finding a C++ compiler, using the correct switches, adding the right include |
21530
7c143e73e921
doc: Don't create end-of-sentence period with "etc." in Texinfo.
Rik <rik@octave.org>
parents:
21319
diff
changeset
|
141 paths for header files, etc.@: is a difficult task. Octave automates this by |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
142 providing the @code{mkoctfile} command with which to build oct-files. The |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
143 command is available from within Octave or at the shell command line. |
6569 | 144 |
145 @DOCSTRING(mkoctfile) | |
146 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
147 Consider the following short example which introduces the basics of writing a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
148 C++ function that can be linked to Octave. |
6569 | 149 |
9906 | 150 @example |
151 @group | |
152 @EXAMPLEFILE(helloworld.cc) | |
153 @end group | |
154 @end example | |
6569 | 155 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
156 The first critical line is @code{#include <octave/oct.h>} which makes available |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
157 most of the definitions necessary for a C++ oct-file. Note that |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
158 @file{octave/oct.h} is a C++ header and cannot be directly @code{#include}'ed |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
159 in a C source file, nor any other language. |
6569 | 160 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
161 Included by @file{oct.h} is a definition for the macro @w{@code{DEFUN_DLD}} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
162 which creates a dynamically loaded function. This macro takes four arguments: |
6569 | 163 |
164 @enumerate 1 | |
6571 | 165 @item The function name as it will be seen in Octave, |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
166 |
6572 | 167 @item The list of arguments to the function of type @code{octave_value_list}, |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
168 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
169 @item The number of output arguments, which can be---and often is---omitted if |
6569 | 170 not used, and |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
171 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
172 @item The string to use for the help text of the function. |
6569 | 173 @end enumerate |
174 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
175 The return type of functions defined with @w{@code{DEFUN_DLD}} is always |
6572 | 176 @code{octave_value_list}. |
6569 | 177 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
178 There are a couple of important considerations in the choice of function name. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
179 First, it must be a valid Octave function name and so must be a sequence of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
180 letters, digits, and underscores not starting with a digit. Second, as Octave |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
181 uses the function name to define the filename it attempts to find the function |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
182 in, the function name in the @w{@code{DEFUN_DLD}} macro must match the filename |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
183 of the oct-file. Therefore, the above function should be in a file |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
184 @file{helloworld.cc}, and would be compiled to an oct-file using the command |
6569 | 185 |
186 @example | |
187 mkoctfile helloworld.cc | |
188 @end example | |
189 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
190 This will create a file called @file{helloworld.oct} that is the compiled |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
191 version of the function. It should be noted that it is perfectly acceptable to |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
192 have more than one @w{@code{DEFUN_DLD}} function in a source file. However, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
193 there must either be a symbolic link to the oct-file for each of the functions |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
194 defined in the source code with the @w{@code{DEFUN_DLD}} macro or the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
195 @code{autoload} (@ref{Function Files}) function should be used. |
6569 | 196 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
197 The rest of the function shows how to find the number of input arguments, how |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
198 to print through the Octave pager, and how to return from the function. After |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
199 compiling this function as above, an example of its use is |
6569 | 200 |
201 @example | |
202 @group | |
6572 | 203 helloworld (1, 2, 3) |
204 @print{} Hello World has 3 input arguments and 0 output arguments. | |
6569 | 205 @end group |
206 @end example | |
207 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
208 Subsequent sections show how to use specific classes from Octave's core |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
209 internals. Base classes like @code{dMatrix} (a matrix of double values) are |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
210 found in the directory @file{liboctave/array}. The definitive reference for |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
211 how to use a particular class is the header file itself. However, it is often |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
212 enough simply to study the examples in the manual in order to be able to use a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
213 class. |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
214 |
6569 | 215 @node Matrices and Arrays in Oct-Files |
216 @subsection Matrices and Arrays in Oct-Files | |
217 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
218 Octave supports a number of different array and matrix classes, the majority of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
219 which are based on the @code{Array} class. The exception are the sparse matrix |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
220 types discussed separately below. There are three basic matrix types: |
6569 | 221 |
6572 | 222 @table @code |
6569 | 223 @item Matrix |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
224 A double precision matrix class defined in @file{dMatrix.h} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
225 |
6569 | 226 @item ComplexMatrix |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
227 A complex matrix class defined in @file{CMatrix.h} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
228 |
6569 | 229 @item BoolMatrix |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
230 A boolean matrix class defined in @file{boolMatrix.h} |
6569 | 231 @end table |
232 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
233 These are the basic two-dimensional matrix types of Octave. In addition there |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
234 are a number of multi-dimensional array types including |
6569 | 235 |
6572 | 236 @table @code |
6569 | 237 @item NDArray |
6572 | 238 A double precision array class defined in @file{dNDArray.h} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
239 |
6569 | 240 @item ComplexNDarray |
6572 | 241 A complex array class defined in @file{CNDArray.h} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
242 |
6569 | 243 @item boolNDArray |
6572 | 244 A boolean array class defined in @file{boolNDArray.h} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
245 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
246 @item int8NDArray |
6572 | 247 @itemx int16NDArray |
248 @itemx int32NDArray | |
249 @itemx int64NDArray | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
250 8, 16, 32, and 64-bit signed array classes defined in |
6572 | 251 @file{int8NDArray.h}, @file{int16NDArray.h}, etc. |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
252 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
253 @item uint8NDArray |
6572 | 254 @itemx uint16NDArray |
255 @itemx uint32NDArray | |
256 @itemx uint64NDArray | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
257 8, 16, 32, and 64-bit unsigned array classes defined in |
6572 | 258 @file{uint8NDArray.h}, @file{uint16NDArray.h}, etc. |
6569 | 259 @end table |
260 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
261 There are several basic ways of constructing matrices or multi-dimensional |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
262 arrays. Using the class @code{Matrix} as an example one can |
6569 | 263 |
264 @itemize @bullet | |
6571 | 265 @item |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
266 Create an empty matrix or array with the empty constructor. For example: |
6569 | 267 |
268 @example | |
269 Matrix a; | |
270 @end example | |
271 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
272 This can be used for all matrix and array types. |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
273 |
6571 | 274 @item |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
275 Define the dimensions of the matrix or array with a dim_vector which has the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
276 same characteristics as the vector returned from @code{size}. For example: |
6569 | 277 |
278 @example | |
279 @group | |
20187
998881b6cdef
external.txi: Fix example case for dim_vector (bug #45100).
Piotr Held <pjheld@gmail.com>
parents:
19721
diff
changeset
|
280 dim_vector dv (2, 3); // 2 rows, 3 columns |
6572 | 281 Matrix a (dv); |
6569 | 282 @end group |
283 @end example | |
284 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
285 This can be used for all matrix and array types. |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
286 |
6569 | 287 @item |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
288 Define the number of rows and columns in the matrix. For example: |
6569 | 289 |
290 @example | |
6572 | 291 Matrix a (2, 2) |
6569 | 292 @end example |
293 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
294 This constructor can @strong{only} be used with matrix types. |
6569 | 295 @end itemize |
296 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
297 These types all share a number of basic methods and operators. Many bear a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
298 resemblance to functions that exist in the interpreter. A selection of useful |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
299 methods include |
6569 | 300 |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
301 @deftypefn {Method} {T&} operator () (octave_idx_type) |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
302 @deftypefnx {Method} {T&} elem (octave_idx_type) |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
303 The @code{()} operator or @code{elem} method allow the values of the matrix or |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
304 array to be read or set. These methods take a single argument, which is of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
305 type @code{octave_idx_type}, that is the index into the matrix or array. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
306 Additionally, the matrix type allows two argument versions of the @code{()} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
307 operator and @code{elem} method, giving the row and column index of the value |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
308 to get or set. |
6572 | 309 @end deftypefn |
6569 | 310 |
7001 | 311 Note that these functions do significant error checking and so in some |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
312 circumstances the user might prefer to access the data of the array or matrix |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
313 directly through the @code{fortran_vec} method discussed below. |
6572 | 314 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
315 @deftypefn {Method} {octave_idx_type} numel (void) const |
6569 | 316 The total number of elements in the matrix or array. |
6572 | 317 @end deftypefn |
318 | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
319 @deftypefn {Method} {size_t} byte_size (void) const |
6569 | 320 The number of bytes used to store the matrix or array. |
6572 | 321 @end deftypefn |
322 | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
323 @deftypefn {Method} {dim_vector} dims (void) const |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
324 The dimensions of the matrix or array in value of type @code{dim_vector}. |
6572 | 325 @end deftypefn |
326 | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
327 @deftypefn {Method} {int} ndims (void) const |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
328 The number of dimensions of the matrix or array. Matrices are always 2-D, but |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
329 arrays can be N-dimensional. |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
330 @end deftypefn |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
331 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
332 @deftypefn {Method} {void} resize (const dim_vector&) |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
333 @deftypefnx {Method} {void} resize (nrows, ncols) |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
334 A method taking either an argument of type @code{dim_vector}, or, in the case |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
335 of a matrix, two arguments of type @code{octave_idx_type} defining the number |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
336 of rows and columns in the matrix. |
6572 | 337 @end deftypefn |
338 | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
339 @deftypefn {Method} {T*} fortran_vec (void) |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
340 This method returns a pointer to the underlying data of the matrix or array so |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
341 that it can be manipulated directly, either within Octave or by an external |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
342 library. |
6572 | 343 @end deftypefn |
6569 | 344 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
345 Operators such as @code{+}, @code{-}, or @code{*} can be used on the majority |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
346 of the matrix and array types. In addition there are a number of methods that |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
347 are of interest only for matrices such as @code{transpose}, @code{hermitian}, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
348 @code{solve}, etc. |
6569 | 349 |
350 The typical way to extract a matrix or array from the input arguments of | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
351 @w{@code{DEFUN_DLD}} function is as follows |
6569 | 352 |
9906 | 353 @example |
21316
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20535
diff
changeset
|
354 @group |
9906 | 355 @EXAMPLEFILE(addtwomatrices.cc) |
21316
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20535
diff
changeset
|
356 @end group |
9906 | 357 @end example |
6569 | 358 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
359 To avoid segmentation faults causing Octave to abort, this function explicitly |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
360 checks that there are sufficient arguments available before accessing these |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
361 arguments. It then obtains two multi-dimensional arrays of type @code{NDArray} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
362 and adds these together. Note that the @code{array_value} method is called |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
363 without using the @code{is_matrix_type} method. If an error occurs when |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
364 attempting to extract the value, Octave will print a message and throw an |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
365 exception. The reason to prefer this coding structure is that the arguments |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
366 might be a type which is not an @code{NDArray}, but for which it would make |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
367 sense to convert them to one. The @code{array_value} method allows this |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
368 conversion to be performed transparently when possible. If you need to catch |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
369 errors like this, and perform some kind of cleanup or other operation, you can |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
370 catch the @code{octave_execution_error} exception. |
6569 | 371 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
372 @code{A + B}, operating on two @code{NDArray} objects returns an |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
373 @code{NDArray}, which is cast to an @code{octave_value} on the return from the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
374 function. An example of the use of this demonstration function is |
6569 | 375 |
376 @example | |
377 @group | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
378 addtwomatrices (ones (2, 2), eye (2, 2)) |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
379 @result{} 2 1 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
380 1 2 |
6569 | 381 @end group |
382 @end example | |
383 | |
6572 | 384 A list of the basic @code{Matrix} and @code{Array} types, the methods to |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
385 extract these from an @code{octave_value}, and the associated header file is |
6572 | 386 listed below. |
6569 | 387 |
388 @multitable @columnfractions .3 .4 .3 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
389 @headitem Type @tab Function @tab Source Code |
6572 | 390 @item @code{RowVector} @tab @code{row_vector_value} @tab @file{dRowVector.h} |
391 @item @code{ComplexRowVector} @tab @code{complex_row_vector_value} @tab @file{CRowVector.h} | |
392 @item @code{ColumnVector} @tab @code{column_vector_value} @tab @file{dColVector.h} | |
393 @item @code{ComplexColumnVector} @tab @code{complex_column_vector_value} @tab @file{CColVector.h} | |
394 @item @code{Matrix} @tab @code{matrix_value} @tab @file{dMatrix.h} | |
395 @item @code{ComplexMatrix} @tab @code{complex_matrix_value} @tab @file{CMatrix.h} | |
396 @item @code{boolMatrix} @tab @code{bool_matrix_value} @tab @file{boolMatrix.h} | |
397 @item @code{charMatrix} @tab @code{char_matrix_value} @tab @file{chMatrix.h} | |
398 @item @code{NDArray} @tab @code{array_value} @tab @file{dNDArray.h} | |
399 @item @code{ComplexNDArray} @tab @code{complex_array_value} @tab @file{CNDArray.h} | |
400 @item @code{boolNDArray} @tab @code{bool_array_value} @tab @file{boolNDArray.h} | |
401 @item @code{charNDArray} @tab @code{char_array_value} @tab @file{charNDArray.h} | |
402 @item @code{int8NDArray} @tab @code{int8_array_value} @tab @file{int8NDArray.h} | |
403 @item @code{int16NDArray} @tab @code{int16_array_value} @tab @file{int16NDArray.h} | |
404 @item @code{int32NDArray} @tab @code{int32_array_value} @tab @file{int32NDArray.h} | |
405 @item @code{int64NDArray} @tab @code{int64_array_value} @tab @file{int64NDArray.h} | |
406 @item @code{uint8NDArray} @tab @code{uint8_array_value} @tab @file{uint8NDArray.h} | |
407 @item @code{uint16NDArray} @tab @code{uint16_array_value} @tab @file{uint16NDArray.h} | |
408 @item @code{uint32NDArray} @tab @code{uint32_array_value} @tab @file{uint32NDArray.h} | |
409 @item @code{uint64NDArray} @tab @code{uint64_array_value} @tab @file{uint64NDArray.h} | |
6569 | 410 @end multitable |
411 | |
6572 | 412 @node Character Strings in Oct-Files |
413 @subsection Character Strings in Oct-Files | |
414 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
415 A character string in Octave is just a special @code{Array} class. Consider |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
416 the example: |
6572 | 417 |
9906 | 418 @example |
419 @EXAMPLEFILE(stringdemo.cc) | |
420 @end example | |
6572 | 421 |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
422 An example of the use of this function is |
6572 | 423 |
424 @example | |
425 @group | |
426 s0 = ["First String"; "Second String"]; | |
427 [s1,s2] = stringdemo (s0) | |
428 @result{} s1 = Second String | |
429 First String | |
430 | |
431 @result{} s2 = First String | |
432 Second String | |
433 | |
434 typeinfo (s2) | |
435 @result{} sq_string | |
436 typeinfo (s1) | |
437 @result{} string | |
438 @end group | |
439 @end example | |
440 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
441 One additional complication of strings in Octave is the difference between |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
442 single quoted and double quoted strings. To find out if an @code{octave_value} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
443 contains a single or double quoted string use one of the predicate tests shown |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
444 below. |
6572 | 445 |
446 @example | |
447 @group | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
448 if (args(0).is_sq_string ()) |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
449 octave_stdout << "First argument is a single quoted string\n"; |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
450 else if (args(0).is_dq_string ()) |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
451 octave_stdout << "First argument is a double quoted string\n"; |
6572 | 452 @end group |
453 @end example | |
454 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
455 Note, however, that both types of strings are represented by the |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
456 @code{charNDArray} type, and so when assigning to an @code{octave_value}, the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
457 type of string should be specified. For example: |
6572 | 458 |
459 @example | |
460 @group | |
461 octave_value_list retval; | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
462 charNDArray ch; |
6572 | 463 @dots{} |
6577 | 464 // Create single quoted string |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
465 retval(1) = octave_value (ch); // default constructor is sq_string |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
466 OR |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
467 retval(1) = octave_value (ch, '\''); // explicitly create sq_string |
6577 | 468 |
469 // Create a double quoted string | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
470 retval(0) = octave_value (ch, '"'); |
6572 | 471 @end group |
472 @end example | |
473 | |
474 @node Cell Arrays in Oct-Files | |
475 @subsection Cell Arrays in Oct-Files | |
476 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
477 Octave's cell type is also available from within oct-files. A cell array is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
478 just an @code{Array} of @code{octave_value}s, and thus each element of the cell |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
479 array can be treated like any other @code{octave_value}. A simple example is |
6572 | 480 |
9906 | 481 @example |
482 @EXAMPLEFILE(celldemo.cc) | |
483 @end example | |
6572 | 484 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
485 Note that cell arrays are used less often in standard oct-files and so the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
486 @file{Cell.h} header file must be explicitly included. The rest of the example |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
487 extracts the @code{octave_value}s one by one from the cell array and returns |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
488 them as individual output arguments. For example: |
6572 | 489 |
490 @example | |
491 @group | |
492 [b1, b2, b3] = celldemo (@{1, [1, 2], "test"@}) | |
493 @result{} | |
494 b1 = 1 | |
495 b2 = | |
496 | |
497 1 2 | |
498 | |
499 b3 = test | |
500 @end group | |
501 @end example | |
502 | |
503 @node Structures in Oct-Files | |
504 @subsection Structures in Oct-Files | |
505 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
506 A structure in Octave is a map between a number of fields represented and their |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
507 values. The Standard Template Library @code{map} class is used, with the pair |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
508 consisting of a @code{std::string} and an Octave @code{Cell} variable. |
6572 | 509 |
510 A simple example demonstrating the use of structures within oct-files is | |
511 | |
9906 | 512 @example |
513 @EXAMPLEFILE(structdemo.cc) | |
514 @end example | |
6572 | 515 |
516 An example of its use is | |
517 | |
518 @example | |
519 @group | |
520 x.a = 1; x.b = "test"; x.c = [1, 2]; | |
521 structdemo (x, "b") | |
522 @result{} selected = test | |
523 @end group | |
524 @end example | |
525 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
526 The example above specifically uses the @code{octave_scalar_map} class which is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
527 for representing a single struct. For structure arrays, the @code{octave_map} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
528 class is used instead. The commented code shows how the demo could be modified |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
529 to handle a structure array. In that case, the @code{contents} method returns |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
530 a @code{Cell} which may have more than one element. Therefore, to obtain the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
531 underlying @code{octave_value} in the single struct example we would write |
6572 | 532 |
533 @example | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
534 octave_value tmp = arg0.contents (arg1)(0); |
6572 | 535 @end example |
536 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
537 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
538 where the trailing @code{(0)} is the @code{()} operator on the @code{Cell} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
539 object. If this were a true structure array with multiple elements we could |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
540 iterate over the elements using the @code{()} operator. |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
541 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
542 Structures are a relatively complex data container and there are more functions |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
543 available in @file{oct-map.h} which make coding with them easier than relying |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
544 on just @code{contents}. |
6572 | 545 |
546 @node Sparse Matrices in Oct-Files | |
547 @subsection Sparse Matrices in Oct-Files | |
6569 | 548 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
549 There are three classes of sparse objects that are of interest to the user. |
6569 | 550 |
6572 | 551 @table @code |
6569 | 552 @item SparseMatrix |
553 A double precision sparse matrix class | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
554 |
6569 | 555 @item SparseComplexMatrix |
556 A complex sparse matrix class | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
557 |
6569 | 558 @item SparseBoolMatrix |
559 A boolean sparse matrix class | |
560 @end table | |
561 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
562 All of these classes inherit from the @code{Sparse<T>} template class, and so |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
563 all have similar capabilities and usage. The @code{Sparse<T>} class was based |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
564 on Octave's @code{Array<T>} class and users familiar with Octave's |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
565 @code{Array} classes will be comfortable with the use of the sparse classes. |
6569 | 566 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
567 The sparse classes will not be entirely described in this section, due to their |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
568 similarity with the existing @code{Array} classes. However, there are a few |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
569 differences due the nature of sparse objects, and these will be described. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
570 First, although it is fundamentally possible to have N-dimensional sparse |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
571 objects, the Octave sparse classes do not allow them at this time; All |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
572 instances of the sparse classes @strong{must} be 2-dimensional. This means |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
573 that @code{SparseMatrix} is actually more similar to Octave's @code{Matrix} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
574 class than it is to the @code{NDArray} class. |
6569 | 575 |
576 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
577 * Array and Sparse Class Differences:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
578 * Creating Sparse Matrices in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
579 * Using Sparse Matrices in Oct-Files:: |
6569 | 580 @end menu |
581 | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
582 @node Array and Sparse Class Differences |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
583 @subsubsection Array and Sparse Class Differences |
6569 | 584 |
585 The number of elements in a sparse matrix is considered to be the number | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
586 of nonzero elements, rather than the product of the dimensions. Therefore, |
6569 | 587 |
588 @example | |
6577 | 589 @group |
590 SparseMatrix sm; | |
591 @dots{} | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
592 int nnz = sm.nelem (); |
6577 | 593 @end group |
6569 | 594 @end example |
595 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
596 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
597 returns the number of nonzero elements (like the interpreter function |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
598 @code{nnz}). If the user really requires the number of elements in the matrix, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
599 including the nonzero elements, they should use @code{numel} rather than |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
600 @code{nelem}. Note that for very large matrices, where the product of the two |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
601 dimensions is larger than the representation of an unsigned int, @code{numel} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
602 can overflow. An example is @code{speye (1e6)} which will create a matrix with |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
603 a million rows and columns, but only a million nonzero elements. In this case, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
604 the number of rows multiplied by the number of columns is more than two hundred |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
605 times the maximum value that can be represented by an unsigned 32-bit int. The |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
606 use of @code{numel} should, therefore, be avoided unless it is known that it |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
607 will not overflow. |
6569 | 608 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
609 Extreme care is also required when using the @code{elem} method or the |
24987
a895967fd5a7
doc: grammarcheck manual (*.txi) ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
610 @code{()} operator which perform essentially the same function. The reason is |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
611 that if a sparse object is non-const, then Octave will assume that a request |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
612 for a zero element in a sparse matrix is in fact a request to create this |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
613 element so it can be filled. Therefore, a piece of code like |
6569 | 614 |
615 @example | |
6577 | 616 @group |
617 SparseMatrix sm; | |
618 @dots{} | |
619 for (int j = 0; j < nc; j++) | |
620 for (int i = 0; i < nr; i++) | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
621 std::cerr << " (" << i << "," << j << "): " << sm(i,j) << "\n"; |
6577 | 622 @end group |
6569 | 623 @end example |
624 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
625 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
626 is a great way of turning a sparse matrix into a dense one, and a very slow |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
627 way at that since it reallocates the sparse object for each zero element in the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
628 matrix. |
6569 | 629 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
630 A simple way of preventing the above from happening is to create a temporary |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
631 constant version of the sparse matrix. Note that only the container for the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
632 sparse matrix will be copied, while the actual representation of the data will |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
633 be shared between the two versions of the sparse matrix; This is not a costly |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
634 operation. The example above, re-written to prevent sparse-to-dense |
23082
e8d64dce0afd
maint: strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
22970
diff
changeset
|
635 conversion, is |
6569 | 636 |
637 @example | |
6577 | 638 @group |
639 SparseMatrix sm; | |
640 @dots{} | |
641 const SparseMatrix tmp (sm); | |
642 for (int j = 0; j < nc; j++) | |
643 for (int i = 0; i < nr; i++) | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
644 std::cerr << " (" << i << "," << j << "): " << tmp(i,j) << "\n"; |
6577 | 645 @end group |
6569 | 646 @end example |
647 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
648 Finally, because the sparse types aren't represented by a contiguous block of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
649 memory, the @nospell{@code{fortran_vec}} method of @code{Array<T>} is not |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
650 available. It is, however, replaced by three separate methods @code{ridx}, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
651 @code{cidx}, and @code{data}, that access the raw compressed column format that |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
652 Octave sparse matrices are stored in. These methods can be used in a manner |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
653 similar to @code{elem} to allow the matrix to be accessed or filled. However, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
654 it is up to the user to respect the sparse matrix compressed column format or |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
655 the matrix will become corrupted. |
6569 | 656 |
6572 | 657 @node Creating Sparse Matrices in Oct-Files |
658 @subsubsection Creating Sparse Matrices in Oct-Files | |
6569 | 659 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
660 There are two useful strategies for creating a sparse matrix. The first is to |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
661 create three vectors representing the row index, column index, and data values, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
662 and from these create the matrix. The second alternative is to create a sparse |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
663 matrix with the appropriate amount of space, and then fill in the values. Both |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
664 techniques have their advantages and disadvantages. |
6569 | 665 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
666 Below is an example of creating a small sparse matrix using the first technique |
6569 | 667 |
668 @example | |
6577 | 669 @group |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
670 int nz, nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
671 nz = 4, nr = 3, nc = 4; |
6577 | 672 |
673 ColumnVector ridx (nz); | |
674 ColumnVector cidx (nz); | |
675 ColumnVector data (nz); | |
6569 | 676 |
18797
96f22d6674c4
Fix incorrect sparse matrix example code in oct-file chapter (bug #41799).
Rik <rik@octave.org>
parents:
18790
diff
changeset
|
677 ridx(0) = 1; cidx(0) = 1; data(0) = 1; |
96f22d6674c4
Fix incorrect sparse matrix example code in oct-file chapter (bug #41799).
Rik <rik@octave.org>
parents:
18790
diff
changeset
|
678 ridx(1) = 2; cidx(1) = 2; data(1) = 2; |
96f22d6674c4
Fix incorrect sparse matrix example code in oct-file chapter (bug #41799).
Rik <rik@octave.org>
parents:
18790
diff
changeset
|
679 ridx(2) = 2; cidx(2) = 4; data(2) = 3; |
96f22d6674c4
Fix incorrect sparse matrix example code in oct-file chapter (bug #41799).
Rik <rik@octave.org>
parents:
18790
diff
changeset
|
680 ridx(3) = 3; cidx(3) = 4; data(3) = 4; |
6577 | 681 SparseMatrix sm (data, ridx, cidx, nr, nc); |
682 @end group | |
6569 | 683 @end example |
684 | |
6572 | 685 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
686 which creates the matrix given in section @ref{Storage of Sparse Matrices}. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
687 Note that the compressed matrix format is not used at the time of the creation |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
688 of the matrix itself, but is used internally. |
6569 | 689 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
690 As discussed in the chapter on Sparse Matrices, the values of the sparse matrix |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
691 are stored in increasing column-major ordering. Although the data passed by |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
692 the user need not respect this requirement, pre-sorting the data will |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
693 significantly speed up creation of the sparse matrix. |
6569 | 694 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
695 The disadvantage of this technique for creating a sparse matrix is that there |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
696 is a brief time when two copies of the data exist. For extremely memory |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
697 constrained problems this may not be the best technique for creating a sparse |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
698 matrix. |
6569 | 699 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
700 The alternative is to first create a sparse matrix with the desired number of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
701 nonzero elements and then later fill those elements in. Sample code: |
6569 | 702 |
6571 | 703 @example |
6577 | 704 @group |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
705 int nz, nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
706 nz = 4, nr = 3, nc = 4; |
6577 | 707 SparseMatrix sm (nr, nc, nz); |
708 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4; | |
709 @end group | |
6569 | 710 @end example |
711 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
712 This creates the same matrix as previously. Again, although not strictly |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
713 necessary, it is significantly faster if the sparse matrix is created and the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
714 elements are added in column-major ordering. The reason for this is that when |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
715 elements are inserted at the end of the current list of known elements then no |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
716 element in the matrix needs to be moved to allow the new element to be |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
717 inserted; Only the column indices need to be updated. |
6569 | 718 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
719 There are a few further points to note about this method of creating a sparse |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
720 matrix. First, it is possible to create a sparse matrix with fewer elements |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
721 than are actually inserted in the matrix. Therefore, |
6569 | 722 |
6571 | 723 @example |
6577 | 724 @group |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
725 int nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
726 nr = 3, nc = 4; |
6577 | 727 SparseMatrix sm (nr, nc, 0); |
728 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4; | |
729 @end group | |
6569 | 730 @end example |
731 | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
732 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
733 is perfectly valid. However, it is a very bad idea because as each new element |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
734 is added to the sparse matrix the matrix needs to request more space and |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
735 reallocate memory. This is an expensive operation that will significantly slow |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
736 this means of creating a sparse matrix. It is possible to create a sparse |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
737 matrix with excess storage, so having @var{nz} greater than 4 in this example |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
738 is also valid. The disadvantage is that the matrix occupies more memory than |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
739 strictly needed. |
6569 | 740 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
741 Of course, it is not always possible to know the number of nonzero elements |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
742 prior to filling a matrix. For this reason the additional unused storage of a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
743 sparse matrix can be removed after its creation with the @code{maybe_compress} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
744 function. In addition to deallocating unused storage, @code{maybe_compress} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
745 can also remove zero elements from the matrix. The removal of zero elements |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
746 from the matrix is controlled by setting the argument of the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
747 @code{maybe_compress} function to be @code{true}. However, the cost of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
748 removing the zeros is high because it implies re-sorting the elements. If |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
749 possible, it is better for the user to avoid adding the unnecessary zeros in |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
750 the first place. An example of the use of @code{maybe_compress} is |
6569 | 751 |
752 @example | |
6577 | 753 @group |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
754 int nz, nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
755 nz = 6, nr = 3, nc = 4; |
6577 | 756 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
757 SparseMatrix sm1 (nr, nc, nz); |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
758 sm1(0,0) = 1; sm1(0,1) = 2; sm1(1,3) = 3; sm1(2,3) = 4; |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
759 sm1.maybe_compress (); // No zero elements were added |
6569 | 760 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
761 SparseMatrix sm2 (nr, nc, nz); |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
762 sm2(0,0) = 1; sm2(0,1) = 2; sm(0,2) = 0; sm(1,2) = 0; |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
763 sm1(1,3) = 3; sm1(2,3) = 4; |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
764 sm2.maybe_compress (true); // Zero elements were added |
6577 | 765 @end group |
6569 | 766 @end example |
767 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
768 The use of the @code{maybe_compress} function should be avoided if possible as |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
769 it will slow the creation of the matrix. |
6569 | 770 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
771 A third means of creating a sparse matrix is to work directly with the data in |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
772 compressed row format. An example of this advanced technique might be |
6569 | 773 |
774 @example | |
6577 | 775 octave_value arg; |
776 @dots{} | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
777 int nz, nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
778 nz = 6, nr = 3, nc = 4; // Assume we know the max # nz |
6577 | 779 SparseMatrix sm (nr, nc, nz); |
780 Matrix m = arg.matrix_value (); | |
6569 | 781 |
6577 | 782 int ii = 0; |
783 sm.cidx (0) = 0; | |
784 for (int j = 1; j < nc; j++) | |
785 @{ | |
786 for (int i = 0; i < nr; i++) | |
787 @{ | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
788 double tmp = m(i,j); |
6577 | 789 if (tmp != 0.) |
790 @{ | |
791 sm.data(ii) = tmp; | |
792 sm.ridx(ii) = i; | |
793 ii++; | |
794 @} | |
795 @} | |
796 sm.cidx(j+1) = ii; | |
797 @} | |
18790
322eb69e30ad
doc: Fix some Latin wording in documentation.
Rik <rik@octave.org>
parents:
18534
diff
changeset
|
798 sm.maybe_compress (); // If don't know a priori the final # of nz. |
6569 | 799 @end example |
800 | |
6572 | 801 @noindent |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
802 which is probably the most efficient means of creating a sparse matrix. |
6569 | 803 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
804 Finally, it may sometimes arise that the amount of storage initially created is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
805 insufficient to completely store the sparse matrix. Therefore, the method |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
806 @code{change_capacity} exists to reallocate the sparse memory. The above |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
807 example would then be modified as |
6569 | 808 |
809 @example | |
6577 | 810 octave_value arg; |
811 @dots{} | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
812 int nz, nr, nc; |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
813 nz = 6, nr = 3, nc = 4; // Guess the number of nz elements |
6577 | 814 SparseMatrix sm (nr, nc, nz); |
815 Matrix m = arg.matrix_value (); | |
6569 | 816 |
6577 | 817 int ii = 0; |
818 sm.cidx (0) = 0; | |
819 for (int j = 1; j < nc; j++) | |
820 @{ | |
821 for (int i = 0; i < nr; i++) | |
822 @{ | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
823 double tmp = m(i,j); |
6577 | 824 if (tmp != 0.) |
825 @{ | |
826 if (ii == nz) | |
827 @{ | |
828 nz += 2; // Add 2 more elements | |
829 sm.change_capacity (nz); | |
830 @} | |
831 sm.data(ii) = tmp; | |
832 sm.ridx(ii) = i; | |
833 ii++; | |
834 @} | |
835 @} | |
836 sm.cidx(j+1) = ii; | |
837 @} | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
838 sm.maybe_compress (); // If don't know a priori the final # of nz. |
6569 | 839 @end example |
840 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
841 Note that both increasing and decreasing the number of nonzero elements in a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
842 sparse matrix is expensive as it involves memory reallocation. Also because |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
843 parts of the matrix, though not its entirety, exist as old and new copies at |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
844 the same time, additional memory is needed. Therefore, if possible avoid |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
845 changing capacity. |
6569 | 846 |
6572 | 847 @node Using Sparse Matrices in Oct-Files |
6569 | 848 @subsubsection Using Sparse Matrices in Oct-Files |
849 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
850 Most of the same operators and functions for sparse matrices that are available |
23567
71bfd507663c
doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents:
23565
diff
changeset
|
851 from the Octave interpreter are also available within oct-files. The basic |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
852 means of extracting a sparse matrix from an @code{octave_value}, and returning |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
853 it as an @code{octave_value}, can be seen in the following example. |
6569 | 854 |
855 @example | |
6577 | 856 @group |
857 octave_value_list retval; | |
6569 | 858 |
6577 | 859 SparseMatrix sm = args(0).sparse_matrix_value (); |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
860 SparseComplexMatrix scm = args(1).sparse_complex_matrix_value (); |
6577 | 861 SparseBoolMatrix sbm = args(2).sparse_bool_matrix_value (); |
862 @dots{} | |
863 retval(2) = sbm; | |
864 retval(1) = scm; | |
865 retval(0) = sm; | |
866 @end group | |
6569 | 867 @end example |
868 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
869 The conversion to an @code{octave_value} is handled by the sparse |
6569 | 870 @code{octave_value} constructors, and so no special care is needed. |
871 | |
872 @node Accessing Global Variables in Oct-Files | |
873 @subsection Accessing Global Variables in Oct-Files | |
874 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
875 Global variables allow variables in the global scope to be accessed. Global |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
876 variables can be accessed within oct-files by using the support functions |
27595
18fea1c5b21a
doc: improve example of using global variables from oct-files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
26376
diff
changeset
|
877 @w{@code{global_varval}} and @w{@code{global_assign}} from the current |
18fea1c5b21a
doc: improve example of using global variables from oct-files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
26376
diff
changeset
|
878 interpreter's symbol table. Both functions take as first argument a string |
18fea1c5b21a
doc: improve example of using global variables from oct-files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
26376
diff
changeset
|
879 representing the variable name to be obtained or assigned. The second |
18fea1c5b21a
doc: improve example of using global variables from oct-files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
26376
diff
changeset
|
880 argument of @w{@code{global_assign}} is the value to be assigned. An |
18fea1c5b21a
doc: improve example of using global variables from oct-files.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
26376
diff
changeset
|
881 example of the use of these two functions is |
6569 | 882 |
9906 | 883 @example |
884 @EXAMPLEFILE(globaldemo.cc) | |
885 @end example | |
6569 | 886 |
887 An example of its use is | |
888 | |
889 @example | |
890 @group | |
891 global a b | |
892 b = 10; | |
893 globaldemo ("b") | |
894 @result{} 10 | |
895 globaldemo ("c") | |
896 @result{} "Global variable not found" | |
897 num2str (a) | |
898 @result{} 42 | |
899 @end group | |
900 @end example | |
901 | |
902 @node Calling Octave Functions from Oct-Files | |
903 @subsection Calling Octave Functions from Oct-Files | |
904 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
905 There is often a need to be able to call another Octave function from within an |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
906 oct-file, and there are many examples of such within Octave itself. For |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
907 example, the @code{quad} function is an oct-file that calculates the definite |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
908 integral by quadrature over a user-supplied function. |
6569 | 909 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
910 There are also many ways in which a function could be given as input. It might |
6571 | 911 be passed as one of |
6569 | 912 |
913 @enumerate 1 | |
914 @item Function Handle | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
915 |
6569 | 916 @item Anonymous Function Handle |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
917 |
6569 | 918 @item String |
919 @end enumerate | |
920 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
921 The code below demonstrates all four methods of passing a function to an |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
922 oct-file. |
6569 | 923 |
9906 | 924 @example |
925 @EXAMPLEFILE(funcdemo.cc) | |
926 @end example | |
6569 | 927 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
928 The first input to the demonstration code is a user-supplied function and the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
929 remaining arguments are all passed to the function. |
6569 | 930 |
931 @example | |
932 @group | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
933 funcdemo (@@sin, 1) |
6569 | 934 @result{} 0.84147 |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
935 funcdemo (@@(x) sin (x), 1) |
6569 | 936 @result{} 0.84147 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
937 funcdemo ("sin", 1) |
6569 | 938 @result{} 0.84147 |
939 funcdemo (@@atan2, 1, 1) | |
940 @result{} 0.78540 | |
941 @end group | |
942 @end example | |
943 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
944 When the user function is passed as a string the treatment of the function is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
945 different. In some cases it is necessary to have the user supplied function as |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
946 an @code{octave_function} object. In that case the string argument can be used |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
947 to create a temporary function as demonstrated below. |
6569 | 948 |
949 @example | |
950 @group | |
6577 | 951 std::octave fcn_name = unique_symbol_name ("__fcn__"); |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
952 std::string fcode = "function y = "; |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
953 fcode.append (fcn_name); |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
954 fcode.append ("(x) y = "); |
6577 | 955 fcn = extract_function (args(0), "funcdemo", fcn_name, |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
956 fcode, "; endfunction"); |
6577 | 957 @dots{} |
958 if (fcn_name.length ()) | |
959 clear_function (fcn_name); | |
6569 | 960 @end group |
961 @end example | |
962 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
963 There are two important things to know in this case. First, the number of |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
964 input arguments to the user function is fixed, and in the above example is a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
965 single argument. Second, to avoid leaving the temporary function in the Octave |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
966 symbol table it should be cleared after use. Also, by convention all internal |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
967 function names begin and end with the character sequence @samp{__}. |
6569 | 968 |
969 @node Calling External Code from Oct-Files | |
970 @subsection Calling External Code from Oct-Files | |
971 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
972 Linking external C code to Octave is relatively simple, as the C functions can |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
973 easily be called directly from C++. One possible issue is that the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
974 declarations of the external C functions may need to be explicitly defined as C |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
975 functions to the compiler. If the declarations of the external C functions are |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
976 in the header @file{foo.h}, then the tactic to ensure that the C++ compiler |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
977 treats these declarations as C code is |
6569 | 978 |
979 @example | |
980 @group | |
981 #ifdef __cplusplus | |
6571 | 982 extern "C" |
6569 | 983 @{ |
984 #endif | |
985 #include "foo.h" | |
986 #ifdef __cplusplus | |
987 @} /* end extern "C" */ | |
988 #endif | |
989 @end group | |
990 @end example | |
991 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
992 Calling Fortran code, however, can pose more difficulties. This is due to |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
993 differences in the manner in which compilers treat the linking of Fortran code |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
994 with C or C++ code. Octave supplies several macros that allow consistent |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
995 behavior across a number of compilers. |
6569 | 996 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
997 The underlying Fortran code should use the @code{XSTOPX} function to replace |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
998 the Fortran @code{STOP} function. @code{XSTOPX} uses the Octave exception |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
999 handler to treat failing cases in the Fortran code explicitly. Note that |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1000 Octave supplies its own replacement @sc{blas} @code{XERBLA} function, which |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1001 uses @code{XSTOPX}. |
6569 | 1002 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1003 If the code calls @code{XSTOPX}, then the @w{@code{F77_XFCN}} macro should be |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1004 used to call the underlying Fortran function. The Fortran exception state can |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1005 then be checked with the global variable @code{f77_exception_encountered}. If |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1006 @code{XSTOPX} will not be called, then the @w{@code{F77_FCN}} macro should be |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1007 used instead to call the Fortran code. |
6569 | 1008 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1009 There is no great harm in using @w{@code{F77_XFCN}} in all cases, except that |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1010 for Fortran code that is short running and executes a large number of times, |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
1011 there is potentially an overhead in doing so. However, if @w{@code{F77_FCN}} |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1012 is used with code that calls @code{XSTOP}, Octave can generate a segmentation |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1013 fault. |
6569 | 1014 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1015 An example of the inclusion of a Fortran function in an oct-file is given in |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1016 the following example, where the C++ wrapper is |
6569 | 1017 |
9906 | 1018 @example |
18369
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1019 @EXAMPLEFILE(fortrandemo.cc) |
9906 | 1020 @end example |
6569 | 1021 |
6572 | 1022 @noindent |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
9906
diff
changeset
|
1023 and the Fortran function is |
6569 | 1024 |
9906 | 1025 @example |
18369
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1026 @EXAMPLEFILE(fortransub.f) |
9906 | 1027 @end example |
6569 | 1028 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1029 This example demonstrates most of the features needed to link to an external |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1030 Fortran function, including passing arrays and strings, as well as exception |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1031 handling. Both the Fortran and C++ files need to be compiled in order for the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1032 example to work. |
6569 | 1033 |
1034 @example | |
1035 @group | |
18369
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1036 mkoctfile fortrandemo.cc fortransub.f |
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1037 [b, s] = fortrandemo (1:3) |
6569 | 1038 @result{} |
1039 b = 1.00000 0.50000 0.33333 | |
1040 s = There are 3 values in the input vector | |
18369
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1041 [b, s] = fortrandemo (0:3) |
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1042 error: fortrandemo: fortransub: divide by zero |
6569 | 1043 @end group |
1044 @end example | |
1045 | |
1046 @node Allocating Local Memory in Oct-Files | |
1047 @subsection Allocating Local Memory in Oct-Files | |
1048 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1049 Allocating memory within an oct-file might seem easy, as the C++ new/delete |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1050 operators can be used. However, in that case great care must be taken to avoid |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1051 memory leaks. The preferred manner in which to allocate memory for use locally |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1052 is to use the @w{@code{OCTAVE_LOCAL_BUFFER}} macro. An example of its use is |
6569 | 1053 |
1054 @example | |
1055 OCTAVE_LOCAL_BUFFER (double, tmp, len) | |
1056 @end example | |
1057 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
1058 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1059 that returns a pointer @code{tmp} of type @code{double *} of length @code{len}. |
6569 | 1060 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1061 In this case, Octave itself will worry about reference counting and variable |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1062 scope and will properly free memory without programmer intervention. |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1063 |
6569 | 1064 @node Input Parameter Checking in Oct-Files |
1065 @subsection Input Parameter Checking in Oct-Files | |
1066 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1067 Because oct-files are compiled functions they open up the possibility of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1068 crashing Octave through careless function calls or memory faults. It is quite |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1069 important that each and every function have a sufficient level of parameter |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1070 checking to ensure that Octave behaves well. |
6580 | 1071 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1072 The minimum requirement, as previously discussed, is to check the number of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1073 input arguments before using them to avoid referencing a nonexistent argument. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1074 However, in some cases this might not be sufficient as the underlying code |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1075 imposes further constraints. For example, an external function call might be |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1076 undefined if the input arguments are not integers, or if one of the arguments |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1077 is zero, or if the input is complex and a real value was expected. Therefore, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1078 oct-files often need additional input parameter checking. |
6580 | 1079 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1080 There are several functions within Octave that can be useful for the purposes |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1081 of parameter checking. These include the methods of the @code{octave_value} |
24987
a895967fd5a7
doc: grammarcheck manual (*.txi) ahead of 4.4 release.
Rik <rik@octave.org>
parents:
24534
diff
changeset
|
1082 class like @code{is_real_matrix}, @code{is_numeric_type}, etc. (see |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1083 @file{ov.h}). Often, with a knowledge of the Octave m-file language, you can |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1084 guess at what the corresponding C++ routine will. In addition there are some |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1085 more specialized input validation functions of which a few are demonstrated |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1086 below. |
6580 | 1087 |
9906 | 1088 @example |
1089 @EXAMPLEFILE(paramdemo.cc) | |
1090 @end example | |
6580 | 1091 |
1092 @noindent | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
1093 An example of its use is: |
6580 | 1094 |
1095 @example | |
1096 @group | |
1097 paramdemo ([1, 2, NaN, Inf]) | |
1098 @result{} Properties of input array: | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1099 includes Inf or NaN values |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1100 includes other values than 1 and 0 |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1101 includes only int, Inf or NaN values |
6580 | 1102 @end group |
1103 @end example | |
6569 | 1104 |
1105 @node Exception and Error Handling in Oct-Files | |
1106 @subsection Exception and Error Handling in Oct-Files | |
1107 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1108 Another important feature of Octave is its ability to react to the user typing |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1109 @key{Control-C} during extended calculations. This ability is based on the C++ |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1110 exception handler, where memory allocated by the C++ new/delete methods is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1111 automatically released when the exception is treated. When writing an oct-file |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1112 which may run for a long time the programmer must periodically use the macro |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1113 @w{@code{OCTAVE_QUIT}}, in order to allow Octave to check and possibly respond |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1114 to a user typing @key{Control-C}. For example: |
6569 | 1115 |
1116 @example | |
1117 @group | |
6577 | 1118 for (octave_idx_type i = 0; i < a.nelem (); i++) |
1119 @{ | |
1120 OCTAVE_QUIT; | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1121 b.elem (i) = 2. * a.elem (i); |
6577 | 1122 @} |
6569 | 1123 @end group |
1124 @end example | |
1125 | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
1126 The presence of the @w{@code{OCTAVE_QUIT}} macro in the inner loop allows |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1127 Octave to detect and acknowledge a @key{Control-C} key sequence. Without this |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1128 macro, the user must either wait for the oct-file function to return before the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1129 interrupt is processed, or the user must press @key{Control-C} three times |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1130 which will force Octave to exit completely. |
6569 | 1131 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1132 The @w{@code{OCTAVE_QUIT}} macro does impose a very small performance penalty; |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1133 For loops that are known to be small it may not make sense to include |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
1134 @w{@code{OCTAVE_QUIT}}. |
6569 | 1135 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1136 When creating an oct-file that uses an external library, the function might |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1137 spend a significant portion of its time in the external library. It is not |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1138 generally possible to use the @w{@code{OCTAVE_QUIT}} macro in this case. The |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1139 alternative code in this case is |
6569 | 1140 |
1141 @example | |
1142 @group | |
6577 | 1143 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
1144 @dots{} some code that calls a "foreign" function @dots{} | |
1145 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
6569 | 1146 @end group |
1147 @end example | |
1148 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1149 The disadvantage of this is that if the foreign code allocates any memory |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1150 internally, then this memory might be lost during an interrupt, without being |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1151 deallocated. Therefore, ideally Octave itself should allocate any memory that |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1152 is needed by the foreign code, with either the @nospell{@code{fortran_vec}} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1153 method or the @w{@code{OCTAVE_LOCAL_BUFFER}} macro. |
6569 | 1154 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1155 The Octave @code{unwind_protect} mechanism (@ref{The unwind_protect Statement}) |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1156 can also be used in oct-files. In conjunction with the exception handling of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1157 Octave, it ensures that certain recovery code is always run even if an |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1158 exception occurs. An example of the use of this mechanism is |
6569 | 1159 |
9906 | 1160 @example |
1161 @EXAMPLEFILE(unwinddemo.cc) | |
1162 @end example | |
6569 | 1163 |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
1164 As can be seen in the example: |
6569 | 1165 |
1166 @example | |
1167 @group | |
6572 | 1168 unwinddemo (1, 0) |
6569 | 1169 @result{} Inf |
1170 1 / 0 | |
1171 @result{} warning: division by zero | |
6593 | 1172 Inf |
6569 | 1173 @end group |
1174 @end example | |
1175 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1176 The warning for division by zero (and in fact all warnings) are disabled in the |
6569 | 1177 @code{unwinddemo} function. |
1178 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1179 @node Documentation and Testing of Oct-Files |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1180 @subsection Documentation and Testing of Oct-Files |
6569 | 1181 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1182 The documentation for an oct-file is contained in the fourth string parameter |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1183 of the @w{@code{DEFUN_DLD}} macro. This string can be formatted in the same |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1184 manner as the help strings for user functions, however there are some issues |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1185 that are particular to the formatting of help strings within oct-files. |
6580 | 1186 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1187 The major issue is that the help string will typically be longer than a single |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1188 line of text, and so the formatting of long multi-line help strings needs to be |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1189 taken into account. There are several possible solutions, but the most common |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1190 is illustrated in the following example, |
6580 | 1191 |
1192 @example | |
1193 @group | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1194 DEFUN_DLD (do_what_i_want, args, nargout, |
6580 | 1195 "-*- texinfo -*-\n\ |
21319
8880d93010d8
Remove further uses of CLASS field in @deftypefn macro.
Rik <rik@octave.org>
parents:
21316
diff
changeset
|
1196 @@deftypefn @{@} @{@} do_what_i_say (@@var@{n@})\n\ |
7081 | 1197 A function that does what the user actually wants rather\n\ |
1198 than what they requested.\n\ | |
6580 | 1199 @@end deftypefn") |
1200 @{ | |
1201 @dots{} | |
1202 @} | |
1203 @end group | |
1204 @end example | |
1205 | |
1206 @noindent | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1207 where each line of text is terminated by @code{\n\} which is an embedded |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1208 newline in the string together with a C++ string continuation character. Note |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1209 that the final @code{\} must be the last character on the line. |
6580 | 1210 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1211 Octave also includes the ability to embed test and demonstration code for a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1212 function within the code itself (@pxref{Test and Demo Functions}). This can be |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1213 used from within oct-files (or in fact any file) with certain provisos. First, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1214 the test and demo functions of Octave look for @code{%!} as the first two |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1215 characters of a line to identify test and demonstration code. This is a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1216 requirement for oct-files as well. In addition, the test and demonstration |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1217 code must be wrapped in a comment block to avoid it being interpreted by the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1218 compiler. Finally, the Octave test and demonstration code must have access to |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1219 the original source code of the oct-file---not just the compiled code---as the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1220 tests are stripped from the compiled code. An example in an oct-file might be |
6580 | 1221 |
1222 @example | |
1223 @group | |
1224 /* | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1225 %!assert (sin ([1,2]), [sin(1),sin(2)]) |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1226 %!error (sin ()) |
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1227 %!error (sin (1,1)) |
6580 | 1228 */ |
1229 @end group | |
1230 @end example | |
6569 | 1231 |
6593 | 1232 @c @node Application Programming Interface for Oct-Files |
1233 @c @subsection Application Programming Interface for Oct-Files | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1234 @c |
6593 | 1235 @c WRITE ME, using Coda section 1.3 as a starting point. |
6569 | 1236 |
1237 @node Mex-Files | |
1238 @section Mex-Files | |
1239 @cindex mex-files | |
1240 @cindex mex | |
1241 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1242 Octave includes an interface to allow legacy mex-files to be compiled and used |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1243 with Octave. This interface can also be used to share compiled code between |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1244 Octave and @sc{matlab} users. However, as mex-files expose @sc{matlab}'s |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1245 internal API, and the internal structure of Octave is different, a mex-file can |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1246 never have the same performance in Octave as the equivalent oct-file. In |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1247 particular, to support the manner in which variables are passed to mex |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1248 functions there are a significant number of additional copies of memory blocks |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1249 when invoking or returning from a mex-file function. For this reason, it is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1250 recommended that any new code be written with the oct-file interface previously |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1251 discussed. |
6569 | 1252 |
1253 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1254 * Getting Started with Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1255 * Working with Matrices and Arrays in Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1256 * Character Strings in Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1257 * Cell Arrays with Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1258 * Structures with Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1259 * Sparse Matrices with Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1260 * Calling Other Functions in Mex-Files:: |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1261 @c * Application Programming Interface for Mex-Files:: |
6569 | 1262 @end menu |
1263 | |
1264 @node Getting Started with Mex-Files | |
1265 @subsection Getting Started with Mex-Files | |
1266 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1267 The basic command to build a mex-file is either @code{mkoctfile --mex} or |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1268 @code{mex}. The first command can be used either from within Octave or from |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1269 the command line. To avoid issues with @sc{matlab}'s own @code{mex} command, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1270 the use of the command @code{mex} is limited to within Octave. Compiled |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1271 mex-files have the extension @file{.mex}. |
6569 | 1272 |
1273 @DOCSTRING(mex) | |
1274 | |
1275 @DOCSTRING(mexext) | |
1276 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1277 Consider the following short example: |
6569 | 1278 |
9906 | 1279 @example |
1280 @group | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1281 @EXAMPLEFILE(myhello.c) |
9906 | 1282 @end group |
1283 @end example | |
6569 | 1284 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1285 The first line @code{#include "mex.h"} makes available all of the definitions |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1286 necessary for a mex-file. One important difference between Octave and |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
1287 @sc{matlab} is that the header file @qcode{"matrix.h"} is implicitly included |
22299
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
21630
diff
changeset
|
1288 through the inclusion of @qcode{"mex.h"}. This is necessary to avoid a |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
21630
diff
changeset
|
1289 conflict with the Octave file @qcode{"Matrix.h"} for operating systems and |
9fc91bb2aec3
doc: grammarcheck documentation for 4.2 release.
Rik <rik@octave.org>
parents:
21630
diff
changeset
|
1290 compilers that don't distinguish between filenames in upper and lower case. |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1291 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1292 The entry point into the mex-file is defined by @code{mexFunction}. The |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1293 function takes four arguments: |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1294 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1295 @enumerate 1 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1296 @item The number of return arguments (# of left-hand side args). |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1297 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1298 @item An array of pointers to return arguments. |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1299 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1300 @item The number of input arguments (# of right-hand side args). |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1301 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1302 @item An array of pointers to input arguments. |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1303 @end enumerate |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1304 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1305 Note that the function name definition is not explicitly included in |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1306 @code{mexFunction} and so there can only be a single @code{mexFunction} entry |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1307 point per file. Instead, the name of the function as seen in Octave is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1308 determined by the name of the mex-file itself minus the extension. If the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1309 above function is in the file @file{myhello.c}, it can be compiled with |
6580 | 1310 |
1311 @example | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1312 mkoctfile --mex myhello.c |
6580 | 1313 @end example |
1314 | |
1315 @noindent | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1316 which creates a file @file{myhello.mex}. The function can then be run from |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1317 Octave as |
6580 | 1318 |
1319 @example | |
1320 @group | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1321 myhello (1,2,3) |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1322 @result{} Hello, World! |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1323 @result{} I have 3 inputs and 0 outputs |
6580 | 1324 @end group |
1325 @end example | |
1326 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1327 It should be noted that the mex-file contains no help string. To document |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1328 mex-files, there should exist an m-file in the same directory as the mex-file |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1329 itself. Taking the above as an example, there would need to be a file |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1330 @file{myhello.m} which might contain the text |
6580 | 1331 |
1332 @example | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1333 %MYHELLO Simple test of the functionality of a mex-file. |
6580 | 1334 @end example |
1335 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1336 In this case, the function that will be executed within Octave will be given by |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1337 the mex-file, while the help string will come from the m-file. This can also |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1338 be useful to allow a sample implementation of the mex-file within the Octave |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1339 language itself for testing purposes. |
6580 | 1340 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1341 Although there cannot be multiple entry points in a single mex-file, one can |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1342 use the @code{mexFunctionName} function to determine what name the mex-file was |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1343 called with. This can be used to alter the behavior of the mex-file based on |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1344 the function name. For example, if |
6580 | 1345 |
9906 | 1346 @example |
1347 @group | |
1348 @EXAMPLEFILE(myfunc.c) | |
1349 @end group | |
1350 @end example | |
6580 | 1351 |
1352 @noindent | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1353 is in the file @file{myfunc.c}, and is compiled with |
6580 | 1354 |
1355 @example | |
1356 @group | |
1357 mkoctfile --mex myfunc.c | |
1358 ln -s myfunc.mex myfunc2.mex | |
1359 @end group | |
1360 @end example | |
1361 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1362 @noindent |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1363 then as can be seen by |
6580 | 1364 |
1365 @example | |
1366 @group | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1367 myfunc () |
6580 | 1368 @result{} You called function: myfunc |
1369 This is the principal function | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1370 myfunc2 () |
6580 | 1371 @result{} You called function: myfunc2 |
1372 @end group | |
1373 @end example | |
1374 | |
1375 @noindent | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1376 the behavior of the mex-file can be altered depending on the function's name. |
6580 | 1377 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1378 Although the user should only include @file{mex.h} in their code, Octave |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1379 declares additional functions, typedefs, etc., available to the user to write |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1380 mex-files in the headers @file{mexproto.h} and @file{mxarray.h}. |
6593 | 1381 |
6580 | 1382 @node Working with Matrices and Arrays in Mex-Files |
1383 @subsection Working with Matrices and Arrays in Mex-Files | |
1384 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1385 The basic mex type of all variables is @code{mxArray}. Any object, such as a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1386 matrix, cell array, or structure, is stored in this basic type. @code{mxArray} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1387 serves essentially the same purpose as the @code{octave_value} class in |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1388 oct-files in that it acts as a container for all the more specialized types. |
6580 | 1389 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1390 The @code{mxArray} structure contains at a minimum, the name of the variable it |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1391 represents, its dimensions, its type, and whether the variable is real or |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1392 complex. It can also contain a number of additional fields depending on the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1393 type of the @code{mxArray}. There are a number of functions to create |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1394 @code{mxArray} structures, including @code{mxCreateDoubleMatrix}, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1395 @code{mxCreateCellArray}, @code{mxCreateSparse}, and the generic |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1396 @code{mxCreateNumericArray}. |
6593 | 1397 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1398 The basic function to access the data in an array is @code{mxGetPr}. Because |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1399 the mex interface assumes that real and imaginary parts of a complex array are |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1400 stored separately, there is an equivalent function @code{mxGetPi} that gets the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1401 imaginary part. Both of these functions are only for use with double precision |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1402 matrices. The generic functions @code{mxGetData} and @code{mxGetImagData} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1403 perform the same operation for all matrix types. For example: |
6593 | 1404 |
1405 @example | |
1406 @group | |
1407 mxArray *m; | |
6686 | 1408 mwSize *dims; |
6593 | 1409 UINT32_T *pr; |
1410 | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1411 dims = (mwSize *) mxMalloc (2 * sizeof (mwSize)); |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1412 dims[0] = 2; dims[1] = 2; |
6593 | 1413 m = mxCreateNumericArray (2, dims, mxUINT32_CLASS, mxREAL); |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1414 pr = (UINT32_T *) mxGetData (m); |
6593 | 1415 @end group |
1416 @end example | |
1417 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1418 There are also the functions @code{mxSetPr}, etc., that perform the inverse, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1419 and set the data of an array to use the block of memory pointed to by the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1420 argument of @code{mxSetPr}. |
6593 | 1421 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1422 Note the type @code{mwSize} used above, and also @code{mwIndex}, are defined as |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1423 the native precision of the indexing in Octave on the platform on which the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1424 mex-file is built. This allows both 32- and 64-bit platforms to support |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1425 mex-files. @code{mwSize} is used to define array dimensions and the maximum |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1426 number or elements, while @code{mwIndex} is used to define indexing into |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1427 arrays. |
6686 | 1428 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1429 An example that demonstrates how to work with arbitrary real or complex double |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1430 precision arrays is given by the file @file{mypow2.c} shown below. |
6593 | 1431 |
9906 | 1432 @example |
1433 @EXAMPLEFILE(mypow2.c) | |
1434 @end example | |
6593 | 1435 |
1436 @noindent | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1437 An example of its use is |
6593 | 1438 |
1439 @example | |
1440 @group | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1441 b = randn (4,1) + 1i * randn (4,1); |
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1442 all (b.^2 == mypow2 (b)) |
6593 | 1443 @result{} 1 |
1444 @end group | |
1445 @end example | |
1446 | |
7096 | 1447 The example above uses the functions @code{mxGetDimensions}, |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1448 @code{mxGetNumberOfElements}, and @code{mxGetNumberOfDimensions} to work with |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1449 the dimensions of multi-dimensional arrays. The functions @code{mxGetM}, and |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1450 @code{mxGetN} are also available to find the number of rows and columns in a |
23567
71bfd507663c
doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents:
23565
diff
changeset
|
1451 2-D matrix (@nospell{MxN} matrix). |
6580 | 1452 |
1453 @node Character Strings in Mex-Files | |
1454 @subsection Character Strings in Mex-Files | |
1455 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1456 As mex-files do not make the distinction between single and double quoted |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1457 strings that Octave does, there is perhaps less complexity in the use of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1458 strings and character matrices. An example of their use that parallels the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1459 demo in @file{stringdemo.cc} is given in the file @file{mystring.c}, as shown |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1460 below. |
6593 | 1461 |
9906 | 1462 @example |
1463 @EXAMPLEFILE(mystring.c) | |
1464 @end example | |
6593 | 1465 |
1466 @noindent | |
1467 An example of its expected output is | |
1468 | |
1469 @example | |
1470 @group | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1471 mystring (["First String"; "Second String"]) |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1472 @result{} Second String |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1473 First String |
6593 | 1474 @end group |
1475 @end example | |
1476 | |
7096 | 1477 Other functions in the mex interface for handling character strings are |
1478 @code{mxCreateString}, @code{mxArrayToString}, and | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1479 @code{mxCreateCharMatrixFromStrings}. In a mex-file, a character string is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1480 considered to be a vector rather than a matrix. This is perhaps an arbitrary |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1481 distinction as the data in the @code{mxArray} for the matrix is consecutive in |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1482 any case. |
6580 | 1483 |
1484 @node Cell Arrays with Mex-Files | |
1485 @subsection Cell Arrays with Mex-Files | |
6569 | 1486 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1487 One can perform exactly the same operations on Cell arrays in mex-files as in |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1488 oct-files. An example that duplicates the function of the @file{celldemo.cc} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1489 oct-file in a mex-file is given by @file{mycell.c} as shown below. |
6593 | 1490 |
9906 | 1491 @example |
1492 @EXAMPLEFILE(mycell.c) | |
1493 @end example | |
6593 | 1494 |
1495 @noindent | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1496 The output is identical to the oct-file version as well. |
6593 | 1497 |
1498 @example | |
1499 @group | |
1500 [b1, b2, b3] = mycell (@{1, [1, 2], "test"@}) | |
1501 @result{} | |
1502 b1 = 1 | |
1503 b2 = | |
1504 | |
1505 1 2 | |
1506 | |
1507 b3 = test | |
1508 @end group | |
1509 @end example | |
1510 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1511 Note in the example the use of the @code{mxDuplicateArray} function. This is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1512 needed as the @code{mxArray} pointer returned by @code{mxGetCell} might be |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1513 deallocated. The inverse function to @code{mxGetCell}, used for setting Cell |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1514 values, is @code{mxSetCell} and is defined as |
6593 | 1515 |
1516 @example | |
1517 void mxSetCell (mxArray *ptr, int idx, mxArray *val); | |
1518 @end example | |
1519 | |
7007 | 1520 Finally, to create a cell array or matrix, the appropriate functions are |
6593 | 1521 |
1522 @example | |
1523 @group | |
1524 mxArray *mxCreateCellArray (int ndims, const int *dims); | |
1525 mxArray *mxCreateCellMatrix (int m, int n); | |
1526 @end group | |
1527 @end example | |
6569 | 1528 |
6572 | 1529 @node Structures with Mex-Files |
1530 @subsection Structures with Mex-Files | |
6569 | 1531 |
6593 | 1532 The basic function to create a structure in a mex-file is |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1533 @code{mxCreateStructMatrix} which creates a structure array with a two |
6593 | 1534 dimensional matrix, or @code{mxCreateStructArray}. |
1535 | |
1536 @example | |
1537 @group | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1538 mxArray *mxCreateStructArray (int ndims, int *dims, |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1539 int num_keys, |
6593 | 1540 const char **keys); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1541 mxArray *mxCreateStructMatrix (int rows, int cols, |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1542 int num_keys, |
6593 | 1543 const char **keys); |
1544 @end group | |
1545 @end example | |
1546 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1547 Accessing the fields of the structure can then be performed with |
6593 | 1548 @code{mxGetField} and @code{mxSetField} or alternatively with the |
1549 @code{mxGetFieldByNumber} and @code{mxSetFieldByNumber} functions. | |
1550 | |
1551 @example | |
1552 @group | |
7081 | 1553 mxArray *mxGetField (const mxArray *ptr, mwIndex index, |
1554 const char *key); | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1555 mxArray *mxGetFieldByNumber (const mxArray *ptr, |
6686 | 1556 mwIndex index, int key_num); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1557 void mxSetField (mxArray *ptr, mwIndex index, |
6593 | 1558 const char *key, mxArray *val); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1559 void mxSetFieldByNumber (mxArray *ptr, mwIndex index, |
6593 | 1560 int key_num, mxArray *val); |
1561 @end group | |
1562 @end example | |
1563 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1564 A difference between the oct-file interface to structures and the mex-file |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1565 version is that the functions to operate on structures in mex-files directly |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1566 include an @code{index} over the elements of the arrays of elements per |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1567 @code{field}; Whereas, the oct-file structure includes a Cell Array per field |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1568 of the structure. |
6593 | 1569 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1570 An example that demonstrates the use of structures in a mex-file can be found |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1571 in the file @file{mystruct.c} shown below. |
6580 | 1572 |
9906 | 1573 @example |
1574 @EXAMPLEFILE(mystruct.c) | |
1575 @end example | |
6580 | 1576 |
6593 | 1577 An example of the behavior of this function within Octave is then |
1578 | |
1579 @example | |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18797
diff
changeset
|
1580 @group |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1581 a(1).f1 = "f11"; a(1).f2 = "f12"; |
7081 | 1582 a(2).f1 = "f21"; a(2).f2 = "f22"; |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1583 b = mystruct (a); |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1584 @result{} field f1(0) = f11 |
6593 | 1585 field f1(1) = f21 |
1586 field f2(0) = f12 | |
1587 field f2(1) = f22 | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1588 b |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1589 @result{} 2x2 struct array containing the fields: |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
1590 |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1591 this |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1592 that |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
1593 |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1594 b(3) |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1595 @result{} scalar structure containing the fields: |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
1596 |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1597 this = this3 |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1598 that = that3 |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18797
diff
changeset
|
1599 @end group |
6593 | 1600 @end example |
6569 | 1601 |
1602 @node Sparse Matrices with Mex-Files | |
1603 @subsection Sparse Matrices with Mex-Files | |
1604 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1605 The Octave format for sparse matrices is identical to the mex format in that it |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1606 is a compressed column sparse format. Also, in both implementations sparse |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1607 matrices are required to be two-dimensional. The only difference of importance |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1608 to the programmer is that the real and imaginary parts of the matrix are stored |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1609 separately. |
6593 | 1610 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1611 The mex-file interface, in addition to using @code{mxGetM}, @code{mxGetN}, |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1612 @code{mxSetM}, @code{mxSetN}, @code{mxGetPr}, @code{mxGetPi}, @code{mxSetPr}, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1613 and @code{mxSetPi}, also supplies the following functions. |
6593 | 1614 |
1615 @example | |
1616 @group | |
6686 | 1617 mwIndex *mxGetIr (const mxArray *ptr); |
1618 mwIndex *mxGetJc (const mxArray *ptr); | |
1619 mwSize mxGetNzmax (const mxArray *ptr); | |
6593 | 1620 |
6686 | 1621 void mxSetIr (mxArray *ptr, mwIndex *ir); |
1622 void mxSetJc (mxArray *ptr, mwIndex *jc); | |
1623 void mxSetNzmax (mxArray *ptr, mwSize nzmax); | |
6593 | 1624 @end group |
1625 @end example | |
6580 | 1626 |
6593 | 1627 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1628 @code{mxGetNzmax} gets the maximum number of elements that can be stored in the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1629 sparse matrix. This is not necessarily the number of nonzero elements in the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1630 sparse matrix. @code{mxGetJc} returns an array with one additional value than |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1631 the number of columns in the sparse matrix. The difference between consecutive |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1632 values of the array returned by @code{mxGetJc} define the number of nonzero |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1633 elements in each column of the sparse matrix. Therefore, |
6580 | 1634 |
6593 | 1635 @example |
1636 @group | |
6686 | 1637 mwSize nz, n; |
1638 mwIndex *Jc; | |
6593 | 1639 mxArray *m; |
1640 @dots{} | |
1641 n = mxGetN (m); | |
1642 Jc = mxGetJc (m); | |
1643 nz = Jc[n]; | |
1644 @end group | |
1645 @end example | |
1646 | |
1647 @noindent | |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18797
diff
changeset
|
1648 returns the actual number of nonzero elements stored in the matrix in |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1649 @code{nz}. As the arrays returned by @code{mxGetPr} and @code{mxGetPi} only |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1650 contain the nonzero values of the matrix, we also need a pointer to the rows of |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1651 the nonzero elements, and this is given by @code{mxGetIr}. A complete example |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1652 of the use of sparse matrices in mex-files is given by the file |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1653 @file{mysparse.c} shown below. |
6593 | 1654 |
9906 | 1655 @example |
1656 @EXAMPLEFILE(mysparse.c) | |
1657 @end example | |
6569 | 1658 |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1659 A sample usage of @code{mysparse} is |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1660 |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1661 @example |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1662 @group |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1663 sm = sparse ([1, 0; 0, pi]); |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1664 mysparse (sm) |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1665 @result{} |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1666 Matrix is 2-by-2 real sparse matrix with 2 elements |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18797
diff
changeset
|
1667 last nonzero element (2, 2) = 3.14159 |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1668 @end group |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1669 @end example |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1670 |
6580 | 1671 @node Calling Other Functions in Mex-Files |
1672 @subsection Calling Other Functions in Mex-Files | |
1673 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1674 It is possible to call other Octave functions from within a mex-file using |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1675 @code{mexCallMATLAB}. An example of the use of @code{mexCallMATLAB} can be see |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1676 in the example below. |
6580 | 1677 |
9906 | 1678 @example |
1679 @EXAMPLEFILE(myfeval.c) | |
1680 @end example | |
6580 | 1681 |
1682 If this code is in the file @file{myfeval.c}, and is compiled to | |
1683 @file{myfeval.mex}, then an example of its use is | |
6569 | 1684 |
6580 | 1685 @example |
1686 @group | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1687 a = myfeval ("sin", 1) |
18367
d1e16bdb3958
myfeval.c: Fix segfault in mex example code.
Rik <rik@octave.org>
parents:
18358
diff
changeset
|
1688 @result{} Starting file myfeval.mex |
d1e16bdb3958
myfeval.c: Fix segfault in mex example code.
Rik <rik@octave.org>
parents:
18358
diff
changeset
|
1689 I have 2 inputs and 1 outputs |
d1e16bdb3958
myfeval.c: Fix segfault in mex example code.
Rik <rik@octave.org>
parents:
18358
diff
changeset
|
1690 I'm going to call the interpreter function sin |
d1e16bdb3958
myfeval.c: Fix segfault in mex example code.
Rik <rik@octave.org>
parents:
18358
diff
changeset
|
1691 a = 0.84147 |
6580 | 1692 @end group |
1693 @end example | |
1694 | |
28427
7a8c69c4eb55
convert obsolete octave_fcn_inline object to @inline class
John W. Eaton <jwe@octave.org>
parents:
27595
diff
changeset
|
1695 Note that it is not possible to use function handles within a mex-file. |
6580 | 1696 |
6593 | 1697 @c @node Application Programming Interface for Mex-Files |
1698 @c @subsection Application Programming Interface for Mex-Files | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1699 @c |
6593 | 1700 @c WRITE ME, refer to mex.h and mexproto.h |
6569 | 1701 |
1702 @node Standalone Programs | |
1703 @section Standalone Programs | |
1704 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1705 The libraries Octave uses itself can be utilized in standalone applications. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1706 These applications then have access, for example, to the array and matrix |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1707 classes, as well as to all of the Octave algorithms. The following C++ |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1708 program, uses class Matrix from @file{liboctave.a} or @file{liboctave.so}. |
6569 | 1709 |
9906 | 1710 @example |
1711 @EXAMPLEFILE(standalone.cc) | |
1712 @end example | |
6569 | 1713 |
6580 | 1714 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1715 mkoctfile can be used to build a standalone application with a command like |
6569 | 1716 |
1717 @example | |
1718 @group | |
8097
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1719 $ 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
|
1720 $ ./standalone |
6569 | 1721 Hello Octave world! |
1722 11 12 | |
1723 21 22 | |
1724 $ | |
1725 @end group | |
1726 @end example | |
1727 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1728 Note that the application @code{standalone} will be dynamically linked against |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1729 the Octave libraries and any Octave support libraries. The above allows the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1730 Octave math libraries to be used by an application. It does not, however, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1731 allow the script files, oct-files, or built-in functions of Octave to be used |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1732 by the application. To do that, the Octave interpreter needs to be initialized |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1733 first. An example of how to do this can then be seen in the code |
8097
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1734 |
9906 | 1735 @example |
1736 @EXAMPLEFILE(embedded.cc) | |
1737 @end example | |
8097
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1738 |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1739 @noindent |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1740 which, as before, is compiled and run as a standalone application with |
8097
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1741 |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1742 @example |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1743 @group |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1744 $ 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
|
1745 $ ./embedded |
16483
fcf1b0b52083
Use a better example of gcd() in embedded.cc
Rik <rik@octave.org>
parents:
16482
diff
changeset
|
1746 GCD of [10, 15] is 5 |
8097
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1747 $ |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1748 @end group |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1749 @end example |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1750 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1751 It is worth re-iterating that, if only built-in functions are to be called from |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1752 a C++ standalone program then it does not need to initialize the interpreter. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1753 The general rule is that for a built-in function named @code{function_name} in |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1754 the interpreter, there will be a C++ function named @code{Ffunction_name} (note |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1755 the prepended capital @code{F}) accessible in the C++ API@. The declarations |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1756 for all built-in functions are collected in the header file |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1757 @code{builtin-defun-decls.h}. This feature should be used with care as the |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1758 list of built-in functions can change. No guarantees can be made that a |
28713
28d2511f2af2
maint: grammarcheck documentation ahead of 6.1 release.
Rik <rik@octave.org>
parents:
28427
diff
changeset
|
1759 function that is currently a built-in won't be implemented as a @file{.m} file |
28d2511f2af2
maint: grammarcheck documentation ahead of 6.1 release.
Rik <rik@octave.org>
parents:
28427
diff
changeset
|
1760 or as a dynamically linked function in the future. An example of how to call |
28d2511f2af2
maint: grammarcheck documentation ahead of 6.1 release.
Rik <rik@octave.org>
parents:
28427
diff
changeset
|
1761 built-in functions from C++ can be seen in the code |
17949
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1762 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1763 @example |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1764 @EXAMPLEFILE(standalonebuiltin.cc) |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1765 @end example |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1766 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1767 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1768 which is compiled and run as a standalone application with |
17949
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1769 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1770 @example |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1771 @group |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1772 $ mkoctfile --link-stand-alone standalonebuiltin.cc -o standalonebuiltin |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1773 $ ./standalonebuiltin |
17949
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1774 This is a matrix: |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1775 11 12 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1776 21 22 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1777 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1778 This is the norm of the matrix: |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1779 34.4952 |
18367
d1e16bdb3958
myfeval.c: Fix segfault in mex example code.
Rik <rik@octave.org>
parents:
18358
diff
changeset
|
1780 $ |
17949
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1781 @end group |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1782 @end example |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1783 |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1784 @node Java Interface |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1785 @section Java Interface |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1786 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1787 @cindex using Octave with Java |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1788 @cindex Java, using with Octave |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1789 @cindex calling Java from Octave |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1790 @cindex Java, calling from Octave |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1791 @cindex calling Octave from Java |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1792 @cindex Octave, calling from Java |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1793 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1794 The Java Interface is designed for calling Java functions from within Octave. |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1795 If you want to do the reverse, and call Octave from within Java, try a library |
25143
13fd0610480f
doc: Use https whenever possible in @url entries.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
1796 like @code{joPas} (@url{http://jopas.sourceforge.net}). |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1797 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1798 @menu |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1799 * Making Java Classes Available:: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1800 * How to use Java from within Octave:: |
25816
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
1801 * Set up the JVM:: |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1802 * Java Interface Functions:: |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1803 @end menu |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1804 |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1805 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1806 @node Making Java Classes Available |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1807 @subsection Making Java Classes Available |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1808 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1809 @c - index - |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1810 @cindex classpath, setting |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1811 @cindex classpath, difference between static and dynamic |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1812 @cindex static classpath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1813 @cindex dynamic classpath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1814 @cindex @file{javaclasspath.txt} |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1815 @cindex @file{classpath.txt} |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1816 @cindex classes, making available to Octave |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1817 @c - index - |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1818 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1819 Java finds classes by searching a @var{classpath} which is a list of Java |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1820 archive files and/or directories containing class files. In Octave the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1821 @var{classpath} is composed of two parts: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1822 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1823 @itemize |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1824 @item the @var{static classpath} is initialized once at startup of the JVM, and |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1825 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1826 @item the @var{dynamic classpath} which can be modified at runtime. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1827 @end itemize |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1828 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1829 Octave searches the @var{static classpath} first, and then the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1830 @var{dynamic classpath}. Classes appearing in the @var{static classpath}, as |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1831 well as in the @var{dynamic classpath}, will therefore be found in the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1832 @var{static classpath} and loaded from this location. Classes which will be |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1833 used frequently, or must be available to all users, should be added to the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1834 @var{static classpath}. The @var{static classpath} is populated once from the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1835 contents of a plain text file named @file{javaclasspath.txt} (or |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1836 @file{classpath.txt} historically) when the Java Virtual Machine starts. This |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1837 file contains one line for each individual classpath to be added to the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1838 @var{static classpath}. These lines can identify directories containing class |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1839 files, or Java archives with complete class file hierarchies. Comment lines |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1840 starting with a @samp{#} or a @samp{%} character are ignored. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1841 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1842 The search rules for the file @file{javaclasspath.txt} (or |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1843 @file{classpath.txt}) are: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1844 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1845 @itemize |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1846 @item |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1847 First, Octave tries to locate it in the current directory (where Octave was |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1848 started from). If such a file is found, it is read and defines the initial |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1849 @var{static classpath}. Thus, it is possible to define a static classpath on a |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1850 'per Octave invocation' basis. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1851 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1852 @item |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1853 Next, Octave searches in the user's home directory. If a file |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1854 @file{javaclasspath.txt} exists here, its contents are appended to the static |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1855 classpath (if any). Thus, it is possible to build an initial static classpath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1856 on a @nospell{'per user'} basis. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1857 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1858 @item |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1859 Finally, Octave looks for a @file{javaclasspath.txt} in the m-file directory |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1860 where Octave Java functions live. This is where the function |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1861 @file{javaclasspath.m} resides, usually something like |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1862 @file{@w{@env{OCTAVE_HOME}}/share/octave/@w{@env{OCTAVE_VERSION}}/m/java/}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1863 You can find this directory by executing the command |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1864 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1865 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1866 which javaclasspath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1867 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1868 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1869 If this file exists here, its contents are also appended to the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1870 @var{static classpath}. Note that the archives and class directories defined |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1871 in this last step will affect all users. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1872 @end itemize |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1873 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1874 Classes which are used only by a specific script should be placed in the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1875 @var{dynamic classpath}. This portion of the classpath can be modified at |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1876 runtime using the @code{javaaddpath} and @code{javarmpath} functions. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1877 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1878 Example: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1879 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1880 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1881 octave> base_path = "C:/Octave/java_files"; |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1882 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1883 octave> # add two JAR archives to the dynamic classpath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1884 octave> javaaddpath ([base_path, "/someclasses.jar"]); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1885 octave> javaaddpath ([base_path, "/moreclasses.jar"]); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1886 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1887 octave> # check the dynamic classpath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1888 octave> p = javaclasspath; |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1889 octave> disp (p@{1@}); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1890 C:/Octave/java_files/someclasses.jar |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1891 octave> disp (p@{2@}); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1892 C:/Octave/java_files/moreclasses.jar |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1893 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1894 octave> # remove the first element from the classpath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1895 octave> javarmpath ([base_path, "/someclasses.jar"]); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1896 octave> p = javaclasspath; |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1897 octave> disp (p@{1@}); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1898 C:/Octave/java_files/moreclasses.jar |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1899 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1900 octave> # provoke an error |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1901 octave> disp (p@{2@}); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1902 error: A(I): Index exceeds matrix dimension. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1903 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1904 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1905 Another way to add files to the @var{dynamic classpath} exclusively for your |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1906 user account is to use the file @file{.octaverc} which is stored in your home |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1907 directory. All Octave commands in this file are executed each time you start a |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1908 new instance of Octave. The following example adds the directory @file{octave} |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1909 to Octave's search path and the archive @file{myclasses.jar} in this directory |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1910 to the Java search path. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1911 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1912 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1913 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1914 # contents of .octaverc: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1915 addpath ("~/octave"); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1916 javaaddpath ("~/octave/myclasses.jar"); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1917 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1918 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1919 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1920 @c ------------------------------------------------------------------------ |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1921 @node How to use Java from within Octave |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1922 @subsection How to use Java from within Octave |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1923 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1924 The function @ref{XREFjavaObject,javaObject,javaObject} creates Java objects. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1925 In fact it invokes the public constructor of the class with the given name |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1926 and with the given parameters. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1927 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1928 The following example shows how to invoke the constructors |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1929 @code{BigDecimal(double)} and @code{BigDecimal(String)} of the builtin Java |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1930 class @code{java.math.BigDecimal}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1931 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1932 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1933 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1934 javaObject ("java.math.BigDecimal", 1.001 ); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1935 javaObject ("java.math.BigDecimal", "1.001"); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1936 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1937 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1938 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1939 Note that parameters of the Octave type @code{double} are implicitly converted |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1940 into the Java type @code{double} and the Octave type (array of) @code{char} is |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1941 converted into the java type @code{String}. A Java object created by |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1942 @ref{XREFjavaObject,javaObject,javaObject} is never automatically converted |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1943 into an Octave type but remains a Java object. It can be assigned to an |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1944 Octave variable. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1945 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1946 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1947 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1948 a = 1.001; |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1949 b = javaObject ("java.math.BigDecimal", a); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1950 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1951 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1952 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1953 Using @ref{XREFisjava,isjava,isjava}, it is possible to check whether a |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1954 variable is a Java object and its class can be determined as well. In |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1955 addition to the previous example: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1956 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1957 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1958 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1959 isjava (a) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1960 @result{} ans = 0 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1961 class (a) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1962 @result{} ans = double |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1963 isjava (b) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1964 @result{} ans = 1 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1965 class (b) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1966 @result{} ans = java.math.BigDecimal |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1967 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1968 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1969 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1970 The example above can be carried out using only Java objects: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1971 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1972 @example |
23565
3a730821e4a2
doc: Peridoc grammarcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
1973 @group |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1974 a = javaObject ("java.lang.Double", 1.001); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1975 b = javaObject ("java.math.BigDecimal", a); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1976 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1977 isjava (a) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1978 @result{} ans = 1 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1979 class (a) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1980 @result{} ans = java.lang.Double |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1981 isjava (b) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1982 @result{} ans = 1 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1983 class (b) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1984 @result{} ans = java.math.BigDecimal |
23565
3a730821e4a2
doc: Peridoc grammarcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
1985 @end group |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1986 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1987 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1988 One can see, that even a @code{java.lang.Double} is not converted to an Octave |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1989 @code{double}, when created by @ref{XREFjavaObject,javaObject,javaObject}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1990 But ambiguities might arise, if the Java classes @code{java.lang.Double} or |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1991 @code{double} are parameters of a method (or a constructor). In this case |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1992 they can be converted into one another, depending on the context. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1993 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1994 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1995 Via @ref{XREFjavaObject,javaObject,javaObject} one may create all kinds of |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1996 Java objects but arrays. The latter are created through |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1997 @ref{XREFjavaArray,javaArray,javaArray}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1998 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1999 It is possible to invoke public member methods on Java objects in Java syntax: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2000 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2001 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2002 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2003 a.toString |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2004 @result{} ans = 1.001 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2005 b.toString |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2006 @result{} ans = 1.000999999999999889865... |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2007 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2008 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2009 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2010 The second result may be surprising, but simply comes from the fact, that |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2011 @code{1.001} cannot exactly be represented as @code{double}, due to rounding. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2012 Note that unlike in Java, in Octave methods without arguments can be invoked |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2013 with and without parentheses @code{()}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2014 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2015 Currently it is not possible to invoke static methods with a Java like syntax |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2016 from within Octave. Instead, one has to use the function |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2017 @ref{XREFjavaMethod,javaMethod,javaMethod} as in the following example: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2018 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2019 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2020 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2021 java.math.BigDecimal.valueOf(1.001); # does not work |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2022 javaMethod ("valueOf", "java.math.BigDecimal", 1.001); # workaround |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2023 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2024 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2025 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2026 As mentioned before, method and constructor parameters are converted |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2027 automatically between Octave and Java types, if appropriate. For functions |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2028 this is also true with return values, whereas for constructors this is not. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2029 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2030 It is also possible to access public fields of Java objects from within Octave |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2031 using Java syntax, with the limitation of static fields: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2032 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2033 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2034 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2035 java.math.BigDecimal.ONE; # does not work |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2036 java_get ("java.math.BigDecimal", "ONE"); # workaround |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2037 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2038 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2039 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2040 Accordingly, with @ref{XREFjava_set,java_set,java_set} the value of a field |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2041 can be set. Note that only public Java fields are accessible from within |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2042 Octave. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2043 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2044 The following example indicates that in Octave empty brackets @code{[]} |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2045 represent Java's @code{null} value and how Java exceptions are represented. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2046 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2047 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2048 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2049 javaObject ("java.math.BigDecimal", []); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2050 @result{} error: [java] java.lang.NullPointerException |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2051 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2052 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2053 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2054 It is not recommended to represent Java's @code{null} value by empty brackets |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2055 @code{[]}, because @code{null} has no type whereas @code{[]} has type |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2056 @code{double}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2057 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2058 In Octave it is possible to provide limited Java reflection by listing the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2059 public fields and methods of a Java object, both static or not. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2060 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2061 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2062 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2063 fieldnames (<Java object>) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2064 methods (<Java object>) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2065 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2066 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2067 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2068 Finally, an examples is shown how to access the stack trace from within |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2069 Octave, where the function @ref{XREFdebug_java,debug_java,debug_java} is used |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2070 to set and to get the current debug state. In debug mode, the Java error and |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2071 the stack trace are displayed. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2072 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2073 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2074 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2075 debug_java (true) # use "false" to omit display of stack trace |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2076 debug_java () |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2077 @result{} ans = 1 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2078 javaObject ("java.math.BigDecimal", "1") ... |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2079 .divide (javaObject ("java.math.BigDecimal", "0")) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2080 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2081 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2082 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2083 |
25816
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2084 @node Set up the JVM |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2085 @subsection Set up the JVM |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2086 @cindex memory, limitations on JVM |
26048 | 2087 @cindex select JVM version |
25816
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2088 |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2089 In order to execute Java code Octave creates a Java Virtual Machine (JVM). By |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2090 default the version of the JVM is used that was detected during configuration |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2091 on Unix-like systems or that is pointed to from the registry keys at |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2092 @file{HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\JRE} or |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2093 @file{HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment} on |
26171
00b3e20eb3bc
doc: Change "overwritten" to "overridden" in Java docs (bug #40111).
Rik <rik@octave.org>
parents:
26048
diff
changeset
|
2094 Windows. The default path to the JVM can be overridden by setting the |
25816
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2095 environment variable @w{@env{JAVA_HOME}} to the path where the JVM is |
26047
1357b79f87bc
doc: grammar check txi files in doc/interpreter/ directory.
Rik <rik@octave.org>
parents:
25816
diff
changeset
|
2096 installed. On Windows that might be, for example, |
1357b79f87bc
doc: grammar check txi files in doc/interpreter/ directory.
Rik <rik@octave.org>
parents:
25816
diff
changeset
|
2097 @file{C:\Program Files\Java\jre-10.0.2}. Make sure that you select a directory |
26048 | 2098 that contains the JVM with a @nospell{bit-ness} that matches Octave's. |
25816
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2099 |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2100 The JVM is only loaded once per Octave session. Thus, to change the used |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2101 version of the JVM, you might have to re-start Octave. To check which version |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2102 of the JVM is currently being used, run @code{version -java}. |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2103 |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2104 The JVM allocates a fixed amount of initial memory and may expand this pool up |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2105 to a fixed maximum memory limit. The default values depend on the Java version |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2106 (@pxref{XREFjavamem,,javamem}). The memory pool is shared by all Java objects |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2107 running in the JVM@. This strict memory limit is intended mainly to avoid |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2108 runaway applications inside web browsers or in enterprise servers which can |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2109 consume all memory and crash the system. When the maximum memory limit is hit, |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2110 Java code will throw exceptions so that applications will fail or behave |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2111 unexpectedly. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2112 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2113 You can specify options for the creation of the JVM inside a file named |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2114 @file{java.opts}. This is a text file where enter you enter lines containing |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2115 @option{-X} and @option{-D} options that are then passed to the JVM during |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2116 initialization. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2117 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2118 The directory where the Java options file is located is specified by the |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2119 environment variable @w{@env{OCTAVE_JAVA_DIR}}. If unset the directory where |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2120 @file{javaclasspath.m} resides is used instead (typically |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2121 @file{@w{@env{OCTAVE_HOME}}/share/octave/@w{@env{OCTAVE_VERSION}}/m/java/}). |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2122 You can find this directory by executing |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2123 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2124 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2125 which javaclasspath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2126 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2127 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2128 The @option{-X} options allow you to increase the maximum amount of memory |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2129 available to the JVM@. The following example allows up to 256 Megabytes to be |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2130 used by adding the following line to the @file{java.opts} file: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2131 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2132 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2133 -Xmx256m |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2134 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2135 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2136 The maximum possible amount of memory depends on your system. On a Windows |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2137 system with 2 Gigabytes main memory you should be able to set this maximum to |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2138 about 1 Gigabyte. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2139 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2140 If your application requires a large amount of memory from the beginning, you |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2141 can also specify the initial amount of memory allocated to the JVM@. Adding |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2142 the following line to the @file{java.opts} file starts the JVM with 64 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2143 Megabytes of initial memory: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2144 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2145 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2146 -Xms64m |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2147 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2148 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2149 For more details on the available @option{-X} options of your Java Virtual |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2150 Machine issue the command @samp{java -X} at the operating system command prompt |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2151 and consult the Java documentation. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2152 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2153 The @option{-D} options can be used to define system properties which can then |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2154 be used by Java classes inside Octave. System properties can be retrieved by |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2155 using the @code{getProperty()} methods of the @code{java.lang.System} class. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2156 The following example line defines the property @var{MyProperty} and assigns it |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2157 the string @code{12.34}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2158 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2159 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2160 -DMyProperty=12.34 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2161 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2162 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2163 The value of this property can then be retrieved as a string by a Java object |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2164 or in Octave: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2165 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2166 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2167 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2168 octave> javaMethod ("getProperty", "java.lang.System", "MyProperty"); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2169 ans = 12.34 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2170 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2171 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2172 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2173 @seealso{javamem} |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2174 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2175 |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2176 @node Java Interface Functions |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2177 @subsection Java Interface Functions |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2178 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2179 The following functions are the core of the Java Interface. They provide a way |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2180 to create a Java object, get and set its data fields, and call Java methods |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2181 which return results to Octave. |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2182 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2183 @cindex object, creating a Java object |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2184 @cindex instance, creating a Java instance |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2185 @DOCSTRING(javaObject) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2186 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2187 @cindex array, creating a Java array |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2188 @DOCSTRING(javaArray) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2189 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2190 There are many different variable types in Octave, but only ones created |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2191 through @code{javaObject} can use Java functions. Before using Java with an |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2192 unknown object the type can be checked with @code{isjava}. |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2193 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2194 @DOCSTRING(isjava) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2195 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2196 Once an object has been created it is natural to find out what fields the |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2197 object has, and to read (get) and write (set) them. |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2198 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2199 @cindex fields, displaying available fields of a Java object |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2200 In Octave the @code{fieldnames} function for structures has been overloaded |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2201 to return the fields of a Java object. For example: |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2202 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2203 @example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2204 @group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2205 dobj = javaObject ("java.lang.Double", pi); |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2206 fieldnames (dobj) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2207 @result{} |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2208 @{ |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2209 [1,1] = public static final double java.lang.Double.POSITIVE_INFINITY |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2210 [1,2] = public static final double java.lang.Double.NEGATIVE_INFINITY |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2211 [1,3] = public static final double java.lang.Double.NaN |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2212 [1,4] = public static final double java.lang.Double.MAX_VALUE |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2213 [1,5] = public static final double java.lang.Double.MIN_NORMAL |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2214 [1,6] = public static final double java.lang.Double.MIN_VALUE |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2215 [1,7] = public static final int java.lang.Double.MAX_EXPONENT |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2216 [1,8] = public static final int java.lang.Double.MIN_EXPONENT |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2217 [1,9] = public static final int java.lang.Double.SIZE |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2218 [1,10] = public static final java.lang.Class java.lang.Double.TYPE |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2219 @} |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2220 @end group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2221 @end example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2222 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2223 @cindex field, returning value of Java object field |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2224 The analogy of objects with structures is carried over into reading and writing |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2225 object fields. To read a field the object is indexed with the @samp{.} |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2226 operator from structures. This is the preferred method for reading fields, but |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2227 Octave also provides a function interface to read fields with @code{java_get}. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2228 An example of both styles is shown below. |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2229 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2230 @example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2231 @group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2232 dobj = javaObject ("java.lang.Double", pi); |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2233 dobj.MAX_VALUE |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2234 @result{} 1.7977e+308 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2235 java_get ("java.lang.Float", "MAX_VALUE") |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2236 @result{} 3.4028e+38 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2237 @end group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2238 @end example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2239 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2240 @DOCSTRING(java_get) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2241 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2242 @cindex field, setting value of Java object field |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2243 @DOCSTRING(java_set) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2244 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2245 @cindex methods, displaying available methods of a Java object |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2246 To see what functions can be called with an object use @code{methods}. For |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2247 example, using the previously created @var{dobj}: |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2248 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2249 @example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2250 @group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2251 methods (dobj) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2252 @result{} |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2253 Methods for class java.lang.Double: |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2254 boolean equals(java.lang.Object) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2255 java.lang.String toString(double) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2256 java.lang.String toString() |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2257 @dots{} |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2258 @end group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2259 @end example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2260 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2261 To call a method of an object the same structure indexing operator @samp{.} is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2262 used. Octave also provides a functional interface to calling the methods of an |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2263 object through @code{javaMethod}. An example showing both styles is shown |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2264 below. |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2265 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2266 @example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2267 @group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2268 dobj = javaObject ("java.lang.Double", pi); |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2269 dobj.equals (3) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2270 @result{} 0 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2271 javaMethod ("equals", dobj, pi) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2272 @result{} 1 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2273 @end group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2274 @end example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2275 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2276 @cindex method, invoking a method of a Java object |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2277 @DOCSTRING(javaMethod) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2278 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2279 The following three functions are used to display and modify the class path |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2280 used by the Java Virtual Machine. This is entirely separate from Octave's |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2281 @env{PATH} variable and is used by the JVM to find the correct code to execute. |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2282 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2283 @cindex classpath, displaying |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2284 @cindex classpath, dynamic |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2285 @cindex dynamic classpath |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2286 @cindex classpath, static |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2287 @cindex static classpath |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2288 @DOCSTRING(javaclasspath) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2289 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2290 @findex javaaddpath |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2291 @cindex classpath, adding new path |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2292 @cindex path, adding to classpath |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2293 @cindex classpath, dynamic |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2294 @cindex dynamic classpath, adding new path |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2295 @DOCSTRING(javaaddpath) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2296 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2297 @cindex classpath, removing path |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2298 @cindex path, removing from classpath |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2299 @DOCSTRING(javarmpath) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2300 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2301 The following functions provide information and control over the interface |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2302 between Octave and the Java Virtual Machine. |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2303 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2304 @DOCSTRING(javachk) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2305 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2306 @DOCSTRING(usejava) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2307 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2308 @cindex memory, displaying Java memory status |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2309 @DOCSTRING(javamem) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2310 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2311 @DOCSTRING(java_matrix_autoconversion) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2312 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2313 @DOCSTRING(java_unsigned_autoconversion) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2314 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2315 @DOCSTRING(debug_java) |