Skip to content

Commit 07778aa

Browse files
committed
ENH: npv: Tidy up Cython version of npv
1 parent d329226 commit 07778aa

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

numpy_financial/_cfinancial.pyx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
from libc.math cimport NAN
2+
cimport cython
23

3-
4-
def _npv(const double[::1] rates, const double[:, ::1] values, double[:, ::1] out):
4+
@cython.boundscheck(False)
5+
@cython.cdivision(True)
6+
def npv(const double[::1] rates, const double[:, ::1] values, double[:, ::1] out):
57
cdef:
68
Py_ssize_t i, j, t
79
double acc
810

9-
for i in range(rates.shape[0]):
10-
for j in range(values.shape[0]):
11-
acc = 0.0
12-
for t in range(values.shape[1]):
13-
if rates[i] == -1.0:
14-
acc = NAN
15-
break
16-
else:
17-
acc += values[j, t] / ((1.0 + rates[i]) ** t)
18-
out[i, j] = acc
11+
with nogil:
12+
for i in range(rates.shape[0]):
13+
for j in range(values.shape[0]):
14+
acc = 0.0
15+
for t in range(values.shape[1]):
16+
if rates[i] == -1.0:
17+
acc = NAN
18+
break
19+
else:
20+
acc = acc + values[j, t] / ((1.0 + rates[i]) ** t)
21+
out[i, j] = acc

numpy_financial/_financial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from decimal import Decimal
1515

1616
import numpy as np
17-
from _cfinancial import _npv
17+
import _cfinancial
1818

1919
__all__ = ['fv', 'pmt', 'nper', 'ipmt', 'ppmt', 'pv', 'rate',
2020
'irr', 'npv', 'mirr',
@@ -934,7 +934,7 @@ def npv(rate, values):
934934

935935
output_shape = _get_output_array_shape(rate_inner, values_inner)
936936
out = np.empty(output_shape)
937-
_npv(rate_inner, values_inner, out)
937+
_cfinancial.npv(rate_inner, values_inner, out)
938938
return _ufunc_like(out)
939939

940940

0 commit comments

Comments
 (0)