def test_pde_separate_add(): x, y, z, t = symbols("x,y,z,t") F, T, X, Y, Z, u = map(Function, 'FTXYZu') eq = Eq(D(u(x, t), x), D(u(x, t), t) * exp(u(x, t))) res = pde_separate_add(eq, u(x, t), [X(x), T(t)]) assert res == [D(X(x), x) * exp(-X(x)), D(T(t), t) * exp(T(t))]
def christoffel_symbol_component(lam: int, mu: int, nu: int, metric: Metric) -> Expr: """Compute a component of the Chrisoffel symbol for a particular metric corresponding to G_mn^l = 1/2 g^ms (p_m g_ns + p_n g_sm - p_s g_mn) Args: lam: int, Upper index of Christoffel symbol mu: int, lower left index of Christoffel symbol nu: int, lower right index of Christoffel symbol metric: Metric Returns: Expression of the G_mn^l """ M = metric.matrix I = metric.inverse.matrix coord_symbols = metric.coord_system.base_symbols() dim = len(coord_symbols) return Rational(1, 2) * sum([ I[lam, sig] * (D(M[nu, sig], coord_symbols[mu]) + D(M[sig, mu], coord_symbols[nu]) - D(M[mu, nu], coord_symbols[sig])) for sig in range(dim) ])
def test_euler_sineg(): psi = Function("psi") t = Symbol("t") x = Symbol("x") L = D(psi(t, x), t)**2 / 2 - D(psi(t, x), x)**2 / 2 + cos(psi(t, x)) assert euler(L, psi(t, x), [t, x]) == [ Eq(-sin(psi(t, x)) - D(psi(t, x), t, t) + D(psi(t, x), x, x), 0) ]
def test_euler_sineg(): psi = Function('psi') t = Symbol('t') x = Symbol('x') L = D(psi(t, x), t)**2 / 2 - D(psi(t, x), x)**2 / 2 + cos(psi(t, x)) assert euler(L, psi(t, x), [t, x]) == [ Eq(-sin(psi(t, x)) - D(psi(t, x), t, t) + D(psi(t, x), x, x)) ]
def test_issue_11726(): x, t = symbols("x t") f = symbols("f", cls=Function) X, T = symbols("X T", cls=Function) u = f(x, t) eq = u.diff(x, 2) - u.diff(t, 2) res = pde_separate(eq, u, [T(x), X(t)]) assert res == [D(T(x), x, x) / T(x), D(X(t), t, t) / X(t)]
def lagrange_equation(L, v): a = L.subs(D(v(t), t), tmp).diff(tmp).subs(tmp, D(v(t), t)) b = L.subs(D(v(t), t), tmp).subs(v(t), v).diff(v).subs(v, v(t)).subs(tmp, D(v(t), t)) c = a.diff(t) - b c = c.subs(sublist) c = trigsimp(simplify(c)) c = collect(c, [th1, th2, dth1, dth2, ddth1, ddth2]) return c
def test_euler_henonheiles(): x = Function('x') y = Function('y') t = Symbol('t') L = sum(D(z(t), t)**2 / 2 - z(t)**2 / 2 for z in [x, y]) L += -x(t)**2 * y(t) + y(t)**3 / 3 assert euler(L, [x(t), y(t)], t) == [ Eq(-2 * x(t) * y(t) - x(t) - D(x(t), t, t)), Eq(-x(t)**2 + y(t)**2 - y(t) - D(y(t), t, t)) ]
def lagrange_equation(L, th): ddL_th = L.subs(D(th(t), t), tmp).diff(tmp, 1).subs(tmp, D(th(t), t)) dL_th = L.subs(D(th(t), t), tmp).subs(th(t), tmp2).diff(tmp2, 1).subs(tmp2, th(t)).subs(tmp, D(th(t), t)) L_equ = ddL_th.diff(t, 1) - dL_th #print("the lagrange equation of %r is:\n"%th) #pprint(simplify(L_equ)) #print("\n") return L_equ
def main(): r, phi, theta = symbols("r,phi,theta") Xi = Function("Xi") R, Phi, Theta, u = map(Function, ["R", "Phi", "Theta", "u"]) C1, C2 = symbols("C1,C2") pprint( "Separation of variables in Laplace equation in spherical coordinates") pprint("Laplace equation in spherical coordinates:") eq = Eq( D(Xi(r, phi, theta), r, 2) + 2 / r * D(Xi(r, phi, theta), r) + 1 / (r**2 * sin(phi)**2) * D(Xi(r, phi, theta), theta, 2) + cos(phi) / (r**2 * sin(phi)) * D(Xi(r, phi, theta), phi) + 1 / r**2 * D(Xi(r, phi, theta), phi, 2), 0, ) pprint(eq) pprint("We can either separate this equation in regards with variable r:") res_r = pde_separate(eq, Xi(r, phi, theta), [R(r), u(phi, theta)]) pprint(res_r) pprint("Or separate it in regards of theta:") res_theta = pde_separate(eq, Xi(r, phi, theta), [Theta(theta), u(r, phi)]) pprint(res_theta) res_phi = pde_separate(eq, Xi(r, phi, theta), [Phi(phi), u(r, theta)]) pprint("But we cannot separate it in regards of variable phi: ") pprint("Result: %s" % res_phi) pprint("\n\nSo let's make theta dependent part equal with -C1:") eq_theta = Eq(res_theta[0], -C1) pprint(eq_theta) pprint("\nThis also means that second part is also equal to -C1:") eq_left = Eq(res_theta[1], -C1) pprint(eq_left) pprint("\nLets try to separate phi again :)") res_theta = pde_separate(eq_left, u(r, phi), [Phi(phi), R(r)]) pprint("\nThis time it is successful:") pprint(res_theta) pprint("\n\nSo our final equations with separated variables are:") pprint(eq_theta) pprint(Eq(res_theta[0], C2)) pprint(Eq(res_theta[1], C2))
def test_euler_interface(): x = Function('x') y = Symbol('y') t = Symbol('t') raises(TypeError, lambda: euler()) raises(TypeError, lambda: euler(D(x(t), t) * y(t), [x(t), y])) raises(ValueError, lambda: euler(D(x(t), t) * x(y), [x(t), x(y)])) raises(TypeError, lambda: euler(D(x(t), t)**2, x(0))) raises(TypeError, lambda: euler(D(x(t), t) * y(t), [t])) assert euler(D(x(t), t)**2 / 2, {x(t)}) == [Eq(-D(x(t), t, t), 0)] assert euler(D(x(t), t)**2 / 2, x(t), {t}) == [Eq(-D(x(t), t, t), 0)]
def test_simplify_deriv_notation(self): """Test simplify deriv notation""" cs = coords.cartesian_coords() t, x, *_ = cs.base_symbols() dt, dx, *_ = cs.base_oneforms() F = Function('F')(t) G = Function('G')(t, x) form = F ** 2 * tpow(dt, 2) + G ** 2 * tpow(dx, 2) g = metric.Metric(twoform=form, components=(F, G)) # Check First-Order Terms assert str(metric.simplify_deriv_notation(D(F, t), g, max_order=1)) == "F'(t)" assert str(metric.simplify_deriv_notation(D(F, t), g, max_order=1, use_dots=True)) == r"\dot{F}(t)" # Check Second-Order Pure Terms assert str(metric.simplify_deriv_notation(D(D(F, t), t), g, max_order=2)) == "F''(t)" assert str(metric.simplify_deriv_notation(D(D(F, t), t), g, max_order=2, use_dots=True)) == r"\ddot{F}(t)" # Check Second-Order Mixed Terms assert str(metric.simplify_deriv_notation(D(D(G, t), t), g, max_order=2)) == "G_{t t}(t, x)" assert str(metric.simplify_deriv_notation(D(D(G, t), x), g, max_order=2)) == "G_{t x}(t, x)"
def riemann_tensor_component(rho: int, sig: int, mu: int, nu: int, metric: Metric, simplify_intermediate: bool = False) -> Expr: """Compute a component of the Riemann Tensor for a particular metric corresponding to R^r_smn = p_m G_ns^r - p_n G_ms^r + G_ml^r G_ns^l - G_nl^r G_ms^l Args: rho: int, Upper index of the Riemann Tensor sig: int, lower left index of Riemann Tensor mu: int, lower middle index of Riemann Tensor nu: int, lower right index of Riemann Tensor metric: Metric Returns: Expression of the R^r_smn """ def G(l, m, n): """Shorthand""" c = christoffel_symbol_component(l, m, n, metric) return sympy.simplify(c) if simplify_intermediate else c coord_symbols = metric.coord_system.base_symbols() dim = len(coord_symbols) return D(G(rho, nu, sig), coord_symbols[mu]) - \ D(G(rho, mu, sig), coord_symbols[nu]) + \ sum([G(rho, mu, lam) * G(lam, nu, sig) for lam in range(dim)]) - \ sum([G(rho, nu, lam) * G(lam, mu, sig) for lam in range(dim)])
def test_pde_separate_mul(): x, y, z, t = symbols("x,y,z,t") c = Symbol("C", real=True) Phi = Function('Phi') F, R, T, X, Y, Z, u = map(Function, 'FRTXYZu') r, theta, z = symbols('r,theta,z') # Something simple :) eq = Eq(D(F(x, y, z), x) + D(F(x, y, z), y) + D(F(x, y, z), z)) # Duplicate arguments in functions raises(ValueError, lambda: pde_separate_mul(eq, F(x, y, z), [X(x), u(z, z)])) # Wrong number of arguments raises(ValueError, lambda: pde_separate_mul(eq, F(x, y, z), [X(x), Y(y)])) # Wrong variables: [x, y] -> [x, z] raises(ValueError, lambda: pde_separate_mul(eq, F(x, y, z), [X(t), Y(x, y)])) assert pde_separate_mul(eq, F(x, y, z), [Y(y), u(x, z)]) == \ [D(Y(y), y)/Y(y), -D(u(x, z), x)/u(x, z) - D(u(x, z), z)/u(x, z)] assert pde_separate_mul(eq, F(x, y, z), [X(x), Y(y), Z(z)]) == \ [D(X(x), x)/X(x), -D(Z(z), z)/Z(z) - D(Y(y), y)/Y(y)] # wave equation wave = Eq(D(u(x, t), t, t), c**2 * D(u(x, t), x, x)) res = pde_separate_mul(wave, u(x, t), [X(x), T(t)]) assert res == [D(X(x), x, x) / X(x), D(T(t), t, t) / (c**2 * T(t))] # Laplace equation in cylindrical coords eq = Eq(1 / r * D(Phi(r, theta, z), r) + D(Phi(r, theta, z), r, 2) + 1 / r**2 * D(Phi(r, theta, z), theta, 2) + D(Phi(r, theta, z), z, 2)) # Separate z res = pde_separate_mul(eq, Phi(r, theta, z), [Z(z), u(theta, r)]) assert res == [ D(Z(z), z, z) / Z(z), -D(u(theta, r), r, r) / u(theta, r) - D(u(theta, r), r) / (r * u(theta, r)) - D(u(theta, r), theta, theta) / (r**2 * u(theta, r)) ] # Lets use the result to create a new equation... eq = Eq(res[1], c) # ...and separate theta... res = pde_separate_mul(eq, u(theta, r), [T(theta), R(r)]) assert res == [ D(T(theta), theta, theta) / T(theta), -r * D(R(r), r) / R(r) - r**2 * D(R(r), r, r) / R(r) - c * r**2 ] # ...or r... res = pde_separate_mul(eq, u(theta, r), [R(r), T(theta)]) assert res == [ r * D(R(r), r) / R(r) + r**2 * D(R(r), r, r) / R(r) + c * r**2, -D(T(theta), theta, theta) / T(theta) ]
# -*- coding: utf-8 -*- from sympy import * from sympy import Derivative as D var("x1 x2 y1 y2 l1 l2 m1 m2 th1 th2 dth1 dth2 ddth1 ddth2 t g tmp") sublist = [ (D(th1(t), t, t), ddth1), (D(th1(t), t), dth1), (D(th2(t), t, t), ddth2), (D(th2(t),t), dth2), (th1(t), th1), (th2(t), th2) ] x1 = l1*sin(th1(t)) y1 = -l1*cos(th1(t)) x2 = l1*sin(th1(t)) + l2*sin(th2(t)) y2 = -l1*cos(th1(t)) - l2*cos(th2(t)) vx1 = diff(x1, t) vx2 = diff(x2, t) vy1 = diff(y1, t) vy2 = diff(y2, t) # 拉格朗日量 L = m1/2*(vx1**2 + vy1**2) + m2/2*(vx2**2 + vy2**2) - m1*g*y1 - m2*g*y2 # 拉格朗日方程 def lagrange_equation(L, v): dvt = D(v(t), t)
print("the lagrange value is:\n") pprint(simplify(L)) print("\n") #define the function which can calculate lagrange equations def lagrange_equation(L, th): ddL_th = L.subs(D(th(t), t), tmp).diff(tmp, 1).subs(tmp, D(th(t), t)) dL_th = L.subs(D(th(t), t), tmp).subs(th(t), tmp2).diff(tmp2, 1).subs(tmp2, th(t)).subs(tmp, D(th(t), t)) L_equ = ddL_th.diff(t, 1) - dL_th #print("the lagrange equation of %r is:\n"%th) #pprint(simplify(L_equ)) #print("\n") return L_equ var("dth_b dth_w dw_w dw_b") sublist = [(D(th_b(t), t, t), dw_b), (D(th_b(t), t), dth_b), (D(th_w(t), t, t), dw_w), (D(th_w(t), t), dth_w)] equ_th_b = expand(simplify(lagrange_equation(L, th_b) - F_th_b)).subs(sublist) equ_th_w = expand(simplify(lagrange_equation(L, th_w) - F_th_w)).subs(sublist) val = solve((equ_th_b, equ_th_w), (dw_b, dw_w)) dw_b, dw_w = val[dw_b], val[dw_w] #pprint(dw_b)
def CouplerSolve(self): M = Matrix([[cos(self.sᵢ), -sin(self.sᵢ)],[sin(self.sᵢ), cos(self.sᵢ)]]) U = Matrix([self.u,self.v]) Xₒ = Matrix([self.xₒ,self.yₒ]) Ẋₒ = Matrix([D(self.xₒ,t),D(self.yₒ,t)]) Ẍₒ = Matrix([D(self.xₒ,t,2),D(self.yₒ,t,2)]) P = Xₒ + M*U Ω = Matrix([[0, -1],[1,0]]) Ṗ = Ẋₒ + D(self.sᵢ,t)*M*Ω*U Γ = Matrix([[D(self.sᵢ,t)**2, D(self.sᵢ,t,2)],[-D(self.sᵢ,t,2), D(self.sᵢ,t)**2]]) P̈ = simplify(Ẍₒ) - simplify(M*Γ*U) Pt = Ṗ; Ptt = P̈ for i in Ac: Ptt = Ptt.subs( D(i,t,2),Ac[i] ) Ptt = Ptt.subs( D(i,t),Vc[i] ) for i in Vc: Pt = Pt.subs( D(i,t),Vc[i] ) desloc = [latex(i) for i in P] veloc = [latex(i) for i in Pt] acel = [latex(i) for i in Ptt] d0 = desloc[0]; d1 = desloc[1] v0 = veloc[0]; v1 = veloc[1] a0 = acel[0]; a1 = acel[1] d0 = d0.replace(" ", ""); d1 = d1.replace(" ", "") v0 = v0.replace(" ", ""); v1 = v1.replace(" ", "") a0 = a0.replace(" ", ""); a1 = a1.replace(" ", "") A = B = C = E = G = H = 1 while (A > 0 or B > 0 or C > 0 or E > 0 or G > 0 or H > 0): A = d0.find('{\\left(t'); B = d1.find('{\\left(t') C = v0.find('{\\left(t'); E = v1.find('{\\left(t') G = a0.find('{\\left(t'); H = a1.find('{\\left(t') if A > 0: d0 = d0[:A]+d0[(A+16):] if B > 0: d1 = d1[:B]+d1[(B+16):] if C > 0: v0 = v0[:C]+v0[(C+16):] if E > 0: v1 = v1[:E]+v1[(E+16):] if G > 0: a0 = a0[:G]+a0[(G+16):] if H > 0: a1 = a1[:H]+a1[(H+16):] display(Markdown("### Deslocamentos")) display(Markdown("$ \qquad x_p $ = $ %s $" %d0)) display(Markdown("$ \qquad y_p $ = $ %s $" %d1)) display(Markdown("### Velocidades")) display(Markdown("$ \qquad \dot x_p $ = $ %s $" %v0)) display(Markdown("$ \qquad \dot y_p $ = $ %s $" %v1)) display(Markdown("### Acelerações")) display(Markdown("$ \qquad \ddot x_p $ = $ %s $" %a0)) display(Markdown("$ \qquad \ddot y_p $ = $ %s $" %a1))
print(result) print(f(x * x).diff(x)) print(g.diff(x)) aaa = g.diff(x, 2) # aaa._args[1] * aaa._args[0] # init_printing() pprint(Integral(sqrt(1 / x), x), use_unicode=True) print(latex(Integral(sqrt(1 / x), x))) print("\n\n") pprint(g.diff((x, 1), (y, 0)), use_unicode=True) # pprint(g.diff((x, 2),(y, 2)), use_unicode=True) pprint(sym.diff(sym.tan(x), x)) pprint(sym.diff(g, x)) print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") from sympy import Derivative as D, collect, Function from sympy.abc import a, b, c, x, y, z f = Function('f')(x) pprint(a * D(D(f, x), x)**2 + b * D(D(f, x), x)**2) pprint(collect(a * D(D(f, x), x)**2 + b * D(D(f, x), x)**2, D(f, x))) # print("----------------------------------------------------") # pprint(deriv.doit().subs({F: Symbol('F')}))
# -*- coding: utf-8 -*- from sympy import * from sympy import Derivative as D var("x1 x2 y1 y2 l1 l2 m1 m2 th1 th2 dth1 dth2 ddth1 ddth2 t g tmp") sublist = [(D(th1(t), t, t), ddth1), (D(th1(t), t), dth1), (D(th2(t), t, t), ddth2), (D(th2(t), t), dth2), (th1(t), th1), (th2(t), th2)] x1 = l1 * sin(th1(t)) y1 = -l1 * cos(th1(t)) x2 = l1 * sin(th1(t)) + l2 * sin(th2(t)) y2 = -l1 * cos(th1(t)) - l2 * cos(th2(t)) vx1 = diff(x1, t) vx2 = diff(x2, t) vy1 = diff(y1, t) vy2 = diff(y2, t) # 拉格朗日量 L = m1 / 2 * (vx1**2 + vy1**2) + m2 / 2 * (vx2**2 + vy2**2) - m1 * g * y1 - m2 * g * y2 # 拉格朗日方程 def lagrange_equation(L, v): dvt = D(v(t), t) a = L.subs(dvt, tmp).diff(tmp).subs(tmp, dvt) b = L.subs(dvt, tmp).subs(v(t), v).diff(v).subs(v, v(t)).subs(tmp, dvt) c = a.diff(t) - b
def test_pde_separate(): x, y, z, t = symbols("x,y,z,t") F, T, X, Y, Z, u = map(Function, "FTXYZu") eq = Eq(D(u(x, t), x), D(u(x, t), t) * exp(u(x, t))) raises(ValueError, lambda: pde_separate(eq, u(x, t), [X(x), T(t)], "div"))
def test_euler_high_order(): # an example from hep-th/0309038 m = Symbol('m') k = Symbol('k') x = Function('x') y = Function('y') t = Symbol('t') L = (m * D(x(t), t)**2 / 2 + m * D(y(t), t)**2 / 2 - k * D(x(t), t) * D(y(t), t, t) + k * D(y(t), t) * D(x(t), t, t)) assert euler(L, [x(t), y(t)]) == [ Eq(2 * k * D(y(t), t, t, t) - m * D(x(t), t, t)), Eq(-2 * k * D(x(t), t, t, t) - m * D(y(t), t, t)) ] w = Symbol('w') L = D(x(t, w), t, w)**2 / 2 assert euler(L) == [Eq(D(x(t, w), t, t, w, w))]
def test_euler_pendulum(): x = Function("x") t = Symbol("t") L = D(x(t), t)**2 / 2 + cos(x(t)) assert euler(L, x(t), t) == [Eq(-sin(x(t)) - D(x(t), t, t), 0)]
def test_euler_high_order(): # an example from hep-th/0309038 m = Symbol("m") k = Symbol("k") x = Function("x") y = Function("y") t = Symbol("t") L = (m * D(x(t), t)**2 / 2 + m * D(y(t), t)**2 / 2 - k * D(x(t), t) * D(y(t), t, t) + k * D(y(t), t) * D(x(t), t, t)) assert euler(L, [x(t), y(t)]) == [ Eq(2 * k * D(y(t), t, t, t) - m * D(x(t), t, t), 0), Eq(-2 * k * D(x(t), t, t, t) - m * D(y(t), t, t), 0), ] w = Symbol("w") L = D(x(t, w), t, w)**2 / 2 assert euler(L) == [Eq(D(x(t, w), t, t, w, w), 0)]
def test_euler_pendulum(): x = Function('x') t = Symbol('t') L = D(x(t), t)**2 / 2 + cos(x(t)) assert euler(L, x(t), t) == [Eq(-sin(x(t)) - D(x(t), t, t))]
for i in ['u','b']: for j in ['x','y','z']: globals() [i +'1'+j] = Symbol(i +'1'+j) p0 = Symbol("p0") b0x = Function("b0x")(x,y,z,t);b0y = Function("b0y")(x,y,z,t) b0z = Function("b0z")(x,y,z,t);u0x = Function("u0x")(x,y,z,t) u0y = Function("u0y")(x,y,z,t);u0z = Function("u0z")(x,y,z,t) f0 = Function("f0")(x,y,z,t);f1 = Function("f1")(x,y,z,t) f2 = Function("f2")(x,y,z,t);rho0 = Function("rho0")(x,y,z,t) f0_2 = Function("f0_2")(x,y,z,t);f1_2 = Function("f1_2")(x,y,z,t) f2_2 = Function("f2_2")(x,y,z,t) g1 = Symbol("g1");Ax = Function("Ax")(x,y,z,t) Ay = Function("Ay")(x,y,z,t);Az = Function("Az")(x,y,z,t) Bx = Function("Bx")(x,y,z,t);By = Function("By")(x,y,z,t) Bz = Function("Bz")(x,y,z,t) AgradB =((Ax*D(Bx,x)+Ay*D(Bx,y)+Az*D(Bx,z))*C.i+ (Ax*D(By,x)+Ay*D(By,y)+Az*D(By,z))*C.j +(Ax*D(Bz,x)+Ay*D(Bz,y)+Az*D(Bz,z))*C.k) U0,qRm,qRe,qRo,qFr,chi,qAl,qRmm = symbols("U0,qRm,qRe,qRo,qFr,chi,qAl,qRmm",real = True) u0 = u0x*C.i + u0y*C.j + u0z*C.k b0 = b0x*C.i + b0y*C.j + b0z*C.k ansatz0=exp(I*(omega*t+kxl*x+kyl*y+kz*z)) g = -g0*C.k f = f0 + e*f1 #+ f2*e**2 Uncomment for smaller scales of topography # Vector normal and tangential to the topography delf = de(f) nfo= delf/sqrt((delf&C.i)**2+(delf&C.j)**2+(delf&C.k)**2)