sympy

How to check if a SymPy expression has analytical integral

谁都会走 提交于 2019-12-11 09:08:46
问题 I want to solve my other question here so I need sympy to return an error whenever there is no analytical/symbolic solution for and integral. For example if I try : from sympy import * init_printing(use_unicode=False, wrap_line=False, no_global=True) x = Symbol('x') integrate(1/cos(x**2), x) It just [pretty] prints the integral itself without solving and/or giving an error about not being able to solve it! P.S. I have also asked this question here on Reddit. 回答1: A "symbolic" solution always

How to eliminate negative solutions from `sympy.solve` result?

爱⌒轻易说出口 提交于 2019-12-11 07:44:11
问题 How can I make sympy.solve not return negative solutions? This seems to be a different task than adding a constraint like positive=True to the symbol I'm solving for. While import sympy x = sympy.symbols("x") print(sympy.solve(x**2-4, x)) x = sympy.symbols("x", positive=True) print(sympy.solve(x**2-4, x)) prints [-2, 2] [2] as expected - I still get a negative solve result for omega with import sympy omega, omega_0, gamma = sympy.symbols("omega, omega_0, gamma", real=True, positive=True) zeta

How to determine which one is free variable in the result of sympy.linsolve

点点圈 提交于 2019-12-11 07:41:30
问题 I want to solve the linear equation for n given points in n dimensional space to get the equation of hyper-plane. for example, in two dimensional case, Ax + By + C = 0 . How can I get one solution if there are infinite solutions in a linear equations ? I have tried scipy.linalg.solve() but it requires coefficient matrix A to be nonsingular. I also tried sympy A = Matrix([[0, 0, 1], [1, 1, 1]]) b = Matrix([0, 0]) linsolve((A, b), [x, y, z]) It returned me this {(−y,y,0)} I have to parse the

TypeError: unhashable type: 'dict' in python

假装没事ソ 提交于 2019-12-11 07:24:19
问题 I'm getting an error message about dictionaries despite having never used a dictionary anywhere in my code Here is my code: mm=[] soln=[] for i in range(len(momvec)): string = str(momvec[i]) num = string[2:] mm.append(Symbol('x'+num)) print num print mm soln.append(solve(mom[i]-momvec[i],mm)) print type(MFK[0]) for m in range(0,len(MFK)): for i in range(0,len(mm)): print MFK[m] MFK[m]= Subs(MFK[m],mm[i], soln[i]).doit() What I am trying to do is replace some items in the list MFK with what is

How to use SymPy to find the point of intersection of two functions?

坚强是说给别人听的谎言 提交于 2019-12-11 06:38:54
问题 I am trying to use the SymPy library to find the point of intersection(s) between two functions: f(x) = e ^ (x / 2) and g(x) = 3 - 3 * x I tried: import sympy as syp x = syp.symbols('x') f_x = syp.E ** (x / 2) g_x = 3 - 3 * x print(syp.nsolve(f_x, g_x, x)) syp.nsolve(f_x, g_x, x) spits out a TypeError . Replacing that line with syp.solve([f_x, g_x], x) results in an empty list [] . This is wrong because f(x) and g(x) intersect at exactly one point. How do I get the x and y values of the point

How to solve an algebraic equation in formal power series?

不问归期 提交于 2019-12-11 06:26:14
问题 Motivation. It is well known that generating function for Catalan numbers satisfies quadratic equation. I would like to have first several coefficients of a function, implicitly defined by an algebraic equation (not necessarily a quadratic one!). Example. import sympy as sp sp.init_printing() # math as latex from IPython.display import display z = sp.Symbol('z') F = sp.Function('F')(z) equation = 1 + z * F**2 - F display(equation) solution = sp.solve(equation, F)[0] display(solution) display

computer algebra soft to minimize the number of operations in a set of polynomials

好久不见. 提交于 2019-12-11 06:08:02
问题 I have systems of polynomials, fairly simple polynomial expressions but rather long to optimize my hand. Expressions are grouped in sets, and in a given set there are common terms in several variables. I would like to know if there is a computer algebra system, such as Mathematica, Matlab, or sympy, which can optimize multiple polynomials with common terms to minimize number of operations. It would be also great if such system can minimize the number of intermediate terms to reduce number of

Python integration using both scipy and sympy

我的未来我决定 提交于 2019-12-11 05:48:59
问题 I want to have a custom python function which: Takes a mathematical expression f(x) , bounds of the integral x1,x2 and desired tolerance tol Uses sympy.integrate(y, x) to check if it has analytical/symbolic solution, if it does then returns the result using sympy.integrate(y, (x,x1,x2).evalf()) If it doesn't have an analytical solution out of the sympy then it uses scipy.integrate.quad or other numerical functions to calculate the integral. The reason is that with this method it will be

On import modules and method names in python

怎甘沉沦 提交于 2019-12-11 04:42:05
问题 Suppose I import the following two modules as follows: from sympy import * from numpy import * both modules have an exp() function defined. How does python pick which one to use? Is there a way to distinguish these functions after the modules have been imported as above? What mechanism exists to warn the user when this is the case? Consider the following set of commands in IDLE =============================== RESTART: Shell =============================== >>> from sympy import * >>> from

Python: SymPy lambdify abs for use with NumPy

ぃ、小莉子 提交于 2019-12-11 04:39:02
问题 I am using SymPy for symbolic manipulation of expression, which is evaluated on huge amounts of data using NumPy. To speed up things, I use sympy.lambdify, but I cannot get abs to work. import sympy import numpy as np x = sympy.symbols('x',real=True) def f(x): return 1-sympy.Abs(x) def g(x): return 1-sympy.sqrt(x) fl = sympy.lambdify(x,f(x),'numpy') gl = sympy.lambdify(x,g(x),'numpy') gl(1) # success gl(np.array([1,2,3])) fl(2) # NameError: global name 'Abs' is not defined fl(np.array([1,2,3]