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

Returning automatic derivatives with Sympy
I'm trying to build a class whose goal is returning the derivative of a function
f
also as a function. I've read about Sympy and I started to try it with this package.Main problem
Let's suppose that I have a simple function with only one parameter, like this:
def f1p(x): return x**2 + 5**x * 2*x + 1
Now, I have a method that ensures the funcion has one only paramether and then calculates the derivative (only as expression):
from sympy import * import inspect def get_derivative(fun): parameters=inspect.getargspec(f).args if(len(parameters)>1): raise ValueError('Function has more than one parameter.') fdiff=fun(Symbol(parameters[0])).diff() print(fdiff)
Let's say that
fdiff
contains the main expression of derivative function, so, the problem I want to solve is returning a function in order to evaluate it, for example:f_deriv=get_derivative(f1p) #f_deriv is a callable function print(f_deriv(a)) #Prints derivative value of f1p in a
Note: I've try
return eval/exec(diff)
but is a bad idea, because declaring parameters is needed. Also I tried to wrap the expresion with:eval('def foo('+parameters[0]+'):\n\treturn '+diff)
And it still is not a good idea.
Extending problem
Suppose now we use (for example) logarithms in our base function, so, we implement it this way
import math def f1p(x): return x**2 + 5**x * 2*x + 1 + math.log(x)
When I apply the previous function to this, I get the following error:
TypeError: can't convert expression to float
This is due to Sympy is not capable of understand
math.log(x)
. So, my questions are: To solve main problem: Is there any way to return a callable function from
get_derivative
function?  To solve extended problem: Is there any way to translate sympy expression to Python evaluable expression?
Thanks in advance.
 To solve main problem: Is there any way to return a callable function from

TypeError occurs when I solve this system of equations
from math import exp, log from sympy import Symbol, symbols, solve, Eq Z = 70;p_abs = 101.325*((12.25577*(10**5)*Z)**5.2559);t_std = 150.0065*Z;RH = 0.5;t_db = 20 K_db = t_db + 273.15 C8 = 5.8002206e+03C9 = 1.3914993e+00;C10 = 4.8640239e02;C11 = 4.1764768e05;C12 = 1.4452093e08;C13 = 6.5459673e+00 p_ws_db = exp(C8/K_db+C9+C10*K_db+C11*K_db**2+C12*K_db**3+C13*log(K_db))/1000 p_w_db = p_ws_db*RH # partial pressure of water vapor_db W_s_db = 0.621945*p_ws_db/(p_absp_ws_db) W_db = 0.621945*p_w_db/(p_absp_w_db) p_w_wb = (W_db*p_abs)/(0.621945+W_db) t_wb, K_wb, W_s_wb, p_ws_wb = symbols('t_wb K_wb W_s_wb p_ws_wb') e1 = Eq(K_wb, t_wb + 273.15) e2 = Eq(p_ws_wb, exp(C8/K_wb+C9+C10*K_wb+C11*K_wb**2+C12*K_wb**3+C13*log(K_wb))/1000) e3 = Eq(W_s_wb, (W_db*((2501+1.86*t_db)4.186*t_wb)+1.006*(t_dbt_wb))/(25012.326*t_wb)) e4 = Eq(p_ws_wb, (W_s_wb*p_abs)/(W_s_wb+0.621945)) print(fsolve([e1,e2,e3,e4], t_wb, p_ws_wb, K_wb, W_s_wb))
This is the code what have 4 equation and 4 unknowns. I am trying solving this equations by using sympy.solve. But it can't solve with an error;
TypeError: can't convert expression to float.
I guess exp or log create a problem.

Vector not defined by components in sympy
Is it possible in the Sympy vector package to initialize a vector without having to declare its components? Often when we do symbolic calculations it is not required to explicitate components.