sympy

How do you evaluate a derivative in python?

只谈情不闲聊 提交于 2019-12-22 07:22:09
问题 I'm a beginner in python. I've recently learned about Sympy and its symbolic manipulation capabilities, in particular, differentiation. I am trying to do the following in the easiest way possible: Define f(x,y) = x^2 + xy^2. Differentiate f with respect to x. So f'(x,y) = 2x + xy^2. Evaluate the derivative, e.g., f'(1,1) = 2 + 1 = 3. I know how to do 1 and 2. The problem is, when I try to evaluate the derivative in step 3, I get an error that python can't calculate the derivative. Here is a

Generate Fortran subroutine with SymPy codegen for a system of equations

こ雲淡風輕ζ 提交于 2019-12-22 04:13:08
问题 Building on a former example that I've found here, I try to find out how to generate a Fortran code that correspond to a specific form that I need to stick to. The required FORTRAN code will look like this (it is based on the FitzHugh–Nagumo model): SUBROUTINE FF(NE,U,PAR,F) ! ---------- -- ! Define the nonlinear term IMPLICIT NONE INTEGER, INTENT(IN) :: NE DOUBLE PRECISION, INTENT(IN) :: U(NE),PAR(*) DOUBLE PRECISION, INTENT(OUT) :: F(NE) DOUBLE PRECISION u,v,e,a1,a0 u=U(1) v=U(2) e=PAR(1)

Is there a vectorized way to calculate the gradient in sympy?

自古美人都是妖i 提交于 2019-12-22 04:04:14
问题 How does one calculate the (symbolic) gradient of a multivariate function in sympy? Obviously I could calculate separately the derivative for each variable, but is there a vectorized operation that does this? For example m=sympy.Matrix(sympy.symbols('a b c d')) Now for i=0..3 I can do: sympy.diff(np.sum(m*m.T),m[i]) which will work, but I rather do something like: sympy.diff(np.sum(m*m.T),m) Which does not work ("AttributeError: ImmutableMatrix has no attribute _diff_wrt"). 回答1: Just use a

Add multiplication signs (*) between coefficients

只谈情不闲聊 提交于 2019-12-22 03:44:59
问题 I have a program in which a user inputs a function, such as sin(x)+1 . I'm using ast to try to determine if the string is 'safe' by whitelisting components as shown in this answer. Now I'd like to parse the string to add multiplication ( * ) signs between coefficients without them. For example: 3x -> 3*x 4(x+5) -> 4*(x+5) sin(3x)(4) -> sin(3x)*(4) ( sin is already in globals, otherwise this would be s*i*n*(3x)*(4) Are there any efficient algorithms to accomplish this? I'd prefer a pythonic

Expand index notation equation using sympy

跟風遠走 提交于 2019-12-21 20:05:28
问题 Below I have an equation written using index notation. This equation can be expressed with the six equations on the figure. The first equation is expanded using index notation (einstein notation: https://en.wikipedia.org/wiki/Einstein_notation). In U_k,k the comma is a convention for derivative. Since we have repeated indices (k,k) we apply the summation convention and get (du_1/dx_1 + du_2/dx_2 + du_3/dx_3). On the figure the terms u_1, u_2 and u_3 are written as u, v and w and they are

Lambdify works with Python, but throws an exception with Cython

我们两清 提交于 2019-12-21 20:04:59
问题 My website runs this Python script that would be way more optimized if Cython is used. Recently I needed to add Sympy with Lambdify, and this is not going well with Cython. So I stripped the problem to a minimum working example. In the code, I have a dictionary with string keys with values that are lists. I would like to use these keys as variables. In the following simplified example, there's only 1 variable, but generally I need more. Please check the following example: import numpy as np

Enforce custom ordering on Sympy print

孤者浪人 提交于 2019-12-21 12:22:53
问题 SymPy does a wonderful work keeping track of all the operations I do to my symbolic expressions. But a the moment of printing the result for latex output I would like to enforce a certain ordering of the term. This is just for convention, and unfortunately that convention is not alphabetical on the symbol name(as reasonably sympy does) import sympy as sp sp.init_printing() U,tp, z, d = sp.symbols('U t_\perp z d') # do many operations with those symbols # the final expression is: z+tp**2+U+U/

How to parse and simplify a string like '3cm/µs² + 4e-4 sqmiles/km/h**2' treating physical units correctly?

混江龙づ霸主 提交于 2019-12-21 09:15:36
问题 I'd like to split a string like 3cm/µs² + 4e-4 sqmiles/km/h**2 into its SI unit (in this case, m/s**2 ) and its magnitude (in multiples of that unit). Since sympy provides both a parsing module and many physical units and SI prefixes, I guess using sympy would be a good idea. But what is a nice way to achieve this? I'd write an algorithm like the following, but I'd like to avoid reinventing a squared wheel: Treat the transition between a number and a letter (except for the 4e-4 like syntax)

Non-sequential substitution in SymPy

余生颓废 提交于 2019-12-21 03:38:40
问题 I'm trying to use [SymPy][1] to substitute multiple terms in an expression at the same time. I tried the [subs function][2] with a dictionary as parameter, but found out that it substitutes sequentially. In : a.subs({a:b, b:c}) Out: c The problem is the first substitution resulted in a term that can be substituted by the second substitution, but it should not (for my cause). Any idea on how to perform the substitutions simultaneously, without them interfering with each other? Edit: This is a

How to perform non-linear optimization with scipy/numpy or sympy?

随声附和 提交于 2019-12-20 12:39:22
问题 I am trying to find the optimal solution to the follow system of equations in Python: (x-x1)^2 + (y-y1)^2 - r1^2 = 0 (x-x2)^2 + (y-y2)^2 - r2^2 = 0 (x-x3)^2 + (y-y3)^2 - r3^2 = 0 Given the values a point(x,y) and a radius (r): x1, y1, r1 = (0, 0, 0.88) x2, y2, r2 = (2, 0, 1) x3, y3, r3 = (0, 2, 0.75) What is the best way to find the optimal solution for the point (x,y) Using the above example it would be: ~ (1, 1) 回答1: If I understand your question correctly, I think this is what you're after