sympy

Calculate curl of a vector field in Python and plot it with matplotlib

不羁岁月 提交于 2019-12-03 06:12:57
I need to calculate the curl of a vector field and plot it with matplotlib. A simple example of what I am looking for could be put like that: How can I calculate and plot the curl of the vector field in the quiver3d_demo.py in the matplotlib gallery? You can use sympy.curl() to calculate the curl of a vector field. Example : Suppose F (x,y,z) = y 2 z i - xy j + z 2 k , then: y would be R[1] , x is R[0] and z is R[2] the unit vectors i , j , k of the 3 axes, would be respectively R.x , R.y , R.z . The code to calculate the vector field curl is: from sympy.physics.vector import ReferenceFrame

How can I get a list of the symbols in a sympy expression?

十年热恋 提交于 2019-12-03 05:22:07
For example, if I run import sympy x, y, z = sympy.symbols('x:z') f = sympy.exp(x + y) - sympy.sqrt(z) is there any method of f that I can use to get a list or tuple of sympy.Symbol objects that the expression contains? I'd rather not have to parse srepr(f) or parse downward through f.args . In this case, g.args[0].args[1].args[0] gives me Symbol("z") , while g.args[1].args[0].args gives me the tuple (Symbol("x"), Symbol("y")) , but obviously these are expression-specific. You can use: f.free_symbols which will return a set of all free symbols. Example: >>> import sympy >>> x, y, z = sympy

What is the best way to convert a SymPy matrix to a numpy array/matrix

江枫思渺然 提交于 2019-12-03 05:16:20
I am not sure if the approach I've been using in sympy to convert a MutableDenseMatrix to a numpy.array or numpy.matrix is a good current practice. I have a symbolic matrix like: g = sympy.Matrix( [[ x, 2*x, 3*x, 4*x, 5*x, 6*x, 7*x, 8*x, 9*x, 10*x] [x**2, x**3, x**4, x**5, x**6, x**7, x**8, x**9, x**10, x**11]] ) and I am converting to a numpy.array doing: g_func = lambda val: numpy.array( g.subs( {x:val} ).tolist(), dtype=float ) where I get an array for a given value of x . Is there a better built-in solution in SymPy to do that? Thank you! This looks like the most straightforward: np.array

Python solve equation for one variable

南楼画角 提交于 2019-12-03 04:23:40
I'm trying to solve an equation in python using SymPy. I have a generated equation (something like function = y(8.0-(y**3.0)) which I use with SymPy to create a new equation like this: eq = sympy.Eq(function, 2) which outputs y(8.0-(y**3.0)) == 2 . but sympy.solve(eq) doesn't seem to work. >>> from sympy import Eq, Symbol as sym, solve >>> y = sym('y') >>> eqa = Eq(y(8.0-(y**3.0)), 8) >>> solve(eqa) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/pymodules/python2.6/sympy/solvers/solvers.py", line 332, in solve result = tsolve(f, *symbols) File "/usr/lib

What is the difference between SymPy and Sage?

蹲街弑〆低调 提交于 2019-12-03 03:41:40
问题 What is the difference between SymPy and Sage a.k.a. SageMath? 回答1: (Full disclosure: I am the lead developer of SymPy) The first thing you should understand is that SymPy and Sage are not quite the same thing. SymPy is a pure Python library, that does computer algebra. Sage is a collection of open source mathematical software. Sage tries to gather together all the major open source mathematics software, and glue it together into a useful system. In fact, Sage includes SymPy as one of its

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

℡╲_俬逩灬. 提交于 2019-12-03 03:09:07
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) If I understand your question correctly, I think this is what you're after: from scipy.optimize import minimize import numpy as np def f(coord,x,y,r): return np.sum( ((coord[0] - x)*

What is the difference between SymPy and Sage?

孤者浪人 提交于 2019-12-02 17:07:40
What is the difference between SymPy and Sage a.k.a. SageMath? (Full disclosure: I am the lead developer of SymPy) The first thing you should understand is that SymPy and Sage are not quite the same thing. SymPy is a pure Python library, that does computer algebra. Sage is a collection of open source mathematical software. Sage tries to gather together all the major open source mathematics software, and glue it together into a useful system. In fact, Sage includes SymPy as one of its systems. Here is a short list of (biased) facts for each (I won't call them pros or cons, just facts): SymPy

sympy autowrap (cython): limit of # of arguments, arguments in array form?

梦想的初衷 提交于 2019-12-02 12:54:16
问题 I have the following issue: I want to use autowrap to generate a compiled version of a sympy matrix, with cells containing sympy expressions. Depending on the specification of my problem, the number of arguments can get very large. I ran into the following 2 issues: The number of arguments that autowrap accepts seems to be limited to 509. i.e., this works: import sympy from sympy.utilities.autowrap import autowrap x = sympy.symbols("x:509") exp = sum(x) cyt = autowrap(exp, backend="cython",

How to read a system of differential equations from a text file to solve the system with scipy.odeint?

≡放荡痞女 提交于 2019-12-02 12:31:02
问题 I have a large (>2000 equations) system of ODE's that I want to solve with python scipy's odeint. I have three problems that I want to solve (maybe I will have to ask 3 different questions?). For simplicity, I will explain them here with a toy model, but please keep in mind that my system is large. Suppose I have the following system of ODE's: dS/dt = -beta*S dI/dt = beta*S - gamma*I dR/dt = gamma*I with beta = c p I where c, p and gamma are parameters that I want to pass to odeint. odeint is

How to read a system of differential equations from a text file to solve the system with scipy.odeint?

戏子无情 提交于 2019-12-02 07:44:05
I have a large (>2000 equations) system of ODE's that I want to solve with python scipy's odeint. I have three problems that I want to solve (maybe I will have to ask 3 different questions?). For simplicity, I will explain them here with a toy model, but please keep in mind that my system is large. Suppose I have the following system of ODE's: dS/dt = -beta*S dI/dt = beta*S - gamma*I dR/dt = gamma*I with beta = c p I where c, p and gamma are parameters that I want to pass to odeint. odeint is expecting a file like this: def myODEs(y, t, params): c,p, gamma = params beta = c*p S = y[0] I = y[1]