sympy

How to derive with respect to a Matrix element with Sympy

风格不统一 提交于 2019-12-10 14:10:28
问题 Given the product of a matrix and a vector A.v with A of shape (m,n) and v of dim n, where m and n are symbols, I need to calculate the Derivative with respect to the matrix elements. I haven't found the way to use a proper vector, so I started with 2 MatrixSymbol : n, m = symbols('n m') j = tensor.Idx('j') i = tensor.Idx('i') l = tensor.Idx('l') h = tensor.Idx('h') A = MatrixSymbol('A', n,m) B = MatrixSymbol('B', m,1) C=A*B Now, if I try to derive with respect to one of A's elements with the

SymPy: Swap two variables

旧街凉风 提交于 2019-12-10 13:44:18
问题 In an expression like import sympy a = sympy.Symbol('a') b = sympy.Symbol('b') x = a + 2*b I'd like to swap a and b to retrieve b + 2*a . I tried y = x.subs([(a, b), (b, a)]) y = x.subs({a: b, b: a}) but neither works; the result is 3*a in both cases as b , for some reason, gets replaced first. Any hints? 回答1: There is a simultaneous argument you can pass to the substitution, which will ensure that all substitutions happen simultaneously and don't interfere with one another as they are doing

Sympy second order ode

社会主义新天地 提交于 2019-12-10 13:35:01
问题 I have a homogeneous solution to a simple second-order ODE, which when I try to solve for initial values using Sympy, returns the same solution. It should substitute for y(0) and y'(0) and yield a solution without constants, but does not. Here is the code to set up the equation (it is a spring balance equation, k = spring constant and m = mass). Some redundant symbols which I use elsewhere, sorry. %matplotlib inline from sympy import * m,k, x,t,s,T, omega,A,B = symbols('m k x t s T omega A B'

In sympy plotting, how can I get a plot with a fixed aspect ratio?

我与影子孤独终老i 提交于 2019-12-10 13:19:59
问题 If I plot a circle with this snippet from sympy import * x, y = symbols('x y') p1 = plot_implicit(Eq(x**2 +y**2, 1),aspect_ratio=(1.,1.)) I will get a figure window like this one Now the aspect ratio is not what I was expecting because I see an ellipse instead of a circle. Moreover, if I change the aspect ratio of the window (dragging the bottom-right corner of the window) I get also a change in the aspect ratio of the plot... The following image is what I get after dragging the corner in

Dealing with piecewise equations returned by sympy integrate

不羁岁月 提交于 2019-12-10 12:34:17
问题 In sympy I have an integral which returns a Piecewise object, e.g. In [2]: from sympy.abc import x,y,z In [3]: test = exp(-x**2/z**2) In [4]: itest = integrate(test,(x,0,oo)) In [5]: itest Out[5]: ⎧ ___ ⎪ ╲╱ π ⋅z │ ⎛ 1 ⎞│ π ⎪ ─────── for │periodic_argument⎜──────────────, ∞⎟│ ≤ ─ ⎪ 2 │ ⎜ 2 ⎟│ 2 ⎪ │ ⎝polar_lift (z) ⎠│ ⎪ ⎪∞ ⎪⌠ ⎨⎮ 2 ⎪⎮ -x ⎪⎮ ─── ⎪⎮ 2 ⎪⎮ z ⎪⎮ ℯ dx otherwise ⎪⌡ ⎪0 ⎩ I would like to extract just the first branch of this piecewise equation, in other words, I would like to be able to

Is it possible to index numpy array with sympy symbols?

会有一股神秘感。 提交于 2019-12-10 11:30:03
问题 Helle I want to do some summation on a numpy array like this import numpy as np import sympy as sy import cv2 i, j = sy.symbols('i j', Integer=True) #next read some grayscale image to create a numpy array of pixels a = cv2.imread(filename) b = sy.summation(sy.summation(a[i][j], (i,0,1)), (j,0,1)) #double summation but I'm facing with an error. is it possible to handle numpy symbols as numpy arrays'indexes? if not can you sugest me a solution? Thanks. 回答1: You can't use numpy object directly

Python Sympy printing differentiated user defined composite function; how to toggle substitution

旧巷老猫 提交于 2019-12-10 11:16:01
问题 I want to get rid of an extra substitution which sympy makes when differentiating a user defined composite function. The code is t = Symbol('t') u = Function('u') f = Function('f') U = Symbol('U') pprint(diff(f(u(t),t),t)) The output is: d d ⎛ d ⎞│ ──(f(u(t), t)) + ──(u(t))⋅⎜───(f(ξ₁, t))⎟│ dt dt ⎝dξ₁ ⎠│ξ₁=u(t) I guess it does this because you can't differentiate w.r.t u(t), so this is ok. What I want to do next is to substitute u(t) with an other variable say U and then get rid of the extra

How to replace the the diagonal elements of a matrix by a vector in SymPy?

大城市里の小女人 提交于 2019-12-10 10:14:54
问题 I have a vector X which I created like this: from sympy import * x1 = Symbol('x1') x2 = Symbol('x2') x3 = Symbol('x3') X = Matrix([x1, x2, x3]) Then I also have a matrix myMat which just contains ones: myMat = ones(3, 3) Matrix([ [1, 1, 1], [1, 1, 1], [1, 1, 1]]) Now I would like to replace the diagonal of the matrix by my vector X ; my desired outcome looks like this: Matrix([ [x1, 1, 1], [1, x2, 1], [1, 1, x3]]) I can of course do it in a for-loop like this: for ind, el in enumerate(X):

Does sympy give me wrong results for the second quantization commutators?

て烟熏妆下的殇ゞ 提交于 2019-12-10 10:09:41
问题 I am using the following code in sympy : from sympy.physics.secondquant import F, Fd, NO, Commutator from sympy import symbols a, b, c, d = symbols("a,b,c,d") comm = NO(Commutator( Fd(a) * F(b), Fd(c) * F(d) ).doit()) print(comm) # gives me 0 Clearly, this should not be zero. Well, maybe I do not understand sympy . Here is what I want to calculate: [F†_a F_b, F†_c F_d] with not necessarily equal indices a, b, c and d. 来源: https://stackoverflow.com/questions/52227318/does-sympy-give-me-wrong

Lambdify A Parametric Integral

天涯浪子 提交于 2019-12-10 10:07:15
问题 I have the following issue: I want to lambdify a sympy expression containing parametric integrals like Integral(tanh(a*x),(x,0,1)) . I tried to do a manual implementation like here. What we want is essentially that the integral gets converted to something like: lambda theta: quad(lambda x: g(x,theta), a,b)[0] where g = sp.lambdify((x,param), f, modules='numpy')) Consider the following MWE: import sympy as sp import numpy as np from scipy.integrate import quad def integral_as_quad(function,