Skip to content

Commit 85ef52c

Browse files
fredrikekreKristofferC
authored andcommitted
add a base keyword to logspace (#22310)
to specify the base (which otherwise defaults to 10)
1 parent 6fdb028 commit 85ef52c

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ Library improvements
6565
git repo. Additionally, the argument order was changed to be consistent with the git
6666
command line tool ([#22062]).
6767

68+
* `logspace` now accepts a `base` keyword argument to specify the base of the logarithmic
69+
range. The base defaults to 10 ([#22310]).
70+
6871
Compiler/Runtime improvements
6972
-----------------------------
7073

base/range.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ function print_range(io::IO, r::Range,
316316
end
317317

318318
"""
319-
logspace(start::Real, stop::Real, n::Integer=50)
319+
logspace(start::Real, stop::Real, n::Integer=50; base=10)
320320
321-
Construct a vector of `n` logarithmically spaced numbers from `10^start` to `10^stop`.
321+
Construct a vector of `n` logarithmically spaced numbers from `base^start` to `base^stop`.
322322
323323
```jldoctest
324324
julia> logspace(1.,10.,5)
@@ -328,9 +328,17 @@ julia> logspace(1.,10.,5)
328328
3.16228e5
329329
5.62341e7
330330
1.0e10
331+
332+
julia> logspace(1.,10.,5,base=2)
333+
5-element Array{Float64,1}:
334+
2.0
335+
9.51366
336+
45.2548
337+
215.269
338+
1024.0
331339
```
332340
"""
333-
logspace(start::Real, stop::Real, n::Integer=50) = 10.^linspace(start, stop, n)
341+
logspace(start::Real, stop::Real, n::Integer=50; base=10) = base.^linspace(start, stop, n)
334342

335343
## interface implementations
336344

test/ranges.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,3 +918,14 @@ let linsp = linspace(1.0, 2.0, 10)
918918
@test Float32(linsp.ref) === convert(Float32, linsp.ref)
919919
@test Float32(linsp.ref) linsp.ref.hi + linsp.ref.lo
920920
end
921+
922+
@testset "logspace" begin
923+
n = 10; a = 2; b = 4
924+
# test default values; n = 50, base = 10
925+
@test logspace(a, b) == logspace(a, b, 50) == 10.^linspace(a, b, 50)
926+
@test logspace(a, b, n) == 10.^linspace(a, b, n)
927+
for base in (10, 2, e)
928+
@test logspace(a, b, base=base) == logspace(a, b, 50, base=base) == base.^linspace(a, b, 50)
929+
@test logspace(a, b, n, base=base) == base.^linspace(a, b, n)
930+
end
931+
end

0 commit comments

Comments
 (0)