# [SciPy-user] strange ValueError in scipy_core

Travis Oliphant oliphant at ee.byu.edu
Thu Nov 10 16:41:22 CST 2005

```Chris Fonnesbeck wrote:

>I am converting some code over to scipy_core which involves populating
>a relatively sparse matrix, so I am generating an array of zeros, and
>substituting the non-zero elements. However, this substitution does
>not seem to be allowed. Here is the relevant section of code.
>
>
>           # Initialize projection matrix
>            matrix = zeros((10,10),typecode='d')
>
>            # Populate with non-zero elements
>            matrix[0,4] = 0.5*s1
>            matrix[1,0] = s2
>            matrix[2,1] = s3
>            matrix[3,2] = s4*(1.-g4)
>            matrix[3,3] = sP*(1.-gP)
>            matrix[4,2] = s4*g4
>            matrix[4,3] = sP*gP
>            matrix[4,5] = sA*gB
>            matrix[5,4] = sA
>            matrix[5,5] = sA*(1.-gB)
>            matrix[6,4] = 0.5*s1
>            matrix[7,6] = s2
>            matrix[8,7] = s3
>            matrix[9,8] = s4
>            matrix[9,9] = sA
>
>But I get the following:
>
>--> 777             matrix[0,4] = 0.5*s1
>        matrix = [[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
> [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
> [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
> [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
> [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
> [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
> [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
> [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
> [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
> [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]]
>        s1 = 0.78583498304255861
>    778             matrix[1,0] = s2
>    779             matrix[2,1] = s3
>
>ValueError: cannot handle misaligned or not writeable arrays.
>
>
>
>
I'm pretty sure this error is coming from the Numeric module (not
scipy).  It could be a problem with the __array_struct__ interface,
though.   Or it could be a problem with one of your arrays having it's
writeable and/or aligned flags set improperly for some reason.

Basically, new scipy arrays can be misaligned and read-only in memory.
This is not supported for Numeric arrays, so if you try to convert a
scipy array with those advanced features to a Numeric array you get this
error (from Numeric).

or scipy) all these things are.  You are definitely mixing scipy with
Numeric.  This should work, but it is less well tested.   We also need
to see why some of your arrays are getting misaligned or are not
writeable.  The code you posted is not enough to discern this.

If needed, the flags for a scipy array can be had as <arr>.flags  which
returns a dictionary of Boolean values.

-Travis

```