Prove Euler's theorem with Sympy - python

I am trying to use Sympy for this problem, however I'm having some trouble with the code. Here's what I have so far. The error message is that "g is not a callable function".
from sympy import *
x, y, z, λ, m = symbols('x y z λ m',real=True)
g = Function('g')(x,y,z)
LHS = g(λ*x,λ*y,z)
RHS = (λ**m)*g(x,y,z)
expr = Eq(LHS,RHS)
display(expr)

Related

how to Find derivative of a LHS expression with respect to a variable in the RHS of a Sympy Eq

Suppose I have an eq1 such that
from sympy import symbols, solve, plot, Eq, diff
a, b, X, Y, U = symbols('a b X Y U')
eq1 = Eq(U, X**a*Y**b)
$U=(X^a)(Y^b)$
but When I run diff(eq1 , X)
the differential does not evaluate I merele just get the DU/DX symbol but not evaluated
I know I could defined the function as
U = X**a * Y**b
and easily compute diff(U)
but printing the U expression will not look nice.
I am surprised that you even get what you got. In general, algebraic operations on Eq are not supported.
>>> from sympy import Derivative
>>> ediff=lambda e, *x: e.func(Derivative(e.lhs,*x), e.rhs.diff(*x))
>>> ediff(eq1, X)
Eq(Derivative(U, X), X**a*Y**b*a/X)

Evaluating the solution of sympy dsolve numerically

I would like to evaluate the solution of a differential equation against some x_test
array
from sympy import *
init_printing()
from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
# Constants
C, R, u_rest = symbols('C R u_rest')
f, g, h = symbols('f g h', cls=Function)
solution = dsolve(C*Derivative(f(x), x) + (1/R)*(f(x) - u_rest ),f(x))
x_text = np.array(range(0,100))
but I fail
# 1. attempt with evalf(Not working)
solution.args[1].evalf(subs={x: 3.14})
# 2. attempt with lambdify(Not working)
lambdify(x,solution.args[1])(3.14)
What is the right way to do it?
If you look at your solution.args[1] value you will see that it is an expression of several variables, including x. It will not evaluate to a number until you supply values for all variables. Your first attempt doesn't fail, but you don't explain why it is not giving you what you hoped for:
>>> solution.args[1].evalf(subs={x: 3.14})
u_rest*(1.0 - exp((C1 - 3.14/R)/C))

Getting the derivative of a function as a function with sympy (for later evaluation and substitution)

I want to work with generic functions as long as possible, and only substitute functions at the end.
I'd like to define a function as the derivative of another one, define a generic expression with the function and its derivative, and substitute the function at the end.
Right now my attempts is as follows, but I get the error 'Derivative' object is not callable:
from sympy import Function
x, y, z = symbols('x y z')
f = Function('f')
df = f(x).diff(x) # <<< I'd like this to be a function of dummy variable x
expr = f(x) * df(z) + df(y) + df(0) # df is unfortunately not callable
# At the end, substitute with example function
expr.replace(f, Lambda(X, cos(X))) # should return: -cos(x)*sin(z) - sin(y) - sin(0)
I think I got it to work with integrals as follows:
I= Lambda( x, integrate( f(y), (y, 0, x))) but that won't work for derivatives.
If that helps, I'm fine restricting myself to functions of a single variable for now.
As a bonus, I'd like to get this to work with any combination (products, derivatives, integrals) of the original function.
It's pretty disappointing that f.diff(x) doesn't work, as you say. Maybe someone will create support it sometime in the future. In the mean time, there are 2 ways to go about it: either substitute x for your y, z, ... OR lambdify df.
I think the first option will work more consistently in the long run (for example, if you decide to extend to multivariate calculus). But the expr in second option is far more natural.
Using substitution:
from sympy import *
x, y, z = symbols('x y z')
X = Symbol('X')
f = Function('f')
df = f(x).diff(x)
expr = f(x) * df.subs(x, z) + df.subs(x, y) + df.subs(x, 0)
print(expr.replace(f, Lambda(X, cos(X))).doit())
Lambdifying df:
from sympy import *
x, y, z = symbols('x y z')
X = Symbol('X')
f = Function('f')
df = lambda t: f(t).diff(t) if isinstance(t, Symbol) else f(X).diff(X).subs(X, t)
expr = f(x) * df(z) + df(y) + df(0)
print(expr.replace(f, Lambda(X, cos(X))).doit())
Both give the desired output.

Numerical and analytical equation solving in python or matlab

Let's suppose that I have two transcendental functions f(x, y) = 0 and g(a, b) = 0.
a and b depend on y, so if I could solve the first equation analytically for y, y = f(x), I could have the second function depending only on x and thus solving it numerically.
I prefer to use python, but if matlab is able to handle this is ok for me.
Is there a way to solve analytically trascendent functions for a variable with python/matlab? Taylor is fine too, as long as I can choose the order of approximation.
I tried running this through Sympy like so:
import sympy
j, k, m, x, y = sympy.symbols("j k m x y")
eq = sympy.Eq(k * sympy.tan(y) + j * sympy.tan(sympy.asin(sympy.sin(y) / x)), m)
eq.simplify()
which turned your equation into
Eq(m, j*sin(y)/(x*sqrt(1 - sin(y)**2/x**2)) + k*tan(y))
which, after a bit more poking, gives us
k * tan(y) + j * sin(y) / sqrt(x**2 - sin(y)**2) == m
We can find an expression for x(y) like
sympy.solve(eq, x)
which returns
[-sqrt(j**2*sin(y)**2/(k*tan(y) - m)**2 + sin(y)**2),
sqrt(j**2*sin(y)**2/(k*tan(y) - m)**2 + sin(y)**2)]
but an analytic solution for y(x) fails.

partial integration of a two dimensional gaussian function

I want to carry out the following partial integration of a 2-D gaussian function of four variables (x, y, alpha and beta), with respect to only x and y, as follows. In the end I want the answer to be a function of alpha and beta only.
I wrote the following code in python to execute the above mentioned integral.
from sympy import Symbol
from sympy import integrate
from math import e
alpha = Symbol('alpha')
beta = Symbol('beta')
x = Symbol('x')
y = Symbol('y')
n = 2
value = integrate( e**( -(x - alpha)**n - (y - beta)**n ), (x, -1, 1), (y, -1, 1) )
However I get the following error:
sympy.polys.polyerrors.DomainError: there is no ring associated with RR
The above mentioned integrate function works fine for n=1. However it breaks down for n>1.
Am I doing something wrong?
Welcome to SO!
Interestingly it works when you substitute alpha and beta into the integral bounds. Try:
from IPython.display import display
import sympy as sy
sy.init_printing() # LaTeX like pretty printing forIPython
alpha, beta, x, y = sy.symbols("alpha, beta, x, y", real=True)
f = sy.exp(-x**2 - y**2) # sy.exp() is better than the numeric constant
val = sy.integrate(f, (x, -1+alpha, 1+alpha), (y, -1+beta, 1+beta))
display(val)

Categories