Questions tagged [sympy]

SymPy is an open source Python library for symbolic mathematics.

SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python and does not require any external libraries.

Some examples of usage can be found here.

4051 questions
74
votes
9 answers

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
  • 5,562
  • 8
  • 29
  • 35
52
votes
4 answers

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 init_printing init_printing(use_latex=True) from sympy…
colinfang
  • 17,887
  • 11
  • 69
  • 146
49
votes
4 answers

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 so on), like; # for example expr.coefficients(x) #…
akai
  • 1,986
  • 3
  • 17
  • 37
41
votes
1 answer

What is the difference between SymPy and Sage?

What is the difference between SymPy and Sage a.k.a. SageMath?
Faouzi FJTech
  • 791
  • 2
  • 11
  • 26
40
votes
3 answers

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 expression contains? I'd rather not have to parse…
Michael A
  • 3,685
  • 6
  • 29
  • 58
37
votes
4 answers

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(-100,100,0.1) f = lambda x: x**2 f(xvals) Using…
mgoi
  • 373
  • 1
  • 3
  • 4
35
votes
5 answers

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
  • 477
  • 1
  • 4
  • 9
34
votes
6 answers

Is it possible to plot implicit equations using Matplotlib?

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
  • 1,073
  • 2
  • 11
  • 22
29
votes
4 answers

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 function, like this: f(x) = x**2 + 1 After that I…
Robin
  • 6,726
  • 4
  • 47
  • 75
29
votes
3 answers

how to handle an asymptote/discontinuity with Matplotlib

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 read that Sage has a [detect_poles] facility…
Geddes
  • 1,073
  • 2
  • 11
  • 22
27
votes
7 answers

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 symbols - as many symbols as there are equations.…
thornate
  • 4,202
  • 9
  • 36
  • 42
27
votes
3 answers

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') >>> f = x + y >>> x = 0 >>> f x + y
Wesley
  • 1,206
  • 1
  • 11
  • 22
25
votes
6 answers

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 finiteset and I want to select some results…
25
votes
4 answers

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 example is (a+b)**2 == a**2 + 2*a*b + b**2 which…
Xico Sim
  • 353
  • 3
  • 4
25
votes
4 answers

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( [[ x, 2*x, 3*x, 4*x, 5*x, 6*x, 7*x, 8*x, …
Saullo G. P. Castro
  • 49,101
  • 22
  • 160
  • 223
1
2 3
99 100