File: Matrices-and-Arrays-in-Oct_002dFiles.html

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (270 lines) | stat: -rw-r--r-- 13,734 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Matrices and Arrays in Oct-Files (GNU Octave (version 6.2.0))</title>

<meta name="description" content="Matrices and Arrays in Oct-Files (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Matrices and Arrays in Oct-Files (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Oct_002dFiles.html" rel="up" title="Oct-Files">
<link href="Character-Strings-in-Oct_002dFiles.html" rel="next" title="Character Strings in Oct-Files">
<link href="Getting-Started-with-Oct_002dFiles.html" rel="prev" title="Getting Started with Oct-Files">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<span id="Matrices-and-Arrays-in-Oct_002dFiles"></span><div class="header">
<p>
Next: <a href="Character-Strings-in-Oct_002dFiles.html" accesskey="n" rel="next">Character Strings in Oct-Files</a>, Previous: <a href="Getting-Started-with-Oct_002dFiles.html" accesskey="p" rel="prev">Getting Started with Oct-Files</a>, Up: <a href="Oct_002dFiles.html" accesskey="u" rel="up">Oct-Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Matrices-and-Arrays-in-Oct_002dFiles-1"></span><h4 class="subsection">A.1.2 Matrices and Arrays in Oct-Files</h4>

<p>Octave supports a number of different array and matrix classes, the majority of
which are based on the <code>Array</code> class.  The exception are the sparse matrix
types discussed separately below.  There are three basic matrix types:
</p>
<dl compact="compact">
<dt><code>Matrix</code></dt>
<dd><p>A double precision matrix class defined in <samp>dMatrix.h</samp>
</p>
</dd>
<dt><code>ComplexMatrix</code></dt>
<dd><p>A complex matrix class defined in <samp>CMatrix.h</samp>
</p>
</dd>
<dt><code>BoolMatrix</code></dt>
<dd><p>A boolean matrix class defined in <samp>boolMatrix.h</samp>
</p></dd>
</dl>

<p>These are the basic two-dimensional matrix types of Octave.  In addition there
are a number of multi-dimensional array types including
</p>
<dl compact="compact">
<dt><code>NDArray</code></dt>
<dd><p>A double precision array class defined in <samp>dNDArray.h</samp>
</p>
</dd>
<dt><code>ComplexNDarray</code></dt>
<dd><p>A complex array class defined in <samp>CNDArray.h</samp>
</p>
</dd>
<dt><code>boolNDArray</code></dt>
<dd><p>A boolean array class defined in <samp>boolNDArray.h</samp>
</p>
</dd>
<dt><code>int8NDArray</code></dt>
<dt><code>int16NDArray</code></dt>
<dt><code>int32NDArray</code></dt>
<dt><code>int64NDArray</code></dt>
<dd><p>8, 16, 32, and 64-bit signed array classes defined in
<samp>int8NDArray.h</samp>, <samp>int16NDArray.h</samp>, etc.
</p>
</dd>
<dt><code>uint8NDArray</code></dt>
<dt><code>uint16NDArray</code></dt>
<dt><code>uint32NDArray</code></dt>
<dt><code>uint64NDArray</code></dt>
<dd><p>8, 16, 32, and 64-bit unsigned array classes defined in
<samp>uint8NDArray.h</samp>, <samp>uint16NDArray.h</samp>, etc.
</p></dd>
</dl>

<p>There are several basic ways of constructing matrices or multi-dimensional
arrays.  Using the class <code>Matrix</code> as an example one can
</p>
<ul>
<li> Create an empty matrix or array with the empty constructor.  For example:

<div class="example">
<pre class="example">Matrix a;
</pre></div>

<p>This can be used for all matrix and array types.
</p>
</li><li> Define the dimensions of the matrix or array with a dim_vector which has the
same characteristics as the vector returned from <code>size</code>.  For example:

<div class="example">
<pre class="example">dim_vector dv (2, 3);  // 2 rows, 3 columns
Matrix a (dv);
</pre></div>

<p>This can be used for all matrix and array types.
</p>
</li><li> Define the number of rows and columns in the matrix.  For example:

<div class="example">
<pre class="example">Matrix a (2, 2)
</pre></div>

<p>This constructor can <strong>only</strong> be used with matrix types.
</p></li></ul>

<p>These types all share a number of basic methods and operators.  Many bear a
resemblance to functions that exist in the interpreter.  A selection of useful
methods include
</p>
<dl>
<dt id="index-operator">Method: <em>T&amp;</em> <strong>operator</strong> <em>() (octave_idx_type)</em></dt>
<dt id="index-elem">Method: <em>T&amp;</em> <strong>elem</strong> <em>(octave_idx_type)</em></dt>
<dd><p>The <code>()</code> operator or <code>elem</code> method allow the values of the matrix or
array to be read or set.  These methods take a single argument, which is of
type <code>octave_idx_type</code>, that is the index into the matrix or array.
Additionally, the matrix type allows two argument versions of the <code>()</code>
operator and <code>elem</code> method, giving the row and column index of the value
to get or set.
</p></dd></dl>

<p>Note that these functions do significant error checking and so in some
circumstances the user might prefer to access the data of the array or matrix
directly through the <code>fortran_vec</code> method discussed below.
</p>
<dl>
<dt id="index-numel-2">Method: <em>octave_idx_type</em> <strong>numel</strong> <em>(void) const</em></dt>
<dd><p>The total number of elements in the matrix or array.
</p></dd></dl>

<dl>
<dt id="index-byte_005fsize">Method: <em>size_t</em> <strong>byte_size</strong> <em>(void) const</em></dt>
<dd><p>The number of bytes used to store the matrix or array.
</p></dd></dl>

<dl>
<dt id="index-dims">Method: <em>dim_vector</em> <strong>dims</strong> <em>(void) const</em></dt>
<dd><p>The dimensions of the matrix or array in value of type <code>dim_vector</code>.
</p></dd></dl>

<dl>
<dt id="index-ndims-1">Method: <em>int</em> <strong>ndims</strong> <em>(void) const</em></dt>
<dd><p>The number of dimensions of the matrix or array.  Matrices are always 2-D, but
arrays can be N-dimensional.
</p></dd></dl>

<dl>
<dt id="index-resize-3">Method: <em>void</em> <strong>resize</strong> <em>(const dim_vector&amp;)</em></dt>
<dt id="index-resize-4">Method: <em>void</em> <strong>resize</strong> <em>(nrows, ncols)</em></dt>
<dd><p>A method taking either an argument of type <code>dim_vector</code>, or, in the case
of a matrix, two arguments of type <code>octave_idx_type</code> defining the number
of rows and columns in the matrix.
</p></dd></dl>

<dl>
<dt id="index-fortran_005fvec">Method: <em>T*</em> <strong>fortran_vec</strong> <em>(void)</em></dt>
<dd><p>This method returns a pointer to the underlying data of the matrix or array so
that it can be manipulated directly, either within Octave or by an external
library.
</p></dd></dl>

<p>Operators such as <code>+</code>, <code>-</code>, or <code>*</code> can be used on the majority
of the matrix and array types.  In addition there are a number of methods that
are of interest only for matrices such as <code>transpose</code>, <code>hermitian</code>,
<code>solve</code>, etc.
</p>
<p>The typical way to extract a matrix or array from the input arguments of
<code><span class="nolinebreak">DEFUN_DLD</span></code><!-- /@w --> function is as follows
</p>
<div class="example">
<pre class="verbatim">#include &lt;octave/oct.h&gt;

DEFUN_DLD (addtwomatrices, args, , &quot;Add A to B&quot;)
{
  if (args.length () != 2)
    print_usage ();

  NDArray A = args(0).array_value ();
  NDArray B = args(1).array_value ();

  return octave_value (A + B);
}
</pre></div>

<p>To avoid segmentation faults causing Octave to abort, this function explicitly
checks that there are sufficient arguments available before accessing these
arguments.  It then obtains two multi-dimensional arrays of type <code>NDArray</code>
and adds these together.  Note that the <code>array_value</code> method is called
without using the <code>is_matrix_type</code> method.  If an error occurs when
attempting to extract the value, Octave will print a message and throw an
exception.  The reason to prefer this coding structure is that the arguments
might be a type which is not an <code>NDArray</code>, but for which it would make
sense to convert them to one.  The <code>array_value</code> method allows this
conversion to be performed transparently when possible.  If you need to catch
errors like this, and perform some kind of cleanup or other operation, you can
catch the <code>octave_execution_error</code> exception.
</p>
<p><code>A + B</code>, operating on two <code>NDArray</code> objects returns an
<code>NDArray</code>, which is cast to an <code>octave_value</code> on the return from the
function.  An example of the use of this demonstration function is
</p>
<div class="example">
<pre class="example">addtwomatrices (ones (2, 2), eye (2, 2))
      &rArr;  2  1
          1  2
</pre></div>

<p>A list of the basic <code>Matrix</code> and <code>Array</code> types, the methods to
extract these from an <code>octave_value</code>, and the associated header file is
listed below.
</p>
<table>
<thead><tr><th width="30%">Type</th><th width="40%">Function</th><th width="30%">Source Code</th></tr></thead>
<tr><td width="30%"><code>RowVector</code></td><td width="40%"><code>row_vector_value</code></td><td width="30%"><samp>dRowVector.h</samp></td></tr>
<tr><td width="30%"><code>ComplexRowVector</code></td><td width="40%"><code>complex_row_vector_value</code></td><td width="30%"><samp>CRowVector.h</samp></td></tr>
<tr><td width="30%"><code>ColumnVector</code></td><td width="40%"><code>column_vector_value</code></td><td width="30%"><samp>dColVector.h</samp></td></tr>
<tr><td width="30%"><code>ComplexColumnVector</code></td><td width="40%"><code>complex_column_vector_value</code></td><td width="30%"><samp>CColVector.h</samp></td></tr>
<tr><td width="30%"><code>Matrix</code></td><td width="40%"><code>matrix_value</code></td><td width="30%"><samp>dMatrix.h</samp></td></tr>
<tr><td width="30%"><code>ComplexMatrix</code></td><td width="40%"><code>complex_matrix_value</code></td><td width="30%"><samp>CMatrix.h</samp></td></tr>
<tr><td width="30%"><code>boolMatrix</code></td><td width="40%"><code>bool_matrix_value</code></td><td width="30%"><samp>boolMatrix.h</samp></td></tr>
<tr><td width="30%"><code>charMatrix</code></td><td width="40%"><code>char_matrix_value</code></td><td width="30%"><samp>chMatrix.h</samp></td></tr>
<tr><td width="30%"><code>NDArray</code></td><td width="40%"><code>array_value</code></td><td width="30%"><samp>dNDArray.h</samp></td></tr>
<tr><td width="30%"><code>ComplexNDArray</code></td><td width="40%"><code>complex_array_value</code></td><td width="30%"><samp>CNDArray.h</samp></td></tr>
<tr><td width="30%"><code>boolNDArray</code></td><td width="40%"><code>bool_array_value</code></td><td width="30%"><samp>boolNDArray.h</samp></td></tr>
<tr><td width="30%"><code>charNDArray</code></td><td width="40%"><code>char_array_value</code></td><td width="30%"><samp>charNDArray.h</samp></td></tr>
<tr><td width="30%"><code>int8NDArray</code></td><td width="40%"><code>int8_array_value</code></td><td width="30%"><samp>int8NDArray.h</samp></td></tr>
<tr><td width="30%"><code>int16NDArray</code></td><td width="40%"><code>int16_array_value</code></td><td width="30%"><samp>int16NDArray.h</samp></td></tr>
<tr><td width="30%"><code>int32NDArray</code></td><td width="40%"><code>int32_array_value</code></td><td width="30%"><samp>int32NDArray.h</samp></td></tr>
<tr><td width="30%"><code>int64NDArray</code></td><td width="40%"><code>int64_array_value</code></td><td width="30%"><samp>int64NDArray.h</samp></td></tr>
<tr><td width="30%"><code>uint8NDArray</code></td><td width="40%"><code>uint8_array_value</code></td><td width="30%"><samp>uint8NDArray.h</samp></td></tr>
<tr><td width="30%"><code>uint16NDArray</code></td><td width="40%"><code>uint16_array_value</code></td><td width="30%"><samp>uint16NDArray.h</samp></td></tr>
<tr><td width="30%"><code>uint32NDArray</code></td><td width="40%"><code>uint32_array_value</code></td><td width="30%"><samp>uint32NDArray.h</samp></td></tr>
<tr><td width="30%"><code>uint64NDArray</code></td><td width="40%"><code>uint64_array_value</code></td><td width="30%"><samp>uint64NDArray.h</samp></td></tr>
</table>

<hr>
<div class="header">
<p>
Next: <a href="Character-Strings-in-Oct_002dFiles.html" accesskey="n" rel="next">Character Strings in Oct-Files</a>, Previous: <a href="Getting-Started-with-Oct_002dFiles.html" accesskey="p" rel="prev">Getting Started with Oct-Files</a>, Up: <a href="Oct_002dFiles.html" accesskey="u" rel="up">Oct-Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>