Miscellaneous tools
Real-indexed vector
This is a simple implementation of a vector that can be accessed with real (floating point) sub-indices. It does simply a linear binning of a specified (semi-open) interval $[min,max)$ on the real line, and provides a convenient Vector-like syntax to access elements. The stored values can be of any type, but the interval and number of bins are fixed at construction.
julia> A = BinnedVector{String}(5,min=0.,max=1.)5-element BinnedVector{String}: #undef #undef #undef #undef #undefjulia> A[0.1]="hello""hello"julia> A[0.2]="bye bye""bye bye"julia> A5-element BinnedVector{String}: "hello" "bye bye" #undef #undef #undef
To initialise elements with a default value, give function with a signature like zeros:
B = BinnedVector{Rational{Int64}}(10,min=2.,max=8.,init=zeros)
B[2.1]=3//5
B[5.3]=4//1
B10-element BinnedVector{Rational{Int64}}:
3//5
0//1
0//1
0//1
0//1
4//1
0//1
0//1
0//1
0//1Alternatively, it is possible to construct specifying bin width and the interval. In this case, one of the values will need to be rounded to get an integer number of bins:
julia> C = BinnedVector{Int}(Δ=0.15,min=5.,max=10.,round_Δ=RoundUp); (C.Δ, interval(C))(0.15151515151515152, (5.0, 10.0))julia> D = BinnedVector{Int}(Δ=0.15,min=5.,max=10.,round_max=RoundUp); (D.Δ, interval(D))(0.15, (5.0, 10.1))julia> E = BinnedVector{Int}(Δ=0.15,min=5.,max=10.,round_min=RoundUp); (E.Δ, interval(E))(0.15, (5.05, 10.0))
RoundDown is also recognised.
Indexing with integers refers to bin numbers.
julia> A[1:2]2-element Vector{String}: "hello" "bye bye"julia> B[1:5]5-element Vector{Rational{Int64}}: 3//5 0//1 0//1 0//1 0//1
Outliers are mapped to bin 0 if below range, or -1 if above range.
julia> B[0.]=10//3 ; B[40.]=99//2;julia> B[0]10//3julia> B[-1]99//2
As an example of use, the Histogram type is implemented using BinnedVector.
Special treatment of zero
In some cases, like when computing space correlation functions, it is convenient to treat the case A[0.] separately from the bin that starts at 0. This is what the ZBinnedvector type does.
julia> Z = ZBinnedVector{Int}(Δ=0.2,max=0.5,round_max=RoundUp,init=zeros)4-element ZBinnedVector{Int64}: 0 0 0 0julia> Z[0.] += 1010julia> Z[0.1] += 1010julia> Z[0.15] += 1020julia> Z4-element ZBinnedVector{Int64}: 10 20 0 0
Note that there is no min argument in the construction of ZBinnedvector, since it is always zero. In other respects, ZBinnedvector behaves like BinnedVector, except that bin centres are not equally spaced: the first (pesudo-bin) is at 0, the next bin centre is delta(Z)/2, and the rest are delta(Z) units apart.
API
BioStatPhys.BinnedVector — TypeBinnedVector{T}Simple real-indexed vector. Maps a real interval to an integer range 1:nbins then used to index a vector of arbitrary type. Can be used e.g. to build histograms. The real interval and number of bins nbins are fixed at the outset.
Example
Create with
A = BinnedVector{Int}(nbins,min=1.,max=10.,init=zeros)for fixed number of bins, or
A = BinnedVector{Int}(;Δ=0.1,min=1.,max=10.init=zeros,round_Δ=RoundUp)
A = BinnedVector{Int}(;Δ=0.1,min=1.,max=10.init=zeros,round_min=RoundUp)
A = BinnedVector{Int}(;Δ=0.1,min=1.,max=10.init=zeros,round_max=RoundUp)to get fixed (min, max), (Δ,max) or (min,Δ) respectively (rounding mode RoundDown is also recognised).
init is optional, defaults to leave elements undefined.
Access or write as a vector:
A[5.2] += 2Numbers above and below range map to two special bins.
If indexed with integers, these are interpreted as bin numbers. A[0] and A[-1] are the outlier bins (below and above, respectively).
BioStatPhys.ZBinnedVector — TypeZBinnedVector{T}This type works like BinnedVector{T} except that the range always starts with 0., and that the 0. is treated specially. The first bin excludes the zero (i.e., unlike the rest of the bins is an open interval on both sides), and the values indexed with 0. are treated separately. When using integer indices, index 1 corresponds to 0., index 2 to the first bin, and so on.
This is useful when dealing for example with space correlation functions, where a zero distance implies a correlation of a particle with itself, which is often convenient to treat separately from the inter-particle correlations.
BioStatPhys.interval — Functioninterval(A::BinnedVector{T})
interval(A::ZBinnedVector{T})Return tuple (min,max) giving the extrema of the real interval mapped to the array bins.
BioStatPhys.delta — Functiondelta(A::BinnedVector{T})
delta(A::ZBinnedVector{T})Return the with of the bins of A
BioStatPhys.nbins — Functionnbins(A::BinnedVector{T})
nbins(A::ZBinnedVector{T})Return number of bins of A
BioStatPhys.bin — Functionbin(A::BinnedVector{T},x::Float64) where {T}
bin(A::ZBinnedVector{T},x::Float64) where {T}Map real value x to bin number. Return 0 if below range, or -1 if above range.
BioStatPhys.binc — Functionbinc(A::BinnedVector{T},i::Int) where {T}
binc(A::ZBinnedVector{T},i::Int) where {T}Return center of bin i, which must be in range 1:size(A,1). Does not perform range check.
binc(his::Histogram,bin)Return position of center of bin bin. No range check performed.
Base.range — MethodBase.range(A::BinnedVector{T}) where{T}
Base.range(A::ZBinnedVector{T}) where{T}Return iterator spanning bin centers of A. With r=collect(range(A)) one obtains a Vector such that r[i]=binc(A,i).
For BinnedVector this is a standard range, for ZBinnedVector it is a range-compatible custom iterator since the distance from 0. to the first bin center is half the distance between bin centers.
Distance binning
The distance_binning function takes a series of points in (2- or 3-$d$) space and creates a BinnedVector that classifies the pairs according to their Euclidean distance.
BioStatPhys.DistanceBinning — TypeAlias for a BinnedVector of Vector{Tuple{Int,Int}}
BioStatPhys.distance_binning — Functiondistance_binning(pos,Δr;rmin=0.,rmax=nothing)Create a BinnedVector of tuples (i,j) with i!=j, where each bin contains all pairs of positions in pos whose distance is within the bin extrema.
pos is expected to be a Matrix where each row pos[i,:] is a 2-d or 3-d position vector.
Regions
Types and functions to describe periodic and non-periodic regions of space.
BioStatPhys.Region — Typeabstract type RegionBase type for D-dimensional regions.
BioStatPhys.Rectangle — TypeRectangle <: NonPeriodicRegionDescribes a rectangular non-periodic 2-d region with an arbitrary origin and size. Create with
rec = Rectangle(Lx,Ly,x0=(x0,y0))BioStatPhys.PeriodicHyperCube — TypePeriodicHyperCube{D} <: PeriodicRegionDescribes a periodic D-dimensional region. Actually the size along each dimension can be different.
BioStatPhys.PeriodicRectangle — TypePeriodicRectangle <: PeriodicRegionDescribes periodic rectangular 2-d region with arbitrary size. Alias for PeriodicHyperCube{2}.
BioStatPhys.PeriodicCube — TypePeriodicCube(Lx,Ly,Lz)Return a periodic cube (actually rectangular cuboid). Alias for PeriodicHyperCube{3}
BioStatPhys.dimension — Functiondimension(r<:Region)Return the dimension of region r, in the sense of dimension of a space or a manifold.
BioStatPhys.volume — FunctionReturn the volume of the given region
BioStatPhys.dborder — Functiondborder(r<:NonPeriodicRegion,p::AbstractVector)Return the distance from the point p (assumed included in region r) to the nearest border.
BioStatPhys.distancesq — Functiondistancesq(r<:PeriodicRegion,x::AbstractVector{<:Number},y::AbstractVector{<:Number})Return the periodic squared distance between points x and y in the periodic region r