Nonlinearly spaced values in Julia with ergonomic APIs. Currently supports logarithmic and power function spacing.
The package is currently not registered, so you'll have to install it from GitHub. Open a Julia REPL and run:
]add https://github.com/Firionus/NonlinearSequences.jl
This package is quite new. It is recommended to only use it for non-critical applications.
julia> using NonlinearSequences
julia> logspace(20, 40, length=4)
4-element Vector{Float64}:
19.999999999999996
25.198420997897465
31.748021039363984
40.0logspace: logarithmically spaced valuesoctspace: logarithmically spaced values with step in octavesdecspace: logarithmically spaced values with step in decadespowspace: values spaced by power function
#
NonlinearSequences.logspace — Function.
logspace(start, stop, length)
logspace(start, stop; length, step, base, adjust_step)
logspace(start; stop, length, step, base, adjust_step)
logspace(; start, stop, length, step, base, adjust_step)Returns an array of logarithmically spaced values.
All arguments can be provided as keyword arguments. Additionally, start, stop and length can be provided as positional arguments.
4 different combinations of arguments are valid:
start,stopandlength:lengthmany values fromstarttostop.start,stop,stepandbase: values fromstartup tostopwhose logarithms of basebaseare spaced linearly withstep. By default, the step size is obeyed and values may end beforestop. To adjust the step to reachstopprecisely, setadjust_step=true.start,step,base,length:lengthmany values starting atstartwhose logarithms of basebaseare spaced linearly withstep.stop,step,base,length:lengthmany values ending atstopwhose logarithms of basebaseare spaced linearly withstep.
Examples
julia> logspace(1, 2, 3)
3-element Vector{Float64}:
1.0
1.414213562373095
2.0
julia> logspace(1, 2.1, step=1/2, base=2)
3-element Vector{Float64}:
1.0
1.414213562373095
2.0
julia> logspace(1, 2.1, step=1/2, base=2, adjust_step=true)
3-element Vector{Float64}:
1.0
1.449137674618944
2.1
julia> logspace(1, step=1/2, base=2, length=3)
3-element Vector{Float64}:
1.0
1.414213562373095
2.0
julia> logspace(stop=2, step=1/2, base=2, length=3)
3-element Vector{Float64}:
1.0
1.414213562373095
2.0
julia> logspace(start=1, stop=2, length=3)
3-element Vector{Float64}:
1.0
1.414213562373095
2.0
julia> logspace(stop=1, step=-1/2, base=2, length=3)
3-element Vector{Float64}:
2.0
1.414213562373095
1.0#
NonlinearSequences.octspace — Function.
octspace(start, stop, step; adjust_step)
octspace(start, stop; step, length, adjust_step)
octspace(start; stop, step, length, adjust_step)
octspace(; start, stop, step, length, adjust_step)Returns an array of values spaced by step octaves.
All arguments can be provided as keyword arguments. Additionally, start, stop and step can be provided as positional arguments.
4 different combinations of arguments are valid:
start,stop,step: values fromstartup tostopspaced bystepin octaves. By default, the step size is obeyed and values may end beforestop. To adjust the step to reachstopprecisely, set the keyword argumentadjust_step=true.start,step,length:lengthmany values starting atstartspaced bystepoctaves.stop,step,length:lengthmany values ending atstopspaced bystepoctaves.start,stop,length:lengthmany logarithmically spaced values fromstarttostop.
Examples
julia> octspace(20, 42, 1/3)
4-element Vector{Float64}:
19.999999999999996
25.198420997897465
31.748021039363984
40.0
julia> octspace(20, 42, 1/3, adjust_step=true)
4-element Vector{Float64}:
19.999999999999996
25.61158329974988
32.79765995600137
42.00000000000001
julia> octspace(20, step=1/3, length=4)
4-element Vector{Float64}:
19.999999999999996
25.198420997897465
31.748021039363984
40.0
julia> octspace(stop=40, step=1/3, length=4)
4-element Vector{Float64}:
19.999999999999996
25.198420997897465
31.748021039363984
40.0
julia> octspace(20, 40, length=4)
4-element Vector{Float64}:
19.999999999999996
25.198420997897465
31.748021039363984
40.0
julia> octspace(40, 20, -1/3)
4-element Vector{Float64}:
40.0
31.748021039363984
25.198420997897465
19.999999999999996#
NonlinearSequences.decspace — Function.
decspace(start, stop, step; adjust_step)
decspace(start, stop; step, length, adjust_step)
decspace(start; stop, step, length, adjust_step)
decspace(; start, stop, step, length, adjust_step)Returns an array of values spaced by step decades.
All arguments can be provided as keyword arguments. Additionally, start, stop and step can be provided as positional arguments.
4 different combinations of arguments are valid:
start,stop,step: values fromstartup tostopspaced bystepin decades. By default, the step size is obeyed and values may end beforestop. To adjust the step to reachstopprecisely, set the keyword argumentadjust_step=true.start,step,length:lengthmany values starting atstartspaced bystepdecades.stop,step,length:lengthmany values ending atstopspaced bystepdecades.start,stop,length:lengthmany logarithmically spaced values fromstarttostop.
Examples
julia> decspace(1, 10, 1/3)
4-element Vector{Float64}:
1.0
2.154434690031884
4.641588833612779
10.000000000000002
julia> decspace(1, step=1/3, length=4)
4-element Vector{Float64}:
1.0
2.154434690031884
4.641588833612779
10.000000000000002
julia> decspace(stop=10, step=1/3, length=4)
4-element Vector{Float64}:
1.0
2.154434690031884
4.641588833612779
10.000000000000002
julia> decspace(1, 10, length=4)
4-element Vector{Float64}:
1.0
2.154434690031884
4.641588833612779
10.000000000000002
julia> decspace(10, 1, -1/3)
4-element Vector{Float64}:
10.000000000000002
4.641588833612779
2.154434690031884
1.0#
NonlinearSequences.powspace — Function.
powspace(start, stop, power, length)Returns an array of values given by a power function.
The length many output values go from start to stop. They are taken from the power function y = x^power where values of x are chosen appropriately.
Examples
Quadratic sequence:
julia> powspace(0, 1, 2, 3)
3-element Vector{Float64}:
0.0
0.25
1.0Square Root sequence:
julia> powspace(0, 1, 1/2, 3)
3-element Vector{Float64}:
0.0
0.7071067811865476
1.0
Feedback on the design of the package, feature requests or bug reports are welcome! Just open an issue.
Possible ideas for the future include:
- lazy computation
- improving the API
- other nonlinear spacings
- reducing code bulk while keeping the ergonomic API
- improving performance
- more control over the output type
- integration into a bigger package or splitting into even smaller packages
If you want to know more about the status of these future ideas or want to discuss, please look at the issues.