summaryrefslogtreecommitdiff
path: root/numpy/core/numeric.py
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-01-04 21:05:36 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-01-04 21:05:36 +0000
commit490712cd35dcecfc9423de4bde0b29cb012dda25 (patch)
tree56b6ccaac48afc370a189c596d5e9e90ac0254d4 /numpy/core/numeric.py
parent7ff852162596a8eaa02ef87730474285b080d594 (diff)
downloadnumpy-490712cd35dcecfc9423de4bde0b29cb012dda25.tar.gz
More numpy fixes...
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r--numpy/core/numeric.py50
1 files changed, 49 insertions, 1 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py
index 03a2e520f..7cdebbf5d 100644
--- a/numpy/core/numeric.py
+++ b/numpy/core/numeric.py
@@ -10,7 +10,7 @@ __all__ = ['newaxis', 'ndarray', 'bigndarray', 'flatiter', 'ufunc',
'array_repr', 'array_str', 'set_string_function',
'little_endian',
'indices', 'fromfunction',
- 'load', 'loads',
+ 'load', 'loads', 'isscalar', 'binary_repr', 'base_repr',
'ones', 'identity', 'allclose',
'seterr', 'geterr', 'setbufsize', 'getbufsize',
'seterrcall', 'geterrcall',
@@ -271,6 +271,54 @@ def fromfunction(function, dimensions, **kwargs):
args = indices(dimensions)
return function(*args,**kwargs)
+def isscalar(num):
+ if isinstance(num, generic):
+ return True
+ else:
+ return type(num) in ScalarType
+
+_lkup = {'0':'000',
+ '1':'001',
+ '2':'010',
+ '3':'011',
+ '4':'100',
+ '5':'101',
+ '6':'110',
+ '7':'111',
+ 'L':''}
+
+def binary_repr(num):
+ """Return the binary representation of the input number as a string.
+
+ This is equivalent to using base_repr with base 2, but about 25x
+ faster.
+ """
+ ostr = oct(num)
+ bin = ''
+ for ch in ostr[1:]:
+ bin += _lkup[ch]
+ ind = 0
+ while bin[ind] == '0':
+ ind += 1
+ return bin[ind:]
+
+def base_repr (number, base=2, padding=0):
+ """Return the representation of a number in any given base.
+ """
+ chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+
+ lnb = math.log(base)
+ res = padding*chars[0]
+ if number == 0:
+ return res + chars[0]
+ exponent = int (math.log (number)/lnb)
+ while(exponent >= 0):
+ term = long(base)**exponent
+ lead_digit = int(number / term)
+ res += chars[lead_digit]
+ number -= term*lead_digit
+ exponent -= 1
+ return res
from cPickle import load, loads
_cload = load