<html lang="en"> <head> <title>Arithmetic Ops - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Expressions.html#Expressions" title="Expressions"> <link rel="prev" href="Calling-Functions.html#Calling-Functions" title="Calling Functions"> <link rel="next" href="Comparison-Ops.html#Comparison-Ops" title="Comparison Ops"> <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"> <a name="Arithmetic-Ops"></a> <p> Next: <a rel="next" accesskey="n" href="Comparison-Ops.html#Comparison-Ops">Comparison Ops</a>, Previous: <a rel="previous" accesskey="p" href="Calling-Functions.html#Calling-Functions">Calling Functions</a>, Up: <a rel="up" accesskey="u" href="Expressions.html#Expressions">Expressions</a> <hr> </div> <h3 class="section">8.3 Arithmetic Operators</h3> <p><a name="index-arithmetic-operators-531"></a><a name="index-operators_002c-arithmetic-532"></a><a name="index-addition-533"></a><a name="index-subtraction-534"></a><a name="index-multiplication-535"></a><a name="index-matrix-multiplication-536"></a><a name="index-division-537"></a><a name="index-quotient-538"></a><a name="index-negation-539"></a><a name="index-unary-minus-540"></a><a name="index-exponentiation-541"></a><a name="index-transpose-542"></a><a name="index-Hermitian-operator-543"></a><a name="index-transpose_002c-complex_002dconjugate-544"></a><a name="index-complex_002dconjugate-transpose-545"></a> The following arithmetic operators are available, and work on scalars and matrices. The element-by-element operators and functions broadcast (see <a href="Broadcasting.html#Broadcasting">Broadcasting</a>). <dl> <dt><var>x</var> + <var>y</var><dd><a name="index-g_t_002b-546"></a>Addition. If both operands are matrices, the number of rows and columns must both agree, or they must be broadcastable to the same shape. <br><dt><var>x</var> .+ <var>y</var><dd><a name="index-g_t_002e_002b-547"></a>Element-by-element addition. This operator is equivalent to <code>+</code>. <br><dt><var>x</var> - <var>y</var><dd><a name="index-g_t_002d-548"></a>Subtraction. If both operands are matrices, the number of rows and columns of both must agree, or they must be broadcastable to the same shape. <br><dt><var>x</var> .- <var>y</var><dd>Element-by-element subtraction. This operator is equivalent to <code>-</code>. <br><dt><var>x</var> * <var>y</var><dd><a name="index-g_t_002a-549"></a>Matrix multiplication. The number of columns of <var>x</var> must agree with the number of rows of <var>y</var>, or they must be broadcastable to the same shape. <br><dt><var>x</var> .* <var>y</var><dd><a name="index-g_t_002e_002a-550"></a>Element-by-element multiplication. If both operands are matrices, the number of rows and columns must both agree, or they must be broadcastable to the same shape. <br><dt><var>x</var> / <var>y</var><dd><a name="index-g_t_002f-551"></a>Right division. This is conceptually equivalent to the expression <pre class="example"> (inverse (y') * x')' </pre> <p class="noindent">but it is computed without forming the inverse of <var>y'</var>. <p>If the system is not square, or if the coefficient matrix is singular, a minimum norm solution is computed. <br><dt><var>x</var> ./ <var>y</var><dd><a name="index-g_t_002e_002f-552"></a>Element-by-element right division. <br><dt><var>x</var> \ <var>y</var><dd><a name="index-g_t_005c-553"></a>Left division. This is conceptually equivalent to the expression <pre class="example"> inverse (x) * y </pre> <p class="noindent">but it is computed without forming the inverse of <var>x</var>. <p>If the system is not square, or if the coefficient matrix is singular, a minimum norm solution is computed. <br><dt><var>x</var> .\ <var>y</var><dd><a name="index-g_t_002e_005c-554"></a>Element-by-element left division. Each element of <var>y</var> is divided by each corresponding element of <var>x</var>. <br><dt><var>x</var> ^ <var>y</var><dt><var>x</var> ** <var>y</var><dd><a name="index-g_t_002a_002a-555"></a><a name="index-g_t_005e-556"></a>Power operator. If <var>x</var> and <var>y</var> are both scalars, this operator returns <var>x</var> raised to the power <var>y</var>. If <var>x</var> is a scalar and <var>y</var> is a square matrix, the result is computed using an eigenvalue expansion. If <var>x</var> is a square matrix, the result is computed by repeated multiplication if <var>y</var> is an integer, and by an eigenvalue expansion if <var>y</var> is not an integer. An error results if both <var>x</var> and <var>y</var> are matrices. <p>The implementation of this operator needs to be improved. <br><dt><var>x</var> .^ <var>y</var><dt><var>x</var> .** <var>y</var><dd><a name="index-g_t_002e_002a_002a-557"></a><a name="index-g_t_002e_005e-558"></a>Element-by-element power operator. If both operands are matrices, the number of rows and columns must both agree, or they must be broadcastable to the same shape. If several complex results are possible, the one with smallest non-negative argument (angle) is taken. This rule may return a complex root even when a real root is also possible. Use <code>realpow</code>, <code>realsqrt</code>, <code>cbrt</code>, or <code>nthroot</code> if a real result is preferred. <br><dt>-<var>x</var><dd><a name="index-g_t_002d-559"></a>Negation. <br><dt>+<var>x</var><dd><a name="index-g_t_002b-560"></a>Unary plus. This operator has no effect on the operand. <br><dt><var>x</var>'<dd><a name="index-g_t_0027-561"></a>Complex conjugate transpose. For real arguments, this operator is the same as the transpose operator. For complex arguments, this operator is equivalent to the expression <pre class="example"> conj (x.') </pre> <br><dt><var>x</var>.'<dd><a name="index-g_t_002e_0027-562"></a>Transpose. </dl> <p>Note that because Octave's element-by-element operators begin with a ‘<samp><span class="samp">.</span></samp>’, there is a possible ambiguity for statements like <pre class="example"> 1./m </pre> <p class="noindent">because the period could be interpreted either as part of the constant or as part of the operator. To resolve this conflict, Octave treats the expression as if you had typed <pre class="example"> (1) ./ m </pre> <p class="noindent">and not <pre class="example"> (1.) / m </pre> <p class="noindent">Although this is inconsistent with the normal behavior of Octave's lexer, which usually prefers to break the input into tokens by preferring the longest possible match at any given point, it is more useful in this case. <p><a name="index-g_t_0027-563"></a><!-- ctranspose src/data.cc --> <a name="doc_002dctranspose"></a> <div class="defun"> — Built-in Function: <b>ctranspose</b> (<var>x</var>)<var><a name="index-ctranspose-564"></a></var><br> <blockquote><p>Return the complex conjugate transpose of <var>x</var>. This function and <code>x'</code> are equivalent. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dtranspose.html#doc_002dtranspose">transpose</a>. </p></blockquote></div> <p><a name="index-g_t_002e_005c-565"></a><!-- ldivide src/data.cc --> <a name="doc_002dldivide"></a> <div class="defun"> — Built-in Function: <b>ldivide</b> (<var>x, y</var>)<var><a name="index-ldivide-566"></a></var><br> <blockquote><p>Return the element-by-element left division of <var>x</var> and <var>y</var>. This function and <code>x . y</code><!-- /@w --> are equivalent. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002drdivide.html#doc_002drdivide">rdivide</a>, <a href="doc_002dmldivide.html#doc_002dmldivide">mldivide</a>. </p></blockquote></div> <p><a name="index-g_t_002d-567"></a><!-- minus src/data.cc --> <a name="doc_002dminus"></a> <div class="defun"> — Built-in Function: <b>minus</b> (<var>x, y</var>)<var><a name="index-minus-568"></a></var><br> <blockquote><p>This function and <code>x - y</code><!-- /@w --> are equivalent. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dplus.html#doc_002dplus">plus</a>. </p></blockquote></div> <p><a name="index-g_t_005c-569"></a><!-- mldivide src/data.cc --> <a name="doc_002dmldivide"></a> <div class="defun"> — Built-in Function: <b>mldivide</b> (<var>x, y</var>)<var><a name="index-mldivide-570"></a></var><br> <blockquote><p>Return the matrix left division of <var>x</var> and <var>y</var>. This function and <code>x y</code><!-- /@w --> are equivalent. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmrdivide.html#doc_002dmrdivide">mrdivide</a>, <a href="doc_002dldivide.html#doc_002dldivide">ldivide</a>. </p></blockquote></div> <p><a name="index-g_t_002a_002a-571"></a><a name="index-g_t_005e-572"></a><!-- mpower src/data.cc --> <a name="doc_002dmpower"></a> <div class="defun"> — Built-in Function: <b>mpower</b> (<var>x, y</var>)<var><a name="index-mpower-573"></a></var><br> <blockquote><p>Return the matrix power operation of <var>x</var> raised to the <var>y</var> power. This function and <code>x ^ y</code><!-- /@w --> are equivalent. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dpower.html#doc_002dpower">power</a>. </p></blockquote></div> <p><a name="index-g_t_002f-574"></a><!-- mrdivide src/data.cc --> <a name="doc_002dmrdivide"></a> <div class="defun"> — Built-in Function: <b>mrdivide</b> (<var>x, y</var>)<var><a name="index-mrdivide-575"></a></var><br> <blockquote><p>Return the matrix right division of <var>x</var> and <var>y</var>. This function and <code>x / y</code><!-- /@w --> are equivalent. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmldivide.html#doc_002dmldivide">mldivide</a>, <a href="doc_002drdivide.html#doc_002drdivide">rdivide</a>. </p></blockquote></div> <p><a name="index-g_t_002a-576"></a><!-- mtimes src/data.cc --> <a name="doc_002dmtimes"></a> <div class="defun"> — Built-in Function: <b>mtimes</b> (<var>x, y</var>)<var><a name="index-mtimes-577"></a></var><br> — Built-in Function: <b>mtimes</b> (<var>x1, x2, <small class="dots">...</small></var>)<var><a name="index-mtimes-578"></a></var><br> <blockquote><p>Return the matrix multiplication product of inputs. This function and <code>x * y</code><!-- /@w --> are equivalent. If more arguments are given, the multiplication is applied cumulatively from left to right: <pre class="example"> (...((x1 * x2) * x3) * ...) </pre> <p>At least one argument is required. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dtimes.html#doc_002dtimes">times</a>. </p></blockquote></div> <p><a name="index-g_t_002b-579"></a><!-- plus src/data.cc --> <a name="doc_002dplus"></a> <div class="defun"> — Built-in Function: <b>plus</b> (<var>x, y</var>)<var><a name="index-plus-580"></a></var><br> — Built-in Function: <b>plus</b> (<var>x1, x2, <small class="dots">...</small></var>)<var><a name="index-plus-581"></a></var><br> <blockquote><p>This function and <code>x + y</code><!-- /@w --> are equivalent. If more arguments are given, the summation is applied cumulatively from left to right: <pre class="example"> (...((x1 + x2) + x3) + ...) </pre> <p>At least one argument is required. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dminus.html#doc_002dminus">minus</a>. </p></blockquote></div> <p><a name="index-g_t_002e_002a_002a-582"></a><a name="index-g_t_002e_005e-583"></a><!-- power src/data.cc --> <a name="doc_002dpower"></a> <div class="defun"> — Built-in Function: <b>power</b> (<var>x, y</var>)<var><a name="index-power-584"></a></var><br> <blockquote><p>Return the element-by-element operation of <var>x</var> raised to the <var>y</var> power. If several complex results are possible, returns the one with smallest non-negative argument (angle). Use <code>realpow</code>, <code>realsqrt</code>, <code>cbrt</code>, or <code>nthroot</code> if a real result is preferred. <p>This function and <code>x .^ y</code><!-- /@w --> are equivalent. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmpower.html#doc_002dmpower">mpower</a>, <a href="doc_002drealpow.html#doc_002drealpow">realpow</a>, <a href="doc_002drealsqrt.html#doc_002drealsqrt">realsqrt</a>, <a href="doc_002dcbrt.html#doc_002dcbrt">cbrt</a>, <a href="doc_002dnthroot.html#doc_002dnthroot">nthroot</a>. </p></blockquote></div> <p><a name="index-g_t_002e_002f-585"></a><!-- rdivide src/data.cc --> <a name="doc_002drdivide"></a> <div class="defun"> — Built-in Function: <b>rdivide</b> (<var>x, y</var>)<var><a name="index-rdivide-586"></a></var><br> <blockquote><p>Return the element-by-element right division of <var>x</var> and <var>y</var>. This function and <code>x ./ y</code><!-- /@w --> are equivalent. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dldivide.html#doc_002dldivide">ldivide</a>, <a href="doc_002dmrdivide.html#doc_002dmrdivide">mrdivide</a>. </p></blockquote></div> <p><a name="index-g_t_002e_002a-587"></a><!-- times src/data.cc --> <a name="doc_002dtimes"></a> <div class="defun"> — Built-in Function: <b>times</b> (<var>x, y</var>)<var><a name="index-times-588"></a></var><br> — Built-in Function: <b>times</b> (<var>x1, x2, <small class="dots">...</small></var>)<var><a name="index-times-589"></a></var><br> <blockquote><p>Return the element-by-element multiplication product of inputs. This function and <code>x .* y</code><!-- /@w --> are equivalent. If more arguments are given, the multiplication is applied cumulatively from left to right: <pre class="example"> (...((x1 .* x2) .* x3) .* ...) </pre> <p>At least one argument is required. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmtimes.html#doc_002dmtimes">mtimes</a>. </p></blockquote></div> <p><a name="index-g_t_002e_0027-590"></a><!-- transpose src/data.cc --> <a name="doc_002dtranspose"></a> <div class="defun"> — Built-in Function: <b>transpose</b> (<var>x</var>)<var><a name="index-transpose-591"></a></var><br> <blockquote><p>Return the transpose of <var>x</var>. This function and <code>x.'</code> are equivalent. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dctranspose.html#doc_002dctranspose">ctranspose</a>. </p></blockquote></div> <p><a name="index-g_t_002d-592"></a><!-- uminus src/data.cc --> <a name="doc_002duminus"></a> <div class="defun"> — Built-in Function: <b>uminus</b> (<var>x</var>)<var><a name="index-uminus-593"></a></var><br> <blockquote><p>This function and <code>- x</code><!-- /@w --> are equivalent. </p></blockquote></div> <p><a name="index-g_t_002b-594"></a><!-- uplus src/data.cc --> <a name="doc_002duplus"></a> <div class="defun"> — Built-in Function: <b>uplus</b> (<var>x</var>)<var><a name="index-uplus-595"></a></var><br> <blockquote><p>This function and <code>+ x</code><!-- /@w --> are equivalent. </p></blockquote></div> </body></html>