sympy

python快速求解不定积分和定积分

怎甘沉沦 提交于 2019-11-28 11:39:30
欢迎点击「算法与编程之美」↑关注我们! 本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列博客。 基本概念 定积分的定义如下: 不定积分定义如下: 如果想了解更多,大家可以继续阅读同济大学《高等数学》,关注公众号,回复关键词'gdsx',可以获得高清电子版。 sympy介绍 sympy库的安装非常的简单,利用conda命令可以快速的完成安装。 conda install sympy 接下来,我们将介绍利用第三方库sympy来完成积分的计算。 python求解不定积分 接下来,我们将介绍上述的不定积分的求解。 首先导入sympy库中的所有类和函数。 from sympy import * 接下来我们需要定义,本次需要使用到的符号变量x,其定义如下: x = symbols('x') 最后我们来计算积分,定积分和不定积分我们都需要用到函数integrate,这个函数的用法非常的简单,完全可以自己领悟。 integrate(cos(x) ,x) > sin(x) 这里面需要注意两点: 1)cos后面要跟一对括号,不能直接写cosx。 2)求解的结果中省略了常数C,需要自己加上。 python求解定积分 定积分的求解和不定积分类似,唯一的区别在于,定积分说明了积分的上下限。 integrate(cos(x), (x,-pi, pi)) 其中(x,-pi,pi

sympy: trigonometric sum-product identities

爱⌒轻易说出口 提交于 2019-11-28 11:18:31
I have an expression: sin(x)+sin(y) There is a well-known trig identity to express this as the product of sin and cos. Is there a way to get sympy to apply this identity? simplify and trigsimp do nothing. trigsimp , as Aristocrates points out, does the reverse, because sin(x) + sin(y) is simpler than 2*sin((x + y)/2)*cos((x - y)/2) . trigsimp internally uses an algorithm based on a paper by Fu, et. al. , which does pattern matching on various trigonometric identities. If you look at the source code , all the identities are written out in individual functions (the functions are named after the

Sympy - Comparing expressions

五迷三道 提交于 2019-11-28 10:50:11
Is there a way to check if two expressions are mathematically equal? I expected tg(x)cos(x) == sin(x) to output True , but it outputs False . Is there a way to make such comparisons with sympy? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False . I found some similar questions, but none covered this exact problem. From the SymPy documentation == represents exact structural equality testing. “Exact” here means that two expressions will compare equal with == only if they are exactly equal structurally. Here, (x+1)^2 and x^2+2x+1 are not the same symbolically

Avoid sorting args in Python module Sympy

≯℡__Kan透↙ 提交于 2019-11-28 09:15:29
问题 I am currently developing a differential operator for sympy that can be placed in matricial form. In this case the order of the args list when creating a Mul object is very important to guarantee that the differentiation is performed where it is required only. The issue is that, when the following is done: input = (t,z,x) Mul(*input).args It returns (t, x, z) because some rearrangement in args took place. How to avoid args to be sorted? 回答1: Why is the arg ordering important for it to be

Force SymPy to keep the order of terms

旧街凉风 提交于 2019-11-28 08:01:20
问题 I have the following code: from sympy import * init_printing() x,y = symbols('x y') u = Function('u')(x,y) ux,uy,uxx,uxy,uyy = symbols("u_x u_y u_xx u_xy u_yy") mainEvaluation = uxx - 2*sin(x)*uxy - (cos(x) ** 2) * uyy - 2*ux + (2 - cos(x) + 2*sin(x) )*uy And when the output of print(mainExpression) is -2*u_x + u_xx - 2*u_xy*sin(x) + u_y*(2*sin(x) - cos(x) + 2) - u_yy*cos(x)**2 The problem is: I want the original order of variables. u_xx - 2*u_xy*sin(x) - u_yy*cos(x)**2 - 2*u_x + u_y*(2*sin(x

Using Sympy Equations for Plotting

血红的双手。 提交于 2019-11-28 07:42:00
What is the best way to create a Sympy equation, do something like take the derivative, and then plot the results of that equation? I have my symbolic equation, but can't figure out how to make an array of values for plotting. Here's my code: from sympy import symbols import matplotlib.pyplot as mpl t = symbols('t') x = 0.05*t + 0.2/((t - 5)**2 + 2) nums = [] for i in range(1000): nums.append(t) t += 0.02 plotted = [x for t in nums] mpl.plot(plotted) mpl.ylabel("Speed") mpl.show() In my case I just calculated the derivative of that equation, and now I want to plot the speed x , so this is

How to calculate expression using sympy in python

百般思念 提交于 2019-11-28 06:55:24
I need a calculate below expression using sympy in python? exp = '(a+b)*40-(c-a)/0.5' In a=6 , b=5 , c=2 this case how to calculate expression using sympy in python? Please help me. The documentation is here: http://docs.sympy.org/ . You should really read it! To "calculate" your expression, write something like this: from sympy import Symbol a = Symbol("a") b = Symbol("b") c = Symbol("c") exp = (a+b)*40-(c-a)/0.5 And that's it. If you meant something else by "calculate", you could also solve exp = 0: sympy.solve(exp) > {a: [0.0476190476190476*c - 0.952380952380952*b], > b: [0.05*c - 1.05*a],

problem displaying sympy rendered svg in python

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 04:41:28
问题 I have the following program which uses sympy and svgmath to render a user's algebraic expression. It nearly works but there are a few issues: The svg is not actually produced until the program exits so obviously cannot be diplayed. Is there a way to improve the performance (not looking up 'svgmath.xml' every time etc.)? Does an actual svg file need to be produced? Can svgmath pass output directly to the QSvgWidget? Many thanks and best wishes. from __future__ import division import sys

Differential Operator usable in Matrix form, in Python module Sympy

六眼飞鱼酱① 提交于 2019-11-28 04:33:47
问题 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

How to substitute multiple symbols in an expression in sympy?

三世轮回 提交于 2019-11-28 02:41:20
问题 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 回答1: To substitute several values: >>> from sympy import Symbol >>> x, y = Symbol('x y') >>> f = x + y >>> f.subs({x:10, y: 20}) >>> f 30 回答2: The command x = Symbol('x') stores Sympy's Symbol('x') into Python's variable x . The Sympy expression f that you create afterwards does contain Symbol(