sympy

Differential Operator usable in Matrix form, in Python module Sympy

倖福魔咒の 提交于 2019-11-29 11:07:26
We need two matrices of differential operators [B] and [C] such as: B = sympy.Matrix([[ D(x), D(y) ], [ D(y), D(x) ]]) C = sympy.Matrix([[ D(x), D(y) ]]) ans = B * sympy.Matrix([[x*y**2], [x**2*y]]) print ans [x**2 + y**2] [ 4*x*y] ans2 = ans * C print ans2 [2*x, 2*y] [4*y, 4*x] This could also be applied to calculate the curl of a vector field like: culr = sympy.Matrix([[ D(x), D(y), D(z) ]]) field = sympy.Matrix([[ x**2*y, x*y*z, -x**2*y**2 ]]) To solve this using Sympy the following Python class had to be created: import sympy class D( sympy.Derivative ): def __init__( self, var ): super( D

How to substitute multiple symbols in an expression in sympy?

浪尽此生 提交于 2019-11-29 09:20:22
Assigning a variable directly does not modify expressions that used the variable retroactively. >>> from sympy import Symbol >>> x = Symbol('x') >>> y = Symbol('y') >>> f = x + y >>> x = 0 >>> f x + y To substitute several values: >>> from sympy import Symbol >>> x, y = Symbol('x y') >>> f = x + y >>> f.subs({x:10, y: 20}) >>> f 30 Actually sympy is designed not to substitute values until you really want to substitute them with subs (see http://docs.sympy.org/latest/tutorial/basic_operations.html ) Try f.subs({x:0}) f.subs(x, 0) # as alternative instead of x = 0 The command x = Symbol('x')

How to evaluate the constants SymPy gives with initial condition?

十年热恋 提交于 2019-11-29 07:56:21
How can I evaluate the constants C1 and C2 from a solution of a differential equation SymPy gives me? There are the initial condition f(0)=0 and f(pi/2)=3. >>> from sympy import * >>> f = Function('f') >>> x = Symbol('x') >>> dsolve(f(x).diff(x,2)+f(x),f(x)) f(x) == C1*sin(x) + C2*cos(x) I tried some ics stuff but it's not working. Example: >>> dsolve(f(x).diff(x,2)+f(x),f(x), ics={f(0):0, f(pi/2):3}) f(x) == C1*sin(x) + C2*cos(x) By the way: C2 = 0 and C1 = 3. There's a pull request implementing initial/boundary conditions, which was merged and should be released in SymPy 1.2. Meanwhile, one

How to serialize sympy lambdified function?

↘锁芯ラ 提交于 2019-11-29 06:59:45
The title says it all. Is there any way to serialize a function generated by sympy.lambdify?: import sympy as sym import pickle import dill a, b = sym.symbols("a, b") expr = sym.sin(a) + sym.cos(b) lambdified_expr = sym.lambdify((a, b), expr, modules="numpy") pickle.dumps(lambdified_expr) # won't work dill.dumps(lambdified_expr) # won't work either ... The reason I want to do this is because my code generates so many lambdified functions but I found it takes too long every time. Mike McKerns You actually can use dill to pickle it. The most recent versions of dill (e.g. on github) has "settings

Setting Assumptions on Variables in Sympy Relative to Other Variables

混江龙づ霸主 提交于 2019-11-29 04:16:46
I know that sympy in python can set assumptions on variables, such as x is positive, negative, real, complex, etc. I was wondering if sympy can set assumptions on variables relative to other variables. For example, if I have variables x and y, can I set sympy to assume that x > y in its solutions. Or, alternatively, if I have two variables, a and B, can I set sympy to assume that a + 2B < 1? These sorts of assumptions would possibly help sympy simplify complicated solutions to solve() and eigenvectors. I've looked all over and haven't found information pertaining to setting these kinds of

Convert sympy expressions to function of numpy arrays

拟墨画扇 提交于 2019-11-29 03:50:17
I have a system of ODEs written in sympy: from sympy.parsing.sympy_parser import parse_expr xs = symbols('x1 x2') ks = symbols('k1 k2') strs = ['-k1 * x1**2 + k2 * x2', 'k1 * x1**2 - k2 * x2'] syms = [parse_expr(item) for item in strs] I would like to convert this into a vector valued function, accepting a 1D numpy array of the x value, a 1D numpy array of the k values, returning a 1D numpy array of the equations evaluated at those points. The signature would look something like this: import numpy as np x = np.array([3.5, 1.5]) k = np.array([4, 2]) xdot = my_odes(x, k) The reason I want

How to define a mathematical function in SymPy?

南楼画角 提交于 2019-11-28 22:33:24
问题 I've been trying this now for hours. I think I don't understand a basic concept, that's why I couldn't answer this question to myself so far. What I'm trying is to implement a simple mathematical function, like this: f(x) = x**2 + 1 After that I want to derive that function. I've defined the symbol and function with: x = sympy.Symbol('x') f = sympy.Function('f')(x) Now I'm struggling with defining the equation to this function f(x) . Something like f.exp("x**2 + 1") is not working. I also

SymPy - Arbitrary number of Symbols

谁说胖子不能爱 提交于 2019-11-28 20:27:54
I am coding a function that solves an arbitrary number of simultaneous equations. The number of equations is set by one of the parameters of the function and each equation is built from a number of symbols - as many symbols as there are equations. This means that I can't simply hardcode the equations, or even the symbols needed to put together the equations; the function needs to be able to handle any number of equations. So, my question is, how do I produce a list of symbols? I have one possible solution, but my gut tells me that it's not going to be very efficient. Please let me know if

How to pretty print in ipython notebook via sympy?

久未见 提交于 2019-11-28 20:05:41
I tried pprint , print , the former only prints Unicode version, and the latter doesn't do pretty prints. from sympy import symbols, Function import sympy.functions as sym from sympy import init_printing init_printing(use_latex=True) from sympy import pprint from sympy import Symbol x = Symbol('x') # If a cell contains only the following, it will render perfectly. (pi + x)**2 # However I would like to control what to print in a function, # so that multiple expressions can be printed from a single notebook cell. pprint((pi + x)**2) you need to use display: from IPython.display import display

How to extract all coefficients in sympy

99封情书 提交于 2019-11-28 18:37:51
You can get a coefficient of a specific term by using coeff(); x, a = symbols("x, a") expr = 3 + x + x**2 + a*x*2 expr.coeff(x) # 2*a + 1 Here I want to extract all the coefficients of x, x**2 (and so on), like; # for example expr.coefficients(x) # want {1: 3, x: (2*a + 1), x**2: 1} There is a method as_coefficients_dict(), but it seems this doesn't work in the way I want; expr.as_coefficients_dict() # {1: 3, x: 1, x**2: 1, a*x: 2} expr.collect(x).as_coefficients_dict() # {1: 3, x**2: 1, x*(2*a + 1): 1} The easiest way is to use Poly >>> a = Poly(expr, x) >>> a.coeffs() [1, 2*a + 1, 3] all