annotate doc/interpreter/dynamic.txi @ 10791:3140cb7a05a1

Add spellchecker scripts for Octave and run spellcheck of documentation interpreter/doccheck: New directory for spelling/grammar scripts. interpreter/doccheck/README: Instructions for using scripts. interpreter/doccheck/spellcheck: Script to spellcheck a Texinfo file. interpreter/doccheck/aspell.conf: GNU Aspell configuration file for Octave documentation. interpreter/doccheck/aspell-octave.en.pws: Private Aspell dictionary. interpreter/doccheck/add_to_aspell_dict: Script to add new Octave-specific words to private Aspell dictionary. interpreter/octave.texi: New @nospell macro which forces Aspell to ignore the word marked by the macro. interpreter/mk_doc_cache.m: Skip new @nospell macro when building doc_cache.
author Rik <octave@nomad.inbox5.com>
date Sat, 17 Jul 2010 19:53:01 -0700
parents 8d20fb66a0dc
children 322f43e0e170
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8828
diff changeset
1 @c Copyright (C) 2007, 2008, 2009 John W. Eaton and David Bateman
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
2 @c Copyright (C) 2007 Paul Thomas and Christoph Spiel
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
3 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
4 @c This file is part of Octave.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
5 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
6 @c Octave is free software; you can redistribute it and/or modify it
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
7 @c under the terms of the GNU General Public License as published by the
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
8 @c Free Software Foundation; either version 3 of the License, or (at
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
9 @c your option) any later version.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
10 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
11 @c Octave is distributed in the hope that it will be useful, but WITHOUT
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
12 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
13 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
14 @c for more details.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
15 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
16 @c You should have received a copy of the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
17 @c along with Octave; see the file COPYING. If not, see
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 7007
diff changeset
18 @c <http://www.gnu.org/licenses/>.
6578
fbc16fe0e86a [project @ 2007-04-25 16:04:59 by jwe]
jwe
parents: 6577
diff changeset
19
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
20 @node Dynamically Linked Functions
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
21 @appendix Dynamically Linked Functions
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
22 @cindex dynamic-linking
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
23
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
24 Octave has the possibility of including compiled code as dynamically
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
25 linked extensions and then using these extensions as if they were part
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
26 of Octave itself. Octave can call C++ code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
27 through its native oct-file interface or C code through its mex
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
28 interface. It can also indirectly call functions written in any other
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
29 language through a simple wrapper. The reasons to write code in a
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
30 compiled language might be either to link to an existing piece of code
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
31 and allow it to be used within Octave, or to allow improved performance
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
32 for key pieces of code.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
33
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
34 Before going further, you should first determine if you really need to
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
35 use dynamically linked functions at all. Before proceeding with writing
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
36 any dynamically linked function to improve performance you should
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
37 address ask yourself
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
38
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
39 @itemize @bullet
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
40 @item
6939
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6686
diff changeset
41 Can I get the same functionality using the Octave scripting language only?
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
42 @item
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
43 Is it thoroughly optimized Octave code? Vectorization of Octave code,
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
44 doesn't just make it concise, it generally significantly improves its
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
45 performance. Above all, if loops must be used, make sure that the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
46 allocation of space for variables takes place outside the loops using an
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
47 assignment to a matrix of the right size, or zeros.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
48 @item
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
49 Does it make as much use as possible of existing built-in library
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
50 routines? These are highly optimized and many do not carry the overhead
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
51 of being interpreted.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
52 @item
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
53 Does writing a dynamically linked function represent useful investment
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
54 of your time, relative to staying in Octave?
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
55 @end itemize
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
56
8475
cc3ad79fd6b7 [docs] octave => Octave
Brian Gough <bjg@gnu.org>
parents: 8347
diff changeset
57 Also, as oct- and mex-files are dynamically linked to Octave, they
8347
fa78cb8d8a5c corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents: 8097
diff changeset
58 introduce the possibility of Octave crashing due to errors in
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
59 the user code. For example a segmentation violation in the user's code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
60 will cause Octave to abort.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
61
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
62 @menu
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
63 * Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
64 * Mex-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
65 * Standalone Programs::
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
66 @end menu
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
67
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
68 @node Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
69 @section Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
70 @cindex oct-files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
71 @cindex mkoctfile
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
72 @cindex oct
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
73
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
74 @menu
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
75 * Getting Started with Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
76 * Matrices and Arrays in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
77 * Character Strings in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
78 * Cell Arrays in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
79 * Structures in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
80 * Sparse Matrices in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
81 * Accessing Global Variables in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
82 * Calling Octave Functions from Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
83 * Calling External Code from Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
84 * Allocating Local Memory in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
85 * Input Parameter Checking in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
86 * Exception and Error Handling in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
87 * Documentation and Test of Oct-Files::
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
88 @c * Application Programming Interface for Oct-Files::
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
89 @end menu
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
90
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
91 @node Getting Started with Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
92 @subsection Getting Started with Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
93
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
94 The basic command to build oct-files is @code{mkoctfile} and it can be
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
95 call from within octave or from the command line.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
96
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
97 @DOCSTRING(mkoctfile)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
98
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
99 Consider the short example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
100
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
101 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
102 @group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
103 @EXAMPLEFILE(helloworld.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
104 @end group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
105 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
106
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
107 This example although short introduces the basics of writing a C++
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
108 function that can be dynamically linked to Octave. The easiest way to
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
109 make available most of the definitions that might be necessary for an
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
110 oct-file in Octave is to use the @code{#include <octave/oct.h>}
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
111 header.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
112
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
113 The macro that defines the entry point into the dynamically loaded
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
114 function is @w{@code{DEFUN_DLD}}. This macro takes four arguments, these being
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
115
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
116 @enumerate 1
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
117 @item The function name as it will be seen in Octave,
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
118 @item The list of arguments to the function of type @code{octave_value_list},
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
119 @item The number of output arguments, which can and often is omitted if
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
120 not used, and
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
121 @item The string that will be seen as the help text of the function.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
122 @end enumerate
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
123
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
124 The return type of functions defined with @w{@code{DEFUN_DLD}} is always
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
125 @code{octave_value_list}.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
126
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
127 There are a couple of important considerations in the choice of function
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
128 name. Firstly, it must be a valid Octave function name and so must be a
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
129 sequence of letters, digits and underscores, not starting with a
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
130 digit. Secondly, as Octave uses the function name to define the filename
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
131 it attempts to find the function in, the function name in the @w{@code{DEFUN_DLD}}
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
132 macro must match the filename of the oct-file. Therefore, the above
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
133 function should be in a file @file{helloworld.cc}, and it should be
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
134 compiled to an oct-file using the command
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
135
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
136 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
137 mkoctfile helloworld.cc
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
138 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
139
8828
8463d1a2e544 Doc fixes.
Brian Gough <bjg@network-theory.co.uk>
parents: 8817
diff changeset
140 This will create a file called @file{helloworld.oct}, that is the compiled
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
141 version of the function. It should be noted that it is perfectly
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
142 acceptable to have more than one @w{@code{DEFUN_DLD}} function in a source
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
143 file. However, there must either be a symbolic link to the oct-file for
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
144 each of the functions defined in the source code with the @w{@code{DEFUN_DLD}}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
145 macro or the autoload (@ref{Function Files}) function should be used.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
146
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
147 The rest of this function then shows how to find the number of input
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
148 arguments, how to print through the octave pager, and return from the
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
149 function. After compiling this function as above, an example of its use
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
150 is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
151
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
152 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
153 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
154 helloworld (1, 2, 3)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
155 @print{} Hello World has 3 input arguments and 0 output arguments.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
156 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
157 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
158
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
159 @node Matrices and Arrays in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
160 @subsection Matrices and Arrays in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
161
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
162 Octave supports a number of different array and matrix classes, the
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
163 majority of which are based on the Array class. The exception is the
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
164 sparse matrix types discussed separately below. There are three basic
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
165 matrix types
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
166
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
167 @table @code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
168 @item Matrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
169 A double precision matrix class defined in dMatrix.h,
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
170 @item ComplexMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
171 A complex matrix class defined in CMatrix.h, and
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
172 @item BoolMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
173 A boolean matrix class defined in boolMatrix.h.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
174 @end table
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
175
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
176 These are the basic two-dimensional matrix types of octave. In
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
177 additional there are a number of multi-dimensional array types, these
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
178 being
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
179
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
180 @table @code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
181 @item NDArray
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
182 A double precision array class defined in @file{dNDArray.h}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
183 @item ComplexNDarray
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
184 A complex array class defined in @file{CNDArray.h}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
185 @item boolNDArray
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
186 A boolean array class defined in @file{boolNDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
187 @item int8NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
188 @itemx int16NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
189 @itemx int32NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
190 @itemx int64NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
191 8, 16, 32 and 64-bit signed array classes defined in
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
192 @file{int8NDArray.h}, @file{int16NDArray.h}, etc.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
193 @item uint8NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
194 @itemx uint16NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
195 @itemx uint32NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
196 @itemx uint64NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
197 8, 16, 32 and 64-bit unsigned array classes defined in
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
198 @file{uint8NDArray.h}, @file{uint16NDArray.h}, etc.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
199 @end table
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
200
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
201 There are several basic means of constructing matrices of
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
202 multi-dimensional arrays. Considering the @code{Matrix} type as an
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
203 example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
204
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
205 @itemize @bullet
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
206 @item
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
207 We can create an empty matrix or array with the empty constructor. For
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
208 example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
209
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
210 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
211 Matrix a;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
212 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
213
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
214 This can be used on all matrix and array types
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
215 @item
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
216 Define the dimensions of the matrix or array with a dim_vector. For
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
217 example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
218
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
219 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
220 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
221 dim_vector dv (2);
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
222 dv(0) = 2; dv(1) = 2;
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
223 Matrix a (dv);
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
224 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
225 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
226
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
227 This can be used on all matrix and array types
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
228 @item
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
229 Define the number of rows and columns in the matrix. For example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
230
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
231 @example
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
232 Matrix a (2, 2)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
233 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
234
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
235 However, this constructor can only be used with the matrix types.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
236 @end itemize
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
237
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
238 These types all share a number of basic methods and operators, a
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
239 selection of which include
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
240
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
241 @deftypefn Method T& {operator ()} (octave_idx_type)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
242 @deftypefnx Method T& elem (octave_idx_type)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
243 The @code{()} operator or @code{elem} method allow the values of the
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
244 matrix or array to be read or set. These can take a single argument,
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
245 which is of type @code{octave_idx_type}, that is the index into the matrix or
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
246 array. Additionally, the matrix type allows two argument versions of the
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
247 @code{()} operator and elem method, giving the row and column index of the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
248 value to obtain or set.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
249 @end deftypefn
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
250
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
251 Note that these functions do significant error checking and so in some
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
252 circumstances the user might prefer to access the data of the array or
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9906
diff changeset
253 matrix directly through the @nospell{fortran_vec} method discussed below.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
254
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
255 @deftypefn Method octave_idx_type nelem (void) const
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
256 The total number of elements in the matrix or array.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
257 @end deftypefn
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
258
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
259 @deftypefn Method size_t byte_size (void) const
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
260 The number of bytes used to store the matrix or array.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
261 @end deftypefn
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
262
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
263 @deftypefn Method dim_vector dims (void) const
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
264 The dimensions of the matrix or array in value of type dim_vector.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
265 @end deftypefn
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
266
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
267 @deftypefn Method void resize (const dim_vector&)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
268 A method taking either an argument of type @code{dim_vector}, or in the
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
269 case of a matrix two arguments of type @code{octave_idx_type} defining
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
270 the number of rows and columns in the matrix.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
271 @end deftypefn
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
272
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
273 @deftypefn Method T* fortran_vec (void)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
274 This method returns a pointer to the underlying data of the matrix or a
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
275 array so that it can be manipulated directly, either within Octave or by
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
276 an external library.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
277 @end deftypefn
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
278
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
279 Operators such an @code{+}, @code{-}, or @code{*} can be used on the
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
280 majority of the above types. In addition there are a number of methods
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
281 that are of interest only for matrices such as @code{transpose},
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
282 @code{hermitian}, @code{solve}, etc.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
283
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
284 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
285 @w{@code{DEFUN_DLD}} function is as follows
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
286
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
287 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
288 @group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
289 @EXAMPLEFILE(addtwomatrices.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
290 @end group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
291 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
292
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
293 To avoid segmentation faults causing Octave to abort, this function
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
294 explicitly checks that there are sufficient arguments available before
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
295 accessing these arguments. It then obtains two multi-dimensional arrays
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
296 of type @code{NDArray} and adds these together. Note that the array_value
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
297 method is called without using the @code{is_matrix_type} type, and instead the
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
298 error_state is checked before returning @code{A + B}. The reason to
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
299 prefer this is that the arguments might be a type that is not an
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
300 @code{NDArray}, but it would make sense to convert it to one. The
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
301 @code{array_value} method allows this conversion to be performed
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
302 transparently if possible, and sets @code{error_state} if it is not.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
303
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
304 @code{A + B}, operating on two @code{NDArray}'s returns an
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
305 @code{NDArray}, which is cast to an @code{octave_value} on the return
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
306 from the function. An example of the use of this demonstration function
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
307 is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
308
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
309 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
310 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
311 addtwomatrices (ones (2, 2), ones (2, 2))
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
312 @result{} 2 2
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
313 2 2
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
314 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
315 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
316
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
317 A list of the basic @code{Matrix} and @code{Array} types, the methods to
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
318 extract these from an @code{octave_value} and the associated header is
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
319 listed below.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
320
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
321 @multitable @columnfractions .3 .4 .3
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
322 @item @code{RowVector} @tab @code{row_vector_value} @tab @file{dRowVector.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
323 @item @code{ComplexRowVector} @tab @code{complex_row_vector_value} @tab @file{CRowVector.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
324 @item @code{ColumnVector} @tab @code{column_vector_value} @tab @file{dColVector.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
325 @item @code{ComplexColumnVector} @tab @code{complex_column_vector_value} @tab @file{CColVector.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
326 @item @code{Matrix} @tab @code{matrix_value} @tab @file{dMatrix.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
327 @item @code{ComplexMatrix} @tab @code{complex_matrix_value} @tab @file{CMatrix.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
328 @item @code{boolMatrix} @tab @code{bool_matrix_value} @tab @file{boolMatrix.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
329 @item @code{charMatrix} @tab @code{char_matrix_value} @tab @file{chMatrix.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
330 @item @code{NDArray} @tab @code{array_value} @tab @file{dNDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
331 @item @code{ComplexNDArray} @tab @code{complex_array_value} @tab @file{CNDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
332 @item @code{boolNDArray} @tab @code{bool_array_value} @tab @file{boolNDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
333 @item @code{charNDArray} @tab @code{char_array_value} @tab @file{charNDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
334 @item @code{int8NDArray} @tab @code{int8_array_value} @tab @file{int8NDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
335 @item @code{int16NDArray} @tab @code{int16_array_value} @tab @file{int16NDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
336 @item @code{int32NDArray} @tab @code{int32_array_value} @tab @file{int32NDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
337 @item @code{int64NDArray} @tab @code{int64_array_value} @tab @file{int64NDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
338 @item @code{uint8NDArray} @tab @code{uint8_array_value} @tab @file{uint8NDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
339 @item @code{uint16NDArray} @tab @code{uint16_array_value} @tab @file{uint16NDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
340 @item @code{uint32NDArray} @tab @code{uint32_array_value} @tab @file{uint32NDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
341 @item @code{uint64NDArray} @tab @code{uint64_array_value} @tab @file{uint64NDArray.h}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
342 @end multitable
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
343
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
344 @node Character Strings in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
345 @subsection Character Strings in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
346
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
347 In Octave a character string is just a special @code{Array} class.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
348 Consider the example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
349
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
350 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
351 @EXAMPLEFILE(stringdemo.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
352 @end example
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
353
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
354 An example of the use of this function is
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
355
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
356 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
357 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
358 s0 = ["First String"; "Second String"];
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
359 [s1,s2] = stringdemo (s0)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
360 @result{} s1 = Second String
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
361 First String
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
362
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
363 @result{} s2 = First String
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
364 Second String
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
365
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
366 typeinfo (s2)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
367 @result{} sq_string
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
368 typeinfo (s1)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
369 @result{} string
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
370 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
371 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
372
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
373 One additional complication of strings in Octave is the difference
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
374 between single quoted and double quoted strings. To find out if an
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
375 @code{octave_value} contains a single or double quoted string an example is
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
376
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
377 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
378 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
379 if (args(0).is_sq_string ())
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
380 octave_stdout <<
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
381 "First argument is a singularly quoted string\n";
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
382 else if (args(0).is_dq_string ())
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
383 octave_stdout <<
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
384 "First argument is a doubly quoted string\n";
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
385 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
386 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
387
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
388 Note however, that both types of strings are represented by the
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
389 @code{charNDArray} type, and so when assigning to an
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
390 @code{octave_value}, the type of string should be specified. For example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
391
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
392 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
393 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
394 octave_value_list retval;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
395 charNDArray c;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
396 @dots{}
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
397 // Create single quoted string
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
398 retval(1) = octave_value (ch, true, '\'');
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
399
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
400 // Create a double quoted string
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
401 retval(0) = octave_value (ch, true);
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
402 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
403 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
404
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
405 @node Cell Arrays in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
406 @subsection Cell Arrays in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
407
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
408 Octave's cell type is equally accessible within oct-files. A cell
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
409 array is just an array of @code{octave_value}s, and so each element of the cell
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
410 array can then be treated just like any other @code{octave_value}. A simple
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
411 example is
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
412
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
413 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
414 @group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
415 @EXAMPLEFILE(celldemo.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
416 @end group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
417 @end example
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
418
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
419 Note that cell arrays are used less often in standard oct-files and so
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
420 the @file{Cell.h} header file must be explicitly included. The rest of this
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
421 example extracts the @code{octave_value}s one by one from the cell array and
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
422 returns be as individual return arguments. For example consider
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
423
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
424 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
425 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
426 [b1, b2, b3] = celldemo (@{1, [1, 2], "test"@})
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
427 @result{}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
428 b1 = 1
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
429 b2 =
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
430
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
431 1 2
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
432
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
433 b3 = test
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
434 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
435 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
436
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
437 @node Structures in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
438 @subsection Structures in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
439
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
440 A structure in Octave is map between a number of fields represented and
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
441 their values. The Standard Template Library @code{map} class is used,
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
442 with the pair consisting of a @code{std::string} and an octave
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
443 @code{Cell} variable.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
444
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
445 A simple example demonstrating the use of structures within oct-files is
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
446
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
447 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
448 @EXAMPLEFILE(structdemo.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
449 @end example
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
450
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
451 An example of its use is
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
452
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
453 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
454 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
455 x.a = 1; x.b = "test"; x.c = [1, 2];
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
456 structdemo (x, "b")
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
457 @result{} selected = test
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
458 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
459 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
460
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
461 The commented code above demonstrates how to iterate over all of the
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
462 fields of the structure, where as the following code demonstrates finding
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
463 a particular field in a more concise manner.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
464
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
465 As can be seen the @code{contents} method of the @code{Octave_map} class
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
466 returns a @code{Cell} which allows structure arrays to be represented.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
467 Therefore, to obtain the underlying @code{octave_value} we write
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
468
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
469 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
470 octave_value tmp = arg0.contents (p1) (0);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
471 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
472
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
473 where the trailing (0) is the () operator on the @code{Cell} object. We
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
474 can equally iterate of the elements of the Cell array to address the
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
475 elements of the structure array.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
476
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
477 @node Sparse Matrices in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
478 @subsection Sparse Matrices in Oct-Files
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
479
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
480 There are three classes of sparse objects that are of interest to the
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
481 user.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
482
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
483 @table @code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
484 @item SparseMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
485 A double precision sparse matrix class
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
486 @item SparseComplexMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
487 A complex sparse matrix class
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
488 @item SparseBoolMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
489 A boolean sparse matrix class
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
490 @end table
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
491
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
492 All of these classes inherit from the @code{Sparse<T>} template class,
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
493 and so all have similar capabilities and usage. The @code{Sparse<T>}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
494 class was based on Octave @code{Array<T>} class, and so users familiar
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
495 with Octave's @code{Array} classes will be comfortable with the use of
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
496 the sparse classes.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
497
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
498 The sparse classes will not be entirely described in this section, due
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
499 to their similarity with the existing @code{Array} classes. However,
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
500 there are a few differences due the different nature of sparse objects,
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
501 and these will be described. Firstly, although it is fundamentally
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
502 possible to have N-dimensional sparse objects, the Octave sparse classes do
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
503 not allow them at this time. So all operations of the sparse classes
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
504 must be 2-dimensional. This means that in fact @code{SparseMatrix} is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
505 similar to Octave's @code{Matrix} class rather than its
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
506 @code{NDArray} class.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
507
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
508 @menu
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
509 * Array and Sparse Differences::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
510 * Creating Sparse Matrices in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
511 * Using Sparse Matrices in Oct-Files::
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
512 @end menu
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
513
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
514 @node Array and Sparse Differences
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
515 @subsubsection The Differences between the Array and Sparse Classes
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
516
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
517 The number of elements in a sparse matrix is considered to be the number
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
518 of non-zero elements rather than the product of the dimensions. Therefore
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
519
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
520 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
521 @group
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
522 SparseMatrix sm;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
523 @dots{}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
524 int nel = sm.nelem ();
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
525 @end group
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
526 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
527
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
528 returns the number of non-zero elements. If the user really requires the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
529 number of elements in the matrix, including the non-zero elements, they
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
530 should use @code{numel} rather than @code{nelem}. Note that for very
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
531 large matrices, where the product of the two dimensions is larger than
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
532 the representation of an unsigned int, then @code{numel} can overflow.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
533 An example is @code{speye(1e6)} which will create a matrix with a million
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
534 rows and columns, but only a million non-zero elements. Therefore the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
535 number of rows by the number of columns in this case is more than two
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
536 hundred times the maximum value that can be represented by an unsigned int.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
537 The use of @code{numel} should therefore be avoided useless it is known
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
538 it won't overflow.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
539
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
540 Extreme care must be take with the elem method and the "()" operator,
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
541 which perform basically the same function. The reason is that if a
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
542 sparse object is non-const, then Octave will assume that a
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
543 request for a zero element in a sparse matrix is in fact a request
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
544 to create this element so it can be filled. Therefore a piece of
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
545 code like
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
546
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
547 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
548 @group
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
549 SparseMatrix sm;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
550 @dots{}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
551 for (int j = 0; j < nc; j++)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
552 for (int i = 0; i < nr; i++)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
553 std::cerr << " (" << i << "," << j << "): " << sm(i,j)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
554 << std::endl;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
555 @end group
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
556 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
557
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
558 is a great way of turning the sparse matrix into a dense one, and a
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
559 very slow way at that since it reallocates the sparse object at each
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
560 zero element in the matrix.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
561
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
562 An easy way of preventing the above from happening is to create a temporary
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
563 constant version of the sparse matrix. Note that only the container for
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
564 the sparse matrix will be copied, while the actual representation of the
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
565 data will be shared between the two versions of the sparse matrix. So this
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
566 is not a costly operation. For example, the above would become
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
567
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
568 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
569 @group
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
570 SparseMatrix sm;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
571 @dots{}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
572 const SparseMatrix tmp (sm);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
573 for (int j = 0; j < nc; j++)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
574 for (int i = 0; i < nr; i++)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
575 std::cerr << " (" << i << "," << j << "): " << tmp(i,j)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
576 << std::endl;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
577 @end group
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
578 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
579
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
580 Finally, as the sparse types aren't just represented as a contiguous
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9906
diff changeset
581 block of memory, the @nospell{@code{fortran_vec}} method of the @code{Array<T>}
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
582 is not available. It is however replaced by three separate methods
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
583 @code{ridx}, @code{cidx} and @code{data}, that access the raw compressed
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
584 column format that the Octave sparse matrices are stored in.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
585 Additionally, these methods can be used in a manner similar to @code{elem},
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
586 to allow the matrix to be accessed or filled. However, in that case it is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
587 up to the user to respect the sparse matrix compressed column format
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
588 discussed previous.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
589
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
590 @node Creating Sparse Matrices in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
591 @subsubsection Creating Sparse Matrices in Oct-Files
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
592
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
593 You have several alternatives for creating a sparse matrix.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
594 You can first create the data as three vectors representing the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
595 row and column indexes and the data, and from those create the matrix.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
596 Or alternatively, you can create a sparse matrix with the appropriate
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
597 amount of space and then fill in the values. Both techniques have their
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
598 advantages and disadvantages.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
599
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
600 Here is an example of how to create a small sparse matrix with the first
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
601 technique
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
602
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
603 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
604 @group
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
605 int nz = 4, nr = 3, nc = 4;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
606
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
607 ColumnVector ridx (nz);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
608 ColumnVector cidx (nz);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
609 ColumnVector data (nz);
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
610
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
611 ridx(0) = 0; ridx(1) = 0; ridx(2) = 1; ridx(3) = 2;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
612 cidx(0) = 0; cidx(1) = 1; cidx(2) = 3; cidx(3) = 3;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
613 data(0) = 1; data(1) = 2; data(2) = 3; data(3) = 4;
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
614
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
615 SparseMatrix sm (data, ridx, cidx, nr, nc);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
616 @end group
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
617 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
618
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
619 @noindent
8817
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8486
diff changeset
620 which creates the matrix given in section
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8486
diff changeset
621 @ref{Storage of Sparse Matrices}. Note that the compressed matrix
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8486
diff changeset
622 format is not used at the time of the creation of the matrix itself,
03b7f618ab3d include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents: 8486
diff changeset
623 however it is used internally.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
624
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
625 As previously mentioned, the values of the sparse matrix are stored
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
626 in increasing column-major ordering. Although the data passed by the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
627 user does not need to respect this requirement, the pre-sorting the
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
628 data significantly speeds up the creation of the sparse matrix.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
629
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
630 The disadvantage of this technique of creating a sparse matrix is
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
631 that there is a brief time where two copies of the data exists. Therefore
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
632 for extremely memory constrained problems this might not be the right
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
633 technique to create the sparse matrix.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
634
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
635 The alternative is to first create the sparse matrix with the desired
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
636 number of non-zero elements and then later fill those elements in. The
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
637 easiest way to do this is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
638
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
639 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
640 @group
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
641 int nz = 4, nr = 3, nc = 4;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
642 SparseMatrix sm (nr, nc, nz);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
643 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
644 @end group
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
645 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
646
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
647 That creates the same matrix as previously. Again, although it is not
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
648 strictly necessary, it is significantly faster if the sparse matrix is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
649 created in this manner that the elements are added in column-major
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
650 ordering. The reason for this is that if the elements are inserted
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
651 at the end of the current list of known elements then no element
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
652 in the matrix needs to be moved to allow the new element to be
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
653 inserted. Only the column indexes need to be updated.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
654
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
655 There are a few further points to note about this technique of creating
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
656 a sparse matrix. Firstly, it is possible to create a sparse matrix
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
657 with fewer elements than are actually inserted in the matrix. Therefore
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
658
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
659 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
660 @group
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
661 int nz = 4, nr = 3, nc = 4;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
662 SparseMatrix sm (nr, nc, 0);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
663 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
664 @end group
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
665 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
666
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
667 @noindent
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
668 is perfectly valid. However it is a very bad idea. The reason is that
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
669 as each new element is added to the sparse matrix the space allocated
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
670 to it is increased by reallocating the memory. This is an expensive
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
671 operation, that will significantly slow this means of creating a sparse
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
672 matrix. Furthermore, it is possible to create a sparse matrix with
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
673 too much storage, so having @var{nz} above equaling 6 is also valid.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
674 The disadvantage is that the matrix occupies more memory than strictly
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
675 needed.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
676
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
677 It is not always easy to know the number of non-zero elements prior
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
678 to filling a matrix. For this reason the additional storage for the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
679 sparse matrix can be removed after its creation with the
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
680 @dfn{maybe_compress} function. Furthermore, the maybe_compress can
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
681 deallocate the unused storage, but it can equally remove zero elements
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
682 from the matrix. The removal of zero elements from the matrix is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
683 controlled by setting the argument of the @dfn{maybe_compress} function
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
684 to be @samp{true}. However, the cost of removing the zeros is high because it
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
685 implies resorting the elements. Therefore, if possible it is better
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
686 is the user doesn't add the zeros in the first place. An example of
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
687 the use of @dfn{maybe_compress} is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
688
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
689 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
690 @group
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
691 int nz = 6, nr = 3, nc = 4;
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
692
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
693 SparseMatrix sm1 (nr, nc, nz);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
694 sm1(0,0) = 1; sm1(0,1) = 2; sm1(1,3) = 3; sm1(2,3) = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
695 sm1.maybe_compress (); // No zero elements were added
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
696
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
697 SparseMatrix sm2 (nr, nc, nz);
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
698 sm2(0,0) = 1; sm2(0,1) = 2; sm(0,2) = 0; sm(1,2) = 0;
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
699 sm1(1,3) = 3; sm1(2,3) = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
700 sm2.maybe_compress (true); // Zero elements were added
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
701 @end group
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
702 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
703
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
704 The use of the @dfn{maybe_compress} function should be avoided if
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
705 possible, as it will slow the creation of the matrices.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
706
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
707 A third means of creating a sparse matrix is to work directly with
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
708 the data in compressed row format. An example of this technique might
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
709 be
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
710
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9906
diff changeset
711 @c Note the @verbatim environment is a relatively new addition to Texinfo.
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
712 @c Therefore use the @example environment and replace @, with @@,
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
713 @c { with @{, etc
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
714
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
715 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
716 octave_value arg;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
717 @dots{}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
718 int nz = 6, nr = 3, nc = 4; // Assume we know the max no nz
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
719 SparseMatrix sm (nr, nc, nz);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
720 Matrix m = arg.matrix_value ();
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
721
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
722 int ii = 0;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
723 sm.cidx (0) = 0;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
724 for (int j = 1; j < nc; j++)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
725 @{
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
726 for (int i = 0; i < nr; i++)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
727 @{
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
728 double tmp = foo (m(i,j));
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
729 if (tmp != 0.)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
730 @{
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
731 sm.data(ii) = tmp;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
732 sm.ridx(ii) = i;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
733 ii++;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
734 @}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
735 @}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
736 sm.cidx(j+1) = ii;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
737 @}
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
738 sm.maybe_compress (); // If don't know a-priori
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
739 // the final no of nz.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
740 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
741
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
742 @noindent
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
743 which is probably the most efficient means of creating the sparse matrix.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
744
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
745 Finally, it might sometimes arise that the amount of storage initially
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
746 created is insufficient to completely store the sparse matrix. Therefore,
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
747 the method @code{change_capacity} exists to reallocate the sparse memory.
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
748 The above example would then be modified as
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
749
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
750 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
751 octave_value arg;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
752 @dots{}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
753 int nz = 6, nr = 3, nc = 4; // Assume we know the max no nz
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
754 SparseMatrix sm (nr, nc, nz);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
755 Matrix m = arg.matrix_value ();
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
756
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
757 int ii = 0;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
758 sm.cidx (0) = 0;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
759 for (int j = 1; j < nc; j++)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
760 @{
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
761 for (int i = 0; i < nr; i++)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
762 @{
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
763 double tmp = foo (m(i,j));
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
764 if (tmp != 0.)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
765 @{
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
766 if (ii == nz)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
767 @{
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
768 nz += 2; // Add 2 more elements
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
769 sm.change_capacity (nz);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
770 @}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
771 sm.data(ii) = tmp;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
772 sm.ridx(ii) = i;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
773 ii++;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
774 @}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
775 @}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
776 sm.cidx(j+1) = ii;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
777 @}
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
778 sm.maybe_mutate (); // If don't know a-priori
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
779 // the final no of nz.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
780 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
781
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
782 Note that both increasing and decreasing the number of non-zero elements in
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
783 a sparse matrix is expensive, as it involves memory reallocation. Also as
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
784 parts of the matrix, though not its entirety, exist as the old and new copy
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
785 at the same time, additional memory is needed. Therefore if possible this
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
786 should be avoided.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
787
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
788 @node Using Sparse Matrices in Oct-Files
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
789 @subsubsection Using Sparse Matrices in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
790
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
791 Most of the same operators and functions on sparse matrices that are
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
792 available from the Octave are equally available with oct-files.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
793 The basic means of extracting a sparse matrix from an @code{octave_value}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
794 and returning them as an @code{octave_value}, can be seen in the
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
795 following example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
796
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
797 @example
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
798 @group
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
799 octave_value_list retval;
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
800
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
801 SparseMatrix sm = args(0).sparse_matrix_value ();
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
802 SparseComplexMatrix scm =
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
803 args(1).sparse_complex_matrix_value ();
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
804 SparseBoolMatrix sbm = args(2).sparse_bool_matrix_value ();
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
805 @dots{}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
806 retval(2) = sbm;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
807 retval(1) = scm;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
808 retval(0) = sm;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
809 @end group
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
810 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
811
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
812 The conversion to an octave-value is handled by the sparse
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
813 @code{octave_value} constructors, and so no special care is needed.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
814
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
815 @node Accessing Global Variables in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
816 @subsection Accessing Global Variables in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
817
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
818 Global variables allow variables in the global scope to be
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
819 accessed. Global variables can easily be accessed with oct-files using
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
820 the support functions @code{get_global_value} and
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
821 @code{set_global_value}. @code{get_global_value} takes two arguments,
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
822 the first is a string representing the variable name to obtain. The
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
823 second argument is a boolean argument specifying what to do in the case
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
824 that no global variable of the desired name is found. An example of the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
825 use of these two functions is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
826
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
827 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
828 @EXAMPLEFILE(globaldemo.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
829 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
830
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
831 An example of its use is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
832
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
833 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
834 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
835 global a b
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
836 b = 10;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
837 globaldemo ("b")
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
838 @result{} 10
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
839 globaldemo ("c")
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
840 @result{} "Global variable not found"
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
841 num2str (a)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
842 @result{} 42
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
843 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
844 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
845
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
846 @node Calling Octave Functions from Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
847 @subsection Calling Octave Functions from Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
848
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
849 There is often a need to be able to call another octave function from
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
850 within an oct-file, and there are many examples of such within octave
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
851 itself. For example the @code{quad} function is an oct-file that
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
852 calculates the definite integral by quadrature over a user supplied
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
853 function.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
854
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
855 There are also many ways in which a function might be passed. It might
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
856 be passed as one of
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
857
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
858 @enumerate 1
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
859 @item Function Handle
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
860 @item Anonymous Function Handle
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
861 @item Inline Function
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
862 @item String
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
863 @end enumerate
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
864
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
865 The example below demonstrates an example that accepts all four means of
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
866 passing a function to an oct-file.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
867
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
868 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
869 @EXAMPLEFILE(funcdemo.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
870 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
871
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
872 The first argument to this demonstration is the user supplied function
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
873 and the following arguments are all passed to the user function.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
874
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
875 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
876 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
877 funcdemo (@@sin,1)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
878 @result{} 0.84147
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
879 funcdemo (@@(x) sin(x), 1)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
880 @result{} 0.84147
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
881 funcdemo (inline ("sin(x)"), 1)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
882 @result{} 0.84147
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
883 funcdemo ("sin",1)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
884 @result{} 0.84147
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
885 funcdemo (@@atan2, 1, 1)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
886 @result{} 0.78540
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
887 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
888 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
889
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
890 When the user function is passed as a string, the treatment of the
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
891 function is different. In some cases it is necessary to always have the
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
892 user supplied function as an @code{octave_function} object. In that
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
893 case the string argument can be used to create a temporary function like
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
894
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
895 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
896 @group
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
897 std::octave fcn_name = unique_symbol_name ("__fcn__");
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
898 std::string fname = "function y = ";
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
899 fname.append (fcn_name);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
900 fname.append ("(x) y = ");
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
901 fcn = extract_function (args(0), "funcdemo", fcn_name,
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
902 fname, "; endfunction");
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
903 @dots{}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
904 if (fcn_name.length ())
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
905 clear_function (fcn_name);
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
906 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
907 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
908
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
909 There are two important things to know in this case. The number of input
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
910 arguments to the user function is fixed, and in the above is a single
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
911 argument, and secondly to avoid leaving the temporary function in the
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
912 Octave symbol table it should be cleared after use.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
913
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
914 @node Calling External Code from Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
915 @subsection Calling External Code from Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
916
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
917 Linking external C code to Octave is relatively simple, as the C
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
918 functions can easily be called directly from C++. One possible issue is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
919 the declarations of the external C functions might need to be explicitly
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
920 defined as C functions to the compiler. If the declarations of the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
921 external C functions are in the header @code{foo.h}, then the manner in
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
922 which to ensure that the C++ compiler treats these declarations as C
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
923 code is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
924
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
925 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
926 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
927 #ifdef __cplusplus
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
928 extern "C"
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
929 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
930 #endif
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
931 #include "foo.h"
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
932 #ifdef __cplusplus
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
933 @} /* end extern "C" */
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
934 #endif
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
935 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
936 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
937
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
938 Calling Fortran code however can pose some difficulties. This is due to
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
939 differences in the manner in compilers treat the linking of Fortran code
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
940 with C or C++ code. Octave supplies a number of macros that allow
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
941 consistent behavior across a number of compilers.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
942
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
943 The underlying Fortran code should use the @code{XSTOPX} function to
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
944 replace the Fortran @code{STOP} function. @code{XSTOPX} uses the Octave
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9906
diff changeset
945 exception handler to treat failing cases in the Fortran code
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
946 explicitly. Note that Octave supplies its own replacement @sc{blas}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
947 @code{XERBLA} function, which uses @code{XSTOPX}.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
948
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
949 If the underlying code calls @code{XSTOPX}, then the @w{@code{F77_XFCN}}
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9906
diff changeset
950 macro should be used to call the underlying Fortran function. The Fortran
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
951 exception state can then be checked with the global variable
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
952 @code{f77_exception_encountered}. If @code{XSTOPX} will not be called,
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
953 then the @w{@code{F77_FCN}} macro should be used instead to call the Fortran
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
954 code.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
955
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
956 There is no harm in using @w{@code{F77_XFCN}} in all cases, except that for
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
957 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
958 there is potentially an overhead in doing so. However, if @w{@code{F77_FCN}}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
959 is used with code that calls @code{XSTOP}, Octave can generate a
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
960 segmentation fault.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
961
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
962 An example of the inclusion of a Fortran function in an oct-file is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
963 given in the following example, where the C++ wrapper is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
964
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
965 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
966 @EXAMPLEFILE(fortdemo.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
967 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
968
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
969 @noindent
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9906
diff changeset
970 and the Fortran function is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
971
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
972 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
973 @EXAMPLEFILE(fortsub.f)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
974 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
975
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
976 This example demonstrates most of the features needed to link to an
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
977 external Fortran function, including passing arrays and strings, as well
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
978 as exception handling. An example of the behavior of this function is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
979
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
980 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
981 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
982 [b, s] = fortdemo (1:3)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
983 @result{}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
984 b = 1.00000 0.50000 0.33333
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
985 s = There are 3 values in the input vector
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
986 [b, s] = fortdemo(0:3)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
987 error: fortsub:divide by zero
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
988 error: exception encountered in Fortran subroutine fortsub_
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9906
diff changeset
989 error: fortdemo: error in Fortran
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
990 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
991 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
992
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
993 @node Allocating Local Memory in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
994 @subsection Allocating Local Memory in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
995
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
996 Allocating memory within an oct-file might seem easy as the C++
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
997 new/delete operators can be used. However, in that case care must be
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
998 taken to avoid memory leaks. The preferred manner in which to allocate
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
999 memory for use locally is to use the @w{@code{OCTAVE_LOCAL_BUFFER}} macro.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1000 An example of its use is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1001
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1002 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1003 OCTAVE_LOCAL_BUFFER (double, tmp, len)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1004 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1005
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1006 that returns a pointer @code{tmp} of type @code{double *} of length
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1007 @code{len}.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1008
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1009 @node Input Parameter Checking in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1010 @subsection Input Parameter Checking in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1011
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1012 As oct-files are compiled functions they have the possibility of causing
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
1013 Octave to abort abnormally. It is therefore important that
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
1014 each and every function has the minimum of parameter
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1015 checking needed to ensure that Octave behaves well.
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1016
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1017 The minimum requirement, as previously discussed, is to check the number
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1018 of input arguments before using them to avoid referencing a non existent
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1019 argument. However, it some case this might not be sufficient as the
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1020 underlying code imposes further constraints. For example an external
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1021 function call might be undefined if the input arguments are not
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1022 integers, or if one of the arguments is zero. Therefore, oct-files often
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1023 need additional input parameter checking.
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1024
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1025 There are several functions within Octave that might be useful for the
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1026 purposes of parameter checking. These include the methods of the
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1027 octave_value class like @code{is_real_matrix}, etc., but equally include
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1028 more specialized functions. Some of the more common ones are
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1029 demonstrated in the following example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1030
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1031 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1032 @EXAMPLEFILE(paramdemo.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1033 @end example
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1034
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1035 @noindent
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1036 and an example of its use is
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1037
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1038 @example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1039 @group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1040 paramdemo ([1, 2, NaN, Inf])
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1041 @result{} Properties of input array:
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1042 includes Inf or NaN values
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1043 includes other values than 1 and 0
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1044 includes only int, Inf or NaN values
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1045 @end group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1046 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1047
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1048 @node Exception and Error Handling in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1049 @subsection Exception and Error Handling in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1050
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1051 Another important feature of Octave is its ability to react to the user
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1052 typing @kbd{Control-C} even during calculations. This ability is based on the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1053 C++ exception handler, where memory allocated by the C++ new/delete
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1054 methods are automatically released when the exception is treated. When
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1055 writing an oct-file, to allow Octave to treat the user typing @kbd{Control-C},
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
1056 the @w{@code{OCTAVE_QUIT}} macro is supplied. For example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1057
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1058 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1059 @group
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
1060 for (octave_idx_type i = 0; i < a.nelem (); i++)
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
1061 @{
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
1062 OCTAVE_QUIT;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
1063 b.elem(i) = 2. * a.elem(i);
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
1064 @}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1065 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1066 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1067
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
1068 The presence of the @w{@code{OCTAVE_QUIT}} macro in the inner loop allows Octave to
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1069 treat the user request with the @kbd{Control-C}. Without this macro, the user
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1070 must either wait for the function to return before the interrupt is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1071 processed, or press @kbd{Control-C} three times to force Octave to exit.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1072
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
1073 The @w{@code{OCTAVE_QUIT}} macro does impose a very small speed penalty, and so for
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1074 loops that are known to be small it might 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
1075 @w{@code{OCTAVE_QUIT}}.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1076
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1077 When creating an oct-file that uses an external libraries, the function
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1078 might spend a significant portion of its time in the external
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
1079 library. It is not generally possible to use the @w{@code{OCTAVE_QUIT}} macro in
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1080 this case. The alternative in this case is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1081
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1082 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1083 @group
6577
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
1084 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
1085 @dots{} some code that calls a "foreign" function @dots{}
f72d6d4b735a [project @ 2007-04-25 15:54:59 by jwe]
jwe
parents: 6572
diff changeset
1086 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1087 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1088 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1089
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1090 The disadvantage of this is that if the foreign code allocates any
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1091 memory internally, then this memory might be lost during an interrupt,
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1092 without being deallocated. Therefore, ideally Octave itself should
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1093 allocate any memory that is needed by the foreign code, with either the
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 9906
diff changeset
1094 @nospell{fortran_vec} method or the @w{@code{OCTAVE_LOCAL_BUFFER}} macro.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1095
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
1096 The Octave unwind_protect mechanism (@ref{The @code{unwind_protect} Statement})
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1097 can also be used in oct-files. In conjunction with the exception
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1098 handling of Octave, it is important to enforce that certain code is run
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1099 to allow variables, etc. to be restored even if an exception occurs. An
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1100 example of the use of this mechanism is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1101
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1102 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1103 @EXAMPLEFILE(unwinddemo.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1104 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1105
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1106 As can be seen in the example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1107
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1108 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1109 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1110 unwinddemo (1, 0)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1111 @result{} Inf
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1112 1 / 0
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1113 @result{} warning: division by zero
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1114 Inf
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1115 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1116 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1117
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1118 The division by zero (and in fact all warnings) is disabled in the
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1119 @code{unwinddemo} function.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1120
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1121 @node Documentation and Test of Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1122 @subsection Documentation and Test of Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1123
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1124 The documentation of an oct-file is the fourth string parameter of the
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
1125 @w{@code{DEFUN_DLD}} macro. This string can be formatted in the same manner
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1126 as the help strings for user functions (@ref{Documentation Tips}),
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1127 however there are some issue that are particular to the formatting of
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1128 help strings within oct-files.
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1129
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1130 The major issue is that the help string will typically be longer than a
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1131 single line of text, and so the formatting of long help strings need to
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
1132 be taken into account. There are several manners in which to treat this
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1133 issue, but the most common is illustrated in the following example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1134
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1135 @example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1136 @group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1137 DEFUN_DLD (do_what_i_want, args, nargout,
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1138 "-*- texinfo -*-\n\
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1139 @@deftypefn @{Function File@} @{@} do_what_i_say (@@var@{n@})\n\
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1140 A function that does what the user actually wants rather\n\
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1141 than what they requested.\n\
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1142 @@end deftypefn")
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1143 @{
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1144 @dots{}
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1145 @}
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1146 @end group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1147 @end example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1148
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1149 @noindent
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1150 where, as can be seen, end line of text within the help string is
8347
fa78cb8d8a5c corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents: 8097
diff changeset
1151 terminated by @code{\n\} which is an embedded new-line in the string
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1152 together with a C++ string continuation character. Note that the final
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1153 @code{\} must be the last character on the line.
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1154
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1155 Octave also includes the ability to embed the test and demonstration
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1156 code for a function within the code itself (@ref{Test and Demo Functions}).
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1157 This can be used from within oct-files (or in fact any file) with
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1158 certain provisos. Firstly, the test and demo functions of Octave look
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1159 for a @code{%!} as the first characters on a new-line to identify test
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1160 and demonstration code. This is equally a requirement for
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1161 oct-files. Furthermore the test and demonstration code must be included
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1162 in a comment block of the compiled code to avoid it being interpreted by
6606
2c19eaa2c6f7 [project @ 2007-05-07 17:09:38 by jwe]
jwe
parents: 6593
diff changeset
1163 the compiler. Finally, the Octave test and demonstration code must have
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1164 access to the source code of the oct-file and not just the compiled code
6606
2c19eaa2c6f7 [project @ 2007-05-07 17:09:38 by jwe]
jwe
parents: 6593
diff changeset
1165 as the tests are stripped from the compiled code. An example in an
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1166 oct-file might be
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1167
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1168 @example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1169 @group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1170 /*
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1171
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1172 %!error (sin())
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1173 %!error (sin(1,1))
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1174 %!assert (sin([1,2]),[sin(1),sin(2)])
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1175
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1176 */
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1177 @end group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1178 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1179
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1180 @c @node Application Programming Interface for Oct-Files
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1181 @c @subsection Application Programming Interface for Oct-Files
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1182 @c
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1183 @c WRITE ME, using Coda section 1.3 as a starting point.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1184
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1185 @node Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1186 @section Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1187 @cindex mex-files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1188 @cindex mex
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1189
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1190 Octave includes an interface to allow legacy mex-files to be compiled
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1191 and used with Octave. This interface can also be used to share code
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1192 between Octave and non Octave users. However, as mex-files expose the
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1193 internal API of an alternative product to Octave, and the internal
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1194 structure of Octave is different to this product, a mex-file can never
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1195 have the same performance in Octave as the equivalent oct-file. In
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1196 particular to support the manner in which mex-files access the variables
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1197 passed to mex functions, there are a significant number of additional
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1198 copies of memory when calling or returning from a mex function. For
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1199 this reason, new code should be written using the oct-file interface
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1200 discussed above if possible.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1201
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1202 @menu
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1203 * Getting Started with Mex-Files::
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1204 * Working with Matrices and Arrays in Mex-Files::
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1205 * Character Strings in Mex-Files::
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1206 * Cell Arrays with Mex-Files::
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1207 * Structures with Mex-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1208 * Sparse Matrices with Mex-Files::
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1209 * Calling Other Functions in Mex-Files::
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1210 @c * Application Programming Interface for Mex-Files::
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1211 @end menu
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1212
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1213 @node Getting Started with Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1214 @subsection Getting Started with Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1215
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1216 The basic command to build a mex-file is either @code{mkoctfile --mex} or
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1217 @code{mex}. The first can either be used from within Octave or from the
8486
fe1bea543cd4 [docs] add
Brian Gough <bjg@gnu.org>
parents: 8475
diff changeset
1218 command line. However, to avoid issues with the installation of other
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1219 products, the use of the command @code{mex} is limited to within Octave.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1220
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1221 @DOCSTRING(mex)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1222
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1223 @DOCSTRING(mexext)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1224
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1225 One important difference between the use of mex with other products and
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1226 with Octave is that the header file "matrix.h" is implicitly included
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1227 through the inclusion of "mex.h". This is to avoid a conflict with the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1228 Octave file "Matrix.h" with operating systems and compilers that don't
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1229 distinguish between filenames in upper and lower case
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1230
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1231 Consider the short example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1232
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1233 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1234 @group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1235 @EXAMPLEFILE(firstmexdemo.c)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1236 @end group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1237 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1238
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1239 This simple example demonstrates the basics of writing a mex-file. The
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1240 entry point into the mex-file is defined by @code{mexFunction}. Note
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1241 that the function name is not explicitly included in the
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1242 @code{mexFunction} and so there can only be a single @code{mexFunction}
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1243 entry point per-file. Also the name of the function is determined by the
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1244 name of the mex-file itself. Therefore if the above function is in the
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1245 file @file{firstmexdemo.c}, it can be compiled with
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1246
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1247 @example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1248 mkoctfile --mex firstmexdemo.c
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1249 @end example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1250
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1251 @noindent
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1252 which creates a file @file{firstmexdemo.mex}. The function can then be run
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1253 from Octave as
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1254
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1255 @example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1256 @group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1257 firstmexdemo()
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1258 @result{} 1.2346
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1259 @end group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1260 @end example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1261
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1262 It should be noted that the mex-file contains no help string for the
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1263 functions it contains. To document mex-files, there should exist an
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1264 m-file in the same directory as the mex-file itself. Taking the above as
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1265 an example, we would therefore have a file @file{firstmexdemo.m} that might
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1266 contain the text
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1267
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1268 @example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1269 %FIRSTMEXDEMO Simple test of the functionality of a mex-file.
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1270 @end example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1271
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1272 In this case, the function that will be executed within Octave will be
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1273 given by the mex-file, while the help string will come from the
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1274 m-file. This can also be useful to allow a sample implementation of the
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1275 mex-file within the Octave language itself for testing purposes.
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1276
8347
fa78cb8d8a5c corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents: 8097
diff changeset
1277 Although we cannot have multiple entry points into a single mex-file,
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1278 we can use the @code{mexFunctionName} function to determine what name
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1279 the mex-file was called with. This can be used to alter the behavior of
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1280 the mex-file based on the function name. For example if
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1281
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1282 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1283 @group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1284 @EXAMPLEFILE(myfunc.c)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1285 @end group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1286 @end example
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1287
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1288 @noindent
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1289 is in file @file{myfunc.c}, and it is compiled with
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1290
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1291 @example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1292 @group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1293 mkoctfile --mex myfunc.c
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1294 ln -s myfunc.mex myfunc2.mex
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1295 @end group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1296 @end example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1297
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1298 Then as can be seen by
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1299
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1300 @example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1301 @group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1302 myfunc()
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1303 @result{} You called function: myfunc
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1304 This is the principal function
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1305 myfunc2()
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1306 @result{} You called function: myfunc2
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1307 @end group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1308 @end example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1309
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1310 @noindent
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1311 the behavior of the mex-file can be altered depending on the functions
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1312 name.
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1313
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1314 Allow the user should only include @code{mex.h} in their code, Octave
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1315 declares additional functions, typedefs, etc., available to the user to
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1316 write mex-files in the headers @code{mexproto.h} and @code{mxarray.h}.
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1317
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1318 @node Working with Matrices and Arrays in Mex-Files
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1319 @subsection Working with Matrices and Arrays in Mex-Files
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1320
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1321 The basic mex type of all variables is @code{mxArray}. All variables,
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1322 such as matrices, cell arrays or structures are all stored in this basic
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1323 type, and this type serves basically the same purpose as the
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1324 octave_value class in oct-files. That is it acts as a container for the
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1325 more specialized types.
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1326
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1327 The @code{mxArray} structure contains at a minimum, the variable it
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1328 represents name, its dimensions, its type and whether the variable is
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1329 real or complex. It can however contain a number of additional fields
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1330 depending on the type of the @code{mxArray}. There are a number of
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1331 functions to create @code{mxArray} structures, including
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1332 @code{mxCreateCellArray}, @code{mxCreateSparse} and the generic
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1333 @code{mxCreateNumericArray}.
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1334
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1335 The basic functions to access the data contained in an array is
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1336 @code{mxGetPr}. As the mex interface assumes that the real and imaginary
6939
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6686
diff changeset
1337 parts of a complex array are stored separately, there is an equivalent
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1338 function @code{mxGetPi} that get the imaginary part. Both of these
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1339 functions are for use only with double precision matrices. There also
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1340 exists the generic function @code{mxGetData} and @code{mxGetImagData}
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1341 that perform the same operation on all matrix types. For example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1342
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1343 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1344 @group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1345 mxArray *m;
6686
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1346 mwSize *dims;
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1347 UINT32_T *pr;
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1348
6686
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1349 dims = (mwSize *) mxMalloc (2 * sizeof(mwSize));
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1350 dims[0] = 2;
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1351 dims[1] = 2;
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1352 m = mxCreateNumericArray (2, dims, mxUINT32_CLASS, mxREAL);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1353 pr = = (UINT32_T *) mxGetData (m);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1354 @end group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1355 @end example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1356
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1357 There are also the functions @code{mxSetPr}, etc., that perform the
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1358 inverse, and set the data of an Array to use the block of memory pointed
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1359 to by the argument of @code{mxSetPr}.
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1360
6686
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1361 Note the type @code{mwSize} used above, and @code{mwIndex} are defined
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1362 as the native precision of the indexing in Octave on the platform on
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1363 which the mex-file is built. This allows both 32- and 64-bit platforms
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1364 to support mex-files. @code{mwSize} is used to define array dimension
6686
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1365 and maximum number or elements, while @code{mwIndex} is used to define
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1366 indexing into arrays.
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1367
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1368 An example that demonstration how to work with arbitrary real or complex
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1369 double precision arrays is given by the file @file{mypow2.c} as given
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1370 below.
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1371
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1372 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1373 @EXAMPLEFILE(mypow2.c)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1374 @end example
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1375
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1376 @noindent
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1377 with an example of its use
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1378
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1379 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1380 @group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1381 b = randn(4,1) + 1i * randn(4,1);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1382 all(b.^2 == mypow2(b))
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1383 @result{} 1
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1384 @end group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1385 @end example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1386
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1387
7096
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1388 The example above uses the functions @code{mxGetDimensions},
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1389 @code{mxGetNumberOfElements}, and @code{mxGetNumberOfDimensions} to work
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1390 with the dimensions of multi-dimensional arrays. The functions
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1391 @code{mxGetM}, and @code{mxGetN} are also available to find the number
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1392 of rows and columns in a matrix.
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1393
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1394 @node Character Strings in Mex-Files
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1395 @subsection Character Strings in Mex-Files
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1396
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1397 As mex-files do not make the distinction between single and double
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1398 quoted strings within Octave, there is perhaps less complexity in the
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1399 use of strings and character matrices in mex-files. An example of their
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1400 use, that parallels the demo in @file{stringdemo.cc}, is given in the
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1401 file @file{mystring.c}, as seen below.
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1402
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1403 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1404 @EXAMPLEFILE(mystring.c)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1405 @end example
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1406
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1407 @noindent
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1408 An example of its expected output is
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1409
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1410 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1411 @group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1412 mystring(["First String"; "Second String"])
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1413 @result{} s1 = Second String
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1414 First String
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1415 @end group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1416 @end example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1417
7096
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1418 Other functions in the mex interface for handling character strings are
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1419 @code{mxCreateString}, @code{mxArrayToString}, and
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1420 @code{mxCreateCharMatrixFromStrings}. In a mex-file, a character string
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1421 is considered to be a vector rather than a matrix. This is perhaps an
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1422 arbitrary distinction as the data in the mxArray for the matrix is
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7081
diff changeset
1423 consecutive in any case.
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1424
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1425 @node Cell Arrays with Mex-Files
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1426 @subsection Cell Arrays with Mex-Files
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1427
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1428 We can perform exactly the same operations in Cell arrays in mex-files
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1429 as we can in oct-files. An example that reduplicates the functional of
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1430 the @file{celldemo.cc} oct-file in a mex-file is given by
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1431 @file{mycell.c} as below
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1432
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1433 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1434 @group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1435 @EXAMPLEFILE(mycell.c)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1436 @end group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1437 @end example
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1438
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1439 @noindent
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1440 which as can be seen below has exactly the same behavior as the oct-file
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1441 version.
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1442
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1443 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1444 @group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1445 [b1, b2, b3] = mycell (@{1, [1, 2], "test"@})
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1446 @result{}
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1447 b1 = 1
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1448 b2 =
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1449
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1450 1 2
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1451
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1452 b3 = test
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1453 @end group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1454 @end example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1455
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1456 Note in the example the use of the @code{mxDuplicateArray} function. This
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1457 is needed as the @code{mxArray} pointer returned by @code{mxGetCell}
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1458 might be deallocated. The inverse function to @code{mxGetCell} is
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1459 @code{mcSetCell} and is defined as
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1460
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1461 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1462 void mxSetCell (mxArray *ptr, int idx, mxArray *val);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1463 @end example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1464
7007
6304d9ea0a30 [project @ 2007-10-11 16:26:36 by jwe]
jwe
parents: 7001
diff changeset
1465 Finally, to create a cell array or matrix, the appropriate functions are
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1466
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1467 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1468 @group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1469 mxArray *mxCreateCellArray (int ndims, const int *dims);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1470 mxArray *mxCreateCellMatrix (int m, int n);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1471 @end group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1472 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1473
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1474 @node Structures with Mex-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1475 @subsection Structures with Mex-Files
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1476
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1477 The basic function to create a structure in a mex-file is
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1478 @code{mxCreateStructMatrix}, which creates a structure array with a two
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1479 dimensional matrix, or @code{mxCreateStructArray}.
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1480
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1481 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1482 @group
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1483 mxArray *mxCreateStructArray (int ndims, int *dims,
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1484 int num_keys,
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1485 const char **keys);
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1486 mxArray *mxCreateStructMatrix (int rows, int cols,
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1487 int num_keys,
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1488 const char **keys);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1489 @end group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1490 @end example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1491
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1492 Accessing the fields of the structure can then be performed with the
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1493 @code{mxGetField} and @code{mxSetField} or alternatively with the
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1494 @code{mxGetFieldByNumber} and @code{mxSetFieldByNumber} functions.
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1495
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1496 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1497 @group
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1498 mxArray *mxGetField (const mxArray *ptr, mwIndex index,
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1499 const char *key);
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1500 mxArray *mxGetFieldByNumber (const mxArray *ptr,
6686
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1501 mwIndex index, int key_num);
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1502 void mxSetField (mxArray *ptr, mwIndex index,
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1503 const char *key, mxArray *val);
6686
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1504 void mxSetFieldByNumber (mxArray *ptr, mwIndex index,
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1505 int key_num, mxArray *val);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1506 @end group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1507 @end example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1508
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1509 A difference between the oct-file interface to structures and the
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1510 mex-file version is that the functions to operate on structures in
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1511 mex-files directly include an @code{index} over the elements of the
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1512 arrays of elements per @code{field}. Whereas the oct-file structure
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1513 includes a Cell Array per field of the structure.
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1514
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1515 An example that demonstrates the use of structures in mex-file can be
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1516 found in the file @file{mystruct.c}, as seen below
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1517
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1518 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1519 @EXAMPLEFILE(mystruct.c)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1520 @end example
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1521
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1522 An example of the behavior of this function within Octave is then
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1523
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1524 @example
7081
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1525 a(1).f1 = "f11"; a(1).f2 = "f12";
503001863427 [project @ 2007-10-31 01:08:14 by jwe]
jwe
parents: 7018
diff changeset
1526 a(2).f1 = "f21"; a(2).f2 = "f22";
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1527 b = mystruct(a)
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1528 @result{} field f1(0) = f11
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1529 field f1(1) = f21
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1530 field f2(0) = f12
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1531 field f2(1) = f22
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1532 b =
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1533 @{
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1534 this =
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1535
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1536 (,
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1537 [1] = this1
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1538 [2] = this2
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1539 [3] = this3
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1540 [4] = this4
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1541 ,)
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1542
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1543 that =
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1544
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1545 (,
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1546 [1] = that1
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1547 [2] = that2
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1548 [3] = that3
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1549 [4] = that4
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1550 ,)
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1551
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1552 @}
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1553 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1554
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1555 @node Sparse Matrices with Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1556 @subsection Sparse Matrices with Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1557
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1558 The Octave format for sparse matrices is identical to the mex format in
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6939
diff changeset
1559 that it is a compressed column sparse format. Also in both, sparse
9209
923c7cb7f13f Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents: 9080
diff changeset
1560 matrices are required to be two-dimensional. The only difference is that
6939
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6686
diff changeset
1561 the real and imaginary parts of the matrix are stored separately.
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1562
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1563 The mex-file interface, as well as using @code{mxGetM}, @code{mxGetN},
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1564 @code{mxSetM}, @code{mxSetN}, @code{mxGetPr}, @code{mxGetPi},
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1565 @code{mxSetPr} and @code{mxSetPi}, the mex-file interface supplies the
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1566 functions
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1567
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1568 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1569 @group
6686
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1570 mwIndex *mxGetIr (const mxArray *ptr);
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1571 mwIndex *mxGetJc (const mxArray *ptr);
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1572 mwSize mxGetNzmax (const mxArray *ptr);
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1573
6686
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1574 void mxSetIr (mxArray *ptr, mwIndex *ir);
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1575 void mxSetJc (mxArray *ptr, mwIndex *jc);
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1576 void mxSetNzmax (mxArray *ptr, mwSize nzmax);
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1577 @end group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1578 @end example
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1579
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1580 @noindent
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1581 @code{mxGetNzmax} gets the maximum number of elements that can be stored
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1582 in the sparse matrix. This is not necessarily the number of non-zero
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1583 elements in the sparse matrix. @code{mxGetJc} returns an array with one
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1584 additional value than the number of columns in the sparse matrix. The
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1585 difference between consecutive values of the array returned by
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1586 @code{mxGetJc} define the number of non-zero elements in each column of
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1587 the sparse matrix. Therefore
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1588
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1589 @example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1590 @group
6686
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1591 mwSize nz, n;
2aad75fcc93a [project @ 2007-06-03 20:58:28 by dbateman]
dbateman
parents: 6606
diff changeset
1592 mwIndex *Jc;
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1593 mxArray *m;
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1594 @dots{}
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1595 n = mxGetN (m);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1596 Jc = mxGetJc (m);
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1597 nz = Jc[n];
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1598 @end group
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1599 @end example
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1600
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1601 @noindent
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1602 returns the actual number of non-zero elements stored in the matrix in
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1603 @code{nz}. As the arrays returned by @code{mxGetPr} and @code{mxGetPi}
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1604 only contain the non-zero values of the matrix, we also need a pointer
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1605 to the rows of the non-zero elements, and this is given by
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1606 @code{mxGetIr}. A complete example of the use of sparse matrices in
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1607 mex-files is given by the file @file{mysparse.c} as seen below
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1608
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1609 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1610 @EXAMPLEFILE(mysparse.c)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1611 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1612
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1613 @node Calling Other Functions in Mex-Files
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1614 @subsection Calling Other Functions in Mex-Files
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1615
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1616 It is also possible call other Octave functions from within a mex-file
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1617 using @code{mexCallMATLAB}. An example of the use of
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1618 @code{mexCallMATLAB} can be see in the example below
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1619
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1620 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1621 @EXAMPLEFILE(myfeval.c)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1622 @end example
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1623
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1624 If this code is in the file @file{myfeval.c}, and is compiled to
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1625 @file{myfeval.mex}, then an example of its use is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1626
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1627 @example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1628 @group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1629 myfeval("sin", 1)
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1630 a = myfeval("sin", 1)
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1631 @result{} Hello, World!
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1632 I have 2 inputs and 1 outputs
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1633 I'm going to call the interpreter function sin
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1634 a = 0.84147
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1635 @end group
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1636 @end example
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1637
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1638 Note that it is not possible to use function handles or inline functions
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1639 within a mex-file.
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1640
6593
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1641 @c @node Application Programming Interface for Mex-Files
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1642 @c @subsection Application Programming Interface for Mex-Files
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1643 @c
3da1f4a41455 [project @ 2007-04-27 08:08:19 by dbateman]
dbateman
parents: 6583
diff changeset
1644 @c WRITE ME, refer to mex.h and mexproto.h
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1645
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1646 @node Standalone Programs
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1647 @section Standalone Programs
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1648
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1649 The libraries Octave itself uses, can be utilized in standalone
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1650 applications. These applications then have access, for example, to the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1651 array and matrix classes as well as to all the Octave algorithms. The
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1652 following C++ program, uses class Matrix from liboctave.a or
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1653 liboctave.so.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1654
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1655 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1656 @group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1657 @EXAMPLEFILE(standalone.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1658 @end group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1659 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1660
6580
d2bb3b8a8d20 [project @ 2007-04-25 22:19:03 by dbateman]
dbateman
parents: 6578
diff changeset
1661 @noindent
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1662 mkoctfile can then be used to build a standalone application with a
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1663 command like
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1664
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1665 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1666 @group
8097
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1667 $ 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
1668 $ ./standalone
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1669 Hello Octave world!
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1670 11 12
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1671 21 22
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1672 $
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1673 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1674 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1675
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1676 Note that the application @code{hello} will be dynamically linked
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1677 against the octave libraries and any octave support libraries. The above
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1678 allows the Octave math libraries to be used by an application. It does
8097
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1679 not however allow the script files, oct-files or builtin functions of
9080
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1680 Octave to be used by the application. To do that the Octave interpreter
ec41eabf4499 Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents: 9038
diff changeset
1681 needs to be initialized first. An example of how to do this can then be
8097
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1682 seen in the code
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1683
9906
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1684 @example
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1685 @group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1686 @EXAMPLEFILE(embedded.cc)
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1687 @end group
8d20fb66a0dc more automake fixes
John W. Eaton <jwe@octave.org>
parents: 9209
diff changeset
1688 @end example
8097
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1689
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1690 @noindent
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1691 which is compiled and run as before as a standalone application with
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1692
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1693 @example
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1694 @group
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1695 $ 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
1696 $ ./embedded
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1697 GCD of [10, 15] is 5
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1698 $
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1699 @end group
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1700 @end example
804c60f92fb1 Add explanationation of initializing the interpreter in a standalone program
David Bateman <dbateman@free.fr>
parents: 7354
diff changeset
1701