sympy

Python optimization using sympy lambdify and scipy

淺唱寂寞╮ 提交于 2019-12-07 10:47:29
问题 I'm trying to maximize a function defined by sympy but cannot make it work. The basic idea can be summarized as follows: import sympy from scipy.optimize import minimize from sympy.utilities.lambdify import lambdify a,b,G = sympy.symbols('a b G') func = (G - a)**2 + b my_func = lambdify((G,a,b), -1*func) results = minimize(my_func,[0.1,0.1,0.1]) The code works if I define a single-variable function, but as long as I have more than one variable, I receive the following error message. TypeError

Evaluating Jacobian at specific points using sympy

二次信任 提交于 2019-12-07 08:42:46
问题 I am trying to evaluate the Jacobian at (x,y)=(0,0) but unable to do so. import sympy as sp from sympy import * import numpy as np x,y=sp.symbols('x,y', real=True) J = Function('J')(x,y) f1=-y f2=x - 3*y*(1-x**2) f1x=diff(f1,x) f1y=diff(f1,y) f2x=diff(f2,x) f2y=diff(f2,y) J=np.array([[f1x,f1y],[f2x,f2y]]) J1=J(0,0) print J1 The error corresponding to ---> 16 J1=J(0,0) is TypeError: 'numpy.ndarray' object is not callable 回答1: The error you're getting is indeed because you're rebinding J to a

Printing greek letters using sympy in text

非 Y 不嫁゛ 提交于 2019-12-07 06:12:01
问题 Lets say I want to print something like "I am pi" where pi should really be the greek letter pi. With sympy I can do import sympy from sympy.abc import pi sympy.pprint(pi) which gives the greek letter pi, but I have problems putting this into a text. For example sympy.pprint("I am"+pi) obviously doesn't work. I can convert the text to a sympy symbol sympy.Symbol('I am'), but then I will get I am+pi 回答1: You want pretty() , which is the same as pprint , but it returns a string instead of

Passing sympy lambda to multiprocessing.Pool.map

霸气de小男生 提交于 2019-12-07 05:56:55
问题 I want to execute a sympy lambda function in parallel. I don't know: why it works in parallel although it is a lambda function why it stops working when I try executing without the pool why it works if I uncomment the first return in lambdify And apparently the markdown preprocessor needs a line of text above the code so this is the code: from multiprocessing import Pool import sympy from sympy.abc import x def f(m): return m.lambdify()(1) class Mult(): def lambdify(self): # return sympy

How to lambdify a SymPy expression containing the erf function for use with NumPy

…衆ロ難τιáo~ 提交于 2019-12-07 05:12:52
问题 I would like to lambdify a symbolic expression containing the erf function with SymPy. This can be done for scalar arguments as follows: log_normal = 0.5 + 0.5 * sym.erf((sym.log(x) - mu) / sym.sqrt(2 * sigma**2)) F = sym.lambdify([x, mu, sigma], log_normal) F(1.0, 0.0, 1.0) I would like to vectorize the above. Normally I would do as follows... log_normal = 0.5 + 0.5 * sym.erf((sym.log(x) - mu) / sym.sqrt(2 * sigma**2)) vector_F = sym.lambdify([x, mu, sigma], log_normal, modules='numpy')

What does `S` signify in sympy

这一生的挚爱 提交于 2019-12-07 03:01:22
问题 I am new to sympy , I can't figure out from sympy.core import S What actually S is? And what does S.true mean? 回答1: There's a bit of confusion because S is actually two things. The first thing it is is the SingletonRegistry . Several classes in SymPy appear so often that they are singletonized , that is, using some metaprogramming they are made so that they can only be instantiated once. For instance, every time you create Integer(0) , this will return the same instance, Zero . All singleton

Optimize code generated by sympy

妖精的绣舞 提交于 2019-12-07 02:21:50
问题 Using SymPy to find a derivative (see this question: https://math.stackexchange.com/questions/726104/apply-chain-rule-to-vector-function-with-chained-dot-and-cross-product ), I came up with this code: from sympy import * from sympy.physics.mechanics import * from sympy.printing import print_ccode from sympy.utilities.codegen import codegen x1, x2, x3 = symbols('x1 x2 x3') y1, y2, y3 = symbols('y1 y2 y3') z1, z2, z3 = symbols('z1 z2 z3') u = ReferenceFrame('u') u1=(u.x*x1 + u.y*y1 + u.z*z1) u2

Sympy: Drop higher order terms in polynomial

▼魔方 西西 提交于 2019-12-07 01:35:50
问题 Using Sympy, say we have an expression f, which is a polynomial of the Symbol "x" (and of potentially other symbols). I would like to know what if there is an efficient way to drop all terms in f of order greater than some integer n. As a special case I have a very complicated function but i want to only keep terms up to 2nd order in x. What's the efficient way to do this? The obvious, not-very-efficient way to do it would be for each m less than n, take m derivatives and set x to 0 to obtain

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

只谈情不闲聊 提交于 2019-12-06 15:59:58
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 substitution \xi_1 ⎞│ ⎟│ ⎠│ξ₁=U To clarify, I want this output: d d ⎛d ⎞ ──(f(U, t)) + ──(U)⋅⎜──(f(U, t

SymPy simplify relational (inequality) expression

本小妞迷上赌 提交于 2019-12-06 12:05:58
Why doesn't the following simplification work, or how could it be fixed: >>> x = Symbol('x', real=True) >>> y = Symbol('y', real=True) >>> simplify(x - 1 < y - 1) x - 1 < y - 1 But this works: >>> simplify(x - 1 - (y - 1) < 0) x - y < 0 Can somehow the first expression be simplified to x < y ? Thanks You could solve for x : import sympy as sy x, y = sy.symbols('x,y', real=True) print(sy.solve(x - 1 < y - 1, x)) yields x < y x, y, z = sy.symbols('x,y,z', real=True) print(sy.solve(x - 1 < y*z - 1, x)) yields x < y*z 来源: https://stackoverflow.com/questions/27825581/sympy-simplify-relational