# HG changeset patch # User jwe # Date 1179777523 0 # Node ID ffee6a1a10f319cab2316dcf2ff3c9864ee0a897 # Parent 27ec13d8499b6297ff921bff16fd079b1f3a2cd8 [project @ 2007-05-21 19:58:43 by jwe] diff -r 27ec13d8499b -r ffee6a1a10f3 doc/ChangeLog --- a/doc/ChangeLog Mon May 21 19:53:58 2007 +0000 +++ b/doc/ChangeLog Mon May 21 19:58:43 2007 +0000 @@ -1,5 +1,8 @@ 2007-05-21 Søren Hauberg + * interpreter/expr.txi: Describe +=, -=, *=, and /= operators. + Add new example. + * interpreter/eval.txi: Partition the chapter into sections. Describe evalin and assignin functions using text from Paul Kienzle. diff -r 27ec13d8499b -r ffee6a1a10f3 doc/interpreter/expr.txi --- a/doc/interpreter/expr.txi Mon May 21 19:53:58 2007 +0000 +++ b/doc/interpreter/expr.txi Mon May 21 19:58:43 2007 +0000 @@ -112,6 +112,25 @@ size whose elements are all one, and then to scale it to produce the desired result. @xref{Special Utility Matrices}. +It is also possible to create a matrix with different values. The +following example create a 10 dimensional row vector @math{a} containing +the values +@iftex +@tex +$a_i = \sqrt{i}$. +@end tex +@end iftex +@ifnottex +a(i) = sqrt(i). +@end ifnottex + +@example +for i = 1:10 + a(i) = sqrt (i); +endfor +@end example + +@noindent Note that it is quite inefficient to create a vector using a loop like the one shown in the example above. In this particular case, it would have been much more efficient to use the expression @@ -301,7 +320,7 @@ Fortran subroutine that cannot be called recursively, so @code{lsode} should not be called either directly or indirectly from within the user-supplied function that @code{lsode} requires. Doing so will result -in undefined behavior.}, recursive function calls are allowed. A +in an error.}, recursive function calls are allowed. A @dfn{recursive function} is one which calls itself, either directly or indirectly. For example, here is an inefficient@footnote{It would be much better to use @code{prod (1:n)}, or @code{gamma (n+1)} instead, @@ -915,6 +934,63 @@ error: evaluating assignment expression near line 8, column 15 @end example +@opindex += +A very common programming pattern is to increment an existing variable +with a given value, like this + +@example +a = a + 2; +@end example + +@noindent +This can be written in a clearer and more condensed form using the +@code{+=} operator + +@example +a += 2; +@end example + +@noindent +@opindex -= +@opindex *= +@opindex /= +Similar operators also exist for subtraction (@code{-=}), +multiplication (@code{*=}), and division (@code{/=}). An expression +of the form + +@example +@var{expr1} @var{op}= @var{expr2} +@end example + +@noindent +is evaluated as + +@example +@var{expr1} = (@var{expr1}) @var{op} (@var{expr2}) +@end example + +@noindent +where @var{op} can be either @code{+}, @code{-}, @code{*}, or @code{/}. +So, the expression + +@example +a *= b+1 +@end example + +@noindent +is evaluated as + +@example +a = a * (b+1) +@end example + +@noindent +and @emph{not} + +@example +a = a * b + 1 +@end example + You can use an assignment anywhere an expression is called for. For example, it is valid to write @code{x != (y = 1)} to set @code{y} to 1 and then test whether @code{x} equals 1. But this style tends to make @@ -1013,7 +1089,8 @@ @samp{;}, @samp{,}. @item assignment -@samp{=}. This operator groups right to left. +@samp{=}, @samp{+=}, @samp{-=}, @samp{*=},@samp{/=}. This operator +groups right to left. @item logical "or" and "and" @samp{||}, @samp{&&}.