Keller Scholl - 1 year ago 72
Python Question

# Is it possible to invert an arbitrary lambda in Python?

I have been playing around with Python and math lately, and I ran in to something I have yet to be able to figure out. Namely, is it possible, given an arbitrary lambda, to return the inverse of that lambda for mathematical operations? That is, invertLambda such that invertLambda(lambda x:(x+2))(2) = 0. The fact that lambdas are restricted to expressions gives me hope, but so far I have not been able to make it work. I understand that any result would have problems with functions that lose information, but I am willing to restrict users and myself to lossless functions if I have to.

Of course not: if lambda is not an injective function, you cannot invert it. Example: you cannot invert lambda mapping `x` to `x*x`, since the sign of the original `x` is lost.
Of course, we could refrain from exact solution, and just invert the function numerically. This is possible, using, well, any method of numerical solving of the equation `lambda(x) = value` will do (the simplest be binary search).