[Numpy-discussion] Propose modification to binary_repr

2007-12-13 Thread David Huard
Hi,

The current behavior of numpy's binary_repr is the following:

 binary_repr(1,width=2)
'01'

 binary_repr(0,width=2)
'0'

This seems inconsistent and I'd suggest always padding with zeros to make
sure that the return string always has length=width.

Objections ?


David
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] resize in old Numeric

2007-12-13 Thread Christian Meesters
Thank you all for your valuable input. Learned something 'bout Numeric
again. And my problem is solved ;-).

Thanks
Christian

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Traceback on divide by zero error

2007-12-13 Thread Robert Kern
Tony S Yu wrote:
 Hello,
 
 This is something that's been bothering for awhile. When numpy raises
 the following divide by zero error:
 Warning: divide by zero encountered in double_scalars
 is there a way to get a Traceback on where that warning occurred.

In [1]: from numpy import *

In [2]: seterr?
Type: function
Base Class:   type 'function'
Namespace:Interactive
File: /Users/rkern/svn/numpy/numpy/core/numeric.py
Definition:   seterr(all=None, divide=None, over=None, under=None, 
invalid=None)
Docstring:
Set how floating-point errors are handled.

Valid values for each type of error are the strings
ignore, warn, raise, and call. Returns the old settings.
If 'all' is specified, values that are not otherwise specified
will be set to 'all', otherwise they will retain their old
values.

Note that operations on integer scalar types (such as int16) are
handled like floating point, and are affected by these settings.

Example:

 seterr(over='raise') # doctest: +SKIP
{'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 
'ignore'}

 seterr(all='warn', over='raise') # doctest: +SKIP
{'over': 'raise', 'divide': 'ignore', 'invalid': 'ignore', 'under': 
'ignore'}

 int16(32000) * int16(3) # doctest: +SKIP
Traceback (most recent call last):
  File stdin, line 1, in ?
FloatingPointError: overflow encountered in short_scalars
 seterr(all='ignore') # doctest: +SKIP
{'over': 'ignore', 'divide': 'ignore', 'invalid': 'ignore', 'under': 
'ignore'}

In [3]: seterr(divide='raise')
Out[3]: {'divide': 'print', 'invalid': 'print', 'over': 'print', 'under': 
'ignore'}

In [4]: ones(10) / zeros(10)
---
FloatingPointErrorTraceback (most recent call last)

/Users/rkern/svn/mpl-toolkits/ipython console in module()

FloatingPointError: divide by zero encountered in divide

In [5]: seterr(all='ignore')
Out[5]: {'divide': 'raise', 'invalid': 'print', 'over': 'print', 'under': 
'ignore'}

In [6]: ones(10) / zeros(10)
Out[6]: array([ Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf])


-- 
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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Propose modification to binary_repr

2007-12-13 Thread David Huard
Change done.

All tests pass.

Should I document the change somewhere ? Although it's a small change, I'm
guessing it could be very annoying to debug for someone depending on the
previous behavior.


2007/12/13, Travis E. Oliphant [EMAIL PROTECTED]:

 David Huard wrote:
  Hi,
 
  The current behavior of numpy's binary_repr is the following:
 
   binary_repr(1,width=2)
  '01'
 
   binary_repr(0,width=2)
  '0'
 
  This seems inconsistent and I'd suggest always padding with zeros to
  make sure that the return string always has length=width.
 
  Objections ?
 
 This sounds like a good idea.   I don't think it would break anything,
 but it would be good to test.

 -Travis O.

  
 
  ___
  Numpy-discussion mailing list
  Numpy-discussion@scipy.org
  http://projects.scipy.org/mailman/listinfo/numpy-discussion
 

 ___
 Numpy-discussion mailing list
 Numpy-discussion@scipy.org
 http://projects.scipy.org/mailman/listinfo/numpy-discussion

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Traceback on divide by zero error

2007-12-13 Thread Tony S Yu

On Dec 13, 2007, at 2:21 PM, Robert Kern wrote:

 Tony S Yu wrote:
 Hello,

 This is something that's been bothering for awhile. When numpy raises
 the following divide by zero error:
 Warning: divide by zero encountered in double_scalars
 is there a way to get a Traceback on where that warning occurred.

 In [1]: from numpy import *
 In [3]: seterr(divide='raise')

seterr(divide='warn') is exactly what I was looking for.

Thanks!
-Tony
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Propose modification to binary_repr

2007-12-13 Thread Stefan van der Walt
On Thu, Dec 13, 2007 at 02:33:01PM -0500, David Huard wrote:
 Change done.
 
 All tests pass.

Now's a good time to fix that :)

Cheers
Stéfan
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Faster array version of ndindex

2007-12-13 Thread Jonathan Taylor
I was needing an array representation of ndindex since ndindex only
gives an iterator but array(list(ndindex)) takes too long.  There is
prob some obvious way to do this I am missing but if not feel free to
include this code which is much faster.

In [252]: time a=np.array(list(np.ndindex(10,10,10,10,10,10)))
CPU times: user 11.61 s, sys: 0.09 s, total: 11.70 s
Wall time: 11.82

In [253]: time a=ndtuples(10,10,10,10,10,10)
CPU times: user 0.32 s, sys: 0.21 s, total: 0.53 s
Wall time: 0.60

def ndtuples(*dims):
Fast implementation of array(list(ndindex(*dims))).

# Need a list because we will go through it in reverse popping
# off the size of the last dimension.
dims = list(dims)

# N will keep track of the current length of the indices.
N = dims.pop()

# At the beginning the current list of indices just ranges over the
# last dimension.
cur = np.arange(N)
cur = cur[:,np.newaxis]

while dims != []:

d = dims.pop()

# This repeats the current set of indices d times.
# e.g. [0,1,2] - [0,1,2,0,1,2,...,0,1,2]
cur = np.kron(np.ones((d,1)),cur)

# This ranges over the new dimension and 'stretches' it by N.
# e.g. [0,1,2] - [0,0,...,0,1,1,...,1,2,2,...,2]
front = np.arange(d).repeat(N)[:,np.newaxis]

# This puts these two together.
cur = np.column_stack((front,cur))
N *= d

return cur
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion