Hey there. I thought numpy logspace worked like numpy linspace so I wrote 'node_distance': np.logspace(0.0001, 0.1, 22).tolist(), thinking it will set my node distance to 22 values BETWEEN 0.0001 and 0.1. Instead I have got values from ~1.0002 and ~1.26. Help me get a log interpolation between 0.0001 and 0.1
Assistant
You're right that numpy.logspace is similar to numpy.linspace, but it operates in the logarithmic scale. The key difference lies in how the function interprets its start and stop parameters:
numpy.linspace generates evenly spaced numbers over a specified interval.
numpy.logspace generates numbers spaced evenly on a log scale (a geometric progression). Specifically, it creates numbers between 10^start and 10^stop.
What Went Wrong
In your case, you used:
python
np.logspace(0.0001, 0.1, 22)
This interprets as creating numbers between:
(10^{0.0001} \approx 1.0002)
(10^{0.1} \approx 1.2589)
Hence, all your generated values are between approximately 1.0002 and 1.2589, not between 0.0001 and 0.1.
How to Achieve Logarithmic Spacing Between 0.0001 and 0.1
To generate 22 logarithmically spaced values between 0.0001 and 0.1, you should specify the exponents corresponding to these numbers in base 10:
(0.0001 = 10^{-4})
(0.1 = 10^{-1})
Here's how you can do it:
python
import numpy as np
node_distance = np.logspace(-4, -1, 22).tolist()
print(node_distance)
Explanation:
start = -4: This corresponds to (10^{-4} = 0.0001).
stop = -1: This corresponds to (10^{-1} = 0.1).
num = 22: The number of points you want between these exponents.
Output
Running the above code will give you a list of 22 values logarithmically spaced between 0.0001 and 0.1. Here's a truncated example of what the output will look like:
These values are spaced evenly on a logarithmic scale between 0.0001 and 0.1, which should work for your node distance settings.
Additional Tip
If you ever need to generate log-spaced values with a different base (not base 10), you can use the natural logarithm (np.logspace uses base 10 by default) in combination with exponentiation. However, for most use cases, including yours, specifying the correct exponents as demonstrated above is sufficient.
Summary:
Use np.logspace with exponents corresponding to your desired range in powers of 10:
python
node_distance = np.logspace(-4, -1, 22).tolist()
This will give you 22 logarithmically spaced values between 0.0001 and 0.1.