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
9
votes
2 answers

Optimize code generated by sympy

Using SymPy to find a derivative (see this question: https://math.stackexchange.com/questions/726104/apply-chain-rule-to-vector-function-with-chained-dot-and-cross-product ), I came up with this code: from sympy import * from sympy.physics.mechanics…
Dirk
  • 1,789
  • 2
  • 21
  • 31
9
votes
1 answer

Sympy: working with equalities manually

I'm currently doing a maths course where my aim is to understand the concepts and process rather than crunch through problem sets as fast as possible. When solving equations, I'd like to be able to poke at them myself rather than have them solved…
Marius
  • 58,213
  • 16
  • 107
  • 105
9
votes
6 answers

How to do dot/cross multiplication of Vectors with Sympy

I would like to know how to do dot multiplication cross multiplication add/sub of vectors with the sympy library. I have tried looking into the official documentation but I have had no luck or It was too complicated. Can anyone help me out on…
Vannen
  • 712
  • 2
  • 7
  • 17
9
votes
2 answers

Fourier transform a trig function in Sympy returns unexpected result

I think Sympy makes a mistake in calculating the Fourier transform of a trig function. For example: from sympy import fourier_transform, sin from sympy.abc import x, k print fourier_transform(sin(x), x, k) The expected answer via Mathematica is…
Kattern
  • 2,949
  • 5
  • 20
  • 28
9
votes
1 answer

Why does SymPy give me the wrong answer when I row-reduce a symbolic matrix?

If I ask SymPy to row-reduce the singular matrix nu = Symbol('nu') lamb = Symbol('lambda') A3 = Matrix([[-3*nu, 1, 0, 0], [3*nu, -2*nu-1, 2, 0], [0, 2*nu, (-1 * nu) - lamb - 2, 3], [0, 0, nu + lamb, -3]]) print…
Jessica
  • 2,335
  • 2
  • 23
  • 36
9
votes
2 answers

Python - write symbolic expression (sympy) to txt file

What is the best way to write a symbolic expression to a txt file (so that I can use the symbolic expression after reading the txt file)? Is there a sympy equivalent of numpy's savetxt() function for example? for example: from sympy import…
baklan
  • 187
  • 3
  • 14
9
votes
3 answers

prevent Sympy from simplifying expression python after a substitution

I'm using Sympy to substiture a set of expressions for another using the Subs function, and I would like for the program not to rearrage or simplify the equations. i.e if i were substituting x+y for a in a+b+c+a to return x+y+b+c+x+y Does anyone…
user124123
  • 1,642
  • 7
  • 30
  • 50
9
votes
3 answers

Factoring polys in sympy

I'm doing a very simple probability calculations of getting subset of X, Y, Z from set of A-Z (with corresponding probabilities x, y, z). And because of very heavy formulas, in order to handle them, I'm trying to simplify (or collect or factor - I…
akaRem
  • 7,326
  • 4
  • 29
  • 43
9
votes
1 answer

SymPy cannot solve an equation that Matlab can

I have an equation which is related to the sun-synchronous resonance condition in orbital mechanics. I'm learning Python at the moment, so I attempted to solve it in SymPy using the following code: from sympy import…
nathanDonaldson
  • 353
  • 1
  • 9
8
votes
0 answers

How to get sympy to give solutions with smaller coefficients

Consider the following example Diophantine equation -118w + 989x + 918y -512z == 0 You can solve this in sympy with: import sympy from sympy.solvers.diophantine import diophantine w, x, y, z = sympy.symbols('w x y z') a, b, c, d = -118, 989, 918,…
Simd
  • 19,447
  • 42
  • 136
  • 271
8
votes
2 answers

Choosing between different expression factorizations in SymPy

Say I have an expression as follows: a*b*c + b*c + a*d One could factorize it as: b*(a*c + c) + (a*d) or as c*(a*b + b) + (a*d) or as a*d + b*c*(a + 1) among other possibilities. For other expressions, the # of possibilities can be much…
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
8
votes
2 answers

Automatically populating matrix elements in SymPy

Is there a way to implicitly define the elements of a symbolic matrix in SymPy following a rule such as: symbol followed by subindices in the matrix (or pairs of numbers) For example, I would like to define a 3 x 2 matrix called M, and I would like…
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
8
votes
1 answer

Partial fraction decomposition using SymPy / Python

How do I find the constants A,B,C,D,K,S such that 1/(x**6+1) = (A*x+B)/(x**2+1) + (C*x+D)/(x**2-sqrt(3)*x+1) + (K*x+S)/(x**2+sqrt(3)*x+1) is true for every real x. I need some sympy code maybe, not sure. Or any other Python lib which could help…
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
8
votes
3 answers

How to combine polynomials in matrix operations in Sympy?

I'm doing some matrix operations, sometimes involving matrices whose entries have constant values. But for some reason, I cannot get the matrix operation to combine the results into a single polynomial, even when the result is simple. for…
makansij
  • 9,303
  • 37
  • 105
  • 183
8
votes
2 answers

How to define a lot of symbols in SymPy

I am trying to define a lot of variables in "sympy" for symbolic processing. import sympy as sp b_0 = sp.symbols('b_0') b_1 = sp.symbols('b_1') ... b_X = sp.symbols('b_X') and so on with the X going from 1 to 1000. Is there an easy way to do it?
stormer1120
  • 97
  • 1
  • 5