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
|
<html lang="en">
<head>
<title>Matrices and Arrays in Oct-Files - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Oct_002dFiles.html#Oct_002dFiles" title="Oct-Files">
<link rel="prev" href="Getting-Started-with-Oct_002dFiles.html#Getting-Started-with-Oct_002dFiles" title="Getting Started with Oct-Files">
<link rel="next" href="Character-Strings-in-Oct_002dFiles.html#Character-Strings-in-Oct_002dFiles" title="Character Strings in Oct-Files">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Matrices-and-Arrays-in-Oct-Files"></a>
<a name="Matrices-and-Arrays-in-Oct_002dFiles"></a>
Next: <a rel="next" accesskey="n" href="Character-Strings-in-Oct_002dFiles.html#Character-Strings-in-Oct_002dFiles">Character Strings in Oct-Files</a>,
Previous: <a rel="previous" accesskey="p" href="Getting-Started-with-Oct_002dFiles.html#Getting-Started-with-Oct_002dFiles">Getting Started with Oct-Files</a>,
Up: <a rel="up" accesskey="u" href="Oct_002dFiles.html#Oct_002dFiles">Oct-Files</a>
<hr>
</div>
<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 Array class. The exception is the
sparse matrix types discussed separately below. There are three basic
matrix types
<dl>
<dt><code>Matrix</code><dd>A double precision matrix class defined in dMatrix.h,
<br><dt><code>ComplexMatrix</code><dd>A complex matrix class defined in CMatrix.h, and
<br><dt><code>BoolMatrix</code><dd>A boolean matrix class defined in boolMatrix.h.
</dl>
<p>These are the basic two-dimensional matrix types of octave. In
additional there are a number of multi-dimensional array types, these
being
<dl>
<dt><code>NDArray</code><dd>A double precision array class defined in <samp><span class="file">dNDArray.h</span></samp>
<br><dt><code>ComplexNDarray</code><dd>A complex array class defined in <samp><span class="file">CNDArray.h</span></samp>
<br><dt><code>boolNDArray</code><dd>A boolean array class defined in <samp><span class="file">boolNDArray.h</span></samp>
<br><dt><code>int8NDArray</code><dt><code>int16NDArray</code><dt><code>int32NDArray</code><dt><code>int64NDArray</code><dd>8, 16, 32 and 64-bit signed array classes defined in
<samp><span class="file">int8NDArray.h</span></samp>, <samp><span class="file">int16NDArray.h</span></samp>, etc.
<br><dt><code>uint8NDArray</code><dt><code>uint16NDArray</code><dt><code>uint32NDArray</code><dt><code>uint64NDArray</code><dd>8, 16, 32 and 64-bit unsigned array classes defined in
<samp><span class="file">uint8NDArray.h</span></samp>, <samp><span class="file">uint16NDArray.h</span></samp>, etc.
</dl>
<p>There are several basic means of constructing matrices of
multi-dimensional arrays. Considering the <code>Matrix</code> type as an
example
<ul>
<li>We can create an empty matrix or array with the empty constructor. For
example
<pre class="example"> Matrix a;
</pre>
<p>This can be used on all matrix and array types
<li>Define the dimensions of the matrix or array with a dim_vector. For
example
<pre class="example"> dim_vector dv (2);
dv(0) = 2; dv(1) = 2;
Matrix a (dv);
</pre>
<p>This can be used on all matrix and array types
<li>Define the number of rows and columns in the matrix. For example
<pre class="example"> Matrix a (2, 2)
</pre>
<p>However, this constructor can only be used with the matrix types.
</ul>
<p>These types all share a number of basic methods and operators, a
selection of which include
<div class="defun">
— Method: T& <b>operator ()</b> (<var>octave_idx_type</var>)<var><a name="index-operator-_0028_0029-2473"></a></var><br>
— Method: T& <b>elem</b> (<var>octave_idx_type</var>)<var><a name="index-elem-2474"></a></var><br>
<blockquote><p>The <code>()</code> operator or <code>elem</code> method allow the values of the
matrix or array to be read or set. These can 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 elem method, giving the row and column index of the
value to obtain or set.
</p></blockquote></div>
<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 fortran_vec method discussed below.
<div class="defun">
— Method: octave_idx_type <b>nelem</b> (<var>void</var>)<var> const<a name="index-nelem-2475"></a></var><br>
<blockquote><p>The total number of elements in the matrix or array.
</p></blockquote></div>
<div class="defun">
— Method: size_t <b>byte_size</b> (<var>void</var>)<var> const<a name="index-byte_005fsize-2476"></a></var><br>
<blockquote><p>The number of bytes used to store the matrix or array.
</p></blockquote></div>
<div class="defun">
— Method: dim_vector <b>dims</b> (<var>void</var>)<var> const<a name="index-dims-2477"></a></var><br>
<blockquote><p>The dimensions of the matrix or array in value of type dim_vector.
</p></blockquote></div>
<div class="defun">
— Method: void <b>resize</b> (<var>const dim_vector&</var>)<var><a name="index-resize-2478"></a></var><br>
<blockquote><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></blockquote></div>
<div class="defun">
— Method: T* <b>fortran_vec</b> (<var>void</var>)<var><a name="index-fortran_005fvec-2479"></a></var><br>
<blockquote><p>This method returns a pointer to the underlying data of the matrix or a
array so that it can be manipulated directly, either within Octave or by
an external library.
</p></blockquote></div>
<p>Operators such an <code>+</code>, <code>-</code>, or <code>*</code> can be used on the
majority of the above 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>The typical way to extract a matrix or array from the input arguments of
<code>DEFUN_DLD</code><!-- /@w --> function is as follows
<pre class="example"><pre class="verbatim"> #include <octave/oct.h>
DEFUN_DLD (addtwomatrices, args, , "Add A to B")
{
int nargin = args.length ();
if (nargin != 2)
print_usage ();
else
{
NDArray A = args(0).array_value ();
NDArray B = args(1).array_value ();
if (! error_state)
return octave_value (A + B);
}
return octave_value_list ();
}
</pre>
</pre>
<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 array_value
method is called without using the <code>is_matrix_type</code> type, and instead the
error_state is checked before returning <code>A + B</code>. The reason to
prefer this is that the arguments might be a type that is not an
<code>NDArray</code>, but it would make sense to convert it to one. The
<code>array_value</code> method allows this conversion to be performed
transparently if possible, and sets <code>error_state</code> if it is not.
<p><code>A + B</code>, operating on two <code>NDArray</code>'s 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
<pre class="example"> addtwomatrices (ones (2, 2), ones (2, 2))
2 2
2 2
</pre>
<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 is
listed below.
<p><table summary=""><tr align="left"><td valign="top" width="30%"><code>RowVector</code> </td><td valign="top" width="40%"><code>row_vector_value</code> </td><td valign="top" width="30%"><samp><span class="file">dRowVector.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>ComplexRowVector</code> </td><td valign="top" width="40%"><code>complex_row_vector_value</code> </td><td valign="top" width="30%"><samp><span class="file">CRowVector.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>ColumnVector</code> </td><td valign="top" width="40%"><code>column_vector_value</code> </td><td valign="top" width="30%"><samp><span class="file">dColVector.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>ComplexColumnVector</code> </td><td valign="top" width="40%"><code>complex_column_vector_value</code> </td><td valign="top" width="30%"><samp><span class="file">CColVector.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>Matrix</code> </td><td valign="top" width="40%"><code>matrix_value</code> </td><td valign="top" width="30%"><samp><span class="file">dMatrix.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>ComplexMatrix</code> </td><td valign="top" width="40%"><code>complex_matrix_value</code> </td><td valign="top" width="30%"><samp><span class="file">CMatrix.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>boolMatrix</code> </td><td valign="top" width="40%"><code>bool_matrix_value</code> </td><td valign="top" width="30%"><samp><span class="file">boolMatrix.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>charMatrix</code> </td><td valign="top" width="40%"><code>char_matrix_value</code> </td><td valign="top" width="30%"><samp><span class="file">chMatrix.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>NDArray</code> </td><td valign="top" width="40%"><code>array_value</code> </td><td valign="top" width="30%"><samp><span class="file">dNDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>ComplexNDArray</code> </td><td valign="top" width="40%"><code>complex_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">CNDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>boolNDArray</code> </td><td valign="top" width="40%"><code>bool_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">boolNDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>charNDArray</code> </td><td valign="top" width="40%"><code>char_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">charNDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>int8NDArray</code> </td><td valign="top" width="40%"><code>int8_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">int8NDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>int16NDArray</code> </td><td valign="top" width="40%"><code>int16_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">int16NDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>int32NDArray</code> </td><td valign="top" width="40%"><code>int32_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">int32NDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>int64NDArray</code> </td><td valign="top" width="40%"><code>int64_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">int64NDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>uint8NDArray</code> </td><td valign="top" width="40%"><code>uint8_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">uint8NDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>uint16NDArray</code> </td><td valign="top" width="40%"><code>uint16_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">uint16NDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>uint32NDArray</code> </td><td valign="top" width="40%"><code>uint32_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">uint32NDArray.h</span></samp>
<br></td></tr><tr align="left"><td valign="top" width="30%"><code>uint64NDArray</code> </td><td valign="top" width="40%"><code>uint64_array_value</code> </td><td valign="top" width="30%"><samp><span class="file">uint64NDArray.h</span></samp>
<br></td></tr></table>
</body></html>
|