d3-array icon indicating copy to clipboard operation
d3-array copied to clipboard

Add d3.linspace.

Open mbostock opened this issue 5 years ago • 5 comments

Related #90.

mbostock avatar Jun 11 '20 16:06 mbostock

Maybe worth adding logspace and geomspace too, while we’re here?

mbostock avatar Jun 11 '20 16:06 mbostock

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 :)

Fil avatar Jun 11 '20 16:06 Fil

Very nice!

curran avatar Jun 11 '20 19:06 curran

This looks like a nice PR. Could be merged I suppose.

curran avatar Aug 08 '20 16:08 curran

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!

mbostock avatar Jul 03 '22 12:07 mbostock