summaryrefslogtreecommitdiff
path: root/numpy/f2py
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2020-10-29 11:30:09 +0200
committerGitHub <noreply@github.com>2020-10-29 11:30:09 +0200
commitc5b2676a5cc32b316a76d4ad636b6b3109f09c2e (patch)
treef61c1b75f48339fec51f51117803e595756b470f /numpy/f2py
parenta6103c0bad99db6efd76b03528634c96d118109b (diff)
parentff26a55e7855f24f065714253fdc5de1c0df9eab (diff)
downloadnumpy-c5b2676a5cc32b316a76d4ad636b6b3109f09c2e.tar.gz
Merge pull request #17662 from eric-wieser/test-getlincoef
TST: f2py: Add a doctest for `getlincoef`
Diffstat (limited to 'numpy/f2py')
-rwxr-xr-xnumpy/f2py/crackfortran.py23
1 files changed, 22 insertions, 1 deletions
diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py
index 2614d3dca..67101b353 100755
--- a/numpy/f2py/crackfortran.py
+++ b/numpy/f2py/crackfortran.py
@@ -2103,8 +2103,9 @@ def buildimplicitrules(block):
def myeval(e, g=None, l=None):
+ """ Like `eval` but returns only integers and floats """
r = eval(e, g, l)
- if type(r) in [type(0), type(0.0)]:
+ if type(r) in [int, float]:
return r
raise ValueError('r=%r' % (r))
@@ -2112,6 +2113,26 @@ getlincoef_re_1 = re.compile(r'\A\b\w+\b\Z', re.I)
def getlincoef(e, xset): # e = a*x+b ; x in xset
+ """
+ Obtain ``a`` and ``b`` when ``e == "a*x+b"``, where ``x`` is a symbol in
+ xset.
+
+ >>> getlincoef('2*x + 1', {'x'})
+ (2, 1, 'x')
+ >>> getlincoef('3*x + x*2 + 2 + 1', {'x'})
+ (5, 3, 'x')
+ >>> getlincoef('0', {'x'})
+ (0, 0, None)
+ >>> getlincoef('0*x', {'x'})
+ (0, 0, 'x')
+ >>> getlincoef('x*x', {'x'})
+ (None, None, None)
+
+ This can be tricked by sufficiently complex expressions
+
+ >>> getlincoef('(x - 0.5)*(x - 1.5)*(x - 1)*x + 2*x + 3', {'x'})
+ (2.0, 3.0, 'x')
+ """
try:
c = int(myeval(e, {}, {}))
return 0, c, None