octave-bug-tracker
[Top][All Lists]
Advanced

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

[Octave-bug-tracker] [bug #46123] diagonal matrix and diagonality preser


From: Rik
Subject: [Octave-bug-tracker] [bug #46123] diagonal matrix and diagonality preserving matrix operations
Date: Wed, 07 Oct 2015 16:03:31 +0000
User-agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0

Follow-up Comment #1, bug #46123 (project octave):

I think there is a difference in goals between what you are proposing and what
Octave is trying to do.  The diagonal class in Octave is intended to be merely
a memory-efficient way of handling certain specific matrices.  Matlab does not
implement this so there is no API to match.  Instead, the "match" is to the
behavior of regular full matrices.  Users shouldn't need to know that there is
anything different happening.

That is one reason why issparse (diag (...)) returns false.  Because a
diagonal matrix has different "rules" than a sparse matrix and it will
automatically convert to a full matrix whenever it needs to.  Thus,


x = diag (1:2)
x =

Diagonal Matrix

   1   0
   0   2

cos (x)
ans =

   0.54030   1.00000
   1.00000  -0.41615


In the diagonal matrix, the zeros really are zeros just like a full matrix and
so cos (0) == 1.

I don't see anything wrong in trying to accomodate the mathematician's view of
diagonal matrices as a closed set where possible, although the first principle
will still be to behave like a full matrix when necessary.

I think the best course of action is to continue to identify places where
Octave can be upgraded without impacting the Matlab-compatible behavior.

For permute/ipermute, Octave returns a full matrix because it doesn't know in
advance whether you are permuting just rows/columns or perhaps introducing a
third or fourth dimension.

For example,


x = diag (1:2);
y = permute (x, [3 1 2])
y =

ans(:,:,1) =

   1   0

ans(:,:,2) =

   0   2



It seems that one could write a special case in do_permute() in
libinterp/corefcn/data.cc to check whether the input was a diagonal matrix,
and that the permute vector had only two elements, and that the elements were
either [1 2] or [2 1].  If all that was true then one could just return the
original diagonal matrix. 

The do_permute code is shown below:


static octave_value
do_permute (const octave_value_list& args, bool inv)
{
  octave_value retval;

  if (args.length () == 2 && args(1).length () >= args(1).ndims ())
    {
      Array<int> vec = args(1).int_vector_value ();

      // FIXME: maybe we should create an idx_vector object
      // here and pass that to permute?

      int n = vec.numel ();

      for (int i = 0; i < n; i++)
        vec(i)--;

      octave_value ret = args(0).permute (vec, inv);

      retval = ret;
    }
  else
    print_usage ();

  return retval;
}




    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?46123>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

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