[Numpy-discussion] Re: Problem with trace function?

Rory Yorke ryorke at telkomsa.net
Thu Dec 2 09:57:05 CST 2004


> Most of the time the numarray code prints out some negative value
> whereas the Numeric code yields positive values.

numarray.greater() returns a Bool array, while Numeric.greater() returns
a 32-bit integer array.

Try this code:

import numarray as na
import Numeric as nu

print na.greater([0.1,0.9],0.5).type()
print nu.greater([0.1,0.9],0.5).typecode() ## prints an 'ell'

for n in range(126,131):
    A=na.ones((n,n)).astype(na.Bool)
    B=nu.ones((n,n)).astype('1') ## that's a one
    print n,na.trace(A),nu.trace(B)

The problem is overflow in the addition of 8 bit signed integers; it
occurs in both numarray and Numeric, given the right array type.

The snippet below should give you some idea of how to find what you want:

from numarray import *
from numarray.random_array import standard_normal

A=standard_normal((1000,1000)) > 0.9

print len(nonzero(diagonal(A))[0])

print trace(A.astype(Int32))

Cheers,

Rory




More information about the Numpy-discussion mailing list