[Numpy-discussion] diagflat

A. M. Archibald peridot.faceted at gmail.com
Mon Sep 4 00:43:10 CDT 2006

On 03/09/06, Charles R Harris <charlesr.harris at gmail.com> wrote:
> Travis has introduced the function diagflat for creating diagonal arrays. It
> flattens whatever array is supplied and returns its values as the diagonal
> of a 2-D array or matrix. As the function is currently undocumented I
> thought now might be a good time to discuss other possible choices for the
> name. Here is a quick list that comes to mind
> diagflat (current name)
> asdiagonal
> todiagonal
> asdiag
> todiag
> ...

I was wishing for that just the other day... I think my vote is for
"diagflat", to emphasize the initial flattening.

In particular, there's another function I could wish for: given an
array, pick an axis, treat the array like an array of vectors along
that axis, and replace each vector with the diagonal matrix. So,
supposing that it was called todiagonal:
>>> A = todiagonal(ones(2,3,4),axis=1)
>>> shape(A)
>>> A[1,:,:,2]
array([[1, 0, 0],
           [0, 1, 0],
           [0, 0, 1]])

Specifically I find the forcible flattening a bit monstrous. And you
can always just do todiagonal(A.flat).

No such function seems to exist at the moment, so I'm attaching one;
probably not as efficient as it could be with appropriate fancy
indexing wizardry.

A. M. Archibald
-------------- next part --------------
A non-text attachment was scrubbed...
Name: diag.py
Type: text/x-python
Size: 840 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060904/8bb730f4/attachment-0001.py 

More information about the Numpy-discussion mailing list