How to change commutativity of a SymPy symbol after creation?
Suppose I create a SymPy symbol x
.
import sympy as sp
x = sp.Symbol('x', commutative = False)
How can I change the commutative assumption to True
without creating a new Symbol?
I tried
with sp.assuming( sp.Q.commutative(x) ):
print( sp.ask( Q.commutative(x) ) )
But it still gives False
.
See also questions close to this topic

Use sympy to solve a transcendental equation
Experienced with Python. New to Sympy.
I have a transcendental equation, f(x) = sin(x)  x.
If y = f(x), I want to solve for x knowing y.
I think Sympy can do this, but I have no experience with it. Can someone explain what I should do?
(The question Transcendental Equation has answers for handrolling the iterative approach, which is my backup.)
Here is what I have tried:
from sympy import * x = symbols('x') solve(Eq(sin(x)  x)) # Exception raised here # NotImplementedError: multiple generators [x, sin(x)] # No algorithms are implemented to solve equation x + sin(x)
I recognize this does not even communicate that I have a known value for y. As you can see, I don't understand what to do at all.
This would be an iterative solution. Is there a way to get sympy to do this, or should I be using a different Python package for iterative solutions?
All help is appreciated.

why sympy count_ops() fail on this result from integration?
I use sympy's
count_opt
as a way to estimate size (leaf count) ofantiderivative
returned byintegrate
.http://docs.sympy.org/latest/modules/core.html
I found it fails on some expressions. This is using sympy 1.1.1 on
Python 3.6.5 Anaconda, Inc. (default, Apr 29 2018, 16:14:56) [GCC 7.2.0] on linux
Here is an example
from sympy import * x,n,a = symbols('x n a') integrand = x**n*log(a*x) anti= integrate(integrand,x) count_ops(anti)
and now
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/anaconda/lib/python3.6/sitepackages/sympy/core/function.py", line 2473, in count_ops if a.is_Rational: AttributeError: 'NoneType' object has no attribute 'is_Rational'
Something about this result it does not like
>>> anti Piecewise((None, Eq(n, 1)), (n*x*x**n*log(a)/(n**2 + 2*n + 1) + n*x*x**n*log(x)/(n**2 + 2*n + 1) + x*x**n*log(a)/(n**2 + 2*n + 1) + x*x**n*log(x)/(n**2 + 2*n + 1)  x*x**n/(n**2 + 2*n + 1), True))
Is this a known issue? Why does it happen? Is this a bug? Should I report it? How?
The above is on linux Manjaro 17.1 XFCE

How to delete all defined SymPy symbols without explicitly listing them?
SymPy and Python newbie here.
I run a script that iterates over a number of test files. Each file defines a set of symbols to use. At the end of each loop iteration, I want to delete all the symbols created earlier using the
symbols()
statement and nothing more, since I have other nonsymbol variables around I am using.I can't do
del(x)
anddel(y)
since each iteration will load a different set of symbols.I can put a list of the symbols used in a list, or tuple, then at end of each iteration, go over the list of symbols that was created, and delete them one at a time using
del
But I can't get this to work.Here is an example.
from sympy import * x,y = symbols('x y') symbolsToDelete = (x,y) #I also tried symbolsToDelete = ('x','y') x=y**3 y=99 #now I want to delete all symbols defined above. But I can't #use del(x) or del(y) explicitly. So I tried for z0 in symbolsToDelete: del(z0)
But the above does not work,
x
andy
are still there, withx=y**3
andy=99
.Again, I know I can do
del(x)
anddel(y)
at the end, but I am reading the names of the variables from a file, and the names are in a list.I could only put the symbols in one variable like I did above, but do not know how to iterate over this tuple and then use
del()
on each entry to remove the corresponding symbol defined above at end of iteration usingsymbols
command.I do not know what the correct syntax should be.
I am using Python 3.6.5.