Skip to content

Instantly share code, notes, and snippets.

@bamford
Created October 16, 2019 12:47
Show Gist options
  • Save bamford/094a34cc9c9ef53b8f77b3eb7efcc2ef to your computer and use it in GitHub Desktop.
Save bamford/094a34cc9c9ef53b8f77b3eb7efcc2ef to your computer and use it in GitHub Desktop.
Test whether a given function is a valid w(z)
from sympy import Symbol, sympify
def test(w):
"""Test if w is valid
Argument w should be a string expression containing the symbol z.
Returns True if w is real and positive, for all real and positive z, otherwise False.
Example:
>>> test('z + 1')
True
>>> test('z - 1')
False
"""
ns = dict(z=Symbol('z', real=True, positive=True))
w = sympify(w, ns)
return (w.is_real is True) and (w.is_positive is True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment