FAQ Database Discussion Community

## sympy: Collect symbols for matrix coefficients?

matrix,sympy,symbolic-math,polynomial-math
I am trying to factor an expression, and separate coefficients to matrix form, such that: Closely related to Factor sympy expression to matrix coefficients?, where Wild symbols are used with match(form) to determine coefficients for its matrix form. However, I am unable to get the match(form) method to work for...

## iteratively intersecting line segments in Sympy… is there a better way?

python,geometry,networkx,sympy
OK. I have the points that comprise the borders of a polygon. I want to (a) use Sympy's geometry module to determine, from all of the possible line-segments between any pair of points, which segments do not cross the perimeter. This result will be the "edges" that are allowed to...

## SymPy integration, constant term

python,python-2.x,sympy,integral
I can't understand behavior of sympy.integrate() function. The simplest example, integrate and differentiate: t = sy.Symbol('t') t1 = sy.Symbol('t1') f = sy.Function('f')(t) I = sy.integrate(f, (t, 0, t1)) f1 = I.diff(t1) print f1 prints the following: f(t1) + Integral(0, (t, 0, t1)) But I expect to see just f(t). Calling...

## integration and differentiation with sympy/pyqt python

python,math,sympy
I'm creating a system which quizes the user on integration and differentiation using python 3. when i display questions they are in a form like: -25*x**(3/5)/3 + 6*x**(4/3) - 5*x**6/3 + x**2/2 - 4*x How could I change it to a form like: -25x^(3/5)/3 + 6x^(4/3) - 5x^6/3 + x^2/2...

## python: comparing strings in a different order

python,string,sympy
I am doing a maths quiz for my coursework based around differentiation and integration using python 3. I'm finding it difficult to check if the answer, typed in by the user, is correct. I'm currently using the code below. However, if the user types in answer but in a different...

## Differentiating a sympy function with an unknown number of variables

python,function,sympy
let's assume I have a script that creates a vector v with a certain number of elements x1, x2, x3, etc (each element is a sympy symbol). import sympy as sp # generated by script, so the number of symbols/length of v is always different: sp.var("x1,x2,x3,x4,...") v = sp.Matrix([x1,x2,x3,x4,...]) g...

## Transforming polynomial variable in Numpy

python,numpy,sympy,symbolic-math,polynomial-math
I am trying to learn how to use Numpy. Consider I have the roots of a polynomial. I use coeff = np.polynomial.polynomial.polyfromroots(roots) to get the coefficients of the polynomial as an array. Then I use print np.poly1d(coeff) To print out the polynomial. Let that polynomial be x^2 +3x + 2...

## How to bin all subsets of a python list into n bins

python,numpy,combinations,sympy,itertools
I have a list: a = range(2) and I am trying to get the list's contents binned into n(=3) bins, in all possible ways, giving (order not important): [[[],[0],[1]], [[],[1],[0]], [[],[0,1],[]], [[],[],[0,1]], [[0],[1],[]], [[0],[],[1]], [[1],[0],[]], [[1],[],[0]], [[0,1],[],[]]] So far, I have been using the sympy.utilities.iterables library, firstly to get all...

## Generating Legendre Polynomials in Sympy

python,math,sympy
I have a project where I would like to use a set of associated legendre polynomials. I had the idea to generate the code for a set of degrees and orders by using sympy. I can get sympy to calculate specific values like explained in the docs: http://docs.sympy.org/dev/modules/mpmath/functions/orthogonal.html#legenp I cannot...

## Not aligned Sympy's nice pritting of division

python,sympy
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 # ─── # -5 # Note that "-5" is displayed slightly more on the right than "-1". Reason/fix for this? EDIT: I...

## Calculate dimension of an affine algebraic set

python,sympy
I'm trying to calculate the dimension of algebraic sets in affine space using sympy. I doubt sympy has functionality for finding the dimension defined as; "the maximal length of all ascending chains of proper distinct non-empty sub varieties" Is there a built in sympy function to calcuate the krull dimension...

## How to rewrite sin(x)^2 to cos(2*x) form in Sympy

sympy
It is easy to obtain such rewrite in other CAS like Mathematica. TrigReduce[Sin[x]^2] (*1/2 (1 - Cos[2 x])*) However, in Sympy, trigsimp with all methods tested returns sin(x)**2 trigsimp(sin(x)*sin(x),method='fu')...

## Installing github version of package with Anaconda

python,git,sympy,anaconda
I have Sympy installed with Anaconda, my version (of Sympy) is 0.7.6 . I want to be able to use the git version of Sympy. Do you know how can I tell to Anaconda to substitute the 2 versions ? I've tried using the git clone command, but the Sympy...

## Sympy: how to factor constants out of sum?

python,sympy
As a Sympy newbie, I was trying to learn how to use it by test-driving it on a simple case (simple least squares fit to a straight line). This is what I have as result of previous calculations (next line is an output from iPython): Sum(2*a*x(i)**2, (i, 1, N)) +...

## Simplify nested exponentials and logs with sympy

python,sympy,symbolic-math
How can I get sympy to simplify an expression like log(exp(exp(x))) to exp(x)? It seems to work on simpler expressions like exp(log(x)) => x. This is a minimal example showing what I've tried so far: import sympy from sympy import exp, log x = sympy.symbols('x') a = exp(log(x)) print a...

## How to evaluate an expression-tree in sympy?

python,sympy
This might be a very basic question, but I am wondering if I am missing a sympy-functionality after reading through the documentation, or if there is really non included for this use-case. I have the following expressions: a = sympify("b + 5") b = sympify("c + 5") c = sympify("5")...

## trouble with sympy solve with mathematical equation (python)

python,math,numpy,sympy,callable-object
I have 8 lists of variables (each identical size). For each element of the list I wish to create a new list that is the result of a mathematical solution involving the variables. Here is my code using Sympy: from sympy.solvers import solve from sympy import Symbol x = Symbol('x')...

## detect if variable is of sympy type

python,sympy,isinstance

## Using sympy's latex() function without calculating the input

math,python-3.x,latex,sympy
I want to get latex output from a sympy expression without calculating the expression. For instance if I do latex((2+3)/7) the output would be 5/7 (with latex), but what I am after is for it to just output (2+3)/7 with latex. Like this: \frac{2+3}{7}

## Is it possible to plot implicit 3d equation using sympy?

python,matplotlib,plot,3d,sympy
Is there a way to plot a 3 variable implicit equation using sympy. Going by docs it has only support for implicit 2d plots. Or are there any other options for plotting a 3d plot using python where the equation can be an input from user

## Find the Most Nested (), Solving with SymPy, and Work Outwards?

python,regex,scipy,sympy
The overall goal is to solve a simple linear [one var] equation in the same way that a human would. So, finding the most nested (), distributing until all the multiplication is done. (then combining like terms and then dividing one side of x's to the other side of constants)...

## Roots of a polynomial mod a prime

algorithm,sympy,polynomial-math,polynomials
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...

## IPython, Ploting a Polynomial

plot,ipython,sympy,polynomials
I've been working with Sympy for an Interpolation, in wich I obtain as a result a 7th degree Polynomial (ax^7 + bx^6 + cx^5 + ...+ h) wich I want to Plot, but wen I try to plot it I get errors, for example, if i try: plt.plot(r,U.subs(x,r)) where r...

## Define functions recursively with derivative in Python

python-2.7,scipy,sympy
How can I define a succession of functions h_k: k=1,2,3,... by using two known functions f=f(x) and g=g(x) as follows: h_1=f/g, h_{k+1}=diff(h_k,x)/g, for k=1,2,3,..... Note that the new functions have two entries h(k,x)=h_k(x). I want to do it in Sympy....

## Why does Sympy cut off polynomial terms with small coefficients?

python,sympy,polynomials,coefficients
I am trying to convert an expression containing terms with various degrees of a symbolic variable z_s into a polynomial in python using sympy.Poly() so that I can then extract the coefficients using .coeffs(). The expression I have is a high-order polynomial with independent, symbolic variable z_s. For some reason,...

## numerical value for an expression in sympy

python,python-2.7,numpy,scipy,sympy
sympy is giving me the following expression: 2.8*x**2 - 4.0*x*Integral(1.0*x**2*sqrt(-x**2 + 1), (x, -1.0, 0.0)) + 1.33333333333333*x + 0.133333333333333 i want sympy to give me numerical values for the coefficients. how can i do this? .evalf and N() didn't work. this is my code from numpy import * from sympy...