2
# necessary imports
import numpy as np
import matplotlib.pyplot as plt

REPRODUCIBLE SETUP

Binet formula is the following, from here:

enter image description here

Let's define this function in python:

def binet(n):
    phi = (1 + 5 ** 0.5) / 2
    return ((phi**n) - (-1/phi)**n) / (5**0.5)

For the phi value, I used this.


WHAT WORKS

Let's calculate binet(n) for n=[0.1,0.2,0.3,0.4,0.5,...,4.9,5.0]:

[binet(x/10) for x in range(1,51)]

Let's plot it:

# our results
plt.plot([n.real for n in binetn],[n.imag for n in binetn])
# classic fibonacci numbers
plt.scatter([1,1,3,5],[0,0,0,0],c='r')

enter image description here

Looks good, aggrees with this & our math knowledge.


WHAT DOESN'T WORK

Based on the above I was confident this is going to work as well:

binetn=[binet(x) for x in np.arange(0.1,5.1,0.1)]

It doesn't, however. binetn becomes:

[nan,nan,nan,nan,nan,nan,nan,nan,nan,1.0,...,nan,nan,5.000000000000001]

Ie it is nan except when binet(n) is real.

It also gives a warning:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in double_scalars


QUESTION

Why can I loop through a list of numbers generated by range() & get complex results, while I cannot do the same with np.arange()?

zabop
  • 6,750
  • 3
  • 39
  • 84

1 Answers1

2

The duplicate focused on np.arange. But that isn't the problem here.

Compare passing a float to binet versus a np.float64:

In [52]: binet(0.1)                                                                                  
Out[52]: (0.06391735396852471-0.13170388861716523j)
In [53]: binet(np.float64(0.1))                                                                      
/usr/local/bin/ipython3:3: RuntimeWarning: invalid value encountered in double_scalars
  # -*- coding: utf-8 -*-
Out[53]: nan

or these iterations:

In [54]: [binet(float(x)) for x in np.arange(0.1, 5.1, 0.1)];                                        
In [55]: [binet(x) for x in np.arange(0.1, 5.1, 0.1).tolist()];                                      
In [56]: [binet(x) for x in np.arange(0.1, 5.1, 0.1)];                                               
/usr/local/bin/ipython3:3: RuntimeWarning: invalid value encountered in double_scalars
  # -*- coding: utf-8 -*-

Digging further, it appears to be related to complex. If the numpy argument is complex, the calculation is ok:

In [58]: binet(np.complex(.1))                                                                       
Out[58]: (0.06391735396852471-0.13170388861716523j)
In [59]: [binet(x) for x in np.arange(0.1, 5.1, 0.1).astype(complex)]; 

In fact we don't need to iterate. A complex array works just fine:

In [60]: x = np.arange(0.1, 5.1, 0.1).astype(complex)                                                
In [61]: binet(x)                                                                                    
Out[61]: 
array([0.06391735-1.31703889e-01j, 0.16378803-2.38746028e-01j,
       0.28913834-3.13167258e-01j, 0.42813892-3.50853867e-01j,
       0.56886448-3.51577584e-01j, 0.70044744-3.18660871e-01j,
       0.81402504-2.58333837e-01j, 0.9034083 -1.78872498e-01j,
       ...
       4.32034432-3.76903988e-02j, 4.54051382-2.60971457e-02j,
       4.76690299-1.30754885e-02j, 5.        +0.00000000e+00j])
hpaulj
  • 221,503
  • 14
  • 230
  • 353