Python : addition of lambda defined functions - python

I am wondering if there is any way of adding to lambda functions at the function level.
import numpy as np
f = lambda x: np.sin(5*x)+3
g = lambda x: np.cos(3*x)**2+1
x = np.linspace(-3.14,3.14,1000)
h = f+g % is there any way to create this ?
h_of_x = h(x)
This would be very helpful.

If you're looking for symbolic mathematics, use sympy.
from sympy import *
x = symbols("x")
f = sin(5*x)+3
g = cos(3*x)**2+1
h = f + g

May be this
h = lambda x: f(x)+g(x)

You can create a function plus that takes two functions as input and return their sum:
def plus(f, g):
def h(x):
return f(x) + g(x)
return h
h = plus(lambda x: x * x, lambda x: x ** 3)
Example:
>>> h(2)
12
Defining plus can have advantages, like:
>>> f = lambda x: x * 2
>>> h = reduce(plus, [f, f, f, f]) # or h = reduce(plus, [f] * 4)
>>> h(2)
16

Related

how to create a lambda function after find derivative of another lambda function?

I want to return a lambda function in check_Derivative like f in the main.
from sympy import *
def check_Derivative(f):
x = symbols('x')
my_f1 =lambda x:f(x).diff(x)
return my_f1
f = lambda x: x**4 + x**3-3*x**2
f1=check_Derivative(f)

How to find the nth derivative given the first derivative with SymPy?

Given some f and the differential equation x'(t) = f(x(t)), how do I compute x(n)(t) in terms of x(t)?
For example, given f(x(t)) = sin(x(t)),
I want to obtain x(3)(t) = (cos(x(t))2 − sin(x(t))2) sin(x(t)).
So far I've tried
>>> from sympy import diff, sin
>>> from sympy.abc import x, t
>>> diff(sin(x(t)), t, 2)
which gives me
-sin(x(t))*Derivative(x(t), t)**2 + cos(x(t))*Derivative(x(t), t, t)
but I'm not sure how to tell SymPy what Derivative(x(t), t) is and have it figure out Derivative(x(t), t, t), etc. automatically.
Answer:
Here's my final solution based on the answers I received below:
def diff(x_derivs_known, t, k, simplify=False):
try: n = len(x_derivs_known)
except TypeError: n = None
if n is None:
result = sympy.diff(x_derivs_known, t, k)
if simplify: result = result.simplify()
elif k < n:
result = x_derivs_known[k]
else:
i = n - 1
result = x_derivs_known[i]
while i < k:
result = result.diff(t)
j = len(x_derivs_known)
x0 = None
while j > 1:
j -= 1
result = result.subs(sympy.Derivative(x_derivs_known[0], t, j), x_derivs_known[j])
i += 1
if simplify: result = result.simplify()
return result
Example:
>>> diff((x(t), sympy.sin(x(t))), t, 3, True)
sin(x(t))*cos(2*x(t))
Here is one approach that returns a list of all derivatives up to n-th order
import sympy as sp
x = sp.Function('x')
t = sp.symbols('t')
f = lambda x: x**2 #sp.exp, sp.sin
n = 4 #3, 4, 5
deriv_list = [x(t), f(x(t))] # list of derivatives [x(t), x'(t), x''(t),...]
for i in range(1,n):
df_i = deriv_list[-1].diff(t).replace(sp.Derivative,lambda *args: f(x(t)))
deriv_list.append(df_i)
print(deriv_list)
[x(t), x(t)**2, 2*x(t)**3, 6*x(t)**4, 24*x(t)**5]
With f=sp.sin it returns
[x(t), sin(x(t)), sin(x(t))*cos(x(t)), -sin(x(t))**3 + sin(x(t))*cos(x(t))**2, -5*sin(x(t))**3*cos(x(t)) + sin(x(t))*cos(x(t))**3]
EDIT: A recursive function for the computation of the n-th derivative:
def der_xt(f, n):
if n==1:
return f(x(t))
else:
return der_xt(f,n-1).diff(t).replace(sp.Derivative,lambda *args: f(x(t)))
print(der_xt(sp.sin,3))
-sin(x(t))**3 + sin(x(t))*cos(x(t))**2
Declare f and use substitution:
>>> f = diff(x(t))
>>> diff(sin(x(t)), t, 2).subs(f, sin(x(t)))
-sin(x(t))**3 + cos(x(t))*Derivative(sin(x(t)), t)

Sum of n lambda functions

I have a list of lambda functions. Lets say this one
l = [lambda x:x**i for i in range(n)]
For every n I need to be able to sum them so I'd have a function like this:
f = lambda x: x + x**2 + x**3 + ... + x**n
Is there any way?
Edit: I wasn't clear. I don't know anything about that functions.
Is this the solution you're looking for?
Python 3.x:
n = 5
g = lambda y: sum( f(y) for f in (lambda x: x**i for i in range(n)) )
print(g(5)) # 781
Python 2.x:
n = 5
g = lambda y: sum( f(y) for f in (lambda x: x**i for i in xrange(n)) )
print g(5) # 781
If you mean a finite sum, up to x**n, use the mathematical shortcut
f = lambda x: (x**(n+1) - 1) / (x - 1) if x != 1 else n
f = lambda x,n: sum( x**i for i in range(n) )
print f(3,4)
>> 40
The simplest way to do this is to avoid creating the list of lambda functions, and to instead sum over a single function. Assuming you've defined x and n, you can do:
f = lambda x, i: x**i
sum(f(x, i) for i in range(n))
In your original example, you have actually created a closure, so your lambda functions do not do what you think they do. Instead, they are all identical, since they all use the final value of i in the closure. That is certainly not what you intended.
n=5
xpower=[]
for i in range(n):
xpower.insert(i, i+1)
print(i,xpower)
f = lambda x, xpower: sum(x**xpower[i] for i in range(len(xpower)))
print("Example with n=5, x=2:"," ", f(2,xpower))

Finding the Limit of (1+1/n)^n as n->infinity using Python/Numpy

I'm trying to use Python to plot how the limit (1+1/n)^n as n->infinity will go towards e at large n.
Why is the plot going towards 1 instead of e?
n = np.arange(0,10000,1)
f = lambda x: np.power(1 + (1/x), x)
plt.plot(n,f(n))
in this line:
f = lambda x: np.power(1 + (1/x), x)
when x is an int so 1/X will always be 0, do
f = lambda x: np.power(1 + (1.0/x), x)

how to print lambda expression

I have two (many) lambdas:
myFoo = lambda x,y: x + y
mySpecFoo = lambda x: myFoo(x, 1)
I want to print resulting expression for mySpecFoo. smth like
x = var('x')
print(mySpecFoo(x))
and I want to see in output:
lambda x: x + 1
Do you know how to do this?
Thank you!
This is called "symbolic evaluation", and you need some external library to do this, for example SymPy:
>>> import sympy
>>> myFoo = lambda x,y: x + y
>>> mySpecFoo = lambda x: myFoo(x, 1)
>>> x = sympy.var("x")
>>> print mySpecFoo(x)
1 + x

Categories

Resources