It looks like the definition of the diagonal changed somewhere between Numeric 24.0 and numpy: In Numeric:
x = Numeric.arange(2*4*4) x = Numeric.reshape(x, (2, 4, 4)) x array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]], [[16, 17, 18, 19], [20, 21, 22, 23], [24, 25, 26, 27], [28, 29, 30, 31]]]) Numeric.diagonal(x) array([[ 0, 5, 10, 15], [16, 21, 26, 31]])
But in numpy:
import numpy as Numeric x = Numeric.arange(2*4*4) x = Numeric.reshape(x, (2, 4, 4)) Numeric.diagonal(x) array([[ 0, 20], [ 1, 21], [ 2, 22], [ 3, 23]])
The old logic seems to be clear: x is a pair of matrices and diagonal returns a pair of diagonals, but the new logic seems unclear: the disagonal returns the first rows of the two matrices transposed. Does anyone know when this change was introduced and why?
Does anyone know when this change was introduced and why?
Isn't it more a problem of default values ? By default, x.diagonal() == x.diagonal(0,0,1) x.diagonal() array([[ 0, 20], [ 1, 21], [ 2, 22], [ 3, 23]]) If you want the paired diagonal: x.diagonal(0,1,-1) array([[ 0, 5, 10, 15], [16, 21, 26, 31]])
I see. However, something needs to be changed. In the current version
help(diagonal) prints the following:
{{{
Help on function diagonal in module numpy.core.oldnumeric:
diagonal(a, offset=0, axis1=0, axis2=1)
diagonal(a, offset=0, axis1=0, axis2=1) returns the given diagonals
defined by the last two dimensions of the array.
}}}
I would think axes 0 and 1 are the first, not the last two dimensions. We
can either change the documentation or change the defaults in the
oldnumeric. I would vote for the change in defaults because oldnumeric is a
compatibility module and should not introduce changes.
In addition, the fact that the reduced axes become the first (rather than
the last or one of the axis1 and axis2) dimension should be spelled out in
the docstring.
On 4/6/06, Pierre GM
Does anyone know when this change was introduced and why?
Isn't it more a problem of default values ? By default, x.diagonal() == x.diagonal(0,0,1)
x.diagonal() array([[ 0, 20], [ 1, 21], [ 2, 22], [ 3, 23]])
If you want the paired diagonal: x.diagonal(0,1,-1) array([[ 0, 5, 10, 15], [16, 21, 26, 31]])
I would think axes 0 and 1 are the first, not the last two dimensions. We can either change the documentation or change the defaults in the oldnumeric. I would vote for the change in defaults because oldnumeric is a compatibility module and should not introduce changes.
So, change the default to: diagonal(a, offset=0, axis1=-2, axis2=-1) ? That'd make sense, I'm for that...
participants (2)
-
Pierre GM
-
Sasha