Skip to content
Snippets Groups Projects
Commit da8dc832 authored by Rafal Skolasinski's avatar Rafal Skolasinski Committed by Joseph Weston
Browse files

Fix compatibility issues with Sympy 1.2. Closes #203.

SymPy 1.2 brought changes to LambdaPrinter that broke kwant.continuum
for inputs containing functions like "sin", "cos", or constant "pi".
By this fix we assert that "kwant.continuum.sympify" represents in
its output all functions as AppliedUndef in favour to built-in SymPy
functions. We also put a hard constaint that all functions and constants
in the input expression (with except to pi) must be provided through params.
parent 731aa242
No related branches found
No related tags found
No related merge requests found
......@@ -190,6 +190,15 @@ def sympify(expr, locals=None):
converter[list] = stored_value
else:
del converter[list]
# We assume that all present functions, like "sin", "cos", will be
# provided by user during the final evaluation through "params".
# Therefore we make sure they are define as AppliedUndef, not built-in
# sympy types.
subs = {r: sympy.Symbol(str(r.func))(*r.args)
for r in hamiltonian.atoms(sympy.Function)}
hamiltonian = hamiltonian.subs(subs)
return hamiltonian
......
......@@ -457,6 +457,11 @@ def _discretize_expression(expression, coords):
class _NumericPrinter(LambdaPrinter):
def __init__(self):
LambdaPrinter.__init__(self)
self.known_functions = {}
self.known_constants = {'pi': 'pi', 'Pi': 'pi'}
def _print_ImaginaryUnit(self, expr):
# prevent sympy from printing 'I' for imaginary unit
return "1j"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment