-3

Assuming the function:

def my_function(a):
    return 2 * a

Is there a Pythonic way to obtain the function that would otherwise be defined as:

def my_opposite_function(a):
    return a / 2

with a syntax such as:

my_opposite_function = something(my_function)
Nicolas Berthier
  • 459
  • 1
  • 8
  • 17
  • 12
    No. In general, a Python function may not even *be* invertible, and even if it is, finding an inverse is computationally undecidable. – chepner Sep 08 '20 at 16:36
  • 2
    For a very simple case such as OP's, you might be able to use the `ast` module to figure out the inverse `return` expression. – AKX Sep 08 '20 at 16:37
  • 2
    possible duplicate: https://stackoverflow.com/questions/15200560/calculate-inverse-of-a-function-library – riddleculous Sep 08 '20 at 16:38
  • 3
    And even if it exists and is decidable, it could still be NP-hard – John Coleman Sep 08 '20 at 16:38
  • 5
    If it's a purely mathematical function, you have `y = f(x)`, and you need to find `x` for a given `y`. This problem can be expressed as `y - f(x) = 0` and solved using a [bunch of different techniques](https://en.wikipedia.org/wiki/Root-finding_algorithms) – Pranav Hosangadi Sep 08 '20 at 16:38
  • 1
    https://docs.scipy.org/doc/scipy/reference/optimize.html – Pranav Hosangadi Sep 08 '20 at 16:40
  • 1
    @PranavHosangadi I agree with this and is good advice, if it's not mathematical the question really doesnt make sense – Derek Eden Sep 08 '20 at 16:41
  • 1
    @AKX that is an interesting idea. For a certain class of functions, this might be possible to do exactly using a combination of `ast` and `sympy`. – John Coleman Sep 08 '20 at 16:46

2 Answers2

2

That's an interesting question, initially I'd say no because the function does not expose it's internal structure easily. If functions didn't then they would not operate like "predictable" black boxes. However, there are ways to get at the underlying logic and perhaps reverse it if that's what you intend to do: get the lists of functions used/called within a function in python

But it's certainly not a proper programming paradigm, i.e. what happens if you change the initial function that you're reversing, you then have an unpredictable effect on the reverse function.

labarna
  • 668
  • 1
  • 9
  • 16
2

Even if we stick to arithmetic, not all functions are invertable. As a quick example, I recently implemented the Callendar-Van Dusen Equation for linearizing the output of a platinum RTD thermometer. The equation changes slightly depending on if the temperature is above or below 0C, so there isn't a simple inverse function. Furthermore the equation gives the resistance of the thermometer based on temperature (because they calibrate probes with a known temperature and measure resistance), but I needed the inverse of that (Temp given resistance). Because there was no analytical solution to the inverse of my equation I was forced to use an approximation technique (others mentioned scipy.optimize). I chose Newton's method because it was easy to solve for the derivative of both equations (for above and below 0C). The code is in Lua rather than python, but the example should still be understandable

local _CVD_root_finder = function(r0, R)
    --The Callendar Van Dusen Equation for Platinum Based RTD Thermometers
    --Newtons method root finder
    local T = 0
    local a, b, c = max31865._A, max31865._B , max31865._C
    local i = max31865._root_find_iterations
    for _ = 1,i do
        if T < 0 then
            local r = r0 * (1 + a*T + b*T*T + c*(T - 100)*T*T*T) - R
            local slope = r0 * (a + 2*b*T + 4*c*(T - 75)*T*T)
            T = T - r/slope
        else
            local r = r0 * (1 + a*T + b*T*T) - R
            local slope = r0 * (a + 2*b*T)
            T = T - r/slope
        end
    end
    return T
end
Aaron
  • 10,133
  • 1
  • 24
  • 40