annotate doc/interpreter/dynamic.txi @ 6572:8e7148b84b59

[project @ 2007-04-25 04:13:44 by jwe]
author jwe
date Wed, 25 Apr 2007 04:14:49 +0000
parents 24d9e0799603
children f72d6d4b735a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1 @node Dynamically Linked Functions
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
2 @appendix Dynamically Linked Functions
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
3 @cindex dynamic-linking
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
4
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
5 Octave has the possibility of including compiled code as dynamically
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
6 linked extensions and then using these extensions as if they were part
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
7 of Octave itself. Octave has the option of directly calling C++ code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
8 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
9 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
10 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
11 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
12 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
13 for key pieces of code.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
14
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
15 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
16 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
17 any dynamically linked function to improve performance you should
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
18 address ask yourself
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
19
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
20 @itemize @bullet
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
21 @item
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
22 Can I get the same functionality using the Octave scripting language only.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
23 @item
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
24 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
25 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
26 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
27 allocation of space for variables takes place outside the loops using an
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
28 assignment to a like matrix or zeros.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
29 @item
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
30 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
31 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
32 of being interpreted.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
33 @item
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
34 Does writing a dynamically linked function represent useful investment
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
35 of your time, relative to staying in Octave?
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
36 @end itemize
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
37
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
38 Also, as oct- and mex-files are dynamically linked to octave, they
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
39 introduce to possibility of having Octave abort due to coding errors in
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
40 the user code. For example a segmentation violation in the users code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
41 will cause Octave to abort.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
42
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
43 @menu
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
44 * Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
45 * Mex-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
46 * Standalone Programs::
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
47 @end menu
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
48
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
49 @node Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
50 @section Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
51 @cindex oct-files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
52 @cindex mkoctfile
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
53 @cindex oct
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
54
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
55 @menu
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
56 * Getting Started with Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
57 * Matrices and Arrays in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
58 * Character Strings in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
59 * Cell Arrays in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
60 * Structures in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
61 * Sparse Matrices in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
62 * Accessing Global Variables in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
63 * Calling Octave Functions from Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
64 * Calling External Code from Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
65 * Allocating Local Memory in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
66 * Input Parameter Checking in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
67 * Exception and Error Handling in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
68 * Documentation and Test of Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
69 * Application Programming Interface for Oct-Files::
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
70 @end menu
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
71
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
72 @node Getting Started with Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
73 @subsection Getting Started with Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
74
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
75 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
76 call from within octave or from the command line.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
77
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
78 @DOCSTRING(mkoctfile)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
79
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
80 Consider the short example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
81
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
82 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
83 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
84 #include <octave/oct.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
85
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
86 DEFUN_DLD (helloworld, args, nargout,
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
87 "Hello World Help String")
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
88 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
89 int nargin = args.length ();
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
90 octave_stdout << "Hello World has " << nargin
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
91 << " input arguments and "
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
92 << nargout << " output arguments.\n";
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
93 return octave_value_list ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
94 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
95 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
96 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
97
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
98 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
99 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
100 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
101 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
102 header.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
103
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
104 The macro that defines the entry point into the dynamically loaded
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
105 function is @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
106
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
107 @enumerate 1
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
108 @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
109 @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
110 @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
111 not used, and
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
112 @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
113 @end enumerate
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
114
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
115 The return type of functions defined with @code{DEFUN_DLD} is always
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
116 @code{octave_value_list}.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
117
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
118 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
119 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
120 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
121 digit. Secondly, as Octave uses the function name to define the filename
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
122 it attempts to find the function in, the function name in the @code{DEFUN_DLD}
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
123 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
124 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
125 compiled to an oct-file using the command
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 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
128 mkoctfile helloworld.cc
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
129 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
130
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
131 This will create a file call helloworld.oct, that is the compiled
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
132 version of the function. It should be noted that it is perfectly
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
133 acceptable to have more than one @code{DEFUN_DLD} function in a source
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
134 file. However, there must either be a symbolic link to the oct-file for
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
135 each of the functions defined in the source code with the @code{DEFUN_DLD}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
136 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
137
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
138 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
139 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
140 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
141 is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
142
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
143 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
144 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
145 helloworld (1, 2, 3)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
146 @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
147 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
148 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
149
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
150 @node Matrices and Arrays in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
151 @subsection Matrices and Arrays in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
152
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
153 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
154 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
155 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
156 matrix types
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
157
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
158 @table @code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
159 @item Matrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
160 A double precision matrix class defined in dMatrix.h,
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
161 @item ComplexMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
162 A complex matrix class defined in CMatrix.h, and
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
163 @item BoolMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
164 A boolean matrix class defined in boolMatrix.h.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
165 @end table
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
166
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
167 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
168 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
169 being
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
170
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
171 @table @code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
172 @item NDArray
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
173 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
174 @item ComplexNDarray
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
175 A complex array class defined in @file{CNDArray.h}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
176 @item boolNDArray
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
177 A boolean array class defined in @file{boolNDArray.h}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
178 @item int8NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
179 @itemx int16NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
180 @itemx int32NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
181 @itemx int64NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
182 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
183 @file{int8NDArray.h}, @file{int16NDArray.h}, etc.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
184 @item uint8NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
185 @itemx uint16NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
186 @itemx uint32NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
187 @itemx uint64NDArray
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
188 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
189 @file{uint8NDArray.h}, @file{uint16NDArray.h}, etc.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
190 @end table
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
191
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
192 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
193 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
194 example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
195
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
196 @itemize @bullet
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
197 @item
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
198 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
199 example
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 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
202 Matrix a;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
203 @end example
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 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
206 @item
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
207 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
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 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
212 dim_vector dv (2);
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
213 dv(0) = 2; dv(1) = 2;
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
214 Matrix a (dv);
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
215 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
216 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
217
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
218 This can be used on all matrix and array types
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
219 @item
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
220 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
221
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
222 @example
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
223 Matrix a (2, 2)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
224 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
225
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
226 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
227 @end itemize
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
228
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
229 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
230 selection of which include
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
231
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
232 @deftypefn Method T& {operator ()} (octave_idx_type)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
233 @deftypefnx Method T& elem (octave_idx_type)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
234 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
235 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
236 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
237 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
238 @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
239 value to obtain or set.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
240 @end deftypefn
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
241
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
242 Note that these function do significant error checking and so in some
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
243 circumstances the user might prefer the access the data of the array or
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
244 matrix directly through the fortran_vec method discussed below.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
245
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
246 @deftypefn Method octave_idx_type nelem (void) const
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
247 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
248 @end deftypefn
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
249
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
250 @deftypefn Method size_t byte_size (void) const
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
251 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
252 @end deftypefn
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
253
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
254 @deftypefn Method dim_vector dims (void) const
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
255 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
256 @end deftypefn
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
257
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
258 @deftypefn Method void resize (const dim_vector&)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
259 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
260 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
261 the number of rows and columns in the matrix.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
262 @end deftypefn
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
263
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
264 @deftypefn Method T* fortran_vec (void)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
265 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
266 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
267 an external library.
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
268 @end deftypefn
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
269
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
270 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
271 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
272 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
273 @code{hermitian}, @code{solve}, etc.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
274
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
275 The typical way to extract a matrix or array from the input arguments of
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
276 @code{DEFUN_DLD} function is as follows
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
277
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
278 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
279 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
280 #include <octave/oct.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
281
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
282 DEFUN_DLD (addtwomatrices, args, , "Add A to B")
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 int nargin = args.length ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
285 if (nargin != 2)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
286 print_usage ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
287 else
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
288 @{
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
289 NDArray A = args(0).array_value ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
290 NDArray B = args(1).array_value ();
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
291 if (! error_state)
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
292 return octave_value (A + B);
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
293 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
294 return octave_value_list ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
295 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
296 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
297 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
298
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
299 To avoid segmentation faults causing Octave to abort, this function
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
300 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
301 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
302 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
303 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
304 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
305 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
306 @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
307 @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
308 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
309
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
310 @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
311 @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
312 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
313 is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
314
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
315 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
316 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
317 addtwomatrices (ones (2, 2), ones (2, 2))
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
318 @result{} 2 2
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
319 2 2
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
320 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
321 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
322
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
323 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
324 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
325 listed below.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
326
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
327 @multitable @columnfractions .3 .4 .3
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
328 @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
329 @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
330 @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
331 @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
332 @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
333 @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
334 @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
335 @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
336 @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
337 @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
338 @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
339 @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
340 @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
341 @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
342 @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
343 @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
344 @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
345 @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
346 @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
347 @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
348 @end multitable
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
349
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
350 @node Character Strings in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
351 @subsection Character Strings in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
352
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
353 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
354 Consider the example
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 #include <octave/oct.h>
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
359
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
360 DEFUN_DLD (stringdemo, args, , "String Demo")
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
361 @{
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
362 int nargin = args.length ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
363 octave_value_list retval;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
364
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
365 if (nargin != 1)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
366 print_usage ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
367 else
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
368 @{
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
369 charMatrix ch = args(0).char_matrix_value ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
370
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
371 if (! error_state)
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 if (args(0).is_sq_string ())
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
374 retval(1) = octave_value (ch, true);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
375 else
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
376 retval(1) = octave_value (ch, true, '\'');
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
377
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
378 octave_idx_type nr = ch.rows();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
379 for (octave_idx_type i = 0; i < nr / 2; i++)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
380 @{
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
381 std::string tmp = ch.row_as_string (i);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
382 ch.insert (ch.row_as_string(nr-i-1).c_str(), i, 0);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
383 ch.insert (tmp.c_str(), nr-i-1, 0);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
384 @}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
385 retval(0) = octave_value (ch, true);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
386 @}
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 return retval;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
389 @}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
390 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
391 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
392
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
393 An example of the of the use of this function is
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
394
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
395 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
396 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
397 s0 = ["First String"; "Second String"];
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
398 [s1,s2] = stringdemo (s0)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
399 @result{} s1 = Second String
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
400 First String
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
401
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
402 @result{} s2 = First String
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
403 Second String
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 typeinfo (s2)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
406 @result{} sq_string
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
407 typeinfo (s1)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
408 @result{} string
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
409 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
410 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
411
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
412 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
413 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
414 @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
415
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
416 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
417 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
418 if (args(0).is_sq_string ())
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
419 octave_stdout << "First argument is a singularly quoted string\n";
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
420 else if (args(0).is_dq_string ())
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
421 octave_stdout << "First argument is a doubly quoted string\n";
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
422 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
423 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
424
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
425 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
426 @code{charNDArray} type, and so when assigning to an
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
427 @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
428
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
429 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
430 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
431 octave_value_list retval;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
432 charNDArray c;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
433 @dots{}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
434 retval(0) = octave_value (ch, true); // Create a double quoted string
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
435 retval(1) = octave_value (ch, true, '\''); // Create single quoted string
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
436 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
437 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
438
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
439 @node Cell Arrays in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
440 @subsection Cell Arrays in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
441
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
442 Octave's cell type is equally accessible within an oct-files. A cell
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
443 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
444 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
445 example is
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
446
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
447 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
448 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
449 #include <octave/oct.h>
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
450 #include <octave/Cell.h>
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
451
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
452 DEFUN_DLD (celldemo, args, , "Cell Demo")
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
453 @{
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
454 octave_value_list retval;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
455 int nargin = args.length();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
456
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
457 if (nargin != 1)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
458 print_usage ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
459 else
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
460 @{
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
461 Cell c = args (0).cell_value ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
462 if (! error_state)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
463 for (octave_idx_type i = 0; i < c.nelem (); i++)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
464 retval(i) = c.elem (i);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
465 @}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
466 return retval;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
467 @}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
468 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
469 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
470
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
471 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
472 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
473 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
474 returns be as individual return arguments. For example consider
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
475
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
476 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
477 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
478 [b1, b2, b3] = celldemo (@{1, [1, 2], "test"@})
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
479 @result{}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
480 b1 = 1
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
481 b2 =
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
482
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
483 1 2
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
484
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
485 b3 = test
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
486 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
487 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
488
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
489 @node Structures in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
490 @subsection Structures in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
491
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
492 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
493 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
494 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
495 @code{Cell} variable.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
496
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
497 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
498
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
499 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
500 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
501 #include <octave/oct.h>
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
502 #include <octave/ov-struct.h>
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
503
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
504 DEFUN_DLD (structdemo, args, , "Struct demo.")
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
505 @{
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
506 int nargin = args.length ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
507 octave_value retval;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
508
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
509 if (nargin != 2)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
510 print_usage ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
511 else
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
512 @{
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
513 Octave_map arg0 = args(0).map_value ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
514 std::string arg1 = args(1).string_value ();
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
515
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
516 if (! error_state && arg0.contains (arg1))
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
517 @{
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
518 // The following two lines might be written as
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
519 // octave_value tmp;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
520 // for (Octave_map::iterator p0 = arg0.begin() ;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
521 // p0 != arg0.end(); p0++ )
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
522 // if (arg0.key (p0) == arg1)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
523 // @{
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
524 // tmp = arg0.contents (p0) (0);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
525 // break;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
526 // @}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
527 // though using seek is more concise.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
528 Octave_map::const_iterator p1 = arg0.seek (arg1);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
529 octave_value tmp = arg0.contents( p1 ) (0);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
530 Octave_map st;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
531 st.assign ("selected", tmp);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
532 retval = octave_value (st);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
533 @}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
534 @}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
535 return retval;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
536 @}
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
537 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
538 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
539
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
540 An example of its use is
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
541
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
542 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
543 @group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
544 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
545 structdemo (x, "b")
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
546 @result{} selected = test
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
547 @end group
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
548 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
549
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
550 The commented code above demonstrates how to iterated over all of the
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
551 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
552 a particular field in a more concise manner.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
553
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
554 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
555 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
556 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
557
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
558 @example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
559 octave_value tmp = arg0.contents (p1) (0);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
560 @end example
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
561
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
562 where the trailing (0) is the () operator on the @code{Cell} object.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
563
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
564 @node Sparse Matrices in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
565 @subsection Sparse Matrices in Oct-Files
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
566
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
567 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
568 user.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
569
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
570 @table @code
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
571 @item SparseMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
572 A double precision sparse matrix class
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
573 @item SparseComplexMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
574 A complex sparse matrix class
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
575 @item SparseBoolMatrix
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
576 A boolean sparse matrix class
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
577 @end table
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
578
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
579 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
580 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
581 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
582 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
583 the sparse classes.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
584
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
585 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
586 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
587 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
588 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
589 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
590 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
591 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
592 similar to Octave's @code{Matrix} class rather than its
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
593 @code{NDArray} class.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
594
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
595 @menu
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
596 * Array and Sparse Differences::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
597 * Creating Sparse Matrices in Oct-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
598 * Using Sparse Matrices in Oct-Files::
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
599 @end menu
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
600
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
601 @node Array and Sparse Differences
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
602 @subsubsection The Differences between the Array and Sparse Classes
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
603
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
604 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
605 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
606
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
607 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
608 SparseMatrix sm;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
609 @dots{}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
610 int nel = sm.nelem ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
611 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
612
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
613 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
614 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
615 should use @code{numel} rather than @code{nelem}. Note that for very
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
616 large matrices, where the product of the two dimensions is large that
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
617 the representation of the an unsigned int, then @code{numel} can overflow.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
618 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
619 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
620 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
621 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
622 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
623 it won't overflow.
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 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
626 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
627 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
628 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
629 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
630 code like
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
631
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
632 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
633 SparseMatrix sm;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
634 @dots{}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
635 for (int j = 0; j < nc; j++)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
636 for (int i = 0; i < nr; i++)
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
637 std::cerr << " (" << i << "," << j << "): " << sm(i,j)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
638 << std::endl;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
639 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
640
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
641 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
642 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
643 zero element in the matrix.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
644
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
645 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
646 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
647 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
648 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
649 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
650
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
651 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
652 SparseMatrix sm;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
653 @dots{}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
654 const SparseMatrix tmp (sm);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
655 for (int j = 0; j < nc; j++)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
656 for (int i = 0; i < nr; i++)
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
657 std::cerr << " (" << i << "," << j << "): " << tmp(i,j)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
658 << std::endl;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
659 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
660
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
661 Finally, as the sparse types aren't just represented as a contiguous
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
662 block of memory, the @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
663 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
664 @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
665 column format that the Octave sparse matrices are stored in.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
666 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
667 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
668 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
669 discussed previous.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
670
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
671 @node Creating Sparse Matrices in Oct-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
672 @subsubsection Creating Sparse Matrices in Oct-Files
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
673
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
674 You have several alternatives for creating a sparse matrix.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
675 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
676 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
677 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
678 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
679 advantages and disadvantages.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
680
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
681 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
682 technique
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
683
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
684 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
685 int nz = 4, nr = 3, nc = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
686 ColumnVector ridx (nz);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
687 ColumnVector cidx (nz);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
688 ColumnVector data (nz);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
689
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
690 ridx(0) = 0; ridx(1) = 0; ridx(2) = 1; ridx(3) = 2;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
691 cidx(0) = 0; cidx(1) = 1; cidx(2) = 3; cidx(3) = 3;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
692 data(0) = 1; data(1) = 2; data(2) = 3; data(3) = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
693
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
694 SparseMatrix sm (data, ridx, cidx, nr, nc);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
695 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
696
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
697 @noindent
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
698 which creates the matrix given in section @ref{Storage}. Note that
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
699 the compressed matrix format is not used at the time of the creation
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
700 of the matrix itself, however it is used internally.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
701
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
702 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
703 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
704 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
705 data significantly speeds up the creation of the sparse matrix.
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 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
708 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
709 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
710 technique to create the sparse matrix.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
711
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
712 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
713 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
714 easiest way to do this is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
715
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
716 @example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
717 int nz = 4, nr = 3, nc = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
718 SparseMatrix sm (nr, nc, nz);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
719 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
720 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
721
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
722 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
723 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
724 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
725 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
726 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
727 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
728 inserted. Only the column indexes need to be updated.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
729
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
730 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
731 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
732 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
733
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
734 @example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
735 int nz = 4, nr = 3, nc = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
736 SparseMatrix sm (nr, nc, 0);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
737 sm(0,0) = 1; sm(0,1) = 2; sm(1,3) = 3; sm(2,3) = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
738 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
739
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
740 @noindent
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
741 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
742 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
743 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
744 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
745 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
746 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
747 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
748 needed.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
749
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
750 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
751 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
752 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
753 @dfn{maybe_compress} function. Furthermore, the maybe_compress can
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
754 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
755 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
756 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
757 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
758 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
759 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
760 the use of @dfn{maybe_compress} is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
761
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
762 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
763 int nz = 6, nr = 3, nc = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
764 SparseMatrix sm1 (nr, nc, nz);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
765 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
766 sm1.maybe_compress (); // No zero elements were added
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
767
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
768 SparseMatrix sm2 (nr, nc, nz);
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
769 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
770 sm1(1,3) = 3; sm1(2,3) = 4;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
771 sm2.maybe_compress (true); // Zero elements were added
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
772 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
773
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
774 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
775 possible, as it will slow the creation of the matrices.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
776
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
777 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
778 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
779 be
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
780
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
781 @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
782 @c Therefore use the @example environment and replace @, with @@,
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
783 @c { with @{, etc
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
784
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
785 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
786 octave_value arg;
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
787
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
788 @dots{}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
789
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
790 int nz = 6, nr = 3, nc = 4; // Assume we know the max no nz
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
791 SparseMatrix sm (nr, nc, nz);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
792 Matrix m = arg.matrix_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
793
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
794 int ii = 0;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
795 sm.cidx (0) = 0;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
796 for (int j = 1; j < nc; j++)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
797 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
798 for (int i = 0; i < nr; i++)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
799 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
800 double tmp = foo (m(i,j));
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
801 if (tmp != 0.)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
802 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
803 sm.data(ii) = tmp;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
804 sm.ridx(ii) = i;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
805 ii++;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
806 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
807 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
808 sm.cidx(j+1) = ii;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
809 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
810 sm.maybe_compress (); // If don't know a-priori the final no of nz.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
811 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
812
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
813 @noindent
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
814 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
815
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
816 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
817 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
818 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
819 The above example would then be modified as
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
820
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
821 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
822 octave_value arg;
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
823
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
824 @dots{}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
825
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
826 int nz = 6, nr = 3, nc = 4; // Assume we know the max no nz
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
827 SparseMatrix sm (nr, nc, nz);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
828 Matrix m = arg.matrix_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
829
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
830 int ii = 0;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
831 sm.cidx (0) = 0;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
832 for (int j = 1; j < nc; j++)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
833 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
834 for (int i = 0; i < nr; i++)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
835 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
836 double tmp = foo (m(i,j));
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
837 if (tmp != 0.)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
838 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
839 if (ii == nz)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
840 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
841 nz += 2; // Add 2 more elements
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
842 sm.change_capacity (nz);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
843 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
844 sm.data(ii) = tmp;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
845 sm.ridx(ii) = i;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
846 ii++;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
847 @}
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 sm.cidx(j+1) = ii;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
850 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
851 sm.maybe_mutate (); // If don't know a-priori the final no of nz.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
852 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
853
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
854 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
855 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
856 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
857 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
858 should be avoided.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
859
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
860 @node Using Sparse Matrices in Oct-Files
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
861 @subsubsection Using Sparse Matrices in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
862
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
863 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
864 available from the Octave are equally available with oct-files.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
865 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
866 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
867 following example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
868
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
869 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
870 octave_value_list retval;
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 SparseMatrix sm = args(0).sparse_matrix_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
873 SparseComplexMatrix scm = args(1).sparse_complex_matrix_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
874 SparseBoolMatrix sbm = args(2).sparse_bool_matrix_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
875
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
876 @dots{}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
877
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
878 retval(2) = sbm;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
879 retval(1) = scm;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
880 retval(0) = sm;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
881 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
882
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
883 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
884 @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
885
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
886 @node Accessing Global Variables in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
887 @subsection Accessing Global Variables in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
888
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
889 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
890 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
891 the support functions @code{get_global_value} and
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
892 @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
893 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
894 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
895 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
896 use of these two functions is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
897
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
898 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
899 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
900 #include <octave/oct.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
901
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
902 DEFUN_DLD (globaldemo, args, , "Global demo.")
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
903 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
904 int nargin = args.length();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
905 octave_value retval;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
906
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
907 if (nargin != 1)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
908 print_usage ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
909 else
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
910 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
911 std::string s = args(0).string_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
912 if (! error_state)
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 octave_value tmp = get_global_value (s, true);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
915 if (tmp.is_defined ())
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
916 retval = tmp;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
917 else
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
918 retval = "Global variable not found";
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
919
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
920 set_global_value ("a", 42.0);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
921 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
922 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
923 return retval;
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 @end group
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
926 @end example
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
927
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
928 An example of its use is
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 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
931 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
932 global a b
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
933 b = 10;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
934 globaldemo ("b")
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
935 @result{} 10
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
936 globaldemo ("c")
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
937 @result{} "Global variable not found"
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
938 num2str (a)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
939 @result{} 42
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
940 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
941 @end example
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 @node Calling Octave Functions from Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
944 @subsection Calling Octave Functions from Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
945
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
946 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
947 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
948 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
949 calculates the definite integral by quadrature over a user supplied
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
950 function.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
951
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
952 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
953 be passed as one of
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
954
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
955 @enumerate 1
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
956 @item Function Handle
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
957 @item Anonymous Function Handle
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
958 @item Inline Function
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
959 @item String
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
960 @end enumerate
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 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
963 passing a function to an oct-file.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
964
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
965 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
966 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
967 #include <octave/oct.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
968 #include <octave/parse.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
969
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
970 DEFUN_DLD (funcdemo, args, nargout, "Function Demo")
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
971 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
972 int nargin = args.length();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
973 octave_value_list retval;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
974
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
975 if (nargin < 2)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
976 print_usage ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
977 else
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
978 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
979 octave_value_list newargs;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
980 for (octave_idx_type i = nargin - 1; i > 0; i--)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
981 newargs (i - 1) = args(i);
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
982 if (args(0).is_function_handle () ||
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
983 args(0).is_inline_function ())
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
984 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
985 octave_function *fcn = args(0).function_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
986 if (! error_state)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
987 retval = feval (fcn, newargs, nargout);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
988 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
989 else if (args(0).is_string ())
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
990 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
991 std::string fcn = args (0).string_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
992 if (! error_state)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
993 retval = feval (fcn, newargs, nargout);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
994 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
995 else
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
996 error ("funcdemo: expected string, inline or function handle");
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
997 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
998 return retval;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
999 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1000 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1001 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1002
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1003 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
1004 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
1005
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1006 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1007 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1008 funcdemo (@@sin,1)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1009 @result{} 0.84147
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1010 funcdemo (@@(x) sin(x), 1)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1011 @result{} 0.84147
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1012 funcdemo (inline ("sin(x)"), 1)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1013 @result{} 0.84147
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1014 funcdemo ("sin",1)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1015 @result{} 0.84147
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1016 funcdemo (@@atan2, 1, 1)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1017 @result{} 0.78540
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1018 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1019 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1020
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1021 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
1022 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
1023 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
1024 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
1025
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1026 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1027 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1028 std::octave fcn_name = unique_symbol_name ("__fcn__");
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1029 std::string fname = "function y = ";
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1030 fname.append (fcn_name);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1031 fname.append ("(x) y = ");
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1032 fcn = extract_function (args(0), "funcdemo", fcn_name,
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1033 fname, "; endfunction");
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1034 @dots{}
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1035 if (fcn_name.length ())
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1036 clear_function (fcn_name);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1037 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1038 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1039
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1040 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
1041 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
1042 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
1043 Octave symbol table it should be cleared after use.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1044
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1045 @node Calling External Code from Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1046 @subsection Calling External Code from Oct-Files
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 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
1049 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
1050 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
1051 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
1052 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
1053 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
1054 code is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1055
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1056 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1057 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1058 #ifdef __cplusplus
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1059 extern "C"
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1060 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1061 #endif
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1062 #include "foo.h"
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1063 #ifdef __cplusplus
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1064 @} /* end extern "C" */
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1065 #endif
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1066 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1067 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1068
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1069 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
1070 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
1071 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
1072 consistent behavior across a number of compilers.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1073
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1074 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
1075 replace the Fortran @code{STOP} function. @code{XSTOPX} uses the Octave
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1076 exception handler to treat failing cases in the fortran code
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1077 explicitly. Note that Octave supplies its own replacement blas
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1078 @code{XERBLA} function, which uses @code{XSTOPX}.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1079
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1080 If the underlying code calls @code{XSTOPX}, then the @code{F77_XFCN}
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1081 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
1082 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
1083 @code{f77_exception_encountered}. If @code{XSTOPX} will not be called,
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1084 then the @code{F77_FCN} macro should be used instead to call the Fortran
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1085 code.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1086
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1087 There is no harm in using @code{F77_XFCN} in all cases, except that for
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1088 Fortran code that is short running and executes a large number of times,
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1089 there is potentially an overhead in doing so. However, if @code{F77_FCN}
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1090 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
1091 segmentation fault.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1092
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1093 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
1094 given in the following example, where the C++ wrapper is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1095
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1096 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1097 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1098 #include <octave/oct.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1099 #include <octave/f77-fcn.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1100
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1101 extern "C"
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1102 @{
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1103 F77_RET_T
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1104 F77_FUNC (fortsub, FORTSUB)
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1105 (const int&, double*, F77_CHAR_ARG_DECL
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1106 F77_CHAR_ARG_LEN_DECL);
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
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1109 DEFUN_DLD (fortdemo, args, , "Fortran Demo.")
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1110 @{
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1111 octave_value_list retval;
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1112 int nargin = args.length();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1113 if (nargin != 1)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1114 print_usage ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1115 else
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1116 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1117 NDArray a = args(0).array_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1118 if (! error_state)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1119 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1120 double *av = a.fortran_vec ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1121 octave_idx_type na = a.nelem ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1122 OCTAVE_LOCAL_BUFFER (char, ctmp, 128);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1123
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1124 F77_XFCN (fortsub, FORTSUB, (na, av, ctmp F77_CHAR_ARG_LEN (128)));
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1125
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1126 if (f77_exception_encountered)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1127 error ("fortdemo: error in fortran");
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1128 else
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1129 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1130 retval(1) = std::string (ctmp);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1131 retval(0) = a;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1132 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1133 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1134 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1135 return retval;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1136 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1137 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1138 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1139
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1140 @noindent
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1141 and the fortran function is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1142
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1143 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1144 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1145 subroutine fortsub (n, a, s)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1146 implicit none
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1147 character*(*) s
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1148 real*8 a(*)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1149 integer*4 i, n, ioerr
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1150 do i = 1, n
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1151 if (a (i) .eq. 0d0) then
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1152 call xstopx ('fortsub:divide by zero')
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1153 else
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1154 a (i) = 1d0 / a (i)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1155 end if
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1156 end do
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1157 write (unit = s, fmt = '(a,i3,a,a)', iostat = ioerr)
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1158 $ 'There are ', n, ' values in the input vector',
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1159 $ char(0)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1160 if (ioerr .ne. 0) then
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1161 call xstopx ('fortsub: error writing string')
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1162 end if
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1163 return
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1164 end
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1165 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1166 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1167
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1168 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
1169 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
1170 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
1171
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1172 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1173 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1174 [b, s] = fortdemo (1:3)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1175 @result{}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1176 b = 1.00000 0.50000 0.33333
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1177 s = There are 3 values in the input vector
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1178 [b, s] = fortdemo(0:3)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1179 error: fortsub:divide by zero
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1180 error: exception encountered in Fortran subroutine fortsub_
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1181 error: fortdemo: error in fortran
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1182 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1183 @end example
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 Allocating Local Memory in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1186 @subsection Allocating Local Memory in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1187
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1188 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
1189 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
1190 taken to avoid memory leaks. The preferred manner in which to allocate
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1191 memory for use locally is to use the @code{OCTAVE_LOCAL_BUFFER} macro.
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1192 An example of its use is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1193
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1194 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1195 OCTAVE_LOCAL_BUFFER (double, tmp, len)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1196 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1197
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1198 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
1199 @code{len}.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1200
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1201 @node Input Parameter Checking in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1202 @subsection Input Parameter Checking in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1203
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1204 WRITE ME
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1205
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1206 @node Exception and Error Handling in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1207 @subsection Exception and Error Handling in Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1208
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1209 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
1210 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
1211 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
1212 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
1213 writing an oct-file, to allow Octave to treat the user typing @kbd{Control-C},
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1214 the @code{OCTAVE_QUIT} macro is supplied. For example
6569
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 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1217 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1218 for (octave_idx_type i = 0; i < a.nelem (); i++)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1219 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1220 OCTAVE_QUIT;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1221 b.elem(i) = 2. * a.elem(i);
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 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1224 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1225
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1226 The presence of the @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
1227 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
1228 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
1229 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
1230
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1231 The @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
1232 loops that are known to be small it might not make sense to include
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1233 @code{OCTAVE_QUIT}.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1234
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1235 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
1236 might spend a significant portion of its time in the external
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1237 library. It is not generally possible to use the @code{OCTAVE_QUIT} macro in
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1238 this case. The alternative in this case is
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1239
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1240 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1241 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1242 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1243 @dots{} some code that calls a "foreign" function @dots{}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1244 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1245 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1246 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1247
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1248 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
1249 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
1250 without being deallocated. Therefore, ideally Octave itself should
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1251 allocate any memory that is needed by the foreign code, with either the
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1252 fortran_vec method or the @code{OCTAVE_LOCAL_BUFFER} macro.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1253
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1254 The Octave unwind_protect mechanism (@ref{The unwind_protect Statement})
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1255 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
1256 handling of Octave, it is important to enforce that certain code is run
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1257 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
1258 example of the use of this mechanism is
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1259
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1260 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1261 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1262 #include <octave/oct.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1263 #include <octave/unwind-prot.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1264
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1265 void
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1266 err_hand (const char *fmt, ...)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1267 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1268 // Do nothing!!
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1269 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1270
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1271 DEFUN_DLD (unwinddemo, args, nargout, "Unwind Demo")
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1272 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1273 int nargin = args.length();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1274 octave_value retval;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1275 if (nargin < 2)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1276 print_usage ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1277 else
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1278 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1279 NDArray a = args(0).array_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1280 NDArray b = args(1).array_value ();
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1281
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1282 if (! error_state)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1283 @{
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1284 unwind_protect::begin_frame ("Funwinddemo");
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1285 unwind_protect_ptr (current_liboctave_warning_handler);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1286 set_liboctave_warning_handler (err_hand);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1287 retval = octave_value (quotient (a, b));
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1288 unwind_protect::run_frame ("Funwinddemo");
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1289 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1290 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1291 return retval;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1292 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1293 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1294 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1295
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1296 As can be seen in the example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1297
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1298 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1299 @group
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1300 unwinddemo (1, 0)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1301 @result{} Inf
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1302 1 / 0
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1303 @result{} warning: division by zero
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1304 Inf
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1305 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1306 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1307
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1308 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
1309 @code{unwinddemo} function.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1310
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1311 @node Documentation and Test of Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1312 @subsection Documentation and Test of Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1313
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1314 WRITE ME, reference how to use Texinfo in oct-file and embed test code.
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1315
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1316 @node Application Programming Interface for Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1317 @subsection Application Programming Interface for Oct-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1318
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1319 WRITE ME, using Coda section 1.3 as a starting point.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1320
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1321 @node Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1322 @section Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1323 @cindex mex-files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1324 @cindex mex
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1325
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1326 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
1327 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
1328 between Octave and non Octave users. However, as mex-files expose the
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1329 intern API of a product alternative to Octave, and the internal
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1330 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
1331 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
1332 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
1333 passed to mex functions, there are a significant number of additional
6571
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1334 copies of memory when calling or returning from a mex function. For this
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1335 reason, new code should be written using the oct-file interface
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1336 discussed above if possible.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1337
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1338 @menu
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1339 * Getting Started with Mex-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1340 * Structures with Mex-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1341 * Sparse Matrices with Mex-Files::
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1342 * Calling External Functions in Mex-Files::
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1343 @end menu
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1344
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1345 @node Getting Started with Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1346 @subsection Getting Started with Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1347
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1348 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
1349 @code{mex}. The first can either be used from within Octave or from the
24d9e0799603 [project @ 2007-04-25 03:20:17 by jwe]
jwe
parents: 6569
diff changeset
1350 commandline. However, to avoid issues with the installation of other
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1351 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
1352
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1353 @DOCSTRING(mex)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1354
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1355 @DOCSTRING(mexext)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1356
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1357 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
1358 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
1359 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
1360 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
1361 distinguish between filenames in upper and lower case
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1362
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1363 Consider the short example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1364
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1365 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1366 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1367 #include "mex.h"
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1368
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1369 void
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1370 mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1371 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1372 mxArray *v = mxCreateDoubleMatrix (1, 1, mxREAL);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1373 double *data = mxGetPr (v);
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1374 *data = 1.23456789;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1375 plhs[0] = v;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1376 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1377 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1378 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1379
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1380 This simple example demonstrates the basics of writing a mex-file.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1381
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1382 WRITE ME
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1383
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1384 @node Structures with Mex-Files
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1385 @subsection Structures with Mex-Files
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1386
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1387 WRITE ME
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1388
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1389 @node Sparse Matrices with Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1390 @subsection Sparse Matrices with Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1391
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1392 WRITE ME
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1393
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1394 @node Calling External Functions in Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1395 @subsection Calling External Functions in Mex-Files
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1396
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1397 WRITE ME
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1398
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1399 @node Standalone Programs
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1400 @section Standalone Programs
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1401
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1402 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
1403 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
1404 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
1405 following C++ program, uses class Matrix from liboctave.a or
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1406 liboctave.so.
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1407
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1408 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1409 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1410 #include <iostream>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1411 #include <octave/oct.h>
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1412 int
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1413 main (void)
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1414 @{
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1415 std::cout << "Hello Octave world!\n";
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1416 int n = 2;
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1417 Matrix a_matrix = Matrix (n, n);
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1418 for (octave_idx_type i = 0; i < n; i++)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1419 @{
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1420 for (octave_idx_type j = 0; j < n; j++)
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1421 @{
6572
8e7148b84b59 [project @ 2007-04-25 04:13:44 by jwe]
jwe
parents: 6571
diff changeset
1422 a_matrix(row,column) = (i+1)*10 + (j+1);
6569
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1423 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1424 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1425 std::cout << a_matrix;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1426 return 0;
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1427 @}
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1428 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1429 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1430
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1431 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
1432 command like
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1433
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1434 @example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1435 @group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1436 $ mkoctfile --link-stand-alone hello.cc -o hello
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1437 $ ./hello
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1438 Hello Octave world!
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1439 11 12
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1440 21 22
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1441 $
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1442 @end group
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1443 @end example
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1444
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1445 Note that the application @code{hello} will be dynamically linked
81a8ab62b2b9 [project @ 2007-04-24 23:01:29 by jwe]
jwe
parents:
diff changeset
1446 against the octave libraries and any octave support libraries.