0

The expression I am interested in:

Rendered expression

It mentions I have to use cmath modules for the equation.

so far I've tried

ans = cmath.sqrt(-1j) + (cmath.e ** (cmath.pi * cmath.sqrt(-1j)))

Is putting j after the irrational number correct since i = sqrt(-1) is a complex number?

4
  • 1
    It depends what you want to do with the equation. You can express it as a string: "i + e^(i * pi)" (BTW it's not an equation, there is no = in it) Commented Sep 6, 2024 at 7:29
  • 2
    Where is the cmath.sqrt() coming from? There is no square root in the formula you posted. Commented Sep 6, 2024 at 7:38
  • 1
    I should save yourself some time (and get better accuracy) and just write 1j-1 Commented Sep 6, 2024 at 9:00
  • You can also try 1j+cmath.rect(1,cmath.pi) Commented Sep 6, 2024 at 16:34

3 Answers 3

4

You can express the math equation i + e^(i * pi) in Python using the cmath module, which provides support for complex numbers.

import cmath

# Define i as the imaginary unit
i = 1j

# Calculate the expression i + e^(i * pi)
result = i + cmath.exp(i * cmath.pi)

print(result)
  • 1j represents the imaginary unit 𝑖
  • cmath.exp() calculates the exponential function for complex numbers
  • cmath.pi is the constant π
Sign up to request clarification or add additional context in comments.

2 Comments

@Andrew - Unfortunately "if is not perfect as it could be" is not a logically sound utterance. Something is either perfect, or not perfect, unless you are writing scripts for toothpaste advertisements.
I'm just trolling your troll of Dima :-) This is a programming site ... remember ... not a mathematician's site.
2

Yes, your use of j is incorrect. Use complex() to represent complex numbers rather than trying to calculate them with sqrt().

import cmath
complex(0, 1) + cmath.exp(complex(0, cmath.pi))
(-1+1.0000000000000002j)

From the help() output of complex()

class complex(object)
 |  complex(real=0, imag=0)
 |  
 |  Create a complex number from a real part and an optional imaginary part.
 |  
 |  This is equivalent to (real + imag*1j) where imag defaults to 0.

As @mkrieger1 points out in the comments, a simpler solution is

1j + cmath.exp(1j * cmath.pi)

3 Comments

Or instead of complex(0, 1) just write 1j.
You just taught me something new. Thank you.
That's a great answer from a programer's perspective. Unfortunately, mathematically, it is not as perfect as it could be. The 100% correct answer is "(-1+1j)", per Euler's identity (ref. en.wikipedia.org/wiki/Euler%27s_formula).
1

Since, as shown in these answers, cmath.exp(1j*w) is broken, at least for w = cmath.pi, the math fact that e^(jw) = cos(w) +jsin(w) can be used instead. Testing has shown that cmath.sin(w) is correct for w in the range of -cmath.pi/2 through cmath.pi/2. Since testing has shown that cmath.cos(w) is not correct for any range of w of size pi/2, the math fact that cos w == sin(w+pi/2) should be used. The math fact that sin(w - pi) = -sin(w) is also needed. The above, altogether:

import cmath

print("i + e^(i * pi) = ", end = '')

# 1j + cmath.exp(1j * cmath.pi) ==
# 1j + cmath.cos(cmath.pi) + 1j * cmath.sin(cmath.pi) == 
# 1j + cmath.sin(cmath.pi + cmath.pi/2) + 1j * cmath.sin(cmath.pi) == 
# 1j + cmath.sin(1.5 * cmath.pi) + 1j * cmath.sin(cmath.pi) ==
# 1j + cmath.sin(1.5 * cmath.pi) - 1j * cmath.sin(cmath.pi - cmath.pi) ==
# 1j + cmath.sin(1.5 * cmath.pi) - 1j * cmath.sin(0) ==
# 1j - cmath.sin(1.5 * cmath.pi - cmath.pi) - 1j * cmath.sin(0) ==
# 1j - cmath.sin(cmath.pi/2) - 1j * cmath.sin(0) ==

print("i + e^(i * pi) = ", end = '')
print(1j - cmath.sin(cmath.pi/2) - 1j * cmath.sin(0))

3 Comments

It would be nice if python fixed this. The logic is not that hard, as shown in this example. Maybe someone involved in python dev can comment (beyond lectures on float issues please).
To clarify, due to float rounding there will still be some errors in the "correct" sin range mentioned above. But, at least the 3 points in this range (-pi/2, 0, and pi/2) where either or both of the real or imaginary parts are zero are correct.
As mentioned by supercat in question 6368055, the angle should also be first converted (using mod) to the normal [0 to 360 degree) = [0 to 2pi radians) range to avoid compiler errors (or float rounding issues if that is how you choose to see it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.