Mercurial > octave
annotate doc/interpreter/external.txi @ 26171:00b3e20eb3bc
doc: Change "overwritten" to "overridden" in Java docs (bug #40111).
* external.txi: Change "overwritten" to "overridden".
author | Rik <rik@octave.org> |
---|---|
date | Wed, 05 Dec 2018 10:39:45 -0800 |
parents | d02e4b1df697 |
children | 00f796120a6d |
rev | line source |
---|---|
25054
6652d3823428
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
24987
diff
changeset
|
1 @c Copyright (C) 2007-2018 John W. Eaton and David Bateman |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2 @c Copyright (C) 2010 Martin Hepperle |
7018 | 3 @c Copyright (C) 2007 Paul Thomas and Christoph Spiel |
4 @c | |
5 @c This file is part of Octave. | |
6 @c | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23567
diff
changeset
|
7 @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
|
8 @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
|
9 @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
|
10 @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
|
11 @c |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22482
diff
changeset
|
12 @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
|
13 @c WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22482
diff
changeset
|
14 @c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22482
diff
changeset
|
15 @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
|
16 @c |
7018 | 17 @c You should have received a copy of the GNU General Public License |
18 @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
|
19 @c <https://www.gnu.org/licenses/>. |
6578 | 20 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
21 @node External Code Interface |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
22 @appendix External Code Interface |
6569 | 23 @cindex dynamic-linking |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
24 @cindex Dynamically Linked Functions |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
25 @cindex Octave API |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
26 |
18953
bcbd309bf272
doc: Fix quote character at beginning of External Code Interfaces chapter.
Rik <rik@octave.org>
parents:
18797
diff
changeset
|
27 "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
|
28 |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
29 --- Ezra Pound |
6569 | 30 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 don't do that because @code{regexp} already exists in Octave). |
6569 | 39 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
52 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
|
53 writing code outside of Octave. |
6569 | 54 |
55 @itemize @bullet | |
56 @item | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
57 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
|
58 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
59 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
|
60 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
|
61 the outside code. |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
62 |
6569 | 63 @item |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
64 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
|
65 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
66 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
|
67 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
|
68 (@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
|
69 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
|
70 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
|
71 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
|
72 |
6569 | 73 @item |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
74 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
|
75 routines? |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
76 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
77 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
|
78 interpreted. |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
79 |
6569 | 80 @item |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
81 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
|
82 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
|
83 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
84 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
|
85 inevitably be issues with tools such as compilers. |
6569 | 86 @end itemize |
87 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 develop. |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
94 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
101 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
|
102 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
|
103 to hardware resources which often have device drivers written in C. |
6569 | 104 |
105 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
106 * Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
107 * Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
108 * Standalone Programs:: |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
109 * Java Interface:: |
6569 | 110 @end menu |
111 | |
112 @node Oct-Files | |
113 @section Oct-Files | |
114 @cindex oct-files | |
115 @cindex mkoctfile | |
116 @cindex oct | |
117 | |
118 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
119 * Getting Started with Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
120 * Matrices and Arrays in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
121 * Character Strings in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
122 * Cell Arrays in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
123 * Structures in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
124 * Sparse Matrices in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
125 * Accessing Global Variables in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
126 * Calling Octave Functions from Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
127 * Calling External Code from Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
128 * Allocating Local Memory in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
129 * Input Parameter Checking in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
130 * 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
|
131 * 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
|
132 @c * Application Programming Interface for Oct-Files:: |
6569 | 133 @end menu |
134 | |
135 @node Getting Started with Oct-Files | |
136 @subsection Getting Started with Oct-Files | |
137 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
138 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
|
139 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
|
140 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
|
141 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 command is available from within Octave or at the shell command line. |
6569 | 146 |
147 @DOCSTRING(mkoctfile) | |
148 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
149 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
|
150 C++ function that can be linked to Octave. |
6569 | 151 |
9906 | 152 @example |
153 @group | |
154 @EXAMPLEFILE(helloworld.cc) | |
155 @end group | |
156 @end example | |
6569 | 157 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
158 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
|
159 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
|
160 @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
|
161 in a C source file, nor any other language. |
6569 | 162 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
163 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
|
164 which creates a dynamically loaded function. This macro takes four arguments: |
6569 | 165 |
166 @enumerate 1 | |
6571 | 167 @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
|
168 |
6572 | 169 @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
|
170 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
171 @item The number of output arguments, which can be---and often is---omitted if |
6569 | 172 not used, and |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
173 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
174 @item The string to use for the help text of the function. |
6569 | 175 @end enumerate |
176 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
177 The return type of functions defined with @w{@code{DEFUN_DLD}} is always |
6572 | 178 @code{octave_value_list}. |
6569 | 179 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
180 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
|
181 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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 @file{helloworld.cc}, and would be compiled to an oct-file using the command |
6569 | 187 |
188 @example | |
189 mkoctfile helloworld.cc | |
190 @end example | |
191 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
192 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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 @code{autoload} (@ref{Function Files}) function should be used. |
6569 | 198 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
199 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
|
200 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
|
201 compiling this function as above, an example of its use is |
6569 | 202 |
203 @example | |
204 @group | |
6572 | 205 helloworld (1, 2, 3) |
206 @print{} Hello World has 3 input arguments and 0 output arguments. | |
6569 | 207 @end group |
208 @end example | |
209 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
210 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 class. |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
216 |
6569 | 217 @node Matrices and Arrays in Oct-Files |
218 @subsection Matrices and Arrays in Oct-Files | |
219 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
220 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
|
221 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
|
222 types discussed separately below. There are three basic matrix types: |
6569 | 223 |
6572 | 224 @table @code |
6569 | 225 @item Matrix |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
226 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
|
227 |
6569 | 228 @item ComplexMatrix |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
229 A complex matrix class defined in @file{CMatrix.h} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
230 |
6569 | 231 @item BoolMatrix |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
232 A boolean matrix class defined in @file{boolMatrix.h} |
6569 | 233 @end table |
234 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
235 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
|
236 are a number of multi-dimensional array types including |
6569 | 237 |
6572 | 238 @table @code |
6569 | 239 @item NDArray |
6572 | 240 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
|
241 |
6569 | 242 @item ComplexNDarray |
6572 | 243 A complex array class defined in @file{CNDArray.h} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
244 |
6569 | 245 @item boolNDArray |
6572 | 246 A boolean array class defined in @file{boolNDArray.h} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
247 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
248 @item int8NDArray |
6572 | 249 @itemx int16NDArray |
250 @itemx int32NDArray | |
251 @itemx int64NDArray | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
252 8, 16, 32, and 64-bit signed array classes defined in |
6572 | 253 @file{int8NDArray.h}, @file{int16NDArray.h}, etc. |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
254 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
255 @item uint8NDArray |
6572 | 256 @itemx uint16NDArray |
257 @itemx uint32NDArray | |
258 @itemx uint64NDArray | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
259 8, 16, 32, and 64-bit unsigned array classes defined in |
6572 | 260 @file{uint8NDArray.h}, @file{uint16NDArray.h}, etc. |
6569 | 261 @end table |
262 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
263 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
|
264 arrays. Using the class @code{Matrix} as an example one can |
6569 | 265 |
266 @itemize @bullet | |
6571 | 267 @item |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
268 Create an empty matrix or array with the empty constructor. For example: |
6569 | 269 |
270 @example | |
271 Matrix a; | |
272 @end example | |
273 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
274 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
|
275 |
6571 | 276 @item |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
277 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
|
278 same characteristics as the vector returned from @code{size}. For example: |
6569 | 279 |
280 @example | |
281 @group | |
20187
998881b6cdef
external.txi: Fix example case for dim_vector (bug #45100).
Piotr Held <pjheld@gmail.com>
parents:
19721
diff
changeset
|
282 dim_vector dv (2, 3); // 2 rows, 3 columns |
6572 | 283 Matrix a (dv); |
6569 | 284 @end group |
285 @end example | |
286 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
287 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
|
288 |
6569 | 289 @item |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
290 Define the number of rows and columns in the matrix. For example: |
6569 | 291 |
292 @example | |
6572 | 293 Matrix a (2, 2) |
6569 | 294 @end example |
295 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
296 This constructor can @strong{only} be used with matrix types. |
6569 | 297 @end itemize |
298 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
299 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
|
300 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
|
301 methods include |
6569 | 302 |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
303 @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
|
304 @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
|
305 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
|
306 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
|
307 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
|
308 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
|
309 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
|
310 to get or set. |
6572 | 311 @end deftypefn |
6569 | 312 |
7001 | 313 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
|
314 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
|
315 directly through the @code{fortran_vec} method discussed below. |
6572 | 316 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
317 @deftypefn {Method} {octave_idx_type} numel (void) const |
6569 | 318 The total number of elements in the matrix or array. |
6572 | 319 @end deftypefn |
320 | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
321 @deftypefn {Method} {size_t} byte_size (void) const |
6569 | 322 The number of bytes used to store the matrix or array. |
6572 | 323 @end deftypefn |
324 | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
325 @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
|
326 The dimensions of the matrix or array in value of type @code{dim_vector}. |
6572 | 327 @end deftypefn |
328 | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
329 @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
|
330 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
|
331 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
|
332 @end deftypefn |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
333 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
334 @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
|
335 @deftypefnx {Method} {void} resize (nrows, ncols) |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
336 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
|
337 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
|
338 of rows and columns in the matrix. |
6572 | 339 @end deftypefn |
340 | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
341 @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
|
342 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
|
343 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
|
344 library. |
6572 | 345 @end deftypefn |
6569 | 346 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
347 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
|
348 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
|
349 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
|
350 @code{solve}, etc. |
6569 | 351 |
352 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
|
353 @w{@code{DEFUN_DLD}} function is as follows |
6569 | 354 |
9906 | 355 @example |
21316
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20535
diff
changeset
|
356 @group |
9906 | 357 @EXAMPLEFILE(addtwomatrices.cc) |
21316
6cc091426e52
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
20535
diff
changeset
|
358 @end group |
9906 | 359 @end example |
6569 | 360 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
361 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
|
362 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
|
363 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
|
364 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
|
365 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
|
366 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
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 catch the @code{octave_execution_error} exception. |
6569 | 373 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
374 @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
|
375 @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
|
376 function. An example of the use of this demonstration function is |
6569 | 377 |
378 @example | |
379 @group | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
380 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
|
381 @result{} 2 1 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
382 1 2 |
6569 | 383 @end group |
384 @end example | |
385 | |
6572 | 386 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
|
387 extract these from an @code{octave_value}, and the associated header file is |
6572 | 388 listed below. |
6569 | 389 |
390 @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
|
391 @headitem Type @tab Function @tab Source Code |
6572 | 392 @item @code{RowVector} @tab @code{row_vector_value} @tab @file{dRowVector.h} |
393 @item @code{ComplexRowVector} @tab @code{complex_row_vector_value} @tab @file{CRowVector.h} | |
394 @item @code{ColumnVector} @tab @code{column_vector_value} @tab @file{dColVector.h} | |
395 @item @code{ComplexColumnVector} @tab @code{complex_column_vector_value} @tab @file{CColVector.h} | |
396 @item @code{Matrix} @tab @code{matrix_value} @tab @file{dMatrix.h} | |
397 @item @code{ComplexMatrix} @tab @code{complex_matrix_value} @tab @file{CMatrix.h} | |
398 @item @code{boolMatrix} @tab @code{bool_matrix_value} @tab @file{boolMatrix.h} | |
399 @item @code{charMatrix} @tab @code{char_matrix_value} @tab @file{chMatrix.h} | |
400 @item @code{NDArray} @tab @code{array_value} @tab @file{dNDArray.h} | |
401 @item @code{ComplexNDArray} @tab @code{complex_array_value} @tab @file{CNDArray.h} | |
402 @item @code{boolNDArray} @tab @code{bool_array_value} @tab @file{boolNDArray.h} | |
403 @item @code{charNDArray} @tab @code{char_array_value} @tab @file{charNDArray.h} | |
404 @item @code{int8NDArray} @tab @code{int8_array_value} @tab @file{int8NDArray.h} | |
405 @item @code{int16NDArray} @tab @code{int16_array_value} @tab @file{int16NDArray.h} | |
406 @item @code{int32NDArray} @tab @code{int32_array_value} @tab @file{int32NDArray.h} | |
407 @item @code{int64NDArray} @tab @code{int64_array_value} @tab @file{int64NDArray.h} | |
408 @item @code{uint8NDArray} @tab @code{uint8_array_value} @tab @file{uint8NDArray.h} | |
409 @item @code{uint16NDArray} @tab @code{uint16_array_value} @tab @file{uint16NDArray.h} | |
410 @item @code{uint32NDArray} @tab @code{uint32_array_value} @tab @file{uint32NDArray.h} | |
411 @item @code{uint64NDArray} @tab @code{uint64_array_value} @tab @file{uint64NDArray.h} | |
6569 | 412 @end multitable |
413 | |
6572 | 414 @node Character Strings in Oct-Files |
415 @subsection Character Strings in Oct-Files | |
416 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
417 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
|
418 the example: |
6572 | 419 |
9906 | 420 @example |
421 @EXAMPLEFILE(stringdemo.cc) | |
422 @end example | |
6572 | 423 |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
424 An example of the use of this function is |
6572 | 425 |
426 @example | |
427 @group | |
428 s0 = ["First String"; "Second String"]; | |
429 [s1,s2] = stringdemo (s0) | |
430 @result{} s1 = Second String | |
431 First String | |
432 | |
433 @result{} s2 = First String | |
434 Second String | |
435 | |
436 typeinfo (s2) | |
437 @result{} sq_string | |
438 typeinfo (s1) | |
439 @result{} string | |
440 @end group | |
441 @end example | |
442 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
443 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
|
444 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
|
445 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
|
446 below. |
6572 | 447 |
448 @example | |
449 @group | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
450 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
|
451 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
|
452 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
|
453 octave_stdout << "First argument is a double quoted string\n"; |
6572 | 454 @end group |
455 @end example | |
456 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
457 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
|
458 @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
|
459 type of string should be specified. For example: |
6572 | 460 |
461 @example | |
462 @group | |
463 octave_value_list retval; | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
464 charNDArray ch; |
6572 | 465 @dots{} |
6577 | 466 // Create single quoted string |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
467 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
|
468 OR |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
469 retval(1) = octave_value (ch, '\''); // explicitly create sq_string |
6577 | 470 |
471 // 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
|
472 retval(0) = octave_value (ch, '"'); |
6572 | 473 @end group |
474 @end example | |
475 | |
476 @node Cell Arrays in Oct-Files | |
477 @subsection Cell Arrays in Oct-Files | |
478 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
479 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
|
480 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
|
481 array can be treated like any other @code{octave_value}. A simple example is |
6572 | 482 |
9906 | 483 @example |
484 @EXAMPLEFILE(celldemo.cc) | |
485 @end example | |
6572 | 486 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
487 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
|
488 @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
|
489 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
|
490 them as individual output arguments. For example: |
6572 | 491 |
492 @example | |
493 @group | |
494 [b1, b2, b3] = celldemo (@{1, [1, 2], "test"@}) | |
495 @result{} | |
496 b1 = 1 | |
497 b2 = | |
498 | |
499 1 2 | |
500 | |
501 b3 = test | |
502 @end group | |
503 @end example | |
504 | |
505 @node Structures in Oct-Files | |
506 @subsection Structures in Oct-Files | |
507 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
508 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
|
509 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
|
510 consisting of a @code{std::string} and an Octave @code{Cell} variable. |
6572 | 511 |
512 A simple example demonstrating the use of structures within oct-files is | |
513 | |
9906 | 514 @example |
515 @EXAMPLEFILE(structdemo.cc) | |
516 @end example | |
6572 | 517 |
518 An example of its use is | |
519 | |
520 @example | |
521 @group | |
522 x.a = 1; x.b = "test"; x.c = [1, 2]; | |
523 structdemo (x, "b") | |
524 @result{} selected = test | |
525 @end group | |
526 @end example | |
527 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
528 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
|
529 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
|
530 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
|
531 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
|
532 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
|
533 underlying @code{octave_value} in the single struct example we would write |
6572 | 534 |
535 @example | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
536 octave_value tmp = arg0.contents (arg1)(0); |
6572 | 537 @end example |
538 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
539 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
540 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
|
541 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
|
542 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
|
543 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
544 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
|
545 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
|
546 on just @code{contents}. |
6572 | 547 |
548 @node Sparse Matrices in Oct-Files | |
549 @subsection Sparse Matrices in Oct-Files | |
6569 | 550 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
551 There are three classes of sparse objects that are of interest to the user. |
6569 | 552 |
6572 | 553 @table @code |
6569 | 554 @item SparseMatrix |
555 A double precision sparse matrix class | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
556 |
6569 | 557 @item SparseComplexMatrix |
558 A complex sparse matrix class | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
559 |
6569 | 560 @item SparseBoolMatrix |
561 A boolean sparse matrix class | |
562 @end table | |
563 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
564 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
|
565 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
|
566 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
|
567 @code{Array} classes will be comfortable with the use of the sparse classes. |
6569 | 568 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
569 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
|
570 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
|
571 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
|
572 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
|
573 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
|
574 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
|
575 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
|
576 class than it is to the @code{NDArray} class. |
6569 | 577 |
578 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
579 * Array and Sparse Class Differences:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
580 * Creating Sparse Matrices in Oct-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
581 * Using Sparse Matrices in Oct-Files:: |
6569 | 582 @end menu |
583 | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
584 @node Array and Sparse Class Differences |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
585 @subsubsection Array and Sparse Class Differences |
6569 | 586 |
587 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
|
588 of nonzero elements, rather than the product of the dimensions. Therefore, |
6569 | 589 |
590 @example | |
6577 | 591 @group |
592 SparseMatrix sm; | |
593 @dots{} | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
594 int nnz = sm.nelem (); |
6577 | 595 @end group |
6569 | 596 @end example |
597 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
598 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
599 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
|
600 @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
|
601 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
|
602 @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
|
603 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
|
604 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
|
605 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
|
606 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
|
607 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
|
608 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
|
609 will not overflow. |
6569 | 610 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
611 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
|
612 @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
|
613 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
|
614 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
|
615 element so it can be filled. Therefore, a piece of code like |
6569 | 616 |
617 @example | |
6577 | 618 @group |
619 SparseMatrix sm; | |
620 @dots{} | |
621 for (int j = 0; j < nc; j++) | |
622 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
|
623 std::cerr << " (" << i << "," << j << "): " << sm(i,j) << "\n"; |
6577 | 624 @end group |
6569 | 625 @end example |
626 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
627 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
628 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
|
629 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
|
630 matrix. |
6569 | 631 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
632 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
|
633 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
|
634 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
|
635 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
|
636 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
|
637 conversion, is |
6569 | 638 |
639 @example | |
6577 | 640 @group |
641 SparseMatrix sm; | |
642 @dots{} | |
643 const SparseMatrix tmp (sm); | |
644 for (int j = 0; j < nc; j++) | |
645 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
|
646 std::cerr << " (" << i << "," << j << "): " << tmp(i,j) << "\n"; |
6577 | 647 @end group |
6569 | 648 @end example |
649 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
650 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
|
651 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
|
652 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
|
653 @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
|
654 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
|
655 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
|
656 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
|
657 the matrix will become corrupted. |
6569 | 658 |
6572 | 659 @node Creating Sparse Matrices in Oct-Files |
660 @subsubsection Creating Sparse Matrices in Oct-Files | |
6569 | 661 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
662 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
|
663 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
|
664 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
|
665 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
|
666 techniques have their advantages and disadvantages. |
6569 | 667 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
668 Below is an example of creating a small sparse matrix using the first technique |
6569 | 669 |
670 @example | |
6577 | 671 @group |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
672 int nz, nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
673 nz = 4, nr = 3, nc = 4; |
6577 | 674 |
675 ColumnVector ridx (nz); | |
676 ColumnVector cidx (nz); | |
677 ColumnVector data (nz); | |
6569 | 678 |
18797
96f22d6674c4
Fix incorrect sparse matrix example code in oct-file chapter (bug #41799).
Rik <rik@octave.org>
parents:
18790
diff
changeset
|
679 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
|
680 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
|
681 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
|
682 ridx(3) = 3; cidx(3) = 4; data(3) = 4; |
6577 | 683 SparseMatrix sm (data, ridx, cidx, nr, nc); |
684 @end group | |
6569 | 685 @end example |
686 | |
6572 | 687 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
688 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
|
689 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
|
690 of the matrix itself, but is used internally. |
6569 | 691 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
692 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
|
693 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
|
694 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
|
695 significantly speed up creation of the sparse matrix. |
6569 | 696 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
697 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
|
698 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
|
699 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
|
700 matrix. |
6569 | 701 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
702 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
|
703 nonzero elements and then later fill those elements in. Sample code: |
6569 | 704 |
6571 | 705 @example |
6577 | 706 @group |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
707 int nz, nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
708 nz = 4, nr = 3, nc = 4; |
6577 | 709 SparseMatrix sm (nr, nc, nz); |
710 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4; | |
711 @end group | |
6569 | 712 @end example |
713 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
714 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
|
715 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
|
716 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
|
717 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
|
718 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
|
719 inserted; Only the column indices need to be updated. |
6569 | 720 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
721 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
|
722 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
|
723 than are actually inserted in the matrix. Therefore, |
6569 | 724 |
6571 | 725 @example |
6577 | 726 @group |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
727 int nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
728 nr = 3, nc = 4; |
6577 | 729 SparseMatrix sm (nr, nc, 0); |
730 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4; | |
731 @end group | |
6569 | 732 @end example |
733 | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
734 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
735 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
|
736 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
|
737 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
|
738 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
|
739 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
|
740 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
|
741 strictly needed. |
6569 | 742 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
743 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
|
744 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
|
745 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
|
746 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
|
747 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
|
748 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
|
749 @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
|
750 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
|
751 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
|
752 the first place. An example of the use of @code{maybe_compress} is |
6569 | 753 |
754 @example | |
6577 | 755 @group |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
756 int nz, nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
757 nz = 6, nr = 3, nc = 4; |
6577 | 758 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
759 SparseMatrix sm1 (nr, nc, nz); |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
760 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
|
761 sm1.maybe_compress (); // No zero elements were added |
6569 | 762 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
763 SparseMatrix sm2 (nr, nc, nz); |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
764 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
|
765 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
|
766 sm2.maybe_compress (true); // Zero elements were added |
6577 | 767 @end group |
6569 | 768 @end example |
769 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
770 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
|
771 it will slow the creation of the matrix. |
6569 | 772 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
773 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
|
774 compressed row format. An example of this advanced technique might be |
6569 | 775 |
776 @example | |
6577 | 777 octave_value arg; |
778 @dots{} | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
779 int nz, nr, nc; |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
780 nz = 6, nr = 3, nc = 4; // Assume we know the max # nz |
6577 | 781 SparseMatrix sm (nr, nc, nz); |
782 Matrix m = arg.matrix_value (); | |
6569 | 783 |
6577 | 784 int ii = 0; |
785 sm.cidx (0) = 0; | |
786 for (int j = 1; j < nc; j++) | |
787 @{ | |
788 for (int i = 0; i < nr; i++) | |
789 @{ | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
790 double tmp = m(i,j); |
6577 | 791 if (tmp != 0.) |
792 @{ | |
793 sm.data(ii) = tmp; | |
794 sm.ridx(ii) = i; | |
795 ii++; | |
796 @} | |
797 @} | |
798 sm.cidx(j+1) = ii; | |
799 @} | |
18790
322eb69e30ad
doc: Fix some Latin wording in documentation.
Rik <rik@octave.org>
parents:
18534
diff
changeset
|
800 sm.maybe_compress (); // If don't know a priori the final # of nz. |
6569 | 801 @end example |
802 | |
6572 | 803 @noindent |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
804 which is probably the most efficient means of creating a sparse matrix. |
6569 | 805 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
806 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
|
807 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
|
808 @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
|
809 example would then be modified as |
6569 | 810 |
811 @example | |
6577 | 812 octave_value arg; |
813 @dots{} | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
814 int nz, nr, nc; |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
815 nz = 6, nr = 3, nc = 4; // Guess the number of nz elements |
6577 | 816 SparseMatrix sm (nr, nc, nz); |
817 Matrix m = arg.matrix_value (); | |
6569 | 818 |
6577 | 819 int ii = 0; |
820 sm.cidx (0) = 0; | |
821 for (int j = 1; j < nc; j++) | |
822 @{ | |
823 for (int i = 0; i < nr; i++) | |
824 @{ | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
825 double tmp = m(i,j); |
6577 | 826 if (tmp != 0.) |
827 @{ | |
828 if (ii == nz) | |
829 @{ | |
830 nz += 2; // Add 2 more elements | |
831 sm.change_capacity (nz); | |
832 @} | |
833 sm.data(ii) = tmp; | |
834 sm.ridx(ii) = i; | |
835 ii++; | |
836 @} | |
837 @} | |
838 sm.cidx(j+1) = ii; | |
839 @} | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
840 sm.maybe_compress (); // If don't know a priori the final # of nz. |
6569 | 841 @end example |
842 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
843 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
|
844 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
|
845 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
|
846 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
|
847 changing capacity. |
6569 | 848 |
6572 | 849 @node Using Sparse Matrices in Oct-Files |
6569 | 850 @subsubsection Using Sparse Matrices in Oct-Files |
851 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
852 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
|
853 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
|
854 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
|
855 it as an @code{octave_value}, can be seen in the following example. |
6569 | 856 |
857 @example | |
6577 | 858 @group |
859 octave_value_list retval; | |
6569 | 860 |
6577 | 861 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
|
862 SparseComplexMatrix scm = args(1).sparse_complex_matrix_value (); |
6577 | 863 SparseBoolMatrix sbm = args(2).sparse_bool_matrix_value (); |
864 @dots{} | |
865 retval(2) = sbm; | |
866 retval(1) = scm; | |
867 retval(0) = sm; | |
868 @end group | |
6569 | 869 @end example |
870 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
871 The conversion to an @code{octave_value} is handled by the sparse |
6569 | 872 @code{octave_value} constructors, and so no special care is needed. |
873 | |
874 @node Accessing Global Variables in Oct-Files | |
875 @subsection Accessing Global Variables in Oct-Files | |
876 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
877 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
|
878 variables can be accessed within oct-files by using the support functions |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
879 @w{@code{get_global_value}} and @w{@code{set_global_value}}. |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
880 @w{@code{get_global_value}} takes two arguments, the first is a string |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
881 representing the variable name to obtain. The second argument is a boolean |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
882 argument specifying what to do if no global variable of the desired name is |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
883 found. An example of the use of these two functions is |
6569 | 884 |
9906 | 885 @example |
886 @EXAMPLEFILE(globaldemo.cc) | |
887 @end example | |
6569 | 888 |
889 An example of its use is | |
890 | |
891 @example | |
892 @group | |
893 global a b | |
894 b = 10; | |
895 globaldemo ("b") | |
896 @result{} 10 | |
897 globaldemo ("c") | |
898 @result{} "Global variable not found" | |
899 num2str (a) | |
900 @result{} 42 | |
901 @end group | |
902 @end example | |
903 | |
904 @node Calling Octave Functions from Oct-Files | |
905 @subsection Calling Octave Functions from Oct-Files | |
906 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
907 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
|
908 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
|
909 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
|
910 integral by quadrature over a user-supplied function. |
6569 | 911 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
912 There are also many ways in which a function could be given as input. It might |
6571 | 913 be passed as one of |
6569 | 914 |
915 @enumerate 1 | |
916 @item Function Handle | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
917 |
6569 | 918 @item Anonymous Function Handle |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
919 |
6569 | 920 @item Inline Function |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
921 |
6569 | 922 @item String |
923 @end enumerate | |
924 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
925 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
|
926 oct-file. |
6569 | 927 |
9906 | 928 @example |
929 @EXAMPLEFILE(funcdemo.cc) | |
930 @end example | |
6569 | 931 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
932 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
|
933 remaining arguments are all passed to the function. |
6569 | 934 |
935 @example | |
936 @group | |
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 |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
939 funcdemo (@@(x) sin (x), 1) |
6569 | 940 @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
|
941 funcdemo (inline ("sin (x)"), 1) |
6569 | 942 @result{} 0.84147 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
943 funcdemo ("sin", 1) |
6569 | 944 @result{} 0.84147 |
945 funcdemo (@@atan2, 1, 1) | |
946 @result{} 0.78540 | |
947 @end group | |
948 @end example | |
949 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
950 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
|
951 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
|
952 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
|
953 to create a temporary function as demonstrated below. |
6569 | 954 |
955 @example | |
956 @group | |
6577 | 957 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
|
958 std::string fcode = "function y = "; |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
959 fcode.append (fcn_name); |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
960 fcode.append ("(x) y = "); |
6577 | 961 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
|
962 fcode, "; endfunction"); |
6577 | 963 @dots{} |
964 if (fcn_name.length ()) | |
965 clear_function (fcn_name); | |
6569 | 966 @end group |
967 @end example | |
968 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
969 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
|
970 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
|
971 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
|
972 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
|
973 function names begin and end with the character sequence @samp{__}. |
6569 | 974 |
975 @node Calling External Code from Oct-Files | |
976 @subsection Calling External Code from Oct-Files | |
977 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
978 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
|
979 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
|
980 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
|
981 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
|
982 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
|
983 treats these declarations as C code is |
6569 | 984 |
985 @example | |
986 @group | |
987 #ifdef __cplusplus | |
6571 | 988 extern "C" |
6569 | 989 @{ |
990 #endif | |
991 #include "foo.h" | |
992 #ifdef __cplusplus | |
993 @} /* end extern "C" */ | |
994 #endif | |
995 @end group | |
996 @end example | |
997 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
998 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
|
999 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
|
1000 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
|
1001 behavior across a number of compilers. |
6569 | 1002 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1003 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
|
1004 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
|
1005 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
|
1006 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
|
1007 uses @code{XSTOPX}. |
6569 | 1008 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1009 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
|
1010 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
|
1011 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
|
1012 @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
|
1013 used instead to call the Fortran code. |
6569 | 1014 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1015 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
|
1016 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
|
1017 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
|
1018 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
|
1019 fault. |
6569 | 1020 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1021 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
|
1022 the following example, where the C++ wrapper is |
6569 | 1023 |
9906 | 1024 @example |
18369
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1025 @EXAMPLEFILE(fortrandemo.cc) |
9906 | 1026 @end example |
6569 | 1027 |
6572 | 1028 @noindent |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
9906
diff
changeset
|
1029 and the Fortran function is |
6569 | 1030 |
9906 | 1031 @example |
18369
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1032 @EXAMPLEFILE(fortransub.f) |
9906 | 1033 @end example |
6569 | 1034 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1035 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
|
1036 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
|
1037 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
|
1038 example to work. |
6569 | 1039 |
1040 @example | |
1041 @group | |
18369
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1042 mkoctfile fortrandemo.cc fortransub.f |
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1043 [b, s] = fortrandemo (1:3) |
6569 | 1044 @result{} |
1045 b = 1.00000 0.50000 0.33333 | |
1046 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
|
1047 [b, s] = fortrandemo (0:3) |
4b32677b6229
Rename Fortran example files from 'fort' prefix to 'fortran' prefix.
Rik <rik@octave.org>
parents:
18367
diff
changeset
|
1048 error: fortrandemo: fortransub: divide by zero |
6569 | 1049 @end group |
1050 @end example | |
1051 | |
1052 @node Allocating Local Memory in Oct-Files | |
1053 @subsection Allocating Local Memory in Oct-Files | |
1054 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1055 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
|
1056 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
|
1057 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
|
1058 is to use the @w{@code{OCTAVE_LOCAL_BUFFER}} macro. An example of its use is |
6569 | 1059 |
1060 @example | |
1061 OCTAVE_LOCAL_BUFFER (double, tmp, len) | |
1062 @end example | |
1063 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
1064 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1065 that returns a pointer @code{tmp} of type @code{double *} of length @code{len}. |
6569 | 1066 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1067 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
|
1068 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
|
1069 |
6569 | 1070 @node Input Parameter Checking in Oct-Files |
1071 @subsection Input Parameter Checking in Oct-Files | |
1072 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1073 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
|
1074 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
|
1075 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
|
1076 checking to ensure that Octave behaves well. |
6580 | 1077 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1078 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
|
1079 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
|
1080 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
|
1081 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
|
1082 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
|
1083 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
|
1084 oct-files often need additional input parameter checking. |
6580 | 1085 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1086 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
|
1087 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
|
1088 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
|
1089 @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
|
1090 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
|
1091 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
|
1092 below. |
6580 | 1093 |
9906 | 1094 @example |
1095 @EXAMPLEFILE(paramdemo.cc) | |
1096 @end example | |
6580 | 1097 |
1098 @noindent | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
1099 An example of its use is: |
6580 | 1100 |
1101 @example | |
1102 @group | |
1103 paramdemo ([1, 2, NaN, Inf]) | |
1104 @result{} Properties of input array: | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1105 includes Inf or NaN values |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1106 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
|
1107 includes only int, Inf or NaN values |
6580 | 1108 @end group |
1109 @end example | |
6569 | 1110 |
1111 @node Exception and Error Handling in Oct-Files | |
1112 @subsection Exception and Error Handling in Oct-Files | |
1113 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1114 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
|
1115 @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
|
1116 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
|
1117 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
|
1118 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
|
1119 @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
|
1120 to a user typing @key{Control-C}. For example: |
6569 | 1121 |
1122 @example | |
1123 @group | |
6577 | 1124 for (octave_idx_type i = 0; i < a.nelem (); i++) |
1125 @{ | |
1126 OCTAVE_QUIT; | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1127 b.elem (i) = 2. * a.elem (i); |
6577 | 1128 @} |
6569 | 1129 @end group |
1130 @end example | |
1131 | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
1132 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
|
1133 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
|
1134 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
|
1135 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
|
1136 which will force Octave to exit completely. |
6569 | 1137 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1138 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
|
1139 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
|
1140 @w{@code{OCTAVE_QUIT}}. |
6569 | 1141 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1142 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
|
1143 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
|
1144 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
|
1145 alternative code in this case is |
6569 | 1146 |
1147 @example | |
1148 @group | |
6577 | 1149 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
1150 @dots{} some code that calls a "foreign" function @dots{} | |
1151 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
6569 | 1152 @end group |
1153 @end example | |
1154 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1155 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
|
1156 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
|
1157 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
|
1158 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
|
1159 method or the @w{@code{OCTAVE_LOCAL_BUFFER}} macro. |
6569 | 1160 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1161 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
|
1162 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
|
1163 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
|
1164 exception occurs. An example of the use of this mechanism is |
6569 | 1165 |
9906 | 1166 @example |
1167 @EXAMPLEFILE(unwinddemo.cc) | |
1168 @end example | |
6569 | 1169 |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
1170 As can be seen in the example: |
6569 | 1171 |
1172 @example | |
1173 @group | |
6572 | 1174 unwinddemo (1, 0) |
6569 | 1175 @result{} Inf |
1176 1 / 0 | |
1177 @result{} warning: division by zero | |
6593 | 1178 Inf |
6569 | 1179 @end group |
1180 @end example | |
1181 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1182 The warning for division by zero (and in fact all warnings) are disabled in the |
6569 | 1183 @code{unwinddemo} function. |
1184 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1185 @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
|
1186 @subsection Documentation and Testing of Oct-Files |
6569 | 1187 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1188 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
|
1189 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
|
1190 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
|
1191 that are particular to the formatting of help strings within oct-files. |
6580 | 1192 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1193 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
|
1194 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
|
1195 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
|
1196 is illustrated in the following example, |
6580 | 1197 |
1198 @example | |
1199 @group | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1200 DEFUN_DLD (do_what_i_want, args, nargout, |
6580 | 1201 "-*- texinfo -*-\n\ |
21319
8880d93010d8
Remove further uses of CLASS field in @deftypefn macro.
Rik <rik@octave.org>
parents:
21316
diff
changeset
|
1202 @@deftypefn @{@} @{@} do_what_i_say (@@var@{n@})\n\ |
7081 | 1203 A function that does what the user actually wants rather\n\ |
1204 than what they requested.\n\ | |
6580 | 1205 @@end deftypefn") |
1206 @{ | |
1207 @dots{} | |
1208 @} | |
1209 @end group | |
1210 @end example | |
1211 | |
1212 @noindent | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1213 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
|
1214 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
|
1215 that the final @code{\} must be the last character on the line. |
6580 | 1216 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1217 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
|
1218 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
|
1219 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
|
1220 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
|
1221 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
|
1222 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
|
1223 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
|
1224 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
|
1225 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
|
1226 tests are stripped from the compiled code. An example in an oct-file might be |
6580 | 1227 |
1228 @example | |
1229 @group | |
1230 /* | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1231 %!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
|
1232 %!error (sin ()) |
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1233 %!error (sin (1,1)) |
6580 | 1234 */ |
1235 @end group | |
1236 @end example | |
6569 | 1237 |
6593 | 1238 @c @node Application Programming Interface for Oct-Files |
1239 @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
|
1240 @c |
6593 | 1241 @c WRITE ME, using Coda section 1.3 as a starting point. |
6569 | 1242 |
1243 @node Mex-Files | |
1244 @section Mex-Files | |
1245 @cindex mex-files | |
1246 @cindex mex | |
1247 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1248 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
|
1249 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
|
1250 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
|
1251 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
|
1252 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
|
1253 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
|
1254 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
|
1255 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
|
1256 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
|
1257 discussed. |
6569 | 1258 |
1259 @menu | |
17152
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1260 * Getting Started with Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1261 * 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
|
1262 * Character Strings in Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1263 * Cell Arrays with Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1264 * Structures with Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1265 * Sparse Matrices with Mex-Files:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
1266 * 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
|
1267 @c * Application Programming Interface for Mex-Files:: |
6569 | 1268 @end menu |
1269 | |
1270 @node Getting Started with Mex-Files | |
1271 @subsection Getting Started with Mex-Files | |
1272 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1273 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
|
1274 @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
|
1275 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
|
1276 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
|
1277 mex-files have the extension @file{.mex}. |
6569 | 1278 |
1279 @DOCSTRING(mex) | |
1280 | |
1281 @DOCSTRING(mexext) | |
1282 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1283 Consider the following short example: |
6569 | 1284 |
9906 | 1285 @example |
1286 @group | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1287 @EXAMPLEFILE(myhello.c) |
9906 | 1288 @end group |
1289 @end example | |
6569 | 1290 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1291 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
|
1292 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
|
1293 @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
|
1294 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
|
1295 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
|
1296 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
|
1297 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1298 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
|
1299 function takes four arguments: |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1300 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1301 @enumerate 1 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1302 @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
|
1303 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1304 @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
|
1305 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1306 @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
|
1307 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1308 @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
|
1309 @end enumerate |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1310 |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1311 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
|
1312 @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
|
1313 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
|
1314 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
|
1315 above function is in the file @file{myhello.c}, it can be compiled with |
6580 | 1316 |
1317 @example | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1318 mkoctfile --mex myhello.c |
6580 | 1319 @end example |
1320 | |
1321 @noindent | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1322 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
|
1323 Octave as |
6580 | 1324 |
1325 @example | |
1326 @group | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1327 myhello (1,2,3) |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1328 @result{} Hello, World! |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1329 @result{} I have 3 inputs and 0 outputs |
6580 | 1330 @end group |
1331 @end example | |
1332 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1333 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
|
1334 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
|
1335 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
|
1336 @file{myhello.m} which might contain the text |
6580 | 1337 |
1338 @example | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1339 %MYHELLO Simple test of the functionality of a mex-file. |
6580 | 1340 @end example |
1341 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1342 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
|
1343 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
|
1344 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
|
1345 language itself for testing purposes. |
6580 | 1346 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1347 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
|
1348 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
|
1349 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
|
1350 the function name. For example, if |
6580 | 1351 |
9906 | 1352 @example |
1353 @group | |
1354 @EXAMPLEFILE(myfunc.c) | |
1355 @end group | |
1356 @end example | |
6580 | 1357 |
1358 @noindent | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1359 is in the file @file{myfunc.c}, and is compiled with |
6580 | 1360 |
1361 @example | |
1362 @group | |
1363 mkoctfile --mex myfunc.c | |
1364 ln -s myfunc.mex myfunc2.mex | |
1365 @end group | |
1366 @end example | |
1367 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1368 @noindent |
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1369 then as can be seen by |
6580 | 1370 |
1371 @example | |
1372 @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
|
1373 myfunc () |
6580 | 1374 @result{} You called function: myfunc |
1375 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
|
1376 myfunc2 () |
6580 | 1377 @result{} You called function: myfunc2 |
1378 @end group | |
1379 @end example | |
1380 | |
1381 @noindent | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1382 the behavior of the mex-file can be altered depending on the function's name. |
6580 | 1383 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1384 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
|
1385 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
|
1386 mex-files in the headers @file{mexproto.h} and @file{mxarray.h}. |
6593 | 1387 |
6580 | 1388 @node Working with Matrices and Arrays in Mex-Files |
1389 @subsection Working with Matrices and Arrays in Mex-Files | |
1390 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1391 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
|
1392 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
|
1393 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
|
1394 oct-files in that it acts as a container for all the more specialized types. |
6580 | 1395 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1396 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
|
1397 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
|
1398 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
|
1399 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
|
1400 @code{mxArray} structures, including @code{mxCreateDoubleMatrix}, |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1401 @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
|
1402 @code{mxCreateNumericArray}. |
6593 | 1403 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1404 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
|
1405 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
|
1406 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
|
1407 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
|
1408 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
|
1409 perform the same operation for all matrix types. For example: |
6593 | 1410 |
1411 @example | |
1412 @group | |
1413 mxArray *m; | |
6686 | 1414 mwSize *dims; |
6593 | 1415 UINT32_T *pr; |
1416 | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1417 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
|
1418 dims[0] = 2; dims[1] = 2; |
6593 | 1419 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
|
1420 pr = (UINT32_T *) mxGetData (m); |
6593 | 1421 @end group |
1422 @end example | |
1423 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1424 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
|
1425 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
|
1426 argument of @code{mxSetPr}. |
6593 | 1427 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1428 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
|
1429 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
|
1430 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
|
1431 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
|
1432 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
|
1433 arrays. |
6686 | 1434 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1435 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
|
1436 precision arrays is given by the file @file{mypow2.c} shown below. |
6593 | 1437 |
9906 | 1438 @example |
1439 @EXAMPLEFILE(mypow2.c) | |
1440 @end example | |
6593 | 1441 |
1442 @noindent | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1443 An example of its use is |
6593 | 1444 |
1445 @example | |
1446 @group | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1447 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
|
1448 all (b.^2 == mypow2 (b)) |
6593 | 1449 @result{} 1 |
1450 @end group | |
1451 @end example | |
1452 | |
7096 | 1453 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
|
1454 @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
|
1455 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
|
1456 @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
|
1457 2-D matrix (@nospell{MxN} matrix). |
6580 | 1458 |
1459 @node Character Strings in Mex-Files | |
1460 @subsection Character Strings in Mex-Files | |
1461 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1462 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
|
1463 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
|
1464 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
|
1465 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
|
1466 below. |
6593 | 1467 |
9906 | 1468 @example |
1469 @EXAMPLEFILE(mystring.c) | |
1470 @end example | |
6593 | 1471 |
1472 @noindent | |
1473 An example of its expected output is | |
1474 | |
1475 @example | |
1476 @group | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1477 mystring (["First String"; "Second String"]) |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1478 @result{} Second String |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1479 First String |
6593 | 1480 @end group |
1481 @end example | |
1482 | |
7096 | 1483 Other functions in the mex interface for handling character strings are |
1484 @code{mxCreateString}, @code{mxArrayToString}, and | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1485 @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
|
1486 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
|
1487 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
|
1488 any case. |
6580 | 1489 |
1490 @node Cell Arrays with Mex-Files | |
1491 @subsection Cell Arrays with Mex-Files | |
6569 | 1492 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1493 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
|
1494 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
|
1495 oct-file in a mex-file is given by @file{mycell.c} as shown below. |
6593 | 1496 |
9906 | 1497 @example |
1498 @EXAMPLEFILE(mycell.c) | |
1499 @end example | |
6593 | 1500 |
1501 @noindent | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1502 The output is identical to the oct-file version as well. |
6593 | 1503 |
1504 @example | |
1505 @group | |
1506 [b1, b2, b3] = mycell (@{1, [1, 2], "test"@}) | |
1507 @result{} | |
1508 b1 = 1 | |
1509 b2 = | |
1510 | |
1511 1 2 | |
1512 | |
1513 b3 = test | |
1514 @end group | |
1515 @end example | |
1516 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1517 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
|
1518 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
|
1519 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
|
1520 values, is @code{mxSetCell} and is defined as |
6593 | 1521 |
1522 @example | |
1523 void mxSetCell (mxArray *ptr, int idx, mxArray *val); | |
1524 @end example | |
1525 | |
7007 | 1526 Finally, to create a cell array or matrix, the appropriate functions are |
6593 | 1527 |
1528 @example | |
1529 @group | |
1530 mxArray *mxCreateCellArray (int ndims, const int *dims); | |
1531 mxArray *mxCreateCellMatrix (int m, int n); | |
1532 @end group | |
1533 @end example | |
6569 | 1534 |
6572 | 1535 @node Structures with Mex-Files |
1536 @subsection Structures with Mex-Files | |
6569 | 1537 |
6593 | 1538 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
|
1539 @code{mxCreateStructMatrix} which creates a structure array with a two |
6593 | 1540 dimensional matrix, or @code{mxCreateStructArray}. |
1541 | |
1542 @example | |
1543 @group | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1544 mxArray *mxCreateStructArray (int ndims, int *dims, |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1545 int num_keys, |
6593 | 1546 const char **keys); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1547 mxArray *mxCreateStructMatrix (int rows, int cols, |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1548 int num_keys, |
6593 | 1549 const char **keys); |
1550 @end group | |
1551 @end example | |
1552 | |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1553 Accessing the fields of the structure can then be performed with |
6593 | 1554 @code{mxGetField} and @code{mxSetField} or alternatively with the |
1555 @code{mxGetFieldByNumber} and @code{mxSetFieldByNumber} functions. | |
1556 | |
1557 @example | |
1558 @group | |
7081 | 1559 mxArray *mxGetField (const mxArray *ptr, mwIndex index, |
1560 const char *key); | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1561 mxArray *mxGetFieldByNumber (const mxArray *ptr, |
6686 | 1562 mwIndex index, int key_num); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1563 void mxSetField (mxArray *ptr, mwIndex index, |
6593 | 1564 const char *key, mxArray *val); |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1565 void mxSetFieldByNumber (mxArray *ptr, mwIndex index, |
6593 | 1566 int key_num, mxArray *val); |
1567 @end group | |
1568 @end example | |
1569 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1570 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
|
1571 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
|
1572 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
|
1573 @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
|
1574 of the structure. |
6593 | 1575 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1576 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
|
1577 in the file @file{mystruct.c} shown below. |
6580 | 1578 |
9906 | 1579 @example |
1580 @EXAMPLEFILE(mystruct.c) | |
1581 @end example | |
6580 | 1582 |
6593 | 1583 An example of the behavior of this function within Octave is then |
1584 | |
1585 @example | |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18797
diff
changeset
|
1586 @group |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
18953
diff
changeset
|
1587 a(1).f1 = "f11"; a(1).f2 = "f12"; |
7081 | 1588 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
|
1589 b = mystruct (a); |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1590 @result{} field f1(0) = f11 |
6593 | 1591 field f1(1) = f21 |
1592 field f2(0) = f12 | |
1593 field f2(1) = f22 | |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1594 b |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1595 @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
|
1596 |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1597 this |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1598 that |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17152
diff
changeset
|
1599 |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1600 b(3) |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1601 @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
|
1602 |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1603 this = this3 |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1604 that = that3 |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18797
diff
changeset
|
1605 @end group |
6593 | 1606 @end example |
6569 | 1607 |
1608 @node Sparse Matrices with Mex-Files | |
1609 @subsection Sparse Matrices with Mex-Files | |
1610 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1611 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
|
1612 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
|
1613 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
|
1614 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
|
1615 separately. |
6593 | 1616 |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1617 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
|
1618 @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
|
1619 and @code{mxSetPi}, also supplies the following functions. |
6593 | 1620 |
1621 @example | |
1622 @group | |
6686 | 1623 mwIndex *mxGetIr (const mxArray *ptr); |
1624 mwIndex *mxGetJc (const mxArray *ptr); | |
1625 mwSize mxGetNzmax (const mxArray *ptr); | |
6593 | 1626 |
6686 | 1627 void mxSetIr (mxArray *ptr, mwIndex *ir); |
1628 void mxSetJc (mxArray *ptr, mwIndex *jc); | |
1629 void mxSetNzmax (mxArray *ptr, mwSize nzmax); | |
6593 | 1630 @end group |
1631 @end example | |
6580 | 1632 |
6593 | 1633 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1634 @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
|
1635 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
|
1636 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
|
1637 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
|
1638 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
|
1639 elements in each column of the sparse matrix. Therefore, |
6580 | 1640 |
6593 | 1641 @example |
1642 @group | |
6686 | 1643 mwSize nz, n; |
1644 mwIndex *Jc; | |
6593 | 1645 mxArray *m; |
1646 @dots{} | |
1647 n = mxGetN (m); | |
1648 Jc = mxGetJc (m); | |
1649 nz = Jc[n]; | |
1650 @end group | |
1651 @end example | |
1652 | |
1653 @noindent | |
18812
9ac2357f19bc
doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents:
18797
diff
changeset
|
1654 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
|
1655 @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
|
1656 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
|
1657 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
|
1658 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
|
1659 @file{mysparse.c} shown below. |
6593 | 1660 |
9906 | 1661 @example |
1662 @EXAMPLEFILE(mysparse.c) | |
1663 @end example | |
6569 | 1664 |
18358
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1665 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
|
1666 |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1667 @example |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1668 @group |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1669 sm = sparse ([1, 0; 0, pi]); |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1670 mysparse (sm) |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1671 @result{} |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1672 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
|
1673 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
|
1674 @end group |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1675 @end example |
adb7c7e6a4a1
doc: Re-write bits of External Code Interface chapter.
Rik <rik@octave.org>
parents:
18075
diff
changeset
|
1676 |
6580 | 1677 @node Calling Other Functions in Mex-Files |
1678 @subsection Calling Other Functions in Mex-Files | |
1679 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1680 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
|
1681 @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
|
1682 in the example below. |
6580 | 1683 |
9906 | 1684 @example |
1685 @EXAMPLEFILE(myfeval.c) | |
1686 @end example | |
6580 | 1687 |
1688 If this code is in the file @file{myfeval.c}, and is compiled to | |
1689 @file{myfeval.mex}, then an example of its use is | |
6569 | 1690 |
6580 | 1691 @example |
1692 @group | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
1693 a = myfeval ("sin", 1) |
18367
d1e16bdb3958
myfeval.c: Fix segfault in mex example code.
Rik <rik@octave.org>
parents:
18358
diff
changeset
|
1694 @result{} Starting file myfeval.mex |
d1e16bdb3958
myfeval.c: Fix segfault in mex example code.
Rik <rik@octave.org>
parents:
18358
diff
changeset
|
1695 I have 2 inputs and 1 outputs |
d1e16bdb3958
myfeval.c: Fix segfault in mex example code.
Rik <rik@octave.org>
parents:
18358
diff
changeset
|
1696 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
|
1697 a = 0.84147 |
6580 | 1698 @end group |
1699 @end example | |
1700 | |
1701 Note that it is not possible to use function handles or inline functions | |
1702 within a mex-file. | |
1703 | |
6593 | 1704 @c @node Application Programming Interface for Mex-Files |
1705 @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
|
1706 @c |
6593 | 1707 @c WRITE ME, refer to mex.h and mexproto.h |
6569 | 1708 |
1709 @node Standalone Programs | |
1710 @section Standalone Programs | |
1711 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1712 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
|
1713 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
|
1714 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
|
1715 program, uses class Matrix from @file{liboctave.a} or @file{liboctave.so}. |
6569 | 1716 |
9906 | 1717 @example |
1718 @EXAMPLEFILE(standalone.cc) | |
1719 @end example | |
6569 | 1720 |
6580 | 1721 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1722 mkoctfile can be used to build a standalone application with a command like |
6569 | 1723 |
1724 @example | |
1725 @group | |
8097
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1726 $ 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
|
1727 $ ./standalone |
6569 | 1728 Hello Octave world! |
1729 11 12 | |
1730 21 22 | |
1731 $ | |
1732 @end group | |
1733 @end example | |
1734 | |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1735 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
|
1736 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
|
1737 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
|
1738 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
|
1739 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
|
1740 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
|
1741 |
9906 | 1742 @example |
1743 @EXAMPLEFILE(embedded.cc) | |
1744 @end example | |
8097
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1745 |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1746 @noindent |
16867
be41c30bcb44
Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents:
16483
diff
changeset
|
1747 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
|
1748 |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1749 @example |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1750 @group |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1751 $ 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
|
1752 $ ./embedded |
16483
fcf1b0b52083
Use a better example of gcd() in embedded.cc
Rik <rik@octave.org>
parents:
16482
diff
changeset
|
1753 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
|
1754 $ |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1755 @end group |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1756 @end example |
804c60f92fb1
Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents:
7354
diff
changeset
|
1757 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1758 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
|
1759 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
|
1760 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
|
1761 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
|
1762 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
|
1763 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
|
1764 @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
|
1765 list of built-in functions can change. No guarantees can be made that a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1766 function that is currently a built-in won't be implemented as a .m file or as a |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1767 dynamically linked function in the future. An example of how to call built-in |
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1768 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
|
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 @EXAMPLEFILE(standalonebuiltin.cc) |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1772 @end example |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1773 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1774 @noindent |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
1775 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
|
1776 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1777 @example |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1778 @group |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1779 $ 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
|
1780 $ ./standalonebuiltin |
17949
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1781 This is a matrix: |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1782 11 12 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1783 21 22 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1784 |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1785 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
|
1786 34.4952 |
18367
d1e16bdb3958
myfeval.c: Fix segfault in mex example code.
Rik <rik@octave.org>
parents:
18358
diff
changeset
|
1787 $ |
17949
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1788 @end group |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1789 @end example |
3341d2f1e5db
Document calling DEFUN functions in C++.
Carlo de Falco <cdf@users.sourceforge.net>
parents:
17744
diff
changeset
|
1790 |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1791 @node Java Interface |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1792 @section Java Interface |
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 @cindex using Octave with Java |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1795 @cindex Java, using with Octave |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1796 @cindex calling Java from Octave |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1797 @cindex Java, calling from Octave |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1798 @cindex calling Octave from Java |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1799 @cindex Octave, calling from Java |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1800 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1801 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
|
1802 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
|
1803 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
|
1804 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1805 @menu |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1806 * Making Java Classes Available:: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1807 * 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
|
1808 * Set up the JVM:: |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1809 * Java Interface Functions:: |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1810 @end menu |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
1811 |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1812 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1813 @node Making Java Classes Available |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1814 @subsection Making Java Classes Available |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1815 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1816 @c - index - |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1817 @cindex classpath, setting |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1818 @cindex classpath, difference between static and dynamic |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1819 @cindex static classpath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1820 @cindex dynamic classpath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1821 @cindex @file{javaclasspath.txt} |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1822 @cindex @file{classpath.txt} |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1823 @cindex classes, making available to Octave |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1824 @c - index - |
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 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
|
1827 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
|
1828 @var{classpath} is composed of two parts: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1829 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1830 @itemize |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1831 @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
|
1832 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1833 @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
|
1834 @end itemize |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1835 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1836 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
|
1837 @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
|
1838 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
|
1839 @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
|
1840 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
|
1841 @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
|
1842 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
|
1843 @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
|
1844 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
|
1845 @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
|
1846 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
|
1847 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
|
1848 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1849 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
|
1850 @file{classpath.txt}) are: |
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 @itemize |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1853 @item |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1854 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
|
1855 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
|
1856 @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
|
1857 'per Octave invocation' basis. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1858 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1859 @item |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1860 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
|
1861 @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
|
1862 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
|
1863 on a @nospell{'per user'} basis. |
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 @item |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1866 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
|
1867 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
|
1868 @file{javaclasspath.m} resides, usually something like |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1869 @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
|
1870 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
|
1871 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1872 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1873 which javaclasspath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1874 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1875 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1876 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
|
1877 @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
|
1878 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
|
1879 @end itemize |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1880 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1881 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
|
1882 @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
|
1883 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
|
1884 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1885 Example: |
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 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1888 octave> base_path = "C:/Octave/java_files"; |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1889 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1890 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
|
1891 octave> javaaddpath ([base_path, "/someclasses.jar"]); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1892 octave> javaaddpath ([base_path, "/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> # check the dynamic classpath |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1895 octave> p = javaclasspath; |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1896 octave> disp (p@{1@}); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1897 C:/Octave/java_files/someclasses.jar |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1898 octave> disp (p@{2@}); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1899 C:/Octave/java_files/moreclasses.jar |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1900 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1901 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
|
1902 octave> javarmpath ([base_path, "/someclasses.jar"]); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1903 octave> p = javaclasspath; |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1904 octave> disp (p@{1@}); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1905 C:/Octave/java_files/moreclasses.jar |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1906 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1907 octave> # provoke an error |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1908 octave> disp (p@{2@}); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1909 error: A(I): Index exceeds matrix dimension. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1910 @end example |
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 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
|
1913 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
|
1914 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
|
1915 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
|
1916 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
|
1917 to the Java search path. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1918 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1919 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1920 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1921 # contents of .octaverc: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1922 addpath ("~/octave"); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1923 javaaddpath ("~/octave/myclasses.jar"); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1924 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1925 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1926 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1927 @c ------------------------------------------------------------------------ |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1928 @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
|
1929 @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
|
1930 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1931 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
|
1932 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
|
1933 and with the given parameters. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1934 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1935 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
|
1936 @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
|
1937 class @code{java.math.BigDecimal}. |
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 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1940 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1941 javaObject ("java.math.BigDecimal", 1.001 ); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1942 javaObject ("java.math.BigDecimal", "1.001"); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1943 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1944 @end example |
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 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
|
1947 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
|
1948 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
|
1949 @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
|
1950 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
|
1951 Octave variable. |
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 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1954 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1955 a = 1.001; |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1956 b = javaObject ("java.math.BigDecimal", a); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1957 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1958 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1959 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1960 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
|
1961 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
|
1962 addition to the previous example: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1963 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1964 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1965 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1966 isjava (a) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1967 @result{} ans = 0 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1968 class (a) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1969 @result{} ans = double |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1970 isjava (b) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1971 @result{} ans = 1 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1972 class (b) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1973 @result{} ans = java.math.BigDecimal |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1974 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1975 @end example |
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 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
|
1978 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1979 @example |
23565
3a730821e4a2
doc: Peridoc grammarcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
1980 @group |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1981 a = javaObject ("java.lang.Double", 1.001); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1982 b = javaObject ("java.math.BigDecimal", a); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1983 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1984 isjava (a) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1985 @result{} ans = 1 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1986 class (a) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1987 @result{} ans = java.lang.Double |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1988 isjava (b) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1989 @result{} ans = 1 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1990 class (b) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1991 @result{} ans = java.math.BigDecimal |
23565
3a730821e4a2
doc: Peridoc grammarcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
1992 @end group |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
1993 @end example |
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 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
|
1996 @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
|
1997 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
|
1998 @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
|
1999 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
|
2000 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2001 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2002 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
|
2003 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
|
2004 @ref{XREFjavaArray,javaArray,javaArray}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2005 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2006 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
|
2007 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2008 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2009 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2010 a.toString |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2011 @result{} ans = 1.001 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2012 b.toString |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2013 @result{} ans = 1.000999999999999889865... |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2014 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2015 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2016 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2017 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
|
2018 @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
|
2019 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
|
2020 with and without parentheses @code{()}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2021 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2022 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
|
2023 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
|
2024 @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
|
2025 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2026 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2027 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2028 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
|
2029 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
|
2030 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2031 @end example |
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 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
|
2034 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
|
2035 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
|
2036 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2037 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
|
2038 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
|
2039 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2040 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2041 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2042 java.math.BigDecimal.ONE; # does not work |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2043 java_get ("java.math.BigDecimal", "ONE"); # workaround |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2044 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2045 @end example |
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 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
|
2048 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
|
2049 Octave. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2050 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2051 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
|
2052 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
|
2053 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2054 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2055 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2056 javaObject ("java.math.BigDecimal", []); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2057 @result{} error: [java] java.lang.NullPointerException |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2058 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2059 @end example |
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 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
|
2062 @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
|
2063 @code{double}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2064 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2065 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
|
2066 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
|
2067 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2068 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2069 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2070 fieldnames (<Java object>) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2071 methods (<Java object>) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2072 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2073 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2074 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2075 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
|
2076 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
|
2077 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
|
2078 the stack trace are displayed. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2079 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2080 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2081 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2082 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
|
2083 debug_java () |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2084 @result{} ans = 1 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2085 javaObject ("java.math.BigDecimal", "1") ... |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2086 .divide (javaObject ("java.math.BigDecimal", "0")) |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2087 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2088 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2089 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2090 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2091 |
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
|
2092 @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
|
2093 @subsection Set up the JVM |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2094 @cindex memory, limitations on JVM |
26048 | 2095 @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
|
2096 |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2097 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
|
2098 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
|
2099 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
|
2100 @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
|
2101 @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
|
2102 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
|
2103 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
|
2104 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
|
2105 @file{C:\Program Files\Java\jre-10.0.2}. Make sure that you select a directory |
26048 | 2106 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
|
2107 |
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 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
|
2109 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
|
2110 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
|
2111 |
98242f7e8a0b
Document how the JVM can be selected at load time (bug #49111).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25143
diff
changeset
|
2112 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
|
2113 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
|
2114 (@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
|
2115 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
|
2116 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
|
2117 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
|
2118 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
|
2119 unexpectedly. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2120 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2121 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
|
2122 @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
|
2123 @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
|
2124 initialization. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2125 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2126 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
|
2127 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
|
2128 @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
|
2129 @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
|
2130 You can find this directory by executing |
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 which javaclasspath |
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 @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
|
2137 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
|
2138 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
|
2139 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2140 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2141 -Xmx256m |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2142 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2143 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2144 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
|
2145 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
|
2146 about 1 Gigabyte. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2147 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2148 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
|
2149 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
|
2150 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
|
2151 Megabytes of initial memory: |
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 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2154 -Xms64m |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2155 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2156 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2157 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
|
2158 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
|
2159 and consult the Java documentation. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2160 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2161 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
|
2162 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
|
2163 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
|
2164 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
|
2165 the string @code{12.34}. |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2166 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2167 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2168 -DMyProperty=12.34 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2169 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2170 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2171 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
|
2172 or in Octave: |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2173 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2174 @example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2175 @group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2176 octave> javaMethod ("getProperty", "java.lang.System", "MyProperty"); |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2177 ans = 12.34 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2178 @end group |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2179 @end example |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2180 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2181 @seealso{javamem} |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2182 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2183 |
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2184 |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2185 @node Java Interface Functions |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2186 @subsection Java Interface Functions |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2187 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2188 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
|
2189 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
|
2190 which return results to Octave. |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2191 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2192 @cindex object, creating a Java object |
23213
b8a186a9760b
doc: Overhaul Java interface description (bug #50299)
Ernst Reissner <rei3ner@arcor.de>
parents:
23083
diff
changeset
|
2193 @cindex instance, creating a Java instance |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2194 @DOCSTRING(javaObject) |
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 @cindex array, creating a Java array |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2197 @DOCSTRING(javaArray) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2198 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2199 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
|
2200 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
|
2201 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
|
2202 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2203 @DOCSTRING(isjava) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2204 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2205 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
|
2206 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
|
2207 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2208 @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
|
2209 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
|
2210 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
|
2211 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2212 @example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2213 @group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2214 dobj = javaObject ("java.lang.Double", pi); |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2215 fieldnames (dobj) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2216 @result{} |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2217 @{ |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2218 [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
|
2219 [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
|
2220 [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
|
2221 [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
|
2222 [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
|
2223 [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
|
2224 [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
|
2225 [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
|
2226 [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
|
2227 [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
|
2228 @} |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2229 @end group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2230 @end example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2231 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2232 @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
|
2233 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
|
2234 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
|
2235 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
|
2236 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
|
2237 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
|
2238 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2239 @example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2240 @group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2241 dobj = javaObject ("java.lang.Double", pi); |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2242 dobj.MAX_VALUE |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2243 @result{} 1.7977e+308 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2244 java_get ("java.lang.Float", "MAX_VALUE") |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2245 @result{} 3.4028e+38 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2246 @end group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2247 @end example |
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 @DOCSTRING(java_get) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2250 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2251 @cindex field, setting value of Java object field |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2252 @DOCSTRING(java_set) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2253 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2254 @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
|
2255 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
|
2256 example, using the previously created @var{dobj}: |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2257 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2258 @example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2259 @group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2260 methods (dobj) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2261 @result{} |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2262 Methods for class java.lang.Double: |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2263 boolean equals(java.lang.Object) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2264 java.lang.String toString(double) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2265 java.lang.String toString() |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2266 @dots{} |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2267 @end group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2268 @end example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2269 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2270 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
|
2271 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
|
2272 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
|
2273 below. |
21630
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2274 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2275 @example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2276 @group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2277 dobj = javaObject ("java.lang.Double", pi); |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2278 dobj.equals (3) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2279 @result{} 0 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2280 javaMethod ("equals", dobj, pi) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2281 @result{} 1 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2282 @end group |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2283 @end example |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2284 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2285 @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
|
2286 @DOCSTRING(javaMethod) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2287 |
22970
cc025b4bd2fb
doc: Review Appendix A, External Code Interface, of manual.
Rik <rik@octave.org>
parents:
22924
diff
changeset
|
2288 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
|
2289 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
|
2290 @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
|
2291 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2292 @cindex classpath, displaying |
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 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2295 @cindex classpath, static |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2296 @cindex static classpath |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2297 @DOCSTRING(javaclasspath) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2298 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2299 @findex javaaddpath |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2300 @cindex classpath, adding new path |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2301 @cindex path, adding to classpath |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2302 @cindex classpath, dynamic |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2303 @cindex dynamic classpath, adding new path |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2304 @DOCSTRING(javaaddpath) |
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 @cindex classpath, removing path |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2307 @cindex path, removing from classpath |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2308 @DOCSTRING(javarmpath) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2309 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2310 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
|
2311 between Octave and the Java Virtual Machine. |
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(javachk) |
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(usejava) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2316 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2317 @cindex memory, displaying Java memory status |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2318 @DOCSTRING(javamem) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2319 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2320 @DOCSTRING(java_matrix_autoconversion) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2321 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2322 @DOCSTRING(java_unsigned_autoconversion) |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2323 |
b5d9b95d1e1a
Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
21530
diff
changeset
|
2324 @DOCSTRING(debug_java) |