Skip to content

improve "monomials" to work with expressions containing functions

So far when calling kwant.continuum._common.monomials on expression that contains function ends up with

NotImplementedError: Getting monomials of expressions containing functions is not implemented.

I overcome the problem here by first finding all functions and temporarily substituting them with temporary symbols.

Now for example behaviour will be as follows:

>>> import kwant
>>> x, y, z = kwant.continuum.position_operators

>>> expr = kwant.continuum.sympify('x * f(t, m) + y * g(a, b)')
>>> kwant.continuum._common.monomials(expr, x, y)
{x: f(t, m), y: g(a, b)}

edit:

There was actually nothing in logic of current implementation that prevented that from working correctly. I am only raising an error if any symbol would be present in both function arguments and monomial's gens:

>>> expr = kwant.continuum.sympify('x * f(x)')
>>> kwant.continuum._common.monomials(expr, x)
ValueError: Functions in "expression" cannot contain any of "gens" as their argument.

Merge request reports