d3-array
d3-array copied to clipboard
Add d3.linspace.
Related #90.
I can see how it would be useful, esp. if it returned a Float64Array (or had some option like dtype), and satisfy #90 at the same time.
Not sure about floor(n), since it could be nice to continuously change n (in the case where * endpoint* is false). [stick to the numpy def]
For logspace and geomspace it looks like there is no particular algorithmic challenge and not much use. I wouldn't want to add weight (bytes and documentation and cognitive load) just for the sake of it :)
Very nice!
This looks like a nice PR. Could be merged I suppose.
I don’t think the positional arguments with defaults are appropriate here because they aren’t analogous to Python’s named arguments. For example in Python you would say stuff like
np.linspace(2.0, 3.0, 5)
np.linspace(2.0, 3.0, num=5)
np.linspace(2.0, 3.0, num=5, endpoint=False)
np.linspace(2.0, 3.0, endpoint=False)
and the meaning is fairly clear. However in JavaScript it would be
d3.linspace(2, 3, 5)
d3.linspace(2, 3, 5)
d3.linspace(2, 3, 5, false)
d3.linspace(2, 3, undefined, false)
which I think is much less obvious. So I wonder if instead we should do something like:
d3.linspace(2, 3, 5)
d3.linspace(2, 3, {num: 5})
d3.linspace(2, 3, {num: 5, endpoint: false})
d3.linspace(2, 3, {endpoint: false})
This would also allow us to create typed arrays:
d3.linspace(2, 3, {num: 5, type: Float64Array})
I also don’t really like feeling stuck with Python’s names for these options. I think I’d prefer n or length instead of num. I’m using type instead of dtype because in JavaScript it would be the type of the array rather than the type of the value. (And I thought about inclusive or exclusive instead of endpoint but I couldn’t make up my mind.)
And lastly I wonder if there should be more symmetry between d3.range and d3.linspace. Like, what about:
d3.range(2, 3, {length: 5, inclusive: true})
Also this feels fairly low priority and ambiguous so I’m not very motivated to think about this too much. 🤷 Sorry!