# [Scipy-tickets] [SciPy] #1255: faulty logic in spatial.distance.squareform

SciPy Trac scipy-tickets@scipy....
Thu Aug 5 16:20:12 CDT 2010

```#1255: faulty logic in spatial.distance.squareform
---------------------------+------------------------------------------------
Reporter:  muellner       |       Owner:  peridot
Type:  defect         |      Status:  new
Priority:  normal         |   Milestone:  0.8.0
Component:  scipy.spatial  |     Version:  0.7.0
Keywords:                 |
---------------------------+------------------------------------------------
The argument parsing in spatial.distance.squareform exhibits a faulty
logic. Feeding it a 2-dimensional matrix with force='tovector' produces
the nonsensical error message 'ValueError: The first argument must be one
or two dimensional array. A 2-dimensional array is not permitted'

Minimal example:
{{{
import numpy
from scipy.spatial.distance import squareform
a = numpy.array([[0,1,2],[1,0,3],[2,3,0]])
# This is OK:
b = squareform( a )
# This is not but it should:
b = squareform( a, force='tovector' )
}}}
In ticket #859, a reversed but somehow consistent logic was suggested,
where the meaning of 'tomatrix' and 'tovector' are switched compared to
the MATLAB behaviour and common sense. The current implementation is
different however. This is the right logic:
{{{
if force.lower() not in ['no', 'tovector', 'tomatrix']:
elif len(s) == 1 and force.lower() != 'tovector':
# Compute X = squareform(v), vector -> matrix
elif len(s) == 2 and force.lower() != 'tomatrix':
# Compute v = squareform(X), matrix -> vector
elif len(s) != 1 and force.lower() == 'tomatrix':
raise ValueError("Forcing 'tomatrix' but input X is not one-
dimensional.")
elif len(s) != 2 and force.lower() == 'tovector':
raise ValueError("Forcing 'tovector' but input X is not two-
dimensional.")
else:
# Other cases, i.e. len(s)>2 and force != 'tovector' nor
'tomatrix'
raise ValueError('The first argument must be a one- or two-
dimensional array. A %d-dimensional array is not permitted' % len(s))
}}}
Also note that the current code is missing a .lower() once.

--
Ticket URL: <http://projects.scipy.org/scipy/ticket/1255>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.
```