File: Arithmetic-Ops.html

package info (click to toggle)
octave 3.6.2-5%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 71,636 kB
  • sloc: cpp: 241,186; fortran: 23,651; sh: 14,790; ansic: 7,153; lex: 3,761; objc: 3,404; yacc: 3,386; makefile: 2,073; awk: 985; perl: 838
file content (345 lines) | stat: -rw-r--r-- 18,024 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<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:&nbsp;<a rel="next" accesskey="n" href="Comparison-Ops.html#Comparison-Ops">Comparison Ops</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Calling-Functions.html#Calling-Functions">Calling Functions</a>,
Up:&nbsp;<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
&lsquo;<samp><span class="samp">.</span></samp>&rsquo;, 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">
&mdash; 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">
&mdash; 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&nbsp;.&nbsp;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">
&mdash; 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&nbsp;-&nbsp;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">
&mdash; 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&nbsp;&nbsp;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">
&mdash; 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&nbsp;^&nbsp;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">
&mdash; 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&nbsp;/&nbsp;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">
&mdash; Built-in Function:  <b>mtimes</b> (<var>x, y</var>)<var><a name="index-mtimes-577"></a></var><br>
&mdash; 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&nbsp;*&nbsp;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">
&mdash; Built-in Function:  <b>plus</b> (<var>x, y</var>)<var><a name="index-plus-580"></a></var><br>
&mdash; 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&nbsp;+&nbsp;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">
&mdash; 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&nbsp;.^&nbsp;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">
&mdash; 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&nbsp;./&nbsp;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">
&mdash; Built-in Function:  <b>times</b> (<var>x, y</var>)<var><a name="index-times-588"></a></var><br>
&mdash; 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&nbsp;.*&nbsp;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">
&mdash; 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">
&mdash; Built-in Function:  <b>uminus</b> (<var>x</var>)<var><a name="index-uminus-593"></a></var><br>
<blockquote><p>This function and <code>-&nbsp;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">
&mdash; Built-in Function:  <b>uplus</b> (<var>x</var>)<var><a name="index-uplus-595"></a></var><br>
<blockquote><p>This function and <code>+&nbsp;x</code><!-- /@w --> are equivalent. 
</p></blockquote></div>

   </body></html>