
On Mon, Dec 27, 2021 at 10:15:05PM -0800, Christopher Barker wrote:
On Mon, Dec 27, 2021 at 4:07 PM Steven D'Aprano
Julia (if I recall correctly) has a nice syntax for automatically turning any function or method into an element-wise function:
And numpy has an even easier one:
np.log(a_scalar)
np.log(an_array)
I’m only being a little bit silly.
Ah, but numpy has to use their own special log function that does something like this: # Obviously just pseudocode def numpy.log(obj): if obj is a scalar: return log(obj) # Scalar version. else: # Apply the log function to every element of the # vector, array or matrix. elements = [log(x) for x in obj] return type(obj)(elements) and has to repeat this boilerplate for every single function that operates on both scalars and vectors etc. Whereas Julia allows you to write the scalar log function and then *automatically* apply it to any vector, with no extra code, just by using the "dot-call" syntax log.(obj) https://docs.julialang.org/en/v1/manual/functions/#man-vectorized And it works with operators too. -- Steve