octave-maintainers
[Top][All Lists]
Advanced

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

Re: Discover ignored output arguments from inside function?


From: Jaroslav Hajek
Subject: Re: Discover ignored output arguments from inside function?
Date: Thu, 29 Jul 2010 13:01:31 +0200

On Thu, Jul 29, 2010 at 3:02 AM, Judd Storrs <address@hidden> wrote:
> Recently octave gained the ability to ignore output arguments. Is there
> currently any way to know whether a particular output is destined to be
> discarded from within the function? For example suppose:
> function [a,b,c,d] = myfunc(e)
>     a = ....
>     b = ....
>     c = ....
>     d = ....
> endfunction
> If I call this like:
> [a,~,~,d] = myfunc(e)
> is there any way for myfunc() to know not to put too much effort into
> computing b and c? I can think of plenty of inelegant kludges that commit
> the sins of verbosity, fragility and complexity. What I'm looking for is
> something direct and automatic like
> function [a,b,c,d] = myfunc(e)
>    if isignored("b")
>        b = ....
>    endif
>    if isignored(3)
>        c = ....
>    endif
>    ...etc...
> endfunction
>
> --judd
>


I find this a really good idea, so I gave it a shot in the morning:
http://hg.savannah.gnu.org/hgweb/octave/rev/1b2fcd122c6a

With this change, I can write:

function [a, b, c] = tf (x)
  if (nargout >= 1 && ! is_ignored_output (1))
    disp ("evaluating a");
    a = x + 1;
  endif

  if (nargout >= 2 && ! is_ignored_output (2))
    disp ("evaluating b");
    b = x + 2;
  endif

  if (nargout >= 3 && ! is_ignored_output (3))
    disp ("evaluating c");
    c = x + 3;
  endif

and then run:

octave:1> [a, b, c]  = tf (1)
evaluating a
evaluating b
evaluating c
a =  2
b =  3
c =  4
octave:2> [a, ~, c]  = tf (1)
evaluating a
evaluating c
a =  2
c =  4
octave:3> [~, ~, c]  = tf (1)
evaluating c
c =  4
octave:4> [~, ~, ~]  = tf (1)
octave:5> [~, x{1:2}]  = tf (1)
evaluating b
evaluating c
x =

{
  [1,1] =  3
  [1,2] =  4
}


This is harder to implement that it seems, because of the way Octave
handles index expressions: the whole index chain is normally (though
there are exceptions) passed to the top-level object and it is up to
that object to handle its part and pass on the rest. Currently, the
feature only works when a function is called directly, not through a
handle or feval. That would require some work.

In general, this should be regarded as pure optimization stuff, i.e.
the function should behave consistently whether or not an output is
ignored.

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
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]