FAQ Database Discussion Community


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...

Units for Sympy Quantum Harmonic Oscillator

python,sympy,units-of-measurement,quantum
I am using the Sympy function for the generating the 3D Quantum Harmonic Oscillator radial wavefunction (http://docs.sympy.org/dev/modules/physics/sho.html#sympy.physics.sho.R_nl) in order to use the Harmonic Oscillator Basis to solve for the energies of the potential well. The documentation for the radial wavefunction is not very clear on what units are being used...

Very Basic SymPy Error

python,scipy,sympy
I'm trying to learn SymPy. I've written the following and its throwing an error. I haven't even gotten to anything complicated... from sympy.solvers import solve from sympy import Symbol string = "10x+4=7" #equation = raw_input("Enter an equation") left, right = string.split('=') string = left + "-(" + right + ")";...

How can I get a list of the symbols in a sympy expression?

python,python-3.x,sympy
For example, if I run import sympy x, y, z = sympy.symbols('x:z') f = sympy.exp(x + y) - sympy.sqrt(z) is there any method of f that I can use to get a list or tuple of sympy.Symbol objects that the expression contains? I'd rather not have to parse srepr(f) or...

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...

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...

AttributeError: 'Add' object has no attribute 'log' Python

python,numpy,sympy,solver
I am making a syntax error, some how my syntax is leading python to think I am writing +.log some where? Please take a look at the code and let me know. Can any one suggest, a function that can be used instead to solve this Algorithm import numpy as...

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...

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")...

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....

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...

how to solve first-order linear differential equations analytically and numerically with sympy?

python,scipy,sympy,numerical-methods,differential-equations
How can simple linear differential equations like this one be solved in sympy? y' + p(t)y = q(t) I'm looking to solve it in two ways: symbolically (analytically) if possible, if sympy can derive the integrating factor, etc., and also a way to do it numerically so that the two...

detect if variable is of sympy type

python,sympy,isinstance
I have a variable which may or may not be a sympy class. I want to convert it to a float but I’m having trouble doing this in a general manner: $ python Python 2.7.3 (default, Dec 18 2014, 19:10:20) [GCC 4.6.3] on linux2 >>> import sympy >>> x =...

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}

Advanced indexing for sympy?

python,numpy,sympy
With numpy, I am able to select an arbitrary set of items from an array with a list of integers: >>> import numpy as np >>> a = np.array([1,2,3]) >>> a[[0,2]] array([1, 3]) The same does not seem to work with sympy matrices, as the code: >>> import sympy as...

solve sympy equation results in an error on numpy array dimensions

arrays,numpy,sympy
I'm currently trying to solve a system of equations using sympy (following this lecture on scientific python) and I'm getting the following error: Traceback (most recent call last): File "VMT.py", line 13, in <module> [Vmc, Vgp, tmc, tgp]) File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 311, in solve _assertRank2(a, b) File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 155,...

Sympy can graph the function but cannot find the obvious root

python,math,sympy,equation-solving
I have a function in sympy which is quite ugly: In [79]: print(expected_c) Out[79]: 2**(n - 2)*n*(n - 1)*binomial(m, n)*factorial(m - 3/2)*factorial(m - n)/(binomial(2*m, n)*factorial(m - n/2)*factorial(m - n/2 - 1/2)) I want to solve it for some $m$ (say 10000) to find $n$ such that my formula values, say...

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...

Error from program using SymPy

python,sympy
I would like to use the SymPy packages to find the roots of a fourth-order polynomial equation. Subsequently I would like to plot these roots as a function of the parameters of the polynomial equations. I have written the piece of code below. It seems to calculate everything fine, but...

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')...

How to do function composition in Sympy?

python,sympy,computer-algebra-systems
I want to do something like h = f(g(x)) and be able to differentiate h, like h.diff(x). For just one function like h = cos(x) this is in fact possible and the documentation makes it clear. But for function compositions it is not so clear. If you have done this,...

Docs for sympy symbol naming conventions for special rendering?

python,sympy
Is there any documentation that lists the predefined symbol names (or spelling conventions) that sympy renders as specialized math symbols? For instance, I found the ones shown below by trial and error. I've tried Google searches and searches within the sympy docs and source code without finding anything other than...

Factor sympy expression to matrix coefficients?

python,matrix,sympy,symbolic-math
I have tried to be diligent in looking through documentation and am coming up empty. I am trying to factor or eliminate terms in a expression to matrix form. My problem appears to differ from polynomial factoring (as I plan to implement a function phi(x,y,z) = a_1 + a_2*x +...

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')...

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

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...

Getting element-wise equations of matrix multiplication in sympy

matrix,sympy
I've got 2 matrices, first of which is sparse with integer coefficients. import sympy A = sympy.eye(2) A.row_op(1, lambda v, j: v + 2*A[0, j]) The 2nd is symbolic, and I perform an operation between them: M = MatrixSymbol('M', 2, 1) X = A * M + A.col(1) Now, what...

No module named sympy

python,python-2.7,ipython,ipython-notebook,sympy
Hi I'm learning linear algebra with python with an Edx course. (http://nbviewer.ipython.org/github/ULAFF/notebooks/tree/may-14-2014/). On "02.4.2.10 Practice with matrix-vector multiplication" with the first box, the code is: import generate_problems as gp print("What is the result of the matrix vector product below?") p = gp.Problem() p.new_problem() generate_problems is a module that the professor...

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...

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...

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...

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...

Sympy Matrix.col_insert not working python 3

python,sympy
So i have tried the following code copied straight from the docs, and it seems to me the function is not doing what it is supposed to: import sympy as sp M = sp.Matrix(3,3,lambda i,j: i+j) V = sp.Matrix.zeros(3, 1) M.col_insert(1,V) print(M) gives the output Matrix([[0, 1, 2], [1, 2,...

How to rearrange sympy expressions containing a relational operator

python,sympy
I have expressions containing a relational operator, symbols, and constants. I'd like to rearrange the expressions so that (to the extent possible) all the constant terms are on one side of the relational operator, and the remaining terms on the other side. For example, I'd like to rearrange: x -...

Sympy: Using logical operators on relationals

python,python-3.x,sympy
Is this possible? What I want to do is something like this: sympify('2>1 | 2<1') I pretty much only need 'or' and 'and'. I also tried doing sympify('2>1') | sympify('2<1'). Ideally I would want to just be able to send in a string with relations and logical operators to a...

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...

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,...

Sympy polynomials with `mpfr` coefficients?

python,python-3.x,sympy,polynomial-math,gmpy
I want to use Sympy's polynomials, but I also want to use higher-precision coefficients. Just Doing It seems to give me polynomials with sympy.core.numbers.float coefficients. import sympy from sympy import Poly from sympy.abc import x from gmpy2 import mpfr, get_context get_context().precision = 150 #float64 can't tell this from 1.0 one_and_change...

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...

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...

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)) +...

Representing rings of algebraic integers

python,sympy
I'm trying to represent the ring; where theta is the root of a monic irreducible polynomial f with integer coefficients of degree d. This ring is a subring of the algebraic integers, which itself is a subring of the field; I can represent this field with sympy's AlgebraicField class Q_theta...

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...

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...

Double integral with variable boundaries in python Scipy + sympy (?)

python,scipy,sympy,integral
The full mathematical problem is here. Briefly I want to integrate a function with a double integral. The inner integral has boundaries 20 and x-2, while the outer has boundaries 22 and 30. I know that with Scipy I can compute the double integral with scipy.integrate.nquad. I would like to...

Sympy right hand side of inequality

python,sympy
I am using sympy to solve a simple inequality. After solving it, I want to assign the right hand side of the solution to a new variable. from sympy.solvers.inequalities import reduce_rational_inequalities from sympy import Symbol x = Symbol('x', real=True) sol = reduce_rational_inequalities([[x*0.2 >= 1]], x) print type(sol), '\n', sol >>...

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)...

Integral of piecewise function gives incorrect result

python,sympy,integral
Using the recent version of sympy (0.7.6) I get the following bad result when determining the integral of a function with support [0,y): from sympy import * a,b,c,x,z = symbols("a,b,c,x,z",real = True) y = Symbol("y",real=True,positive=True) inner = Piecewise((0,(x>=y)|(x<0)|(b>c)),(a,True)) I = Integral(inner,(x,0,z)) Eq(I,I.doit()) This is incorrect as the actual result should...