numpy e^i(theta) and trigonometric cos(theta) + isin(theta) does not match

Question:

I read,

enter image description here

So I tried to do apply this to a list of points as shown below

enter image description here

And below part works perfectly as expected.

Zj = np.array([1. +0.j , 0.5+0.5j, 0. +0.j , 0.5-0.5j, 1. +0.j ])
δj      = Zj[1: ] - Zj[:-1]
assert(np.allclose(δj, np.array([-.5+0.5j , -.5-.5j, 0.5-.5j, .5+.5j])))
assert(np.allclose(np.angle(δj, deg=True) , np.array([ 135., -135.,  -45.,   45.])))

But when I take $e^{itheta}$ that does not work as intended.

e_iVj   = -1j * ((δj)/abs(δj))
assert(np.allclose(np.cos(np.angle(δj)) + np.sin(np.angle(δj)), e_iVj))

EDIT:

As @hpaulg suggested, I’ve added 1j component in to sin. but still its not quite match up with ref to signs. Values are correct

>>> np.cos(np.angle(δj)) + 1j* np.sin(np.angle(δj))
array([-0.70710678+0.70710678j, -0.70710678-0.70710678j,
        0.70710678-0.70710678j,  0.70710678+0.70710678j])
>>> e_iVj
array([ 0.70710678+0.70710678j, -0.70710678+0.70710678j,
       -0.70710678-0.70710678j,  0.70710678-0.70710678j])
>>> 1j * ((δj)/abs(δj))
array([-0.70710678-0.70710678j,  0.70710678-0.70710678j,
        0.70710678+0.70710678j, -0.70710678+0.70710678j])

@peterwhy

Could you please let me know if the equation on the first snippet is correct as per your understanding.

-- Confirming Angles are represented correctly
>>> Zj= Zj[0]
>>> Zj_p1 = Zj_p1[0]
>>> δj = Zj_p1 - Zj
>>> np.angle(δj, deg=True)
135.0 # Confirmed ok
-- Showing e^iϑ = δj/abs(δj)
>>> ϑ = np.angle(δj)
>>> e_iϑ = np.exp(1j*ϑ)
>>> e_iϑ
(-0.7071067811865475+0.7071067811865476j)

>>> δj/abs(δj)
(-0.7071067811865475+0.7071067811865475j)

>>> np.isclose(np.exp(1j*ϑ), δj/abs(δj))
True # confirmed both methods yield the same result
-- Showing e^-iϑ = abs(δj)/ δj
>>> np.exp(-1j*ϑ)
(-0.7071067811865475-0.7071067811865476j)

>>> abs(δj)/δj
(-0.7071067811865476-0.7071067811865476j)

>>> np.isclose(np.exp(-1j*ϑ), abs(δj)/δj)
True # confirmed both methods yield the same result

With reluctance, my guess is -i there is a typo. Above is what I’ve got with python which indicates -i there is not really required. Could you confirm ?

Asked By: XYZ

||

Answers:

You’re not computing e^(i*theta) or cos(theta)+i*sin(theta). Your expressions don’t make sense.

For an angle theta, this is how you would compute e^(i*theta):

np.exp(1j*theta)

and this is how you would compute cos(theta)+i*sin(theta):

np.cos(theta) + 1j*np.sin(theta)
Answered By: user2357112

Instead of calculating the angle from np.angle(δj), find the angle from either

  • np.angle(-1j * δj), or
  • np.angle(e_iVj)

and then pass this angle to np.cos(...) + 1j * np.sin(...).

That’s the angle your formula uses in its RHS, not simply δj.

(I come from a linked math.stackexchange question and an updated answer is given there.)

Answered By: peterwhy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.