help-octave
[Top][All Lists]
Advanced

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

Re: Octave equivalent to A = mxGetPr(prhs[0]);


From: Paul Kienzle
Subject: Re: Octave equivalent to A = mxGetPr(prhs[0]);
Date: Fri, 9 Jun 2006 20:50:55 -0400


On Jun 9, 2006, at 9:25 AM, John W. Eaton wrote:

Your code may also have some unwanted side effects.  If someone using
your functions writes

  A = generate_large_matrix ();
  B = A;                            ## no actual copy of data here.
  your_function (A);

then both A and B will change.  This may surprise some users, since
this is not the way Octave (or Matlab) is supposed to work.  You
might argue that this sort of behavior should be possible, but it is
not part of the "Matlab" language (yet, that I know of).

Python has this behaviour and yes it does surprise users,
even those with no experience with matlab.  With some functions
operating inplace and others making copies it is even more
surprising.

Ideally a clever interpreter would identify opportunities for
inplace updates.  It might not be so bad: if A = some expression
involving only one instance of A, then A can be marked as a
candidate for inplace update.  Even with the user providing
the marking (e.g., using A = f(inplace(A))), there are still
challenges to make it work.

Since nobody is going to put in the effort to make this
work, I would recommend using a consistent naming scheme
such as inplace_fn(A) so that the user maintaining the code
will know that A is being modified.  It would be nice to
check that the reference count on the octave_value and the
matrix are what you would expect if there is only one
variable referencing the value.

- Paul



reply via email to

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