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

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

[Octave-bug-tracker] [bug #61199] Allow 'char' input sets to nchoosek fo


From: Rik
Subject: [Octave-bug-tracker] [bug #61199] Allow 'char' input sets to nchoosek for Matlab compatibility
Date: Thu, 23 Sep 2021 11:08:54 -0400 (EDT)
User-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Follow-up Comment #7, bug #61199 (project octave):

I think a different coding strategy is necessary.  Changeset 30204 allows
'char' inputs, but what about cell arrays? or struct arrays?  Matlab accepts
both because when input V is a vector this is about manipulating indices,
rather than the underlying data object.

This input validation seems to work for the wider class of inputs.


  if (nargin != 2
      || ! isvector (v)
      || ! (isreal (k) && isscalar (k)))


Preserving the class of the input in a null output is simpler with indexing
than if/elseif/else tree.  Particularly if the input is expanded to include
cell arrays.

Instead of


  elseif (k == 0)
    if (is_sq_string (v))
      C = resize ('', 1, 0);
    elseif (is_dq_string (v))
      C = resize ("", 1, 0);
    else
      C = zeros (1, 0, class (v));
    endif


one can write


  elseif (k == 0)
    C = v([]);


One question is whether we want to support bug-for-bug compatibility with
Matlab.  Matlab returns empty objects of size 1x0, but there's no real reason
to do that.  They are empty so returning a 0x0 object seems better to me. 
However, if Octave wants to preserve that then indexing can be done with an
appropriately sized zero vector


  elseif (k == 0)
    C = v(zeros (1,0));


Similarly, this code cane be simplified with null indexing.


  elseif (k > n)
    if (is_sq_string (v))
      C = resize ('', 0, k);
    elseif (is_dq_string (v))
      C = resize ("", 0, k);
    else
      C = zeros (0, k, class (v));
    endif


Replacement:


  elseif (k > n)
    C = v([]);


For the corner case of comment #5, I think we need to check that 'v' is
numeric before assuming that the binomial coefficient needs to be calculated. 
This should do it.


  if (n == 1 && isnumeric (v))


I would add a BIST test for


nchoosek ({1,2,3}, 2)


to verify it works for cell arrays.


    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?61199>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

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