Search code examples
pythonoctavetrigonometrycomplex-numbers

Result of complex arithmetic in Octave and Python differs


When I calculate these values, v and w, in octave and python the output values are very similar, see some outputs below:

 v octave values =

 Columns 1 through 3:

          0 - 223.4950i          0 - 194.4942i          0 - 167.4933i

 ...

 Columns 16 through 18:

          0 +        0i     1.4142 +        0i          0 +   2.0000i

 ... 
 Columns 28 through 30:

          0 + 142.4921i          0 + 167.4933i          0 + 194.4942i

 Column 31:

          0 + 223.4950i
 v python values 
 [-0.        -223.49496639j -0.        -194.49421585j
 -0.        -167.49328345j -0.        -142.49210504j
...
 -1.41421356  +0.j          0.          +0.j
 ...
  0.        +167.49328345j  0.        +194.49421585j
  0.        +223.49496639j]
w octave values =

Columns 1 through 4: %before 0

  -4.7124 + 8.5456i  -4.7124 + 8.3371i  -4.7124 + 8.1130i  -4.7124 + 7.8705i

 ...

 Columns 13 through 16:

  -4.7124 + 3.4386i  -4.7124 + 1.6479i  -1.8464 +      0i        0 +      0i

...

 Columns 25 through 28: %after 0

   4.7124 - 6.9950i   4.7124 - 7.3165i   4.7124 - 7.6064i   4.7124 - 7.8705i

 Columns 29 through 31:

   4.7124 - 8.1130i   4.7124 - 8.3371i   4.7124 - 8.5456i
w python values 
 [-4.71238898+8.54562334j -4.71238898+8.33714905j -4.71238898+8.11296806j
 ...
  0.        +0.j          1.84643913+0.j          
...
  4.71238898+7.87049456j  4.71238898+8.11296806j  4.71238898+8.33714905j
  4.71238898+8.54562334j]

but when I added these values v + w and got z, z = v+w, I got differents outputs values in python and octave. This it's giving me trouble with the code below.

You can see the outputs for z = v + w below:

 z = v+w octave values = 

 Columns 1 through 3:

    -4.7124 - 214.9493i    -4.7124 - 186.1571i    -4.7124 - 159.3803i
 ...
 Columns 16 through 18: %after 0

          0 +        0i     3.2607 +        0i     4.7124 +   0.3521i

 Columns 19 through 21:

     4.7124 +   3.9098i     4.7124 +   9.9835i     4.7124 +  18.2866i

 ...
 Columns 28 through 30:

     4.7124 + 134.6216i     4.7124 + 159.3803i     4.7124 + 186.1571i

 Column 31:

     4.7124 + 214.9493i
z= v+w python values 
 [-4.71238898-214.94934305j -4.71238898-186.1570668j
 -4.71238898-159.38031539j -4.71238898-134.62161049j
 ...
-3.26065269  +0.j          0.          +0.j
  3.26065269  +0.j          4.71238898  +3.64791843j
 ...
  4.71238898+127.09702398j  4.71238898+150.3625996j
  4.71238898+175.60625151j  4.71238898+202.8313649j
  4.71238898+232.04058972j]

Now i share the python/octave codes:

%octave code

x = -15:1:15;
E = 3;

p = sqrt(E - x.^2);
v = x.*p;

w = E.*asin(x./sqrt(E));

z = w + v;
#python Code
import numpy as np

x = np.arange(-15, 16, 1)
E = 3

p = np.sqrt(E - x.astype(complex)**2)
v = x.astype(complex)*p

w = E*np.arcsin(x.astype(complex)/np.sqrt(E))

z = w + v

I don't know what is wrong and can't find information in internet.

Does anyone know why this is happening and how I can fix it?


Solution

  • Python np.arcsin()and Octave asin() behave differently:

    >>> np.arcsin(0.5)
    0.5235987755982988
    >>> np.arcsin(2.0+0j)
    (1.5707963267948966+1.3169578969248166j)
    
    >> asin(0.5)
    ans =  0.52360
    >> asin(2.0)
    ans =  1.5708 - 1.3170i
    

    When the result is complex, the sign of the imaginary part differ. Both results are correct, it's just that numpy and Octave use different conventions.

    Mathematically the inverse sine has an infinity of solutions, numpy and Octave just don't pick the same one.