HWIK
HWIK

Reputation: 69

SYMPY's simplify not canceling out variables in a fraction

I am trying to simplify a symbolic expression with sympy.simplify, and the expression looks something like this, where all I should be left with is a e^c. But simplify does nothing to that expression. Is there a different function to do that? Or am I missing something?

Thanks!

[Edit:] The code I'm running is:

# Given the following probability function: f(c)=4*pi*(m/(2*pi*kb*T))^(3/2)*c^2*exp(-1/2*m*c^2/(kb*T))
# The ratio of the fraction of molecules with the rms speed, over 3*c_rms
# Where: c_rms=sqrt(3*kb*T/m)
# So the ratio is: f(c_rms)/f(3*c_rms)

from sympy import *

f = Symbol('f')
c = Symbol('c')
kb = Symbol('kb')
T = Symbol('T')
m = Symbol('m')

def f(c):
    f=4*pi*(m/(2*pi*kb*T))**(3/2)*c**2*exp(-1/2*m*c**2/(kb*T))
    return f

cr1= (3*kb*T/m)**(1/2)
cr3 = 3*cr1
p1 = f(cr1)
p3= f(cr3)
ratio = simplify(p1/p3)
print(ratio)

Where ratio is the expression to be simplified.

Upvotes: 0

Views: 392

Answers (1)

smichr
smichr

Reputation: 19047

When you use 1/2 as an exponent (or as the first operation in a product of factors) you create the python float 0.5. You will get a simpler result when you use S(1)/2 to create the rational 1/2 -- simplify will not, then, even be necessary:

from sympy import *

f = Symbol('f')
c = Symbol('c')
kb = Symbol('kb')
T = Symbol('T')
m = Symbol('m')

def f(c):
    f=4*pi*(m/(2*pi*kb*T))**(S(3)/2)*c**2*exp(-S(1)/2*m*c**2/(kb*T))
    return f

cr1= (3*kb*T/m)**(S(1)/2)
cr3 = 3*cr1
p1 = f(cr1)
p3= f(cr3)
ratio = p1/p3

ratio is exp(12)/9.

Upvotes: 1

Related Questions