[SciPy-Dev] scipy.sparse spdiags/dia_matrix additional calling mode

Pauli Virtanen pav@iki...
Thu Oct 6 04:33:52 CDT 2011


Hi,

06.10.2011 10:59, Tony Stillfjord kirjoitti:
> Given that Python is not MATLAB, would it be possible to add an
> additional, in some cases more intuitive, way of calling the spdiags
> function? This function creates a sparse matrix given its non-zero
diagonals.
> 
> Currently, the first input has to be an array 'data' where every row
> corresponds to a diagonal - which one is specified in the next input, 'diags'. However,
> as the diagonals have different length, the actual values have to be zero-padded on
> either the left or right end to fit the array.
[clip]

I was just thinking about the same thing: the way it works now is a bit
of a pain.

Currently, I'm using this:

    def spdiags2(data, diags, M, N, **kw):
        n = min(M, N)
        data_mat = np.zeros((len(data), n),
                            dtype=np.common_type(*data))
        for j, q in enumerate(data):
            m = n - abs(diags[j])
            if diags[j] >= 0:
                data_mat[j,-m:] = q
            else:
                data_mat[j,:m] = q
        return sparse.spdiags(data_mat, diags, M, N, **kw)

But I agree the `spdiags` function itself should be fixed.

-- 
Pauli Virtanen



More information about the SciPy-Dev mailing list