help-octave
[Top][All Lists]
Advanced

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

indexing operations in 2.1.39


From: John W. Eaton
Subject: indexing operations in 2.1.39
Date: Mon, 4 Nov 2002 21:06:22 -0600

On  4-Nov-2002, Andy Adler <address@hidden> wrote:

| I'm having trouble with my sparse code with
| octave-2.1.39.
| 
| The code compiles, but Indexing operations give:
| 
| address@hidden /usr/src/octave-forge/main/sparse
| $ /usr/local/oct-2.1.39test/bin/octave-2.1.39.exe -q
| octave-2.1.39:1> s=sparse(eye(2));
| octave-2.1.39:2> s(:)
| error: can't perform indexing operations for sparse type
| 
| I have dug around, and found that indexing operations
| no longer call "do_multi_index_op", but now call
| the method "subsref"
| 
| I have got sparse to work correctly by adding the method:
| 
| octave_value_list
| octave_sparse::subsref (const std::string type,
|                         const SLList<octave_value_list>& idx,
|                         int nargout)
| { return do_multi_index_op( nargout, idx.front()); }
| 
| $ /usr/src/octave-2.1.39/src/octave.exe -q
| octave:1> s=sparse(eye(2));
| octave:2> s(:)'
| ans =
|   1  0  0  1
| 
| 
| MY QUESTION IS:
| Is this the correct use of subsref?
| Is this the right way to do this?

Does your do_multi_index_op function really use nargout?  If so, when
is it used?

Sorry about the lack of documentation.

I think you want something like what is in the ov-base-mat.cc file:

  template <class MT>
  octave_value
  octave_base_matrix<MT>::subsref (const std::string type,
                                   const SLList<octave_value_list>& idx)
  {
    octave_value retval;

    switch (type[0])
      {
      case '(':
        retval = do_index_op (idx.front ());
        break;

      case '{':
      case '.':
        {
          std::string nm = type_name ();
          error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
        }
        break;

      default:
        panic_impossible ();
      }

    return retval.next_subsref (type, idx);
  }

The last line is needed to handle things like

  x(i)(j)

The current function has handled the first index, then it is using
next_subsref to pop the first elements of TYPE and IDX and call
subsref again, possibly recursively.

If you look at the code for subsref in ov-struct.cc, you'll see that
it can handle more than one index so that it can handle s(i), s.field,
and s(i).field.  This means that for structure arrays, the call to
next_subsref is slightly different, asking it to skip one or two
elements of TYPE and IDX.

The way this works now means you can define a data type that takes any
number of the three types of indices in any order, and when it reaches
a point where it no longer can do anything, it can hand off to the
subsref function for the value it returns.

jwe



-------------------------------------------------------------
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 via email to

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