help-octave
[Top][All Lists]
Advanced

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

Re: Oct-file magic


From: Jaroslav Hajek
Subject: Re: Oct-file magic
Date: Thu, 3 Jul 2008 10:29:06 +0200

On Thu, Jul 3, 2008 at 4:36 AM, Michael Friedlander <address@hidden> wrote:
> Hi Folks,
>
> I'd like to write an oct-file that modifies a vector in-place.  Of
> course, this isn't a good idea in general, but I'd like to squeeze
> some efficiency out of a routine that's called many times over.
>
> The oct-file is a wrapper to a simple C-routine that already works in-
> place and has the simple interface
>
>    void foo(int m, double x);
>
> Here is my current oct-file:
>
> DEFUN_DLD(foo, args, nargout, "foo a vector") {
>     octave_value_list retval;
>     Matrix x = args(0).matrix_value();
>     int m = x.nelem();
>     double *xv = x.fortran_vec();
>     foo(m, xv);
>     retval(0) = x;
>     return retval;
> }
>
> If I make the call
>
>   x = foo(x);
>
> from within Octave, does this oct-file work in-place, or does it
> modify a copy of x?

Currently, not. Octave uses copy-on-write pass-by-value semantics,
which essentially
does not allow writing into an object owned by someone else. This was
already discussed before; it is certainly possible to optimize such
calls in some way but issues arise (what to do when an error occurs
inside the subroutine).

> If it's the latter, how can I trick the Octave
> interface into doing things in-place, so that I can just call the
> function as
>
>   foo(x);

I don't think this is a good idea - I find Octave's clear separation
of input and output arguments to be a real win. In Octave, you can
think that everything is passed by value - and that is a very clear
and simple data model to work with.
(For instance, I like Python a lot; however, its reference semantics
is sometimes giving me a hard time, it's not something you get used to
easily especially if you are exposed to other languages as well).

Some kind of such a mechanism is nevertheless a long-term Octave's TODO.
I'm currently thinking about an extension that would allow to solve
this (to a certain extent)
and also another issue - the hardwired one-based indexing. I'll try to
come up with a proposition shortly.

>
> Many thanks for your help!
>
> Michael
>
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
>



-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

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