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.

SymPy includes features ranging from basic symbolic arithmetic to calculus, algebra, discrete mathematics, quantum physics. It is capable of formatting the result of the computations as LaTeX code.

Some Core capabilities:

  • Basic arithmetic: *, /, +, -, **
  • Simplification
  • Expansion
  • Functions: trigonometric, hyperbolic, exponential, roots, logarithms, absolute value, spherical harmonics, factorials and gamma functions, zeta functions, polynomials, hypergeometric, special functions, ...
  • Substitution
  • Arbitrary precision integers, rational and floats
  • Non-commutative symbols
  • Pattern matching
  • Polynomials
  • Basic arithmetic: division, gcd, ...
  • Factorization
  • Square-free factorization
  • Calculus
  • Limits
  • Differentiation
  • Pretty-printing: ASCII/Unicode pretty-printing, LaTeX
  • Code generation: C, Fortran, Python
5602 questions
17
votes
4 answers

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 from Enter to calling __sympifyit_wrapper(a,b) in…
daltonb
  • 635
  • 8
  • 26
17
votes
3 answers

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 the first box, the code is: import…
pythonlearner
  • 359
  • 2
  • 3
  • 11
17
votes
4 answers

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.e. I need to set it to be in the interval [0,1].…
Se Norm
  • 1,715
  • 5
  • 23
  • 40
16
votes
5 answers

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('Alive'))/sqrt(2) d =…
user4624500
  • 286
  • 1
  • 10
15
votes
1 answer

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'), S('60')), Eq(S('vi'), S('5')) ] print…
Tim McLean
  • 1,576
  • 3
  • 14
  • 20
15
votes
3 answers

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 as subplots: import matplotlib.pyplot as plt fig =…
M. Heuer
  • 395
  • 3
  • 13
15
votes
1 answer

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.solve(eq1,q) Based on some testing I figured out…
Moritz
  • 5,130
  • 10
  • 40
  • 81
15
votes
3 answers

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:…
DotPi
  • 3,977
  • 6
  • 33
  • 53
14
votes
2 answers

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 case V1 and V2. So as a result I'd expect …
viraptor
  • 33,322
  • 10
  • 107
  • 191
14
votes
2 answers

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 the N() function, I would like to evaluate pi (or other…
2Cubed
  • 3,401
  • 7
  • 23
  • 40
14
votes
3 answers

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 collect like terms. Therefore the list should have…
harsh
  • 2,399
  • 9
  • 29
  • 47
14
votes
2 answers

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 that with Scipy I can compute the double integral…
gc5
  • 9,468
  • 24
  • 90
  • 151
14
votes
1 answer

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, -128, 1173, 1001, -105, 35, 862, -772, -771],…
user4555363
  • 217
  • 1
  • 2
  • 8
14
votes
2 answers

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 does this? For…
Bitwise
  • 7,577
  • 6
  • 33
  • 50
14
votes
2 answers

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 points out that this is a feature which will be…
Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62