octave-maintainers
[Top][All Lists]
Advanced

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

Re: How to realize 'all (isfinite (x)(:))' in C++


From: Rik
Subject: Re: How to realize 'all (isfinite (x)(:))' in C++
Date: Mon, 16 Nov 2015 20:03:17 -0800

On 11/13/2015 09:00 AM, address@hidden wrote:
Subject:
How to realize 'all (isfinite (x)(:))' in C++
From:
Lukas Reichlin <address@hidden>
Date:
11/13/2015 07:18 AM
To:
octave maintainers mailing list <address@hidden>
List-Post:
<mailto:address@hidden>
Content-Transfer-Encoding:
quoted-printable
Precedence:
list
MIME-Version:
1.0 (Mac OS X Mail 9.1 \(3096.5\))
Message-ID:
<address@hidden>
Content-Type:
text/plain; charset=us-ascii
Message:
4

Dear Octave community,

In the control package, there are a couple of helper functions written in C++, one of these functions is 'is_real_matrix' [1]. In an attempt to improve argument checks (see bug #46330 [2] for the motivation), I would like to extend 'is_real_matrix' in a way such that the revised function works like

	is_real_matrix (x) && all (isfinite (x)(:))

To do so, I need to realize the _expression_ 'all (isfinite (x)(:))' in C++ and add it to the if-statement below (this if-statement is part of is_real_matrix.cc [1])

            if (args(i).ndims () != 2 || ! args(i).is_numeric_type ()
                || ! args(i).is_real_type ())

I know that Octave's 'isfinite' function is defined in 'libinterp/corefcn/mappers.cc' and boils down to

    retval = args(0).finite ();

Now my problem is that 'args(i).finite ()' works element-wise but I need the answer matrix-wise. So I had a look at function 'all' in 'libinterp/corefcn/data.cc', which is a one-liner:

  ANY_ALL (all);

ANY_ALL is a macro defined just the lines before 'all'. I have to admit that I'm not proficient in C macros.

OK then, I could try to realize 'all (isfinite (x)(:))' as

	ANY_ALL (ANY_ALL (args(i).finite ()))

but it would look like bad style to me and I'm convinced that there is a more elegant solution to this problem. So what would be the/a proper way to realize 'all (isfinite (x)(:))' in C++?

Thanks in advance and best regards,
Lukas


[1]
http://sourceforge.net/p/octave/control/ci/default/tree/src/is_real_matrix.cc

[2]
http://savannah.gnu.org/bugs/?46330

Lukas,

There are many ways to do this.  The following is probably closest to looking like the m-file version.  First it gets the array, then it re-shapes it to a column vector (equivalent to x(:)), then it does the performs "all (isfinite (...))".  The result is a 1x1 boolNDArray so I indexed into it to get the first element which is of type bool.

  NDArray tmp = args(0).array_value ();
  tmp = tmp.reshape (dim_vector (tmp.numel (), 1));
  bool isfinite = (tmp.isfinite ().all ())(0);

Cheers,
Rik

    


reply via email to

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