[Numpy-svn] r8530 - in trunk/numpy/core: . tests
numpy-svn at scipy.org
numpy-svn at scipy.org
Wed Jul 28 11:09:57 EDT 2010
Author: rgommers
Date: 2010-07-28 10:09:57 -0500 (Wed, 28 Jul 2010)
New Revision: 8530
Modified:
trunk/numpy/core/numeric.py
trunk/numpy/core/tests/test_numeric.py
Log:
BUG: New implementation of base_repr. Fixes #1549, is several times faster and
adds tests. Thanks to K. Maglione.
Modified: trunk/numpy/core/numeric.py
===================================================================
--- trunk/numpy/core/numeric.py 2010-07-27 22:49:05 UTC (rev 8529)
+++ trunk/numpy/core/numeric.py 2010-07-28 15:09:57 UTC (rev 8530)
@@ -695,7 +695,7 @@
warnings.warn("""
The current behavior of correlate is deprecated for 1.4.0, and will be removed
for NumPy 1.5.0.
-
+
The new behavior fits the conventional definition of correlation: inputs are
never swapped, and the second argument is conjugated for complex arrays.""",
DeprecationWarning)
@@ -1752,13 +1752,13 @@
bin = bin.zfill(width)
return sign + bin
-def base_repr (number, base=2, padding=0):
+def base_repr(number, base=2, padding=0):
"""
Return a string representation of a number in the given base system.
Parameters
----------
- number : scalar
+ number : int
The value to convert. Only positive values are handled.
base : int, optional
Convert `number` to the `base` number system. The valid range is 2-36,
@@ -1773,8 +1773,7 @@
See Also
--------
- binary_repr : Faster version of `base_repr` for base 2 that also handles
- negative numbers.
+ binary_repr : Faster version of `base_repr` for base 2.
Examples
--------
@@ -1791,26 +1790,21 @@
'20'
"""
+ digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+ if base > len(digits):
+ raise ValueError("Bases greater than 36 not handled in base_repr.")
+
+ num = abs(number)
+ res = []
+ while num:
+ res.append(digits[num % base])
+ num /= base
+ if padding:
+ res.append('0' * padding)
if number < 0:
- raise ValueError("negative numbers not handled in base_repr")
- if base > 36:
- raise ValueError("bases greater than 36 not handled in base_repr")
+ res.append('-')
+ return ''.join(reversed(res or '0'))
- chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- import math
- 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
_file = open
Modified: trunk/numpy/core/tests/test_numeric.py
===================================================================
--- trunk/numpy/core/tests/test_numeric.py 2010-07-27 22:49:05 UTC (rev 8529)
+++ trunk/numpy/core/tests/test_numeric.py 2010-07-28 15:09:57 UTC (rev 8530)
@@ -306,7 +306,21 @@
assert_equal(binary_repr(-1), '-1')
assert_equal(binary_repr(-1, width=8), '11111111')
+class TestBaseRepr(TestCase):
+ def test_base3(self):
+ assert_equal(base_repr(3**5, 3), '100000')
+ def test_positive(self):
+ assert_equal(base_repr(12, 10), '12')
+ assert_equal(base_repr(12, 10, 4), '000012')
+ assert_equal(base_repr(12, 4), '30')
+ assert_equal(base_repr(3731624803700888, 36), '10QR0ROFCEW')
+
+ def test_negative(self):
+ assert_equal(base_repr(-12, 10), '-12')
+ assert_equal(base_repr(-12, 10, 4), '-000012')
+ assert_equal(base_repr(-12, 4), '-30')
+
class TestArrayComparisons(TestCase):
def test_array_equal(self):
res = array_equal(array([1,2]), array([1,2]))
More information about the Numpy-svn
mailing list