octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

proper lvalues and whatnot


From: John W. Eaton
Subject: proper lvalues and whatnot
Date: Fri, 18 Aug 2000 12:14:52 -0500 (CDT)

On 18-Aug-2000, Ross A. Lippert <address@hidden> wrote:

| One of the neat things about octave is its incorporation of
| C-like syntax, with cool operators like ++ and += and so on.
| 
| I am wondering why it is not the case that assignment expressions
| are not likewise lvalues?
| 
| Is there some reason why
| octave> (v = zeros(1,2))++ 
| should not give
|  v = [1 1]
| ??

Hmm.  Because the result of an assingment is not an lvalue, even in C?

| Also, I am wondering why it is that while subscripted vectors/matrices
| are lvalues, that
| octave> v = [1 1];
| octave> v([1 1 2])++
| does not give
|  v = [3 2];
| ??

Now I think this is a valid thing to want to do, and it corresponds to
something that does work in C:

  #include <stdio.h>

  int
  main (void)
  {
    int x[1];

    x[0] = 0;

    x[0]++;

    printf ("%d\n", x[0]);

    return 0;
  }

Octave also accepts this syntax, but maybe it doesn't do exactly what
you expect.  For example (using the current sources, which are close
to 2.1.31),

  octave:1> v = 1:10
  v =

     1   2   3   4   5   6   7   8   9  10

  octave:2> v([3, 4])++
  ans =

    3  4

  octave:3> v
  v =

     1   2   4   5   5   6   7   8   9  10


That seems reasonable, doesn't it?

But your example was


  octave:4> v = [1, 1] 
  v =

    1  1

  octave:5> v([1, 1, 2])++
  ans =

    1  1  1

  octave:6> v
  v =

    2  2

which doesn't do exactly what you seem to expect.  Does the actual
Octave behavior make sense though?  I think it does, if you think
about it in the following way:

  expr++  <==>  expr += 1  <==>  expr = expr + 1

then what you have is

  v = [1, 1]
  v([1, 1, 2]) = v([1, 1, 2]) + 1
               = [1, 1, 1] + 1
               = [2, 2, 2]

and now the indexed assignment works in order, assigning LHS(i) =
RHS(i) for i = 1 to N.  You can see this behavior clearly by
performing the operation v([1,1,1]) = [1, 2, 3].

FWIW, I'm not sure that this should be considered guaranteed behavior
or just a quirk of the current implementation.

| OK, so maybe I am a sick puppy.  But this is what I am really after.
| 
| > [i,j,nz] = find(M);
| > (y=zeros(max(i),1))(i) += nz.*x(j)
| and have
|  y = M*x

I guess I'm dense, but I don't understand what the purpose of this is,
or how it is equivalent to y = M*x.

| I have a lot of sparse matrices I want to play with and this sort of
| syntax would rock.

Are you using an actual sparse matrix data type or trying to emulate
them using full storage (but trying to avoid operations on zeros)?  If
the latter, wouldn't it make more sense to work on a sparse matrix
data type instead?

| Is there a conceptual reason why I should not try to modify octave to
| do this (i.e. how does this break the .m language as a language)?
| 
| Is there a technical reason why I should not try to modify octave to
| do this (i.e. requires a total interpreter rewrite, I'd be in over my
| head)?

Seems to me that it is somewhat difficult to pin down exactly what
these operations should do, and then arrange for everyone to agree.

jwe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]