0

Given that cos(pi/3) is 1/2, and that tan(pi/4) is 1, I expected that acos(1/2) returns pi/3 and atan(1/1) returns pi/4. But SymPy returns 1.04719755119660 and 0.785398163397448, respectively. However, by using sqrt() as a workaround, then the correct pi/3 and pi/4 values are returned. Why is that?

import sympy as sp

print(sp.acos(1/2))
# 1.04719755119660
print(sp.acos(sp.sqrt(1)/2))
# pi/3

print(sp.atan(1/1))
# 0.785398163397448
print(sp.atan(sp.sqrt(1)/1))
# pi/4
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Quiche31
  • 119
  • 10

2 Answers2

1

Okay, so I reckon you're running into this issue 'cause SymPy isn't recognizing the inputs you're giving it as special values right off the bat. When you feed it 1/2 or 1/1, it just thinks "ah, a decimal number, let me give you a decimal answer." And it doesn't even check whether the decimal you gave it might represent a neat fraction of pi or anything like that.

On the other hand, when you use sqrt(), it's like a flag to SymPy to say "hey, this isn't just any old number, it's a special one!" Because sqrt(1) is more mathematically "complex" than just 1, SymPy pauses to think "wait a minute, I better check if this is a special value before I spew out a decimal answer." And so it recognizes that sqrt(1)/2 and sqrt(1)/1 are really 1/2 and 1/1, and it checks if those are special values (which they are, corresponding to pi/3 and pi/4), and so it gives you the answer in terms of pi.

It's a bit like if I asked you "what's half of 2?" and you said "1," but if I asked you "what's half of the square root of 4?" you might think "wait, square root of 4, that's 2, oh, half of 2 is 1!" It's the same answer, but the way I asked the question made you think about it a little differently.

  • You nailed it, if I may say so! – Quiche31 Jul 08 '23 at 08:12
  • This answer looks like it was generated by an AI (like ChatGPT), not by an actual human being. You should be aware that [posting AI-generated output is officially **BANNED** on Stack Overflow](https://meta.stackoverflow.com/q/421831). If this answer was indeed generated by an AI, then I strongly suggest you delete it before you get yourself into even bigger trouble: **WE TAKE PLAGIARISM SERIOUSLY HERE.** Please read: [Why posting GPT and ChatGPT generated answers is not currently acceptable](https://stackoverflow.com/help/gpt-policy). – tchrist Jul 08 '23 at 18:15
0

https://stackoverflow.com/users/22159264/nail-steiger was correct, I verified his statement with the following:

import sympy as sp
import numpy as np

print(type(1/1))
# float
print(type(np.sqrt(1)/1))
# numpy.float64
print(type(sp.sqrt(1)/1))
# sympy.core.numbers.One

The motivation for my question was to get a simple function that converts a complex number from cartesian to polar form (perhaps this already exists in some library). so the trick is to always use sympy.sqrt(1) to force the parameter of atan to be of type sympy.core.numbers.* instead of float or numpy.float64. Here is teh working code:

import sympy as sp

def to_polar(z):
    r = sp.sqrt(sp.re(z)**2 + sp.im(z)*2)
    theta = sp.atan(sp.sqrt(1)*sp.im(z)/sp.re(z))
    return (r, theta)

# polar form for -3+3i
print(to_polar(-3+3*I))
# (3*sqrt(2), -pi/4)

# polar form for 4+i
print(to_polar(4+I))
# (sqrt(17), atan(1/4))

# polar form for -2+pi*i
print(to_polar(-2+pi*I))
# (sqrt(4 + pi**2), -atan(pi/2))
Quiche31
  • 119
  • 10