Function to obtain the maximum value of each element of an array with a second argument of zero?

Is there a function in Julia which takes an array and a scalar as input and returns an array as output where the values of each element of the array are as follows:

out_i = max(input_i, v)

where v is some arbitrary scalar value. (In this case, zero.)

In Python, this function is called maximum (or fmax) which is different from max. It is part of numpy. Example:

output_array = numpy.fmax(input_array, 0.0)

In Julia, max is for comparison of two elements and maximum is for the highest value in an iterable. Here, you want comparison between two elements to be broadcasted over every value of your array, so you can do it as follows:

julia> a = rand(10)
10-element Vector{Float64}:
 0.22992278012202094
 0.2474774269332295
 0.027469463935002603
 0.2212289112594984
 0.6776369063933763
 0.560465219340989
 0.7627282729774861
 0.33638924323772235
 0.8933858558943317
 0.5810967639065461

julia> max.(a, 0.5)
10-element Vector{Float64}:
 0.5
 0.5
 0.5
 0.5
 0.6776369063933763
 0.560465219340989
 0.7627282729774861
 0.5
 0.8933858558943317
 0.5810967639065461
3 Likes