sqrt(2*pi) disply error

42 views
Skip to first unread message

Rodrigo Koblitz

unread,
Aug 8, 2024, 6:20:15 AM8/8/24
to sympy

Hi everyone,

I'm new to SymPy and I'm probably missing something basic. I'm trying to create the probability density function of the normal distribution, but I've run into a problem. When I try to create the sqrt(2*pi) part of the function, Colab displays it as the square of 2 multiplied by the square of pi, instead of the square root of their product.

I would really appreciate it if someone could help me with this.

Thanks!

init_printing(use_unicode=True)
result = sqrt(pi*2)
result
sympy_square_display.jpg

Oscar Gustafsson

unread,
Aug 8, 2024, 10:24:27 AM8/8/24
to sy...@googlegroups.com
It is mathematically correct, just that it may not be the expected output format. SymPy makes (educated) guesses how to represent an expression and how to print it (not always the same guesses are used for the two cases).

Your expression is represented as (use the srepr function on the expression):
Mul(Pow(Integer(2), Rational(1, 2)), Pow(pi, Rational(1, 2)))

So the two solutions are to avoid splitting Pow(Mul(..., ...), ...) into Mul(Pow(..., ...), Pow(..., ...)) or let the printer look for expressions with the same exponent and print those together.

A work-around, if you really want it to print in that exact way is to create the expression without automatic evaluation (which splits the expression) as:
Pow(Mul(Integer(2), pi), Rational(1,2), evaluate=False)

However, personally, I do not recommend this. The purpose of a CAS is to represent expressions "efficiently", so automatic evaluation is a good thing (try Add(Integer(1), Integer(-1), evaluate=False) ) in general.

BR Oscar


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/190de6eb-d75c-411e-a1e7-167a947242d9n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages