Toy NDArray example

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Toy NDArray example

Keith Goodman
I'm trying, for the first time, to port a m-file to an oct-file.

I got stuck on NDArrays. Are there any toy examples around that I can
copy? All I can find are long functions that I cannot follow.

Here's my own (unsuccessful) attempt at a toy NDArray example:

#include <oct.h>
       
DEFUN_DLD(ndarraytest, args, nargout, "I have no idea what I am doing.")
{
        // input matrix x is 3D
        NDArray x = args(0).array_value();

        // n3 is size(x,3).
        n3 = ???;
               
        // I would like to loop over the 3rd dimension of x
        for (int i=0; i < (n3-1); i++) {
                y(:,:,i) = inv(x(:,:,i));
        }

        // Now return y
        octave_value_list retval;
        retval.append(y);
        return retval;
}



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: Toy NDArray example

David Bateman-6
I'd think the following example is something like what you want

D.


--
David Bateman                                [hidden email]
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin    +33 1 69 35 77 01 (Fax)
91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as:

[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary


#include <oct.h>

DEFUN_DLD(ndarraytoy, args, nargout, "y = ndarraytoy(x).")
{
  int nargin = args.length ();
  octave_value retval;

  // Important to check the number of arguments as if incorrect will seg-fault
  if (nargin != 1)
    {
      print_usage ("ndarraytoy");
      return retval;
    }

  // input matrix x is 3D. Define const so we don't make a copy later
  const NDArray x = args(0).array_value();

  if (!error_state)
    {
      // Ok it was an NDArray

      //Note if  2.1.x need  int instead of octave_idx_type
      dim_vector dv = x.dims();
      octave_idx_type len = x.length();
     
      // Define output NDArray
      NDArray y(dv);
               
      // Can only loop over one index value for NDArray.
      for (octave_idx_type i = 0; i < len; i++)
        y(i) = 1 / x(i);

      retval = y;
    }
  return retval;
}



Reply | Threaded
Open this post in threaded view
|

Re: Toy NDArray example

Keith Goodman
On 1/18/06, David Bateman <[hidden email]> wrote:
> I'd think the following example is something like what you want

Nice example. Thank you. Pairing a bunch of toy m-functions with the
corresponding oct-functions would make a nice document.

Since I can only index a NDArray with a single index, how do I pull
out an entire 2D slice of a 3D array?

n = [3 2 4];
x = rand(n);

I want y = x(:,:,j), so in an oct function would I have to do the equivalent of

ind = (j - 1)*n(1)*n(2) + (1:(n(1)*n(2)));
y = x(ind);
y = reshape(x,n(1),n(2));

or if reshape is not available loop over the rows and columns of y?



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: Toy NDArray example

David Bateman-3
Keith Goodman a écrit :

> On 1/18/06, David Bateman <[hidden email]> wrote:
> > I'd think the following example is something like what you want
>
> Nice example. Thank you. Pairing a bunch of toy m-functions with the
> corresponding oct-functions would make a nice document.
>

You should look at the oct-file programming documents of Christophe
Spiel (http://octave.sourceforge.net/coda/coda.pdf) and Paul Thomas
(http://perso.wanadoo.fr/prthomas/intro.html). Essentially, I believe
what you discuss is already pretty well discussed there, so I don't see
the need for further effort to write something new.

> Since I can only index a NDArray with a single index, how do I pull
> out an entire 2D slice of a 3D array?
>
> n = [3 2 4];
> x = rand(n);
>
> I want y = x(:,:,j), so in an oct function would I have to do the
> equivalent of
>
> ind = (j - 1)*n(1)*n(2) + (1:(n(1)*n(2)));
> y = x(ind);
> y = reshape(x,n(1),n(2));
>
> or if reshape is not available loop over the rows and columns of y?
>
With idx_vector's in an oct-file you can do this. But not very
efficiently. The thread

http://www.octave.org/octave-lists/archive/help-octave.2005/msg00328.html

is probably of interest in this case. However, for the case you have
here, due the fortran style indexing of octave matrices, what you want
to do can be easily implemented as in the attached file

D.



#include <oct.h>

DEFUN_DLD(ndarraytoy, args, nargout, "y = ndarraytoy(x, j).")
{
  int nargin = args.length ();
  octave_value retval;

  // Important to check the number of arguments as if incorrect will seg-fault
  if (nargin != 2)
    {
      print_usage ("ndarraytoy");
      return retval;
    }

  // input matrix x is 3D. Define const so we don't make a copy later
  const NDArray x = args(0).array_value();
  octave_idx_type j = args(1).int_value() - 1;

  if (!error_state)
    {
      // Ok it was an NDArray

      //Note if  2.1.x need  int instead of octave_idx_type
      dim_vector dv = x.dims();
     
      if (dv.length() < 3 || j < 0 || j >= dv(2))
        error ("ndarrytoy: index out of range");
      else
        {
          // Define output NDArray
          NDArray y(dim_vector(dv(0),dv(1)));
          octave_idx_type len = y.length();
          octave_idx_type offset = len * j;
          std::cerr << "len,offset: " << len << " " << offset << "\n";
               
          // Can only loop over one index value for NDArray.
          for (octave_idx_type i = 0; i < len; i++)
            y(i) = x(i+offset);

          retval = y;
        }
    }
  return retval;
}



Reply | Threaded
Open this post in threaded view
|

Re: Toy NDArray example

Stéfan van der Walt
On Thu, Jan 19, 2006 at 10:52:25AM +0100, David Bateman wrote:
> You should look at the oct-file programming documents of Christophe
> Spiel (http://octave.sourceforge.net/coda/coda.pdf) and Paul Thomas
> (http://perso.wanadoo.fr/prthomas/intro.html). Essentially, I believe
> what you discuss is already pretty well discussed there, so I don't see

That version of Coda is outdated.  Please refer to the one on the wiki instead.

Regards
Stéfan



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------