[Numpy-discussion] fixing diag() for matrices

Robert Kern robert.kern at gmail.com
Tue Jul 25 13:37:00 CDT 2006


Sven Schreiber wrote:
> That would be fine with me. However, I'd like to point out that after
> some bug-squashing currently all numpy functions deal with
> numpy-matrices correctly, afaik. The current behavior of numpy.diag
> could be viewed as a violation of that principle. (Because if x has
> shape (n,1), diag(x) returns only the first entry, which is pretty
> stupid for a diag-function operating on a vector.)

I don't think so. It's operating exactly as it is documented to. It doesn't do 
what you want, but it's not supposed to read your mind and guess that you are 
treating (n,1) and (1,n) arrays as linear algebraic vectors and that you want to 
form a diagonal matrix from that vector. It handles matrix objects just fine; it 
does not obey a particular convention that you want to use, though. That's 
neither broken nor a violation of the principle that most numpy functions should 
accept matrix objects; it's just not what you want.

I don't want to introduce a backwards-compatibility-breaking special case to the 
function. "Special cases aren't special enough to break the rules." Different 
functionality should go into a different function.

-- 
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 Numpy-discussion mailing list