[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: a=0; a([1,1])++ -> a == 2
From: |
Etienne Grossmann |
Subject: |
Re: a=0; a([1,1])++ -> a == 2 |
Date: |
Thu, 03 Aug 2000 08:14:55 +0100 |
User-agent: |
WEMI/1.13.7 (Shimada) FLIM/1.13.2 (Kasanui) Emacs/20.7 (i386-debian-linux-gnu) (with unibyte mode) |
Hello,
# From address@hidden Wed Aug 2 19:49:40 2000
# On 2-Aug-2000, Etienne Grossmann <address@hidden> wrote:
#
# | sorry if the answer to my question is a clear-cut "no" or if it has
# | already been discussed.
# |
# | Wouldn't it make sense to have the code
# |
# | a=0; b = a([1,1])++ ;
# |
# | be equivalent to either
# |
# | a=0; b=a([1,1]); b++; for i in [1,1], a(i)++ ; end
# |
# | yielding a == 2, b == [1;1]
#
# Hmm. I can agree with what is inside the loop, but not the b++ that
# is outside. The way a statement like
#
# b = a([1,1])++
#
# is evaluated requires that the operation on the right hand side
# produce a value, and then that result is copied to the left hand side
# of the assignment. So, what should the value of a([1,1])++ be? Since
# a([1,1]) references the same location twice, I suppose it is
# reasonable to think that it should increment twice, but it also seems
# reasonable that it should only increment once, since you have one `++'
# operator operating on one location. It also makes sense when you
# think about it this way:
#
# a = 0; a([1,1])++
#
# is equivalent to
#
# a = 0; t = a([1,1]); a([1,1]) += 1; t
#
# or
#
# a = 0; t = a([1,1]); a([1,1]) = a([1,1]) + 1; t
Yes, that is true (and unfortunate, imho).
# With the value of the expression being `t'.
#
# | or
# | a=0; b=zeros(size(a)); for i in [1,1], b(i) = a(i)++ ; end
# |
# | yielding a == 2, b == [1;2]
This b==[1;2] is clearly a mistake; ++a(i) could lead to b==[1;2].
# I don't think this is the correct interpretation of what this code
# would currently do in Octave. With my current sources (approximately
# the same as the latest 2.1.x release), I see
#
# octave:11> a=0; b=zeros(size(a)); for i = [1,1], b(i) = a(i)++ ; end, a, b
# a = 2
# b = 1
#
# | The current behavior yields :
# |
# | a == 1, b == [1;1]
^^^
My mistake again, it's [0;0]. Prefer_column_vector==1.
# Hmm. Not what I see:
#
# octave:9> a=0; b = a([1,1])++; a, b
# a = 1
# b =
#
# 0 0
#
# Are you using some different version of Octave that behaves
# differently?
No.
Maybe a function a2 = loop_add (a1, indices, b) that does
a2=a1; for i=indices, a1(i)+=b(i); end
would make me happy. I will have a look when I have time. Maybe
together with loop_mul and loop_exp.
Cheers,
Etienne
-----------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.che.wisc.edu/octave/octave.html
How to fund new projects: http://www.che.wisc.edu/octave/funding.html
Subscription information: http://www.che.wisc.edu/octave/archive.html
-----------------------------------------------------------------------