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
10
votes
1 answer

Add multiplication signs (*) between coefficients

I have a program in which a user inputs a function, such as sin(x)+1. I'm using ast to try to determine if the string is 'safe' by whitelisting components as shown in this answer. Now I'd like to parse the string to add multiplication (*) signs…
Luke Taylor
  • 8,631
  • 8
  • 54
  • 92
10
votes
1 answer

how to disable init_printing in sympy + IPython

ok, I know how to use init_printing to get sympy to automatically render IPython output using MathJax. from sympy import init_printing init_printing() How do I get it to stop? (Yeah, I could reset my notebook but I'd like to turn it on just for a…
Jason S
  • 184,598
  • 164
  • 608
  • 970
10
votes
2 answers

Determinant using sympy

The following code generates a 3x3 matrix in terms of x,y,z. I want to generate the determinant of the matrix. But unable to do so. import numpy as np import sympy as sp from sympy import * from sympy.matrices import Matrix x,y,z…
Chikorita Rai
  • 851
  • 5
  • 13
  • 17
10
votes
1 answer

How to Render Math Table Properly in IPython Notebook

The math problem that I'm solving gives different analytical solutions in different scenarios, and I would like to summarize the result in a nice table. IPython Notebook renders the list nicely: for example: import sympy from pandas import…
Titanic
  • 557
  • 1
  • 8
  • 21
10
votes
2 answers

How do I replace floats with rationals in a sympy expression?

I have an expression from a sympy calculation: sqrt(pi)*(0.333333333333333*a + 0.333333333333333*b - 2.66666666666667*c**2) where a,b,c are symbols, and would like to parse it so that the floats are replaced with rationals like in sqrt(pi)*(1/3*a +…
acortis
  • 400
  • 3
  • 15
9
votes
5 answers

Testing equivalence of mathematical expressions in Python

I have got two strings in Python, A m * B s / (A m + C m) and C m * B s / (C m + A m) that are both equivalent functions of the unordered set (A, C) and the unordered set (B). m and s indicate units that can be swapped among the same but not with…
Michael Schubert
  • 2,726
  • 4
  • 27
  • 49
9
votes
3 answers

Sympy latex export using python

I am trying to use the following python code Solved (Hint by @TheFool): By putting latex() into the print function it works. from sympy import * from sympy.printing.mathml import mathml init_printing(use_unicode=True) # allow LaTeX printing #…
MrYouMath
  • 547
  • 2
  • 13
  • 34
9
votes
1 answer

How to tell Sympy that one symbol is "greater than other"

Suppose I have two symbols x,y=symbols('x y') My objective is to tell Sympy that x is always greater than y (x>y). Is there any way to achieve this?
davidaap
  • 1,569
  • 1
  • 18
  • 43
9
votes
4 answers

How to solve a system of linear equations over the nonnegative integers?

Given a linear system Ax = b, where matrix A and vector b have integer values, I want to find all nonnegative integer vectors x that solve this equation. So far, I have found some techniques such as the Smith normal form or the Hermite normal form…
while1fork
  • 374
  • 4
  • 15
9
votes
4 answers

Getting a particular symbol in SymPy

I am trying to achieve a symbol for the Greek letter nu followed by a prime superscript. This can be achieved easily using LaTeX: $\nu'$ I tried many variants in SymPy, none of which gave me the right symbol: nuprime = symbols('{\nu}\'') nuprime =…
user32882
  • 5,094
  • 5
  • 43
  • 82
9
votes
2 answers

Sympy allows definition of integer symbols but does not account for their behavior

I have the following set of commands to do a definite integral n [2]: from sympy import * init_printing() x,L = symbols('x, L') n = symbols('n', integer = True) exp = sin((n+Rational(1,2))*pi*x/L)**2 integrate(exp, (x,0,L)) The result of these…
user32882
  • 5,094
  • 5
  • 43
  • 82
9
votes
1 answer

Round Floats within an expression

I ran into the following problem: I have a sympy matrix of sympy expressions, each cell looking something like the following example, call it ex1: In: ex1 Out: -mu_0_0 + t*((0.8*mu_0_1 + 2.22044604925031e-16)*Max(0, alpha_0_1_4)**2 + (0.8*mu_1_1 +…
DavidP
  • 105
  • 3
  • 12
9
votes
2 answers

How to derive with respect to a Matrix element with Sympy

Given the product of a matrix and a vector A.v with A of shape (m,n) and v of dim n, where m and n are symbols, I need to calculate the Derivative with respect to the matrix elements. I haven't found the way to use a proper vector, so I started…
9
votes
2 answers

How can I solve y = (x+1)**3 -2 for x in sympy?

I'd like to solve y = (x+1)**3 - 2 for x in sympy to find its inverse function. I tried using solve, but I didn't get what I expected. Here's what I wrote in IPython console in cmd (sympy 1.0 on Python 3.5.2): In [1]: from sympy import * In [2]:…
DragonautX
  • 860
  • 1
  • 12
  • 22
9
votes
1 answer

Define a variable in sympy to be a CONSTANT

from sympy import * from sympy.stats import * mu, Y = symbols('mu Y', real = True, constant = True) sigma = symbols('sigma', real = True, positive=True) X = Normal('X', mu, sigma) When asking for: E(X, evaluate=False) I get: ∞ …
Yevgeniy Loboda
  • 161
  • 1
  • 1
  • 8