We still miss a lazy version as a range. The broadcast solution above is greedy and breaks code that expects range objects.
I tried to make one here but, well, maybe it was a bad time.
That proposal would write the case above as logrange(0.01, 100, length=50)
i.e. specifying start and end points, exactly like range
, not powers of some base. (To be clear, there is no need to specify a base. The geometric mean does not depend on such a thing; this just generalises to many intermediate points.)
Edit: by “above” I meant an earlier thread, whose example was 10 .^ range(-2, 2, length=50)
. In ancient versions of Julia, this was written logspace(-2, 2, 50, base=10)
, but this was removed since 10 .^ range
is clearer if you want to specify the powers.
If a function expects a range object, it is probably because it requires a constant stepsize, which is not true for log-scaled ranges. If it doesn’t require a step
, why wouldn’t it accept any AbstractArray
subtype?
Note that a lazy O(1)-storage log-scaled array is possible with GitHub - JuliaArrays/MappedArrays.jl: Lazy in-place transformations of arrays, via mappedarray(exp10, range(-2, 10, length=10^6))
or similar.
If all you want is a lazy iterator (i.e. you don’t need it to act like an AbstractArray
, there is Iterators.map
:
julia> for i in Iterators.map(exp10, -3:3)
@show i
end
i = 0.001
i = 0.01
i = 0.1
i = 1.0
i = 10.0
i = 100.0
i = 1000.0