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

SymPy polynomials over finite fields

import sympy as S F = S.FiniteField(101) When I call f = S.poly(y ** 2 - x ** 3 - x - 1,F) I get the following error: 'FiniteField' object has no attribute 'is_commutative' But finite fields are commutative by definition! So I'm not really sure…
Kevin Johnson
  • 820
  • 11
  • 24
12
votes
2 answers

Sympy Simplification with Square Root

I have an expression that I think could be simplified somewhat and for some reason sympy is not performing what I think are simple simplifications. My code with the example is as follows: # coding: utf-8 # In[1]: from __future__ import…
Justace Clutter
  • 2,097
  • 3
  • 18
  • 31
12
votes
3 answers

Multivariate Taylor approximation in sympy

I aim to write a multidimensional Taylor approximation using sympy, which uses as many builtin code as possible, computes the truncated Taylor approximation of a given function of two variables returns the result without the Big-O-remainder term,…
flonk
  • 3,726
  • 3
  • 24
  • 37
12
votes
2 answers

Prevent Sympy from rearranging the equation

Perhaps im overlooking the obvious but how do you prevent sympy from rearranging equations? Im using Sympy in the iPython notebook so i can easily copy-paste the Latex code to Lyx, but i want the equations in the same order as i defined them. For…
Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97
11
votes
2 answers

Solving Inequalities in Sympy

I tried to solve the following inequality in sympy: (10000 / x) - 1 < 0 So I issued the command: solve_poly_inequality( Poly((10000 / x) - 1 ), '<') However, I got: [Interval.open(-oo, 1/10000)] However, my manual computations give either x < 0…
olidem
  • 1,961
  • 2
  • 20
  • 45
11
votes
2 answers

Why does SymPy calculate wrong intersections of planes?

I have the strange problem, that the intersection of planes in SymPy works with simple examples, but fails for one with more complicated coordinates. I post both a simple example that works, and the one that fails. As the Povray images show, I have…
Watchduck
  • 1,076
  • 1
  • 9
  • 29
11
votes
3 answers

How to keep fractions in your equation output

I've been using Python to calculate math equations. For example: from sympy import Symbol, Derivative, Integral x = Symbol('x') d = Symbol('d') Integral(8*x**(6/5)-7*x**(3/2),x).doit() Which results in the output: 3.63636363636364*x**2.2 -…
d84_n1nj4
  • 1,712
  • 6
  • 23
  • 40
11
votes
3 answers

How to find the eigenvalues and eigenvectors of a matrix with SymPy?

I want to calculate the eigenvectors x from a system A by using this: A x = λ x The problem is that I don't know how to solve the eigenvalues by using SymPy. Here is my code. I want to get some values for x1 and x2 from matrix A from sympy import…
euraad
  • 2,467
  • 5
  • 30
  • 51
11
votes
2 answers

SymPy : creating a numpy function from diagonal matrix that takes a numpy array

Building on an example I've found here, I am trying to create a function from a diagonal matrix that was created using sumpy.diag myM = Matrix([ [x1, 4, 4], [4, x2, 4], [4, 4, x3]]) Where this was created using this routine for example: import…
Ohm
  • 2,312
  • 4
  • 36
  • 75
11
votes
1 answer

How to rewrite an expression in terms of an other expression in sympy

EDIT: I am not asking how to solve an equation in terms of a given variable (as in this supposed duplicated question), but how to represent an expression in terms of an other one, as specified in the question. I believe it is the "duplicated"…
Antonello
  • 6,092
  • 3
  • 31
  • 56
11
votes
2 answers

How do I define a conditional function using sympy?

I want to be able to define an expression which takes all the values of variable where it is defined and evaluates the expression as 0 when it is not defined. Similar to this: - import numpy as np import sympy as sp def expr(k1, k2): …
Manish
  • 458
  • 6
  • 19
11
votes
1 answer

Sympy: Solving Matrices in a finite field

For my project, I need to solve for a matrix X given matrices Y and K. (XY=K) The elements of each matrix must be integers modulo a random 256-bit prime. My first attempt at solving this problem used SymPy's mod_inv(n) function. The problem with…
arnbobo
  • 2,515
  • 2
  • 12
  • 18
11
votes
2 answers

Generate python code from a sympy expression?

The Question: Given a sympy expression, is there an easy way to generate python code (in the end I want a .py or perhaps a .pyc file)? I imagine this code would contain a function that is given any necessary inputs and returns the value of the…
stochastic
  • 3,155
  • 5
  • 27
  • 42
11
votes
2 answers

Sympy: Drop higher order terms in polynomial

Using Sympy, say we have an expression f, which is a polynomial of the Symbol "x" (and of potentially other symbols). I would like to know what if there is an efficient way to drop all terms in f of order greater than some integer n. As a special…
11
votes
1 answer

Python: Find principal value of an integral numerically

I'm solving the integral numerically using python: where a(x) can take on any value; positive, negative, inside or outside the the [-1;1] and eta is an infinitesimal positive quantity. There is a second outer integral of which changes the value of…