sympy

How do you evaluate a derivative in python?

匆匆过客 提交于 2019-12-05 11:18:59
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 minimal working example: import sympy as sym import math def f(x,y): return x**2 + x*y**2 x, y = sym

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

佐手、 提交于 2019-12-05 11:00:36
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') vector_F(1.0, 0.0, 1.0) However the above raises a NameError ... ------------------------------------------

Printing greek letters using sympy in text

点点圈 提交于 2019-12-05 10:35:52
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 You want pretty() , which is the same as pprint , but it returns a string instead of printing it. In [1]: pretty(pi) Out[1]: 'π' In [2]: "I am %s" % pretty(pi) Out[2]: 'I am π' If all you care about

Passing sympy lambda to multiprocessing.Pool.map

孤街醉人 提交于 2019-12-05 09:23:58
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.lambdify(x, 2*x, 'numpy') self._lambdify = sympy.lambdify(x, 2 * x, 'numpy') return self._lambdify if _

Enforce custom ordering on Sympy print

房东的猫 提交于 2019-12-05 08:33:37
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/(z-3*tp)+d My problem is that SymPy presents the expression ordered as U + U/(-3*t_\perp + z) + d + t_

What does `S` signify in sympy

丶灬走出姿态 提交于 2019-12-05 08:06:10
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? 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 instances are attributes of the S object, so Integer(0) can also be accessed as S.Zero . Singletonization

Optimize code generated by sympy

微笑、不失礼 提交于 2019-12-05 07:46:35
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=(u.x*x2 + u.y*y2 + u.z*z2) u3=(u.x*x3 + u.y*y3 + u.z*z3) s1=(u1-u2).normalize() s2=(u2-u3).normalize()

Create a formal linear function in Sympy

家住魔仙堡 提交于 2019-12-05 05:11:40
I have an expression in Sympy (like -M - n + x(n) ) and I would like to create a formal linear function, says f, and apply it to my expression, in order to get, after simplification: -f(M) - f(n) + f(x(n)) Is it possible to tell sympy that a property such as linearity is verified? A very hacky way to do it would be to apply the function f to every subexpression which is in a sum. For instance when given an expressions like the first one I gave, it would be nice to simply access the terms appearing in the sum (here it would be [-M, -n , x(n)] Then mapping f on the list and sum it to get what is

How to get a latex table of sympy expressions in ipython-notebook?

你。 提交于 2019-12-05 03:12:27
问题 I'm using sympy to collect terms from several expressions and would like to format the results (within ipython-notebook) in a table with the terms down the left most column and each subsequent column representing one expression. The entries in the column are from the dict returned by sympy.collect(syms, evaluate=False) So far I have: from IPython.display import display, Latex import pandas as pd import sympy as sym sym.init_printing() x,y,z = sym.symbols('x,y,z') da,db,dc = sym.symbols('{

Add multiplication signs (*) between coefficients

泪湿孤枕 提交于 2019-12-05 01:33:13
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 solution (i.e. not complex regexes, not because they're pythonic, but just because I don't understand