[SciPy-dev] patch for scipy_base.diag

p.berkes at biologie.hu-berlin.de p.berkes at biologie.hu-berlin.de
Thu May 26 11:11:24 CDT 2005


There is a casting problem in the 'diag' function:

>>> a = scipy.array([1,2,3], 'f')
>>> scipy.diag(a).typecode()
>>> 'd'

while it should be 'f'.

The function can be easily patched (only the calls to eye are modified):

In file:  /home2/scipy/local/lib/python2.3/site-packages/scipy_base/matrix_base.py

def diag(v, k=0):
    """ returns the k-th diagonal if v is a matrix or returns a matrix
        with v as the k-th diagonal if v is a vector.
    v = asarray(v)
    s = v.shape
    if len(s)==1:
        n = s[0]+abs(k)
        if k > 0:
            v = concatenate((zeros(k, v.typecode()),v))
        elif k < 0:
            v = concatenate((v, zeros(-k, v.typecode())))
        return eye(n, k=k, typecode=v.typecode())*v
    elif len(s)==2:
        v = add.reduce(eye(s[0], s[1], k=k, typecode=v.typecode())*v)
        if k > 0: return v[k:]
        elif k < 0: return v[:k]
        else: return v
        raise ValueError, "Input must be 1- or 2-D."

Best, Pietro.

More information about the Scipy-dev mailing list