annotate doc/interpreter/expr.texi @ 2689:8c7955a8d49f

[project @ 1997-02-18 09:06:10 by jwe]
author jwe
date Tue, 18 Feb 1997 09:09:12 +0000
parents 18192eea4973
children 91589ab98e37
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
1 @c Copyright (C) 1996, 1997 John W. Eaton
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
2 @c This is part of the Octave manual.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
3 @c For copying conditions, see the file gpl.texi.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
4
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
5 @node Expressions, Evaluation, Variables, Top
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
6 @chapter Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
7 @cindex expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
8
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
9 Expressions are the basic building block of statements in Octave. An
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
10 expression evaluates to a value, which you can print, test, store in a
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
11 variable, pass to a function, or assign a new value to a variable with
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
12 an assignment operator.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
13
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
14 An expression can serve as a statement on its own. Most other kinds of
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
15 statements contain one or more expressions which specify data to be
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
16 operated on. As in other languages, expressions in Octave include
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
17 variables, array references, constants, and function calls, as well as
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
18 combinations of these with various operators.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
19
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
20 @menu
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
21 * Index Expressions::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
22 * Calling Functions::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
23 * Arithmetic Ops::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
24 * Comparison Ops::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
25 * Boolean Expressions::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
26 * Assignment Ops::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
27 * Increment Ops::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
28 * Operator Precedence::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
29 @end menu
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
30
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
31 @node Index Expressions, Calling Functions, Expressions, Expressions
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
32 @section Index Expressions
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
33
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
34 @opindex (
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
35 @opindex )
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
36
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
37 An @dfn{index expression} allows you to reference or extract selected
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
38 elements of a matrix or vector.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
39
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
40 Indices may be scalars, vectors, ranges, or the special operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
41 @samp{:}, which may be used to select entire rows or columns.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
42
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
43 Vectors are indexed using a single expression. Matrices require two
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
44 indices unless the value of the built-in variable
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
45 @code{do_fortran_indexing} is nonzero, in which case matrices may
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
46 also be indexed by a single expression.
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
47
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
48 @defvr {Built-in Variable} do_fortran_indexing
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
49 If the value of @code{do_fortran_indexing} is nonzero, Octave allows
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
50 you to select elements of a two-dimensional matrix using a single index
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
51 by treating the matrix as a single vector created from the columns of
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
52 the matrix. The default value is 0.
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
53 @end defvr
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
54
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
55 Given the matrix
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
56
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
57 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
58 a = [1, 2; 3, 4]
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
59 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
60
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
61 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
62 all of the following expressions are equivalent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
63
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
64 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
65 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
66 a (1, [1, 2])
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
67 a (1, 1:2)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
68 a (1, :)
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
69 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
70 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
71
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
72 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
73 and select the first row of the matrix.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
74
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
75 A special form of indexing may be used to select elements of a matrix or
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
76 vector. If the indices are vectors made up of only ones and zeros, the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
77 result is a new matrix whose elements correspond to the elements of the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
78 index vector that are equal to one. For example,
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
79
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
80 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
81 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
82 a = [1, 2; 3, 4];
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
83 a ([1, 0], :)
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
84 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
85 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
86
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
87 @noindent
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
88 selects the first row of the matrix @code{a}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
89
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
90 This operation can be useful for selecting elements of a matrix based on
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
91 some condition, since the comparison operators return matrices of ones
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
92 and zeros.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
93
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
94 This special zero-one form of indexing leads to a conflict with the
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
95 standard indexing operation. For example, should the following
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
96 statements
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
97
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
98 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
99 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
100 a = [1, 2; 3, 4];
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
101 a ([1, 1], :)
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
102 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
103 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
104
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
105 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
106 return the original matrix, or the matrix formed by selecting the first
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
107 row twice? Although this conflict is not likely to arise very often in
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
108 practice, you may select the behavior you prefer by setting the built-in
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
109 variable @code{prefer_zero_one_indexing}.
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
110
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
111 @defvr {Built-in Variable} prefer_zero_one_indexing
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
112 If the value of @code{prefer_zero_one_indexing} is nonzero, Octave
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
113 will perform zero-one style indexing when there is a conflict with the
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
114 normal indexing rules. @xref{Index Expressions}. For example, given a
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
115 matrix
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
116
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
117 @example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
118 a = [1, 2, 3, 4]
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
119 @end example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
120
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
121 @noindent
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
122 with @code{prefer_zero_one_indexing} is set to nonzero, the
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
123 expression
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
124
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
125 @example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
126 a ([1, 1, 1, 1])
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
127 @end example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
128
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
129 @noindent
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
130 results in the matrix @code{[ 1, 2, 3, 4 ]}. If the value of
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
131 @code{prefer_zero_one_indexing} set to 0, the result would be
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
132 the matrix @code{[ 1, 1, 1, 1 ]}.
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
133
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
134 In the first case, Octave is selecting each element corresponding to a
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
135 @samp{1} in the index vector. In the second, Octave is selecting the
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
136 first element multiple times.
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
137
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
138 The default value for @code{prefer_zero_one_indexing} is 0.
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
139 @end defvr
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
140
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
141 Finally, indexing a scalar with a vector of ones can be used to create a
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
142 vector the same size as the the index vector, with each element equal to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
143 the value of the original scalar. For example, the following statements
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
144
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
145 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
146 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
147 a = 13;
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
148 a ([1, 1, 1, 1])
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
149 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
150 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
151
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
152 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
153 produce a vector whose four elements are all equal to 13.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
154
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
155 Similarly, indexing a scalar with two vectors of ones can be used to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
156 create a matrix. For example the following statements
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
157
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
158 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
159 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
160 a = 13;
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
161 a ([1, 1], [1, 1, 1])
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
162 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
163 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
164
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
165 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
166 create a 2 by 3 matrix with all elements equal to 13.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
167
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
168 This is an obscure notation and should be avoided. It is better to
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
169 use the function @code{ones} to generate a matrix of the appropriate
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
170 size whose elements are all one, and then to scale it to produce the
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
171 desired result. @xref{Special Utility Matrices}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
172
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
173 @defvr {Built-in Variable} prefer_column_vectors
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
174 If @code{prefer_column_vectors} is nonzero, operations like
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
175
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
176 @example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
177 for i = 1:10
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
178 a (i) = i;
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
179 endfor
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
180 @end example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
181
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
182 @noindent
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
183 (for @code{a} previously undefined) produce column vectors. Otherwise, row
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
184 vectors are preferred. The default value is 0.
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
185
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
186 If a variable is already defined to be a vector (a matrix with a single
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
187 row or column), the original orientation is respected, regardless of the
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
188 value of @code{prefer_column_vectors}.
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
189 @end defvr
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
190
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
191 @defvr {Built-in Variable} resize_on_range_error
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
192 If the value of @code{resize_on_range_error} is nonzero, expressions
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
193 like
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
194
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
195 @example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
196 for i = 1:10
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
197 a (i) = sqrt (i);
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
198 endfor
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
199 @end example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
200
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
201 @noindent
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
202 (for @code{a} previously undefined) result in the variable @code{a}
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
203 being resized to be just large enough to hold the new value. New
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
204 elements that have not been given a value are set to zero. If the value
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
205 of @code{resize_on_range_error} is 0, an error message is printed and
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
206 control is returned to the top level. The default value is 1.
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
207 @end defvr
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
208
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
209 Note that it is quite inefficient to create a vector using a loop like
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
210 the one shown in the example above. In this particular case, it would
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
211 have been much more efficient to use the expression
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
212
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
213 @example
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
214 a = sqrt (1:10);
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
215 @end example
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
216
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
217 @noindent
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
218 thus avoiding the loop entirely. In cases where a loop is still
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
219 required, or a number of values must be combined to form a larger
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
220 matrix, it is generally much faster to set the size of the matrix first,
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
221 and then insert elements using indexing commands. For example, given a
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
222 matrix @code{a},
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
223
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
224 @example
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
225 @group
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
226 [nr, nc] = size (a);
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
227 x = zeros (nr, n * nc);
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
228 for i = 1:n
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
229 x(:,(i-1)*n+1:i*n) = a;
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
230 endfor
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
231 @end group
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
232 @end example
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
233
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
234 @noindent
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
235 is considerably faster than
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
236
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
237 @example
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
238 @group
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
239 x = a;
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
240 for i = 1:n-1
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
241 x = [x, a];
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
242 endfor
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
243 @end group
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
244 @end example
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
245
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
246 @noindent
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
247 particularly for large matrices because Octave does not have to
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
248 repeatedly resize the result.
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
249
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
250 @node Calling Functions, Arithmetic Ops, Index Expressions, Expressions
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
251 @section Calling Functions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
252
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
253 A @dfn{function} is a name for a particular calculation. Because it has
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
254 a name, you can ask for it by name at any point in the program. For
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
255 example, the function @code{sqrt} computes the square root of a number.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
256
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
257 A fixed set of functions are @dfn{built-in}, which means they are
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
258 available in every Octave program. The @code{sqrt} function is one of
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
259 these. In addition, you can define your own functions.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
260 @xref{Functions and Scripts}, for information about how to do this.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
261
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
262 @cindex arguments in function call
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
263 The way to use a function is with a @dfn{function call} expression,
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
264 which consists of the function name followed by a list of
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
265 @dfn{arguments} in parentheses. The arguments are expressions which give
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
266 the raw materials for the calculation that the function will do. When
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
267 there is more than one argument, they are separated by commas. If there
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
268 are no arguments, you can omit the parentheses, but it is a good idea to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
269 include them anyway, to clearly indicate that a function call was
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
270 intended. Here are some examples:
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
271
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
272 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
273 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
274 sqrt (x^2 + y^2) # @r{One argument}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
275 ones (n, m) # @r{Two arguments}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
276 rand () # @r{No arguments}
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
277 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
278 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
279
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
280 Each function expects a particular number of arguments. For example, the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
281 @code{sqrt} function must be called with a single argument, the number
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
282 to take the square root of:
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
283
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
284 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
285 sqrt (@var{argument})
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
286 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
287
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
288 Some of the built-in functions take a variable number of arguments,
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
289 depending on the particular usage, and their behavior is different
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
290 depending on the number of arguments supplied.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
291
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
292 Like every other expression, the function call has a value, which is
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
293 computed by the function based on the arguments you give it. In this
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
294 example, the value of @code{sqrt (@var{argument})} is the square root of
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
295 the argument. A function can also have side effects, such as assigning
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
296 the values of certain variables or doing input or output operations.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
297
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
298 Unlike most languages, functions in Octave may return multiple values.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
299 For example, the following statement
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
300
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
301 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
302 [u, s, v] = svd (a)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
303 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
304
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
305 @noindent
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
306 computes the singular value decomposition of the matrix @code{a} and
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
307 assigns the three result matrices to @code{u}, @code{s}, and @code{v}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
308
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
309 The left side of a multiple assignment expression is itself a list of
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
310 expressions, and is allowed to be a list of variable names or index
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
311 expressions. See also @ref{Index Expressions}, and @ref{Assignment Ops}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
312
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
313 @menu
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
314 * Call by Value::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
315 * Recursion::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
316 @end menu
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
317
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
318 @node Call by Value, Recursion, Calling Functions, Calling Functions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
319 @subsection Call by Value
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
320
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
321 In Octave, unlike Fortran, function arguments are passed by value, which
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
322 means that each argument in a function call is evaluated and assigned to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
323 a temporary location in memory before being passed to the function.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
324 There is currently no way to specify that a function parameter should be
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
325 passed by reference instead of by value. This means that it is
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
326 impossible to directly alter the value of function parameter in the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
327 calling function. It can only change the local copy within the function
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
328 body. For example, the function
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
329
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
330 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
331 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
332 function f (x, n)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
333 while (n-- > 0)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
334 disp (x);
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
335 endwhile
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
336 endfunction
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
337 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
338 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
339
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
340 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
341 displays the value of the first argument @var{n} times. In this
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
342 function, the variable @var{n} is used as a temporary variable without
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
343 having to worry that its value might also change in the calling
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
344 function. Call by value is also useful because it is always possible to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
345 pass constants for any function parameter without first having to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
346 determine that the function will not attempt to modify the parameter.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
347
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
348 The caller may use a variable as the expression for the argument, but
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
349 the called function does not know this: it only knows what value the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
350 argument had. For example, given a function called as
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
351
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
352 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
353 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
354 foo = "bar";
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
355 fcn (foo)
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
356 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
357 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
358
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
359 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
360 you should not think of the argument as being ``the variable
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
361 @code{foo}.'' Instead, think of the argument as the string value,
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
362 @code{"bar"}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
363
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
364 Even though Octave uses pass-by-value semantics for function arguments,
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
365 values are not copied unnecessarily. For example,
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
366
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
367 @example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
368 @group
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
369 x = rand (1000);
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
370 f (x);
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
371 @end group
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
372 @end example
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
373
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
374 @noindent
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
375 does not actually force two 1000 by 1000 element matrices to exist
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
376 @emph{unless} the function @code{f} modifies the value of its
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
377 argument. Then Octave must create a copy to avoid changing the
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
378 value outside the scope of the function @code{f}, or attempting (and
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
379 probably failing!) to modify the value of a constant or the value of a
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
380 temporary result.
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
381
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
382 @node Recursion, , Call by Value, Calling Functions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
383 @subsection Recursion
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
384 @cindex factorial function
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
385
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
386 With some restrictions@footnote{Some of Octave's function are
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
387 implemented in terms of functions that cannot be called recursively.
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
388 For example, the ODE solver @code{lsode} is ultimately implemented in a
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
389 Fortran subroutine that cannot be called recursively, so @code{lsode}
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
390 should not be called either directly or indirectly from within the
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
391 user-supplied function that @code{lsode} requires. Doing so will result
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
392 in undefined behavior.}, recursive function calls are allowed. A
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
393 @dfn{recursive function} is one which calls itself, either directly or
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
394 indirectly. For example, here is an inefficient@footnote{It would be
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
395 much better to use @code{prod (1:n)}, or @code{gamma (n+1)} instead,
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
396 after first checking to ensure that the value @code{n} is actually a
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
397 positive integer.} way to compute the factorial of a given integer:
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
398
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
399 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
400 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
401 function retval = fact (n)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
402 if (n > 0)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
403 retval = n * fact (n-1);
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
404 else
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
405 retval = 1;
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
406 endif
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
407 endfunction
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
408 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
409 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
410
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
411 This function is recursive because it calls itself directly. It
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
412 eventually terminates because each time it calls itself, it uses an
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
413 argument that is one less than was used for the previous call. Once the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
414 argument is no longer greater than zero, it does not call itself, and
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
415 the recursion ends.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
416
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
417 There is currently no limit on the recursion depth, so infinite
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
418 recursion is possible. If this happens, Octave will consume more and
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
419 more memory attempting to store intermediate values for each function
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
420 call context until there are no more resources available. This is
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
421 obviously undesirable, and will probably be fixed in some future version
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
422 of Octave by allowing users to specify a maximum allowable recursion
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
423 depth.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
424
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
425 @node Arithmetic Ops, Comparison Ops, Calling Functions, Expressions
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
426 @section Arithmetic Operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
427 @cindex arithmetic operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
428 @cindex operators, arithmetic
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
429 @cindex addition
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
430 @cindex subtraction
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
431 @cindex multiplication
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
432 @cindex matrix multiplication
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
433 @cindex division
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
434 @cindex quotient
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
435 @cindex negation
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
436 @cindex unary minus
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
437 @cindex exponentiation
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
438 @cindex transpose
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
439 @cindex Hermitian operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
440 @cindex transpose, complex-conjugate
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
441 @cindex complex-conjugate transpose
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
442
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
443 The following arithmetic operators are available, and work on scalars
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
444 and matrices.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
445
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
446 @table @code
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
447 @item @var{x} + @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
448 @opindex +
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
449 Addition. If both operands are matrices, the number of rows and columns
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
450 must both agree. If one operand is a scalar, its value is added to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
451 all the elements of the other operand.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
452
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
453 @item @var{x} .+ @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
454 @opindex .+
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
455 Element by element addition. This operator is equivalent to @code{+}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
456
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
457 @item @var{x} - @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
458 @opindex -
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
459 Subtraction. If both operands are matrices, the number of rows and
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
460 columns of both must agree.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
461
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
462 @item @var{x} .- @var{y}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
463 Element by element subtraction. This operator is equivalent to @code{-}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
464
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
465 @item @var{x} * @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
466 @opindex *
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
467 Matrix multiplication. The number of columns of @var{x} must agree
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
468 with the number of rows of @var{y}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
469
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
470 @item @var{x} .* @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
471 @opindex .*
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
472 Element by element multiplication. If both operands are matrices, the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
473 number of rows and columns must both agree.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
474
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
475 @item @var{x} / @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
476 @opindex /
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
477 Right division. This is conceptually equivalent to the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
478
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
479 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
480 (inverse (y') * x')'
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
481 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
482
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
483 @noindent
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
484 but it is computed without forming the inverse of @var{y'}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
485
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
486 If the system is not square, or if the coefficient matrix is singular,
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
487 a minimum norm solution is computed.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
488
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
489 @item @var{x} ./ @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
490 @opindex ./
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
491 Element by element right division.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
492
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
493 @item @var{x} \ @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
494 @opindex \
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
495 Left division. This is conceptually equivalent to the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
496
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
497 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
498 inverse (x) * y
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
499 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
500
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
501 @noindent
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
502 but it is computed without forming the inverse of @var{x}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
503
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
504 If the system is not square, or if the coefficient matrix is singular,
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
505 a minimum norm solution is computed.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
506
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
507 @item @var{x} .\ @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
508 @opindex .\
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
509 Element by element left division. Each element of @var{y} is divided
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
510 by each corresponding element of @var{x}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
511
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
512 @item @var{x} ^ @var{y}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
513 @itemx @var{x} ** @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
514 @opindex **
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
515 @opindex ^
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
516 Power operator. If @var{x} and @var{y} are both scalars, this operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
517 returns @var{x} raised to the power @var{y}. If @var{x} is a scalar and
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
518 @var{y} is a square matrix, the result is computed using an eigenvalue
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
519 expansion. If @var{x} is a square matrix. the result is computed by
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
520 repeated multiplication if @var{y} is an integer, and by an eigenvalue
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
521 expansion if @var{y} is not an integer. An error results if both
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
522 @var{x} and @var{y} are matrices.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
523
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
524 The implementation of this operator needs to be improved.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
525
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
526 @item @var{x} .^ @var{y}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
527 @item @var{x} .** @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
528 @opindex .**
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
529 @opindex .^
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
530 Element by element power operator. If both operands are matrices, the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
531 number of rows and columns must both agree.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
532
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
533 @item -@var{x}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
534 @opindex -
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
535 Negation.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
536
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
537 @item +@var{x}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
538 @opindex +
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
539 Unary plus. This operator has no effect on the operand.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
540
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
541 @item @var{x}'
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
542 @opindex '
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
543 Complex conjugate transpose. For real arguments, this operator is the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
544 same as the transpose operator. For complex arguments, this operator is
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
545 equivalent to the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
546
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
547 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
548 conj (x.')
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
549 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
550
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
551 @item @var{x}.'
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
552 @opindex .'
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
553 Transpose.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
554 @end table
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
555
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
556 Note that because Octave's element by element operators begin with a
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
557 @samp{.}, there is a possible ambiguity for statements like
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
558
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
559 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
560 1./m
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
561 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
562
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
563 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
564 because the period could be interpreted either as part of the constant
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
565 or as part of the operator. To resolve this conflict, Octave treats the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
566 expression as if you had typed
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
567
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
568 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
569 (1) ./ m
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
570 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
571
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
572 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
573 and not
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
574
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
575 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
576 (1.) / m
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
577 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
578
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
579 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
580 Although this is inconsistent with the normal behavior of Octave's
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
581 lexer, which usually prefers to break the input into tokens by
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
582 preferring the longest possible match at any given point, it is more
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
583 useful in this case.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
584
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
585 @defvr {Built-in Variable} warn_divide_by_zero
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
586 If the value of @code{warn_divide_by_zero} is nonzero, a warning
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
587 is issued when Octave encounters a division by zero. If the value is
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
588 0, the warning is omitted. The default value is 1.
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
589 @end defvr
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
590
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
591 @node Comparison Ops, Boolean Expressions, Arithmetic Ops, Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
592 @section Comparison Operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
593 @cindex comparison expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
594 @cindex expressions, comparison
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
595 @cindex relational operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
596 @cindex operators, relational
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
597 @cindex less than operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
598 @cindex greater than operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
599 @cindex equality operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
600 @cindex tests for equality
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
601 @cindex equality, tests for
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
602
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
603 @dfn{Comparison operators} compare numeric values for relationships
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
604 such as equality. They are written using
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
605 @emph{relational operators}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
606
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
607 All of Octave's comparison operators return a value of 1 if the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
608 comparison is true, or 0 if it is false. For matrix values, they all
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
609 work on an element-by-element basis. For example,
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
610
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
611 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
612 @group
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
613 [1, 2; 3, 4] == [1, 3; 2, 4]
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
614
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
615 @result{} 1 0
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
616 0 1
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
617 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
618 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
619
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
620 If one operand is a scalar and the other is a matrix, the scalar is
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
621 compared to each element of the matrix in turn, and the result is the
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
622 same size as the matrix.
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
623
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
624 @table @code
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
625 @item @var{x} < @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
626 @opindex <
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
627 True if @var{x} is less than @var{y}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
628
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
629 @item @var{x} <= @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
630 @opindex <=
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
631 True if @var{x} is less than or equal to @var{y}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
632
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
633 @item @var{x} == @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
634 @opindex ==
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
635 True if @var{x} is equal to @var{y}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
636
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
637 @item @var{x} >= @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
638 @opindex >=
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
639 True if @var{x} is greater than or equal to @var{y}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
640
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
641 @item @var{x} > @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
642 @opindex >
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
643 True if @var{x} is greater than @var{y}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
644
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
645 @item @var{x} != @var{y}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
646 @itemx @var{x} ~= @var{y}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
647 @itemx @var{x} <> @var{y}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
648 @opindex !=
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
649 @opindex ~=
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
650 @opindex <>
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
651 True if @var{x} is not equal to @var{y}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
652 @end table
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
653
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
654 String comparisons may also be performed with the @code{strcmp}
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
655 function, not with the comparison operators listed above.
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
656 @xref{Strings}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
657
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
658 @node Boolean Expressions, Assignment Ops, Comparison Ops, Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
659 @section Boolean Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
660 @cindex expressions, boolean
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
661 @cindex boolean expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
662 @cindex expressions, logical
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
663 @cindex logical expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
664 @cindex operators, boolean
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
665 @cindex boolean operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
666 @cindex logical operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
667 @cindex operators, logical
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
668 @cindex and operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
669 @cindex or operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
670 @cindex not operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
671
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
672 @menu
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
673 * Element-by-element Boolean Operators::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
674 * Short-circuit Boolean Operators::
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
675 @end menu
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
676
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
677 @node Element-by-element Boolean Operators, Short-circuit Boolean Operators, Boolean Expressions, Boolean Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
678 @subsection Element-by-element Boolean Operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
679 @cindex element-by-element evaluation
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
680
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
681 An @dfn{element-by-element boolean expression} is a combination of
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
682 comparison expressions using the boolean
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
683 operators ``or'' (@samp{|}), ``and'' (@samp{&}), and ``not'' (@samp{!}),
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
684 along with parentheses to control nesting. The truth of the boolean
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
685 expression is computed by combining the truth values of the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
686 corresponding elements of the component expressions. A value is
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
687 considered to be false if it is zero, and true otherwise.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
688
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
689 Element-by-element boolean expressions can be used wherever comparison
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
690 expressions can be used. They can be used in @code{if} and @code{while}
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
691 statements. However, if a matrix value used as the condition in an
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
692 @code{if} or @code{while} statement is only true if @emph{all} of its
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
693 elements are nonzero.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
694
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
695 Like comparison operations, each element of an element-by-element
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
696 boolean expression also has a numeric value (1 if true, 0 if false) that
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
697 comes into play if the result of the boolean expression is stored in a
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
698 variable, or used in arithmetic.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
699
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
700 Here are descriptions of the three element-by-element boolean operators.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
701
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
702 @table @code
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
703 @item @var{boolean1} & @var{boolean2}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
704 @opindex &
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
705 Elements of the result are true if both corresponding elements of
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
706 @var{boolean1} and @var{boolean2} are true.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
707
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
708 @item @var{boolean1} | @var{boolean2}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
709 @opindex |
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
710 Elements of the result are true if either of the corresponding elements
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
711 of @var{boolean1} or @var{boolean2} is true.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
712
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
713 @item ! @var{boolean}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
714 @itemx ~ @var{boolean}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
715 @opindex ~
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
716 @opindex !
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
717 Each element of the result is true if the corresponding element of
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
718 @var{boolean} is false.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
719 @end table
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
720
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
721 For matrix operands, these operators work on an element-by-element
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
722 basis. For example, the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
723
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
724 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
725 [1, 0; 0, 1] & [1, 0; 2, 3]
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
726 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
727
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
728 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
729 returns a two by two identity matrix.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
730
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
731 For the binary operators, the dimensions of the operands must conform if
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
732 both are matrices. If one of the operands is a scalar and the other a
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
733 matrix, the operator is applied to the scalar and each element of the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
734 matrix.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
735
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
736 For the binary element-by-element boolean operators, both subexpressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
737 @var{boolean1} and @var{boolean2} are evaluated before computing the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
738 result. This can make a difference when the expressions have side
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
739 effects. For example, in the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
740
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
741 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
742 a & b++
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
743 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
744
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
745 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
746 the value of the variable @var{b} is incremented even if the variable
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
747 @var{a} is zero.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
748
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
749 This behavior is necessary for the boolean operators to work as
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
750 described for matrix-valued operands.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
751
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
752 @node Short-circuit Boolean Operators, , Element-by-element Boolean Operators, Boolean Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
753 @subsection Short-circuit Boolean Operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
754 @cindex short-circuit evaluation
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
755
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
756 Combined with the implicit conversion to scalar values in @code{if} and
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
757 @code{while} conditions, Octave's element-by-element boolean operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
758 are often sufficient for performing most logical operations. However,
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
759 it is sometimes desirable to stop evaluating a boolean expression as
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
760 soon as the overall truth value can be determined. Octave's
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
761 @dfn{short-circuit} boolean operators work this way.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
762
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
763 @table @code
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
764 @item @var{boolean1} && @var{boolean2}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
765 @opindex &&
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
766 The expression @var{boolean1} is evaluated and converted to a scalar
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
767 using the equivalent of the operation @code{all (all (@var{boolean1}))}.
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
768 If it is false, the result of the overall expression is 0. If it is
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
769 true, the expression @var{boolean2} is evaluated and converted to a
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
770 scalar using the equivalent of the operation @code{all (all
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
771 (@var{boolean1}))}. If it is true, the result of the overall expression
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
772 is 1. Otherwise, the result of the overall expression is 0.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
773
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
774 @item @var{boolean1} || @var{boolean2}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
775 @opindex ||
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
776 The expression @var{boolean1} is evaluated and converted to a scalar
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
777 using the equivalent of the operation @code{all (all (@var{boolean1}))}.
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
778 If it is true, the result of the overall expression is 1. If it is
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
779 false, the expression @var{boolean2} is evaluated and converted to a
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
780 scalar using the equivalent of the operation @code{all (all
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
781 (@var{boolean1}))}. If it is true, the result of the overall expression
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
782 is 1. Otherwise, the result of the overall expression is 0.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
783 @end table
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
784
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
785 The fact that both operands may not be evaluated before determining the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
786 overall truth value of the expression can be important. For example, in
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
787 the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
788
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
789 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
790 a && b++
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
791 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
792
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
793 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
794 the value of the variable @var{b} is only incremented if the variable
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
795 @var{a} is nonzero.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
796
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
797 This can be used to write somewhat more concise code. For example, it
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
798 is possible write
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
799
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
800 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
801 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
802 function f (a, b, c)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
803 if (nargin > 2 && isstr (c))
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
804 @dots{}
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
805 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
806 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
807
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
808 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
809 instead of having to use two @code{if} statements to avoid attempting to
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
810 evaluate an argument that doesn't exist. For example, without the
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
811 short-circuit feature, it would be necessary to write
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
812
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
813 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
814 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
815 function f (a, b, c)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
816 if (nargin > 2)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
817 if (isstr (c))
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
818 @dots{}
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
819 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
820 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
821
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
822 Writing
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
823
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
824 @example
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
825 @group
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
826 function f (a, b, c)
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
827 if (nargin > 2 & isstr (c))
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
828 @dots{}
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
829 @end group
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
830 @end example
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
831
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
832 @noindent
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
833 would result in an error if @code{f} were called with one or two
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
834 arguments because Octave would be forced to try to evaluate both of the
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
835 operands for the operator @samp{&}.
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
836
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
837 @node Assignment Ops, Increment Ops, Boolean Expressions, Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
838 @section Assignment Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
839 @cindex assignment expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
840 @cindex assignment operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
841 @cindex operators, assignment
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
842 @cindex expressions, assignment
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
843
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
844 @opindex =
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
845
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
846 An @dfn{assignment} is an expression that stores a new value into a
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
847 variable. For example, the following expression assigns the value 1 to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
848 the variable @code{z}:
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
849
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
850 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
851 z = 1
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
852 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
853
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
854 After this expression is executed, the variable @code{z} has the value 1.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
855 Whatever old value @code{z} had before the assignment is forgotten.
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
856 The @samp{=} sign is called an @dfn{assignment operator}.
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
857
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
858 Assignments can store string values also. For example, the following
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
859 expression would store the value @code{"this food is good"} in the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
860 variable @code{message}:
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
861
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
862 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
863 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
864 thing = "food"
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
865 predicate = "good"
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
866 message = [ "this " , thing , " is " , predicate ]
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
867 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
868 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
869
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
870 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
871 (This also illustrates concatenation of strings.)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
872
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
873 @cindex side effect
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
874 Most operators (addition, concatenation, and so on) have no effect
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
875 except to compute a value. If you ignore the value, you might as well
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
876 not use the operator. An assignment operator is different. It does
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
877 produce a value, but even if you ignore the value, the assignment still
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
878 makes itself felt through the alteration of the variable. We call this
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
879 a @dfn{side effect}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
880
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
881 @cindex lvalue
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
882 The left-hand operand of an assignment need not be a variable
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
883 (@pxref{Variables}). It can also be an element of a matrix
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
884 (@pxref{Index Expressions}) or a list of return values
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
885 (@pxref{Calling Functions}). These are all called @dfn{lvalues}, which
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
886 means they can appear on the left-hand side of an assignment operator.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
887 The right-hand operand may be any expression. It produces the new value
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
888 which the assignment stores in the specified variable, matrix element,
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
889 or list of return values.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
890
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
891 It is important to note that variables do @emph{not} have permanent types.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
892 The type of a variable is simply the type of whatever value it happens
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
893 to hold at the moment. In the following program fragment, the variable
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
894 @code{foo} has a numeric value at first, and a string value later on:
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
895
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
896 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
897 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
898 octave:13> foo = 1
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
899 foo = 1
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
900 octave:13> foo = "bar"
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
901 foo = bar
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
902 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
903 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
904
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
905 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
906 When the second assignment gives @code{foo} a string value, the fact that
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
907 it previously had a numeric value is forgotten.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
908
2689
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
909 Assignment of a scalar to an indexed matrix sets all of the elements
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
910 that are referenced by the indices to the scalar value. For example, if
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
911 @code{a} is a matrix with at least two columns,
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
912
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
913 @example
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
914 @group
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
915 a(:, 2) = 5
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
916 @end group
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
917 @end example
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
918
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
919 @noindent
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
920 sets all the elements in the second column of @code{a} to 5.
8c7955a8d49f [project @ 1997-02-18 09:06:10 by jwe]
jwe
parents: 2670
diff changeset
921
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
922 Assigning an empty matrix @samp{[]} works in most cases to allow you to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
923 delete rows or columns of matrices and vectors. @xref{Empty Matrices}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
924 For example, given a 4 by 5 matrix @var{A}, the assignment
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
925
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
926 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
927 A (3, :) = []
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
928 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
929
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
930 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
931 deletes the third row of @var{A}, and the assignment
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
932
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
933 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
934 A (:, 1:2:5) = []
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
935 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
936
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
937 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
938 deletes the first, third, and fifth columns.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
939
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
940 An assignment is an expression, so it has a value. Thus, @code{z = 1}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
941 as an expression has the value 1. One consequence of this is that you
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
942 can write multiple assignments together:
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
943
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
944 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
945 x = y = z = 0
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
946 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
947
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
948 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
949 stores the value 0 in all three variables. It does this because the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
950 value of @code{z = 0}, which is 0, is stored into @code{y}, and then
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
951 the value of @code{y = z = 0}, which is 0, is stored into @code{x}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
952
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
953 This is also true of assignments to lists of values, so the following is
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
954 a valid expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
955
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
956 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
957 [a, b, c] = [u, s, v] = svd (a)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
958 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
959
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
960 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
961 that is exactly equivalent to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
962
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
963 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
964 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
965 [u, s, v] = svd (a)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
966 a = u
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
967 b = s
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
968 c = v
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
969 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
970 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
971
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
972 In expressions like this, the number of values in each part of the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
973 expression need not match. For example, the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
974
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
975 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
976 [a, b, c, d] = [u, s, v] = svd (a)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
977 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
978
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
979 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
980 is equivalent to the expression above, except that the value of the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
981 variable @samp{d} is left unchanged, and the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
982
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
983 @example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
984 [a, b] = [u, s, v] = svd (a)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
985 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
986
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
987 @noindent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
988 is equivalent to
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
989
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
990 @example
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
991 @group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
992 [u, s, v] = svd (a)
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
993 a = u
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
994 b = s
2449
31d5588dbb61 [project @ 1996-10-30 22:58:44 by jwe]
jwe
parents: 2333
diff changeset
995 @end group
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
996 @end example
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
997
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
998 You can use an assignment anywhere an expression is called for. For
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
999 example, it is valid to write @code{x != (y = 1)} to set @code{y} to 1
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1000 and then test whether @code{x} equals 1. But this style tends to make
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1001 programs hard to read. Except in a one-shot program, you should rewrite
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1002 it to get rid of such nesting of assignments. This is never very hard.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1003
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1004 @cindex increment operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1005 @cindex decrement operator
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1006 @cindex operators, increment
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1007 @cindex operators, decrement
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1008
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1009 @node Increment Ops, Operator Precedence, Assignment Ops, Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1010 @section Increment Operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1011
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1012 @emph{Increment operators} increase or decrease the value of a variable
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1013 by 1. The operator to increment a variable is written as @samp{++}. It
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1014 may be used to increment a variable either before or after taking its
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1015 value.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1016
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1017 For example, to pre-increment the variable @var{x}, you would write
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1018 @code{++@var{x}}. This would add one to @var{x} and then return the new
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1019 value of @var{x} as the result of the expression. It is exactly the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1020 same as the expression @code{@var{x} = @var{x} + 1}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1021
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1022 To post-increment a variable @var{x}, you would write @code{@var{x}++}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1023 This adds one to the variable @var{x}, but returns the value that
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1024 @var{x} had prior to incrementing it. For example, if @var{x} is equal
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1025 to 2, the result of the expression @code{@var{x}++} is 2, and the new
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1026 value of @var{x} is 3.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1027
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1028 For matrix and vector arguments, the increment and decrement operators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1029 work on each element of the operand.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1030
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1031 Here is a list of all the increment and decrement expressions.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1032
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1033 @table @code
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1034 @item ++@var{x}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
1035 @opindex ++
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1036 This expression increments the variable @var{x}. The value of the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1037 expression is the @emph{new} value of @var{x}. It is equivalent to the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1038 expression @code{@var{x} = @var{x} + 1}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1039
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1040 @item --@var{x}
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
1041 @opindex @code{--}
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1042 This expression decrements the variable @var{x}. The value of the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1043 expression is the @emph{new} value of @var{x}. It is equivalent to the
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1044 expression @code{@var{x} = @var{x} - 1}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1045
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1046 @item @var{x}++
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
1047 @opindex ++
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1048 This expression causes the variable @var{x} to be incremented. The
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1049 value of the expression is the @emph{old} value of @var{x}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1050
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1051 @item @var{x}--
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
1052 @opindex @code{--}
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1053 This expression causes the variable @var{x} to be decremented. The
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1054 value of the expression is the @emph{old} value of @var{x}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1055 @end table
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1056
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1057 It is not currently possible to increment index expressions. For
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1058 example, you might expect that the expression @code{@var{v}(4)++} would
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1059 increment the fourth element of the vector @var{v}, but instead it
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1060 results in a parse error. This problem may be fixed in a future
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1061 release of Octave.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1062
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1063 @node Operator Precedence, , Increment Ops, Expressions
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1064 @section Operator Precedence
2670
18192eea4973 [project @ 1997-02-13 18:29:53 by jwe]
jwe
parents: 2653
diff changeset
1065 @cindex operator precedence
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1066
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1067 @dfn{Operator precedence} determines how operators are grouped, when
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1068 different operators appear close by in one expression. For example,
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1069 @samp{*} has higher precedence than @samp{+}. Thus, the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1070 @code{a + b * c} means to multiply @code{b} and @code{c}, and then add
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1071 @code{a} to the product (i.e., @code{a + (b * c)}).
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1072
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1073 You can overrule the precedence of the operators by using parentheses.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1074 You can think of the precedence rules as saying where the parentheses
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1075 are assumed if you do not write parentheses yourself. In fact, it is
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1076 wise to use parentheses whenever you have an unusual combination of
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1077 operators, because other people who read the program may not remember
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1078 what the precedence is in this case. You might forget as well, and then
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1079 you too could make a mistake. Explicit parentheses will help prevent
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1080 any such mistake.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1081
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1082 When operators of equal precedence are used together, the leftmost
2653
e7908588548a [project @ 1997-02-01 16:53:52 by jwe]
jwe
parents: 2449
diff changeset
1083 operator groups first, except for the assignment and exponentiation
2333
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1084 operators, which group in the opposite order. Thus, the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1085 @code{a - b + c} groups as @code{(a - b) + c}, but the expression
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1086 @code{a = b = c} groups as @code{a = (b = c)}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1087
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1088 The precedence of prefix unary operators is important when another
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1089 operator follows the operand. For example, @code{-x^2} means
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1090 @code{-(x^2)}, because @samp{-} has lower precedence than @samp{^}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1091
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1092 Here is a table of the operators in Octave, in order of increasing
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1093 precedence.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1094
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1095 @table @code
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1096 @item statement separators
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1097 @samp{;}, @samp{,}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1098
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1099 @item assignment
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1100 @samp{=}. This operator groups right to left.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1101
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1102 @item logical "or" and "and"
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1103 @samp{||}, @samp{&&}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1104
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1105 @item element-wise "or" and "and"
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1106 @samp{|}, @samp{&}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1107
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1108 @item relational
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1109 @samp{<}, @samp{<=}, @samp{==}, @samp{>=}, @samp{>}, @samp{!=},
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1110 @samp{~=}, @samp{<>}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1111
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1112 @item colon
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1113 @samp{:}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1114
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1115 @item add, subtract
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1116 @samp{+}, @samp{-}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1117
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1118 @item multiply, divide
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1119 @samp{*}, @samp{/}, @samp{\}, @samp{.\}, @samp{.*}, @samp{./}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1120
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1121 @item transpose
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1122 @samp{'}, @samp{.'}
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1123
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1124 @item unary plus, minus, increment, decrement, and ``not''
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1125 @samp{+}, @samp{-}, @samp{++}, @samp{--}, @samp{!}, @samp{~}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1126
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1127 @item exponentiation
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1128 @samp{^}, @samp{**}, @samp{.^}, @samp{.**}.
b1a56412c385 [project @ 1996-07-19 02:20:16 by jwe]
jwe
parents:
diff changeset
1129 @end table