From b3814d42386a4b51edf7c28ee87eb3e28842ae65 Mon Sep 17 00:00:00 2001 From: Fei Liu Date: Sat, 27 Sep 2014 14:03:07 -0400 Subject: [PATCH 1/4] rate==0.0 fix --- numpy/lib/financial.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/numpy/lib/financial.py b/numpy/lib/financial.py index 5b96e5b8e979..e79502a76e76 100644 --- a/numpy/lib/financial.py +++ b/numpy/lib/financial.py @@ -211,8 +211,13 @@ def pmt(rate, nper, pv, fv=0, when='end'): temp = (1+rate)**nper miter = np.broadcast(rate, nper, pv, fv, when) zer = np.zeros(miter.shape) - fact = np.where(rate == zer, nper + zer, - (1 + rate*when)*(temp - 1)/rate + zer) + fact=zer + if rate==0.0: + fact=map(np.asarray,[nper + zer]) + else: + fact=map(np.asarray,[(1 + rate*when)*(temp - 1)/rate + zer]) + # fact = np.where(rate == 0.0, nper + zer, + # (1 + rate*when)*(temp - 1)/rate + zer) return -(fv + pv*temp) / fact def nper(rate, pmt, pv, fv=0, when='end'): From 2345c295c564d8b31fe80fe38cd4210a77e71f46 Mon Sep 17 00:00:00 2001 From: Fei Liu Date: Sat, 27 Sep 2014 14:33:30 -0400 Subject: [PATCH 2/4] test case for the rate==0.0 issue --- numpy/lib/tests/test_financial.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py index a4b9cfe2ed32..454743256537 100644 --- a/numpy/lib/tests/test_financial.py +++ b/numpy/lib/tests/test_financial.py @@ -42,6 +42,10 @@ def test_fv(self): def test_pmt(self): assert_almost_equal(np.pmt(0.08/12, 5*12, 15000), -304.146, 3) + # This is to test the edge case where rate == 0.0. + # It would fail on this case if the fix for checking rate == 0.0 was not there. + assert_almost_equal(np.pmt(0.0, 5*12, 15000), + -250.0, 3) def test_ppmt(self): np.round(np.ppmt(0.1/12, 1, 60, 55000), 2) == 710.25 From da1019ec80e4152a061820adc535030919cf6184 Mon Sep 17 00:00:00 2001 From: Fei Liu Date: Sat, 27 Sep 2014 15:55:56 -0400 Subject: [PATCH 3/4] fix the broadcast way of calling financial --- numpy/lib/financial.py | 14 +++++++++----- numpy/lib/tests/test_financial.py | 2 ++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/numpy/lib/financial.py b/numpy/lib/financial.py index e79502a76e76..0d282b7d10bc 100644 --- a/numpy/lib/financial.py +++ b/numpy/lib/financial.py @@ -211,11 +211,15 @@ def pmt(rate, nper, pv, fv=0, when='end'): temp = (1+rate)**nper miter = np.broadcast(rate, nper, pv, fv, when) zer = np.zeros(miter.shape) - fact=zer - if rate==0.0: - fact=map(np.asarray,[nper + zer]) - else: - fact=map(np.asarray,[(1 + rate*when)*(temp - 1)/rate + zer]) + fact= np.zeros(miter.shape) + # result=np.zeros(miter.shape) + numerator = (1 + rate*when)*(temp - 1) + np.divide(numerator, rate, where=(rate!=0), out=fact) + # print ('fact' , fact) + factforZeroRate = nper + zer + np.copyto(fact, factforZeroRate , where=(rate==0)) + # print ('fact' , fact) + # np.copyto(fact, factforZeroRate , where=(rate==0)) # fact = np.where(rate == 0.0, nper + zer, # (1 + rate*when)*(temp - 1)/rate + zer) return -(fv + pv*temp) / fact diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py index 454743256537..a7faa1de75a3 100644 --- a/numpy/lib/tests/test_financial.py +++ b/numpy/lib/tests/test_financial.py @@ -46,6 +46,8 @@ def test_pmt(self): # It would fail on this case if the fix for checking rate == 0.0 was not there. assert_almost_equal(np.pmt(0.0, 5*12, 15000), -250.0, 3) + # This one handles the case where we use broadcast and arguments passed in are arrays + assert_almost_equal(np.pmt([[0.0,0.8],[0.3, 0.8]], [12,3], [2000,20000]),np.array([[ -166.666, -19311.258],[ -626.908, -19311.258]]), 3) def test_ppmt(self): np.round(np.ppmt(0.1/12, 1, 60, 55000), 2) == 710.25 From 484745599c384608ad1f6669c6cac0cfe29b0bf8 Mon Sep 17 00:00:00 2001 From: Fei Liu Date: Sat, 27 Sep 2014 16:30:31 -0400 Subject: [PATCH 4/4] asscalar does not take scalar fix --- numpy/lib/tests/test_type_check.py | 5 +++++ numpy/lib/type_check.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py index 3931f95e5fb9..05377d9d8c1d 100644 --- a/numpy/lib/tests/test_type_check.py +++ b/numpy/lib/tests/test_type_check.py @@ -89,6 +89,11 @@ def test_basic(self): assert_(np.isscalar(long(10))) assert_(np.isscalar(4.0)) +class TestAsscalar(TestCase): + + def test_basic(self): + assert_(np.asscalar(3)) + assert_(np.asscalar(np.array([3]))) class TestReal(TestCase): diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py index a45d0bd865c3..1df1f1f7a520 100644 --- a/numpy/lib/type_check.py +++ b/numpy/lib/type_check.py @@ -460,7 +460,7 @@ def asscalar(a): 24 """ - return a.item() + return array(a).item() #-----------------------------------------------------------------------------