bugGNU Octave - Bugs: bug #42764, squareform in statistics package...

 
 

bug #42764: squareform in statistics package returns column vector, Matlab returns row vector

Submitter:  None
Submitted:  Sun 13 Jul 2014 04:42:57 PM UTC
   
 
Category:  Octave Package Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  nikolaas n. oosterhof Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * 3.8.1
Operating System:  * GNU/Linux Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Sun 09 Nov 2014 03:59:54 PM UTC, comment #4: 

Thanks. I applied your second patch to the repository. This bug can be closed.

Arno Onken <asnelt>
Sun 09 Nov 2014 12:20:53 PM UTC, comment #3: 

Sorry about that! Just before patching, I reverted the code to what I thought was an earlier version (after testing on MatLab showed that `squareform' should accept both row and column vectors as arguments). I should have retested to make sure all was well.

I ran multiple tests, and ended up rewriting two argument checking lines to use built-in Octave routines (`isnumeric' and `issymmetric'). The rewritten lines should run faster and be easier to maintain. I've attached a new patch `squareform2' with these changes.

(file #32394)

JD Walsh <jdwalsh>
Sat 08 Nov 2014 05:37:02 PM UTC, comment #2: 

I tried your patch, but there is a parse error near line 91:

  syntax error

>>>     if (floor (sy) ~= sy))


I can remove the bracket, but can you apply some additional testing to make sure that there are no other glitches?

Thanks,
Arno

Arno Onken <asnelt>
Fri 07 Nov 2014 05:36:34 PM UTC, comment #1: 

TL;DR I've attached a patch to bring `squareform' into line with MatLab. With this patch, it tests arguments and returns a row vector (when appropriate).

The lack of argument testing in the current `squareform' creates problems with both vector and matrix input. Currently, `squareform' doesn't complain if the input matrix fails to match the requirements of a distance matrix: a square symmetric matrix with zeros along the diagonal. For example,

`squareform([1,2,3; 4,5,6; 7,8,9])' returns

   4
   7
   8

When `squareform' is asked to convert a vector `x' to an n by n matrix, it finds the value of n by solving the quadratic equation n^2 - n - 2k = 0, where k = length(x). Because the current `squareform' does no error checking, it is possible for `n' to take non-integer values. The function `zeros' which creates the matrix strips off non-integers without displaying an error. This can lead to unexpected results in the returned matrix. For instance,

`squareform ([1,2,3,4])' returns

   0   1   2
   1   0   3
   2   3   0

In both the above cases, MatLab's `squareform' returns an error and aborts computation.

Since the most recent maintainer of `squareform' stepped down in 2011, I have attached a patch for these issues. My patched `squareform' converts matrices to row vectors instead of column vectors. It also checks for incorrect input types, matrices with an incorrect format, and vectors of an invalid length. The MatLab description for `squareform' states that the routine requires a symmetric matrix. However, MatLab 2013b does not throw an error when a non-symmetric matrix is used as input. To maintain compatibility with existing MatLab code which may rely on this lapse, when a non-symmetric matrix is encountered by my version of `squareform', it gives a warning (which the user can choose to turn off).

The patched `squareform' also includes minor formatting changes to bring the code more into line with Octave's current recommendations.


(file #32389)

JD Walsh <jdwalsh>
Sun 13 Jul 2014 04:42:57 PM UTC, original submission:  

the squareform function (in the octaveforge statistics package) shows different behaviour in Matlab and Octave when using this function in the 'tovector' direction.

In octave (3.8.1 on linux, 3.8.0 on OSX):

>> squareform(1-eye(3),'tovector')

ans =

   1
   1
   1

In matlab (R2012b on linux, R2013a on OSX):

>> squareform(1-eye(3),'tovector')


ans =

     1     1     1

I expected the results to be the same for Matlab and Octave.

I'm not sure to what extent this really is a bug in the sense that is shows behaviour inconsistent with the documentation; neither Matlab's nor Octave's help say explicitly that the output is a row vector. The example given in the Matlab help does however show that the output in Matlab is a row vector.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #32394:  squareform2.patch added by jdwalsh (6KiB - text/x-patch)
file #32389:  squareform.patch added by jdwalsh (6KiB - text/x-patch)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by asnelt (Posted a comment)
  • -email is unavailable- added by jdwalsh (Updated the item)
  • -email is unavailable- added by jdwalsh
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 8 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2014-11-09 carandraug StatusNone Fixed
        Open/ClosedOpen Closed
    2014-11-09 jdwalsh Attached File- Added squareform2.patch, #32394
    2014-11-07 jdwalsh Attached File- Added squareform.patch, #32389
        Carbon-Copy- Added asnelt
    2014-07-14 rik5 Carbon-CopyRemoved 72865 -
    2014-07-14 rik5 CategoryOctave Function Octave Package
        SummaryOctave\'s squareform returns column vector, Matlab returns row vector squareform in statistics package returns column vector, Matlab returns row vector

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code