Editing Tips and tricks
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
=C++= | |||
== Real matrix operations== | |||
This is a table of matrix operations commonly performed in Octave and their equivalents in C++ when using the octave libraries. | This is a table of matrix operations commonly performed in Octave and their equivalents in C++ when using the octave libraries. | ||
Line 78: | Line 10: | ||
<tr><td>element multiplication</td><td><code>A.*B</code></td><td><code>product(A,B) </code></td></tr> | <tr><td>element multiplication</td><td><code>A.*B</code></td><td><code>product(A,B) </code></td></tr> | ||
<tr><td>element division</td><td><code>A./B</code></td><td><code>quotient(A,B) </code></td></tr> | <tr><td>element division</td><td><code>A./B</code></td><td><code>quotient(A,B) </code></td></tr> | ||
<tr><td>transpose*</td><td><code>A | <tr><td>transpose*</td><td><code>A'</code></td><td><code>A.transpose()</code></td></tr> | ||
<tr><td>select element m,n of A**</td><td><code>A(m,n)</code></td><td><code>A(m-1,n-1)</code></td></tr> | <tr><td>select element m,n of A**</td><td><code>A(m,n)</code></td><td><code>A(m-1,n-1)</code></td></tr> | ||
<tr><td>select row N of A**</td><td><code>A(N,:)</code></td><td><code>A.row(N-1)</code></td></tr> | <tr><td>select row N of A**</td><td><code>A(N,:)</code></td><td><code>A.row(N-1)</code></td></tr> | ||
Line 98: | Line 30: | ||
<tr><td>column vector</td><td><code>A(:)</code></td><td><code>ColumnVector(A.reshape (dim_vector(A.length())))</code></td></tr> | <tr><td>column vector</td><td><code>A(:)</code></td><td><code>ColumnVector(A.reshape (dim_vector(A.length())))</code></td></tr> | ||
<tr><td>row vector</td><td><code>A(:)'</code></td><td><code>RowVector(A.reshape (dim_vector(A.length())))</code></td></tr> | <tr><td>row vector</td><td><code>A(:)'</code></td><td><code>RowVector(A.reshape (dim_vector(A.length())))</code></td></tr> | ||
<tr><td>check for Inf or NaN</td><td><code>any(~isfinite(A))</code></td><td><code>A.any_element_is_inf_or_nan()</code></td></tr> | <tr><td>check for Inf or <a href="wiki.pl?NaN">NaN</a></td><td><code>any(~isfinite(A))</code></td><td><code>A.any_element_is_inf_or_nan()</code></td></tr> | ||
<tr><td>stack two matrices vertically</td><td><code>A=[B;C]</code></td><td><code>B.stack(C)</code></td></tr> | <tr><td>stack two matrices vertically</td><td><code>A=[B;C]</code></td><td><code>B.stack(C)</code></td></tr> | ||
<tr><td>uniform random matrix</td><td><code>rand(a,b)</code></td><td><code>octave_rand::distribution("uniform"); octave_rand::matrix(a,b)</code></td></tr> | <tr><td>uniform random matrix</td><td><code>rand(a,b)</code></td><td><code>octave_rand::distribution("uniform"); octave_rand::matrix(a,b)</code></td></tr> | ||
Line 113: | Line 45: | ||
<tr><td>number of rows</td><td><code>size(A,1)</code></td><td><code>A.rows()</code></td></tr> | <tr><td>number of rows</td><td><code>size(A,1)</code></td><td><code>A.rows()</code></td></tr> | ||
<tr><td>number of columns</td><td><code>size(A,2)</code></td><td><code>A.cols()</code></td></tr> | <tr><td>number of columns</td><td><code>size(A,2)</code></td><td><code>A.cols()</code></td></tr> | ||
</table> | </table> | ||
Notes: | Notes: | ||
<nowiki> | |||
*Transpose, addition, and multiplication operations also apply to RowVector, ComplexRowVector, ColumnVector, and ComplexColumnVector data types when the dimensions are in agreement. | |||
* | **The difference is due to the fact that arrays are zero-based in C++, but one-based in Octave. | ||
* The difference is due to the fact that arrays are zero-based in C++, but one-based in Octave | |||
***The names of Octave internal functions, such as mx_el_gt, are not documented and are subject to change. Functions such as mx_el_gt may eventually be available at both the scripting level and in C++ under more common names such as gt. | |||
</nowiki> | |||
==Complex Matrix Operations== | |||
<table> | <table> | ||
<tr><td><b>Operation</b></td><td><b>Octave</b></td><td><b>C++</b></td></tr> | <tr><td><b>Operation</b></td><td><b>Octave</b></td><td><b>C++</b></td></tr> | ||
<tr><td>conjugate tranpose</td><td><code>A'</code></td><td><code>A.hermitian()</code></td></tr> | <tr><td>conjugate tranpose</td><td><code>A'</code></td><td><code>A.hermitian()</code></td></tr> | ||
</table> | </table> | ||