Questions tagged [sympy]

SymPy is an open source Python library for symbolic mathematics.

sympy
Filter by
Sorted by
Tagged with
93 votes
9 answers
176k views

How to solve a pair of nonlinear equations using Python?

What's the (best) way to solve a pair of non linear equations using Python. (Numpy, Scipy or Sympy) eg: x+y^2 = 4 e^x+ xy = 3 A code snippet which solves the above pair will be great
AIB's user avatar
  • 5,964
62 votes
5 answers
51k views

How to extract all coefficients in sympy

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 ...
akai's user avatar
  • 2,160
61 votes
4 answers
59k views

How to pretty print in ipython notebook via sympy?

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 ...
colinfang's user avatar
  • 21.4k
60 votes
3 answers
27k views

How can I get a list of the symbols in a sympy expression?

For example, if I run import sympy x, y, z = sympy.symbols('x:z') f = sympy.exp(x + y) - sympy.sqrt(z) is there any method of f that I can use to get a list or tuple of sympy.Symbol objects that the ...
Michael A's user avatar
  • 4,501
55 votes
1 answer
22k views

What is the difference between SymPy and Sage?

What is the difference between SymPy and Sage a.k.a. SageMath?
Faouzi FJTech's user avatar
51 votes
6 answers
68k views

How can I solve system of linear equations in SymPy?

Sorry, I am pretty new to sympy and python in general. I want to solve the following underdetermined linear system of equations: x + y + z = 1 x + y + 2z = 3
Aniket Vij's user avatar
44 votes
5 answers
48k views

What is the best way to convert a SymPy matrix to a numpy array/matrix

I am not sure if the approach I've been using in sympy to convert a MutableDenseMatrix to a numpy.array or numpy.matrix is a good current practice. I have a symbolic matrix like: g = sympy.Matrix( [[...
Saullo G. P. Castro's user avatar
43 votes
6 answers
48k views

How to define a mathematical function in SymPy?

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 ...
Robin's user avatar
  • 8,248
42 votes
4 answers
25k views

Evaluate sympy expression from an array of values

I'm experimenting with sympy and I've hit upon an issue I can't work out. Using scipy I can write an expression and evaluate it for an array of x values as follows: import scipy xvals = scipy.arange(...
mgoi's user avatar
  • 423
40 votes
6 answers
47k views

Is it possible to plot implicit equations?

I would like to plot implicit equations (of the form f(x, y)=g(x, y) eg. X^y=y^x) in Matplotlib. Is this possible?
Geddes's user avatar
  • 1,209
38 votes
7 answers
24k views

Get a value from solution set returned as finiteset by Sympy

I`m creating a script in Python Sympy library and trying to access the result returned by solveset() and linsolve() functions. My problem is that the object returned by these functions is of type ...
Plinio Bueno Andrade Silva's user avatar
37 votes
5 answers
19k views

Sympy - Comparing expressions

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 ...
soap's user avatar
  • 721
35 votes
3 answers
63k views

How to substitute multiple symbols in an expression in sympy?

Assigning a variable directly does not modify expressions that used the variable retroactively. >>> from sympy import Symbol >>> x = Symbol('x') >>> y = Symbol('y') >>...
Wesley's user avatar
  • 1,442
33 votes
8 answers
183k views

plus/minus operator for python ±

I am looking for a way to do a plus/minus operation in python 2 or 3. I do not know the command or operator, and I cannot find a command or operator to do this. Am I missing something?
Whitequill Riclo's user avatar
33 votes
7 answers
18k views

SymPy - Arbitrary number of Symbols

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 ...
thornate's user avatar
  • 5,002
33 votes
4 answers
15k views

how to handle an asymptote/discontinuity

When plotting a graph with a discontinuity/asymptote/singularity/whatever, is there any automatic way to prevent Matplotlib from 'joining the dots' across the 'break'? (please see code/image below). I ...
Geddes's user avatar
  • 1,209
28 votes
3 answers
11k views

Is there any way to get the step-by-step solution in SymPy?

Is there any way to get the step-by-step solution in SymPy? For example: x**2-5 = 4 step 1 x**2-5+5=4+5 step 2 : x**2=9 step 3 :x = 3 or x= -3
Xyz-47's user avatar
  • 343
25 votes
5 answers
30k views

How to calculate expression using sympy in python

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.
Zeck's user avatar
  • 6,523
25 votes
2 answers
95k views

Any way to solve a system of coupled differential equations in python?

I've been working with sympy and scipy, but can't find or figure out how to solve a system of coupled differential equations (non-linear, first-order). So is there any way to solve coupled ...
bynx's user avatar
  • 760
23 votes
3 answers
32k views

Inverse of a matrix in SymPy?

I was wondering how to create a matrix and compute its inverse using SymPy in Python? For example, for this symbolic matrix:
Tim's user avatar
  • 96.9k
23 votes
3 answers
16k views

How to check the current version of sympy and upgrade to the latest version?

How to check the current version of SymPy and upgrade to the latest version? I am using macOS. The way I installed my current version is using pip install sympy.
MAS's user avatar
  • 4,723
22 votes
3 answers
17k views

Convert a LaTex formula to a type that can be used inside SymPy

I want to parse LaTeX formulas and directly use them as SymPy expressions. In other words, what I need is something similar to sympify: from sympy import sympify f = sympify('x^2 + sin(y) + 1/2') ...
user2243748's user avatar
20 votes
2 answers
34k views

Using Sympy Equations for Plotting

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 ...
MANA624's user avatar
  • 1,016
20 votes
4 answers
17k views

SymPy: Limit symbol/variable to interval

Using SymPy, is it possible to limit the possible values of a symbol/variable to a certain range? I now I can set some properties while defining symbols, like positive=True, but I need more control, i....
Se Norm's user avatar
  • 1,755
19 votes
2 answers
41k views

Using "from __future__ import division" in my program, but it isn't loaded with my program

I wrote the following program in Python 2 to do Newton's method computations for my math problem set, and while it works perfectly, for reasons unbeknownst to me, when I initially load it in ipython ...
Sara Fauzia's user avatar
19 votes
2 answers
2k views

Representing rings of algebraic integers

I'm trying to represent the ring; where theta is the root of a monic irreducible polynomial f with integer coefficients of degree d. This ring is a subring of the algebraic integers, which itself ...
Kevin Johnson's user avatar
18 votes
3 answers
24k views

Calculate curl of a vector field in Python and plot it with matplotlib

I need to calculate the curl of a vector field and plot it with matplotlib. A simple example of what I am looking for could be put like that: How can I calculate and plot the curl of the vector field ...
celestos's user avatar
  • 311
18 votes
4 answers
3k views

How does sympy work? How does it interact with the interactive Python shell, and how does the interactive Python shell work?

What happens internally when I press Enter? My motivation for asking, besides plain curiosity, is to figure out what happens when you from sympy import * and enter an expression. How does it go ...
daltonb's user avatar
  • 665
18 votes
2 answers
16k views

Convert sympy expressions to function of numpy arrays

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 =...
drhagen's user avatar
  • 8,938
18 votes
1 answer
6k views

Dealing with piecewise equations returned by sympy integrate

In sympy I have an integral which returns a Piecewise object, e.g. In [2]: from sympy.abc import x,y,z In [3]: test = exp(-x**2/z**2) In [4]: itest = integrate(test,(x,0,oo)) In [5]: itest Out[5]: ...
wxs's user avatar
  • 5,747
17 votes
3 answers
26k views

How to get the Gradient and Hessian | Sympy

Here is the situation: I have a symbolic function lamb which is function of the elements of the variable z and the functions elements of the variable h. Here is an image of the lamb symbolic function ...
Randerson's user avatar
  • 775
17 votes
3 answers
71k views

No module named sympy

Hi I'm learning linear algebra with python with an Edx course. (http://nbviewer.ipython.org/github/ULAFF/notebooks/tree/may-14-2014/). On "02.4.2.10 Practice with matrix-vector multiplication" with ...
pythonlearner's user avatar
17 votes
5 answers
7k views

Evaluating a mathematical expression (function) for a large number of input values fast

The following questions Evaluating a mathematical expression in a string Equation parsing in Python Safe way to parse user-supplied mathematical formula in Python Evaluate math equations from ...
s-m-e's user avatar
  • 3,493
17 votes
5 answers
3k views

Evaluate inner product of bra and ket in Sympy Quantum

In sympy I have defined a two kets and a corresponding bra, when I apply the bra to the kets... from sympy import sqrt from sympy.physics.quantum import Bra,Ket,qapply superpos = (Ket('Dead')+Ket('...
user4624500's user avatar
16 votes
3 answers
5k views

What does `S` signify in SymPy?

I am new to SymPy, and I can't figure out from sympy.core import S What is S actually? And what does S.true mean?
Yathartha Joshi's user avatar
15 votes
4 answers
4k views

Display two Sympy plots as two Matplotlib subplots

This code from sympy import * x=Symbol('x') p1 = plot(x**2,(x,-2,2)) p2 = plot(x**3,(x,-2,2)) results in two separate plots. Instead of two separate plots, I want to display them with matplotlib ...
M. Heuer's user avatar
  • 397
15 votes
2 answers
15k views

Double integral with variable boundaries in python Scipy + sympy (?)

The full mathematical problem is here. Briefly I want to integrate a function with a double integral. The inner integral has boundaries 20 and x-2, while the outer has boundaries 22 and 30. I know ...
gc5's user avatar
  • 9,668
15 votes
2 answers
22k views

From string to sympy expression

Recently I was working on a web application, using Flask and sympy libraries. The user enters his equation in a textarea and Flask rechieve it as a string. I would like to have the possibility to ...
Vasile's user avatar
  • 821
15 votes
3 answers
9k views

IPython Notebook Sympy Math Rendering

I have just started with using IPython Notebook and have been fascinated by its power. I have been using a few examples available on the net to get started with. I was following this tutorial: http://...
DotPi's user avatar
  • 4,057
15 votes
1 answer
16k views

Python Sympy Pretty Output of Matrix

Matrix([[607000, 907, 259, -2165, -1846, 185, -60, -1593, 1445, 1405], [-1000, -2, 0, -1, 0, 0, -1, 0, 0, -1], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [-4000, -7, -3, 5, 4, 1, -1, 4, -4, -2], [-317000, -469, -...
user4555363's user avatar
15 votes
1 answer
12k views

Solving Systems of Equations with SymPy

I'm trying to solve a set of equations with SymPy 0.7.1: from sympy import * equations = [ Eq(S('vf'), S('vi + a*t')), Eq(S('d'), S('vi*t + 1/2*a*t**2')), Eq(S('a'), S('10')), Eq(S('d'...
Tim McLean's user avatar
  • 1,606
15 votes
1 answer
2k views

sympy: order of result from solving a quadratic equation

I solved a quadratic equation using sympy: import sympy as sp q,qm,k,c0,c,vt,vm = sp.symbols('q qm k c0 c vt vm') c = ( c0 * vt - q * vm) / vt eq1 = sp.Eq(qm * k * c / (1 + k * c) ,q) q_solve = sp....
Moritz's user avatar
  • 5,288
14 votes
6 answers
17k views

Ignore imaginary roots in sympy

I'm using sympy to solve a polynomial: x = Symbol('x') y = solve(int(row["scaleA"])*x**3 + int(row["scaleB"])*x**2 + int(row["scaleC"])*x + int(row["scaleD"]), x) y is a list of possible solutions. ...
Kreuzade's user avatar
  • 767
14 votes
2 answers
16k views

How to do a symbolic taylor expansion of an unknown function $f(x)$ using sympy

In sage it is fairly easy to do a Taylor expansion of an unknown function f(x), x = var('x') h = var('h') f = function('f',x) g1 = taylor(f,x,h,2) How can this be done in sympy? Update asmeurer ...
Daniel Farrell's user avatar
14 votes
2 answers
828 views

Evaluate Irrational with Julia

Julia has the built-in constant pi, with type Irrational. julia> pi π = 3.1415926535897... julia> π π = 3.1415926535897... julia> typeof(pi) Irrational{:π} Coming from SymPy, which has ...
2Cubed's user avatar
  • 3,481
14 votes
1 answer
17k views

How to create vector of symbolic variables in sympy

How to create vector of symbolic variables in sympy? I want to do something like x, x1, x2, x3 = symbols ('x x1 x2 x3') A = [x+x1,x+x2,x+x3] B = A * Transpose(A) print (B) A is array of symbolic ...
user7423098's user avatar
14 votes
2 answers
6k views

Is there a vectorized way to calculate the gradient in sympy?

How does one calculate the (symbolic) gradient of a multivariate function in sympy? Obviously I could calculate separately the derivative for each variable, but is there a vectorized operation that ...
Bitwise's user avatar
  • 7,707
14 votes
3 answers
5k views

How to find all terms in an expression in Sympy

I have an expression as: 1/(x+1)+4*x/(x-1)+3-4*x**2+10*x**2 What I need is a list that contain the terms in this expression. i.e. [1/(x+1), 4*x/(x-1), 3, -4*x**2 , 10*x**2] update: It should not ...
harsh's user avatar
  • 2,421
14 votes
2 answers
8k views

Factor/collect expression in Sympy

I have an equation like: R₂⋅V₁ + R₃⋅V₁ - R₃⋅V₂ i₁ = ───────────────────── R₁⋅R₂ + R₁⋅R₃ + R₂⋅R₃ defined and I'd like to split it into factors that include only single variable - in this ...
viraptor's user avatar
  • 33.6k
14 votes
1 answer
2k views

How to simplify matrix expressions in SymPy?

Consider the following example import sympy as sy n = sy.symbols('n') A = sy.MatrixSymbol("A",n,n) B = sy.MatrixSymbol("B",n,n) C = sy.MatrixSymbol("C",n,n) M = A.inverse()*B.inverse() - A.inverse()*...
sarasvati119's user avatar

1
2 3 4 5
117