Python 3.7.4 lacks a proper linspace function?

  • Topic: Python 
  • Thread starter Wrichik Basu
  • Start date
  • Tags Tags
    Function Python
2 replies · 2K views · last reply
Science Advisor
Insights Author
Gold Member
Messages
2,188
Reaction score
2,694
Basically, I wanted to create a Numpy array with linearly spaced integers between 0 and 3, the increment being 0.01.

Yes, I know Numpy offers a linspace function. I used it like this: x = np.linspace(0, 3, num=300) (where np is numpy), and got this:

Printed Python console output showing a NumPy array of 300 floating-point values from 0 to 3, with values like 0., 0.01003344, 0.02006689 in rows of eight, with long decimal tails.


I know that the numbers cannot be exact (can you explain why?). Matlab returns something similar when the linspace function is used.

But then, in Matlab, I can write x = 0:0.01:3, which will give me this:

MATLAB console output of x showing a row vector of 40 values from 0.00000 to 0.39000 in steps of 0.01000, displayed in five column groups of eight.


Does Python have something similar?
 
Have you read the docs on this function?

It seems like the only difference is between
Python:
import numpy as np

np.linspace(0, 3,300, False)
np.linspace(0, 3,300, True)
 
  • Like
Likes   Reactions: Wrichik Basu