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
13
votes
4 answers

Bicubic interpolation Python

I have developed Bicubic interpolation for demonstration to some undergraduate students using Python Programming language. The methodology is as explained in wikipedia, The code is working fine except the results I am getting are slightly…
Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
13
votes
1 answer

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() -…
sarasvati119
  • 162
  • 8
13
votes
1 answer

Difference between var and Symbol in sympy

Is there any difference between the two methods var and symbol in the sympy module in python? cause both are working the same way. I googled it and I did not find a detailed explanation for a difference. Are they really the exact same thing or one…
Safwat Alshazly
  • 331
  • 4
  • 11
13
votes
1 answer

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 variables. I checked with sympy documentation, but…
user7423098
  • 311
  • 2
  • 3
  • 9
13
votes
3 answers

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
  • 716
  • 1
  • 14
  • 29
13
votes
4 answers

Force SymPy to keep the order of terms

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) +…
UpmostScarab
  • 960
  • 10
  • 29
13
votes
2 answers

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 calculate the result of this equation,by using sympy…
Vasile
  • 801
  • 2
  • 13
  • 31
13
votes
5 answers

Non-sequential substitution in SymPy

I'm trying to use [SymPy][1] to substitute multiple terms in an expression at the same time. I tried the [subs function][2] with a dictionary as parameter, but found out that it substitutes sequentially. In : a.subs({a:b, b:c}) Out: c The problem…
Mad Scientist
  • 18,090
  • 12
  • 83
  • 109
13
votes
2 answers

Roots of a polynomial mod a prime

I'm looking for a speedy algorithm to find the roots of a univariate polynomial in a prime finite field. That is, if f = a0 + a1x + a2x2 + ... + anxn (n > 0) then an algorithm that finds all r < p satisfying f(r) = 0 mod p, for a given prime p. I…
Kevin Johnson
  • 820
  • 11
  • 24
13
votes
2 answers

Setting Assumptions on Variables in Sympy Relative to Other Variables

I know that sympy in python can set assumptions on variables, such as x is positive, negative, real, complex, etc. I was wondering if sympy can set assumptions on variables relative to other variables. For example, if I have variables x and y, can I…
Nate
  • 1,888
  • 3
  • 18
  • 26
13
votes
3 answers

How do I get numpy.einsum to play well with sympy?

Ok, so I have several, multi-dimensional numpy arrays of sympy objects (expressions). For example: A = array([[1.0*cos(z0)**2 + 1.0, 1.0*cos(z0)], [1.0*cos(z0), 1.00000000000000]], dtype=object) and so on. What I would like to do is…
vlsd
  • 945
  • 6
  • 18
13
votes
6 answers

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. However, I need to ignore the imaginary ones and…
Kreuzade
  • 757
  • 5
  • 11
  • 22
12
votes
2 answers

Is there a proper way to set compound greek letters as a symbol in SymPy?

As silly as it may sound, I would like to use compound greek letters as a single symbol in SymPy. For example, if the following is entered in a Jupyter notebook: import sympy as sp ab = sp.Symbol("alpha beta") sp.pprint(ab) ab behaves as desired…
Andrew Franklin
  • 478
  • 1
  • 5
  • 13
12
votes
2 answers

How to expand matrix expressions in SymPy?

In SymPy, I am trying to perform a matrix multiplication and expand it afterwards. However, SymPy does not seem to support the expansion of matrix expressions. For example, here is the 4th order Runge-Kutta (RK4) for matrices: from sympy import…
D.Thomas
  • 349
  • 3
  • 12
12
votes
3 answers

Not aligned Sympy's nice pritting of division

I'm trying to nice print some divisions with Sympy but I noticed it didn't display aligned. import sympy sympy.init_printing(use_unicode=True) sympy.pprint(sympy.Mul(-1, sympy.Pow(-5, -1, evaluate=False), evaluate=False)) # Output: # -1 # ─── # …
jeromej
  • 10,508
  • 2
  • 43
  • 62