3

I'm trying to work with linear functions for data conversion (like meters to feet).

I'm trying to find a way to build a lambda function that returns the inverse function and another lambda function that return the composition of those functions

inches_to_meters=lambda x:x*0.0254
inches_to_feets=lambda x:x*(1/12)
miles_to_feets=lambda x:x*5280
composition=lambda x,y,z: lambda x,y: x(y(z))
opposite=lambda x: 1/x
meters_to_inches=opposite(inches_to_meters)
miles_to_inches = composition(feets_to_inches, miles_to_feets)
print(meters_to_inches(10))    

how can I make the opposite function (F^-1(x))?

(For example: y=x/12, then 12*y=x, the opposite is: 12*x=y).

j-i-l
  • 10,281
  • 3
  • 53
  • 70
arthur
  • 43
  • 4

3 Answers3

6

First, it is better to use the def ... statement rather than lambda to define named functions. Have a look at this SO question and its answers for more details. But if I understand correctly what you want to achieve, it might be better to define the linear function as a class (see below).


The inverse of the linear function:

linear function

is given by:

inverse of linear function

and depends on both the slope a and the intercept b. An important consequence of this is that you need to know both a and b to define its inverse in a functional form.

In python you can achieve this for example if you define a class of linear functions and the inverse as one of its methods:

class f_lin:
    def __init__(self, a, b=0):
        self.a = float(a)
        self.b = b

    def __call__(self, x):
        return self.a * x + self.b

    def inv(self, y):
        return (y - self.b) / self.a

Now you can define:

inches_to_meters = f_lin(0.0254)

and use it like so:

inches_to_meters(10)
Out[38]: 0.254
inches_to_meters.inv(0.254)
Out[39]: 10.0
inches_to_meters.inv(inches_to_meters(10))
Out[40]: 10.0

Alternatively you could also create a new object for the inverse, as the inverse of a linear function is still a linear function:

class f_lin:
    def __init__(self, a, b=0):
        self.a = float(a)
        self.b = b

    def __call__(self, x):
        return self.a * x + self.b

    def get_inverse(self):
        return self.__class__(1/self.a, - self.b/self.a)

Then you can define the inverse function like so:

inches_to_meters = f_lin(0.0254)
meters_to_inches = inches_to_meters.get_inverse()

Now you have both the linear function and its inverse as objects:

inches_to_meters(10)
Out[43]: 0.254
meters_to_inches(0.254)
Out[44]: 10.0
inches_to_meters(meters_to_inches(10))
Out[45]: 10.0

Those are just 2 ways of doing, both not using lambda as requested. However I would advise against doing such things with lambda.

j-i-l
  • 10,281
  • 3
  • 53
  • 70
0
opposite = lambda f : (lambda x: x*x*1/f(x))

work perfect

0

I agree with the accepted answer that lambda is not recommended due to the pool tracebacks and string representations of lambda expression in general.

However in my opinion, the lambda could still be useful in this case of linear function inverse. However if the functions gets complicated, i'd still prefer def statement in general.

Anyway, Below is the lambda implementation:

a = 2
b = 3 
func = lambda x: a*x+b
inverse = lambda f: (lambda y: (y-b) / a)
func_inv = inverse(func)

# test
for x in range(10):
    y = func(x)
    x1 = func_inv(y)
    print('For y=f(x)={}x+{} when x={},'.format(a, b, x), 'y={}'.format(y))
    print("inverse function of y={} is {}\n".format(y, x1))
    assert x == x1

Output:

For y=f(x)=2x+3 when x=0, y=3
inverse function of y=3 is 0.0

For y=f(x)=2x+3 when x=1, y=5
inverse function of y=5 is 1.0

For y=f(x)=2x+3 when x=2, y=7
inverse function of y=7 is 2.0

For y=f(x)=2x+3 when x=3, y=9
inverse function of y=9 is 3.0

For y=f(x)=2x+3 when x=4, y=11
inverse function of y=11 is 4.0

For y=f(x)=2x+3 when x=5, y=13
inverse function of y=13 is 5.0

For y=f(x)=2x+3 when x=6, y=15
inverse function of y=15 is 6.0

For y=f(x)=2x+3 when x=7, y=17
inverse function of y=17 is 7.0

For y=f(x)=2x+3 when x=8, y=19
inverse function of y=19 is 8.0

For y=f(x)=2x+3 when x=9, y=21
inverse function of y=21 is 9.0
MaThMaX
  • 1,995
  • 1
  • 12
  • 23