[SciPy-user] Trouble with arrays of arrays

Robert Kern robert.kern at gmail.com
Sat Jan 20 00:01:28 CST 2007

Nicholas Kottenstette wrote:
> Hey All,
> I was trying to write some code to handle polynomial matrices.
> I want a matrix in which each entry is a reference to a possibly 
> different length array
> describing the i,jth polynomial.
> For simplicity I want to represent the following 2x2 matrix
> [2s+3, s+2;
> 3.3s+4.3, 1.2s+6.3]
> M = array(
> In [75]: N = 
> array([[array([2.0,3.0]),array([1.0,2.0])],[array([3.3,4.3]),array([1.2,6.3])]],dtype=object_) 
> In [76]: N
> Out[76]:
> array([[[2.0, 3.0],
>        [1.0, 2.0]],
>       [[3.3, 4.3],
>        [1.2, 6.3]]], dtype=object)
> In [77]: N.shape
> Out[77]: (2, 2, 2)
> Clearly this is not what I wanted to happen, I was expecting a 2x2 array 
> with references to each object array.

Well, it's not clear to array(). There's no way for it to know how deep you
wanted it to go. Object arrays with elements that are sequences are simply
ambiguous and array() does the best that it can with the information it has

The generally accepted approach to resolve this ambiguity is to use empty() to
make an empty object array of the shape that you want and fill it in from your

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the SciPy-user mailing list