Skip to content

Commit

Permalink
sparse constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimbrand committed Feb 1, 2025
1 parent 5e8c947 commit c0a2e51
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/BitStringAddresses/bosefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ BoseFS(vals::Integer...) = BoseFS(vals) # specify occupation numbers
BoseFS(val::Integer) = BoseFS((val,)) # single mode address
BoseFS{N,M}(vals::Integer...) where {N,M} = BoseFS{N,M}(vals)

# Sparse constructors
BoseFS(M::Integer, pairs::Pair...) = BoseFS(M, pairs)
BoseFS(M::Integer, pairs) = BoseFS(sparse_to_onr(M, pairs))
BoseFS{N,M}(pairs::Pair...) where {N,M} = BoseFS{N,M}(pairs)
Expand Down
6 changes: 4 additions & 2 deletions src/BitStringAddresses/fermifs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ chosen automatically based on the properties of the address.
particles in `bs` is equal to `N`, or whether each mode only contains one particle.
* [`@fs_str`](@ref): Addresses are sometimes printed in a compact manner. This
representation can also be used as a constructor. See the last example below.
representation can also be used as a constructor. See the examples below.
# Examples
Expand Down Expand Up @@ -95,7 +95,8 @@ function FermiFS{N,M}(onr::Union{AbstractArray{<:Integer},NTuple{M,<:Integer}})
end
return FermiFS{N,M,S}(from_fermi_onr(S, onr))
end
function FermiFS(onr::Union{AbstractArray,Tuple})
function FermiFS(onr)
onr = Tuple(onr)
M = length(onr)
N = sum(onr)
return FermiFS{N,M}(onr)
Expand All @@ -109,6 +110,7 @@ FermiFS(M::Integer, pairs::Pair...) = FermiFS(M, pairs)
FermiFS(M::Integer, pairs) = FermiFS(sparse_to_onr(M, pairs))
FermiFS{N,M}(pairs::Vararg{Pair,N}) where {N,M} = FermiFS{N,M}(pairs)
FermiFS{N,M}(pairs) where {N,M} = FermiFS{N,M}(sparse_to_onr(M, pairs))
FermiFS(pairs::Pair...) = throw(ArgumentError("number of modes must be provided"))

function print_address(io::IO, f::FermiFS{N,M}; compact=false) where {N,M}
if compact && f.bs isa SortedParticleList
Expand Down
18 changes: 16 additions & 2 deletions src/BitStringAddresses/occupationnumberfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ OccupationNumberFS{5, UInt8}(0, 0, 0, 0, 0)
julia> OccupationNumberFS(i for i in 1:3) # use list comprehension
OccupationNumberFS{3, UInt8}(1, 2, 3)
julia> OccupationNumberFS(4, 1=>2, 3=>4) # sparse constructor
OccupationNumberFS{4, UInt8}(2, 0, 4, 0)
```
"""
struct OccupationNumberFS{M,T<:Unsigned} <: SingleComponentFockAddress{missing,M}
Expand All @@ -51,9 +54,10 @@ function OccupationNumberFS(arg)
return OccupationNumberFS{length(t)}(t)
end

function OccupationNumberFS(args...)
function OccupationNumberFS(args::Integer...)
return OccupationNumberFS{length(args)}(args)
end
OccupationNumberFS(arg::Integer) = OccupationNumberFS{1}(arg) # to resolve ambiguity

function OccupationNumberFS{M}(args...) where M
sv = SVector{M}(args...)
Expand All @@ -71,9 +75,19 @@ end
function OccupationNumberFS{M,T}() where {M,T<:Unsigned}
return OccupationNumberFS(SVector{M,T}(zero(T) for _ in 1:M))
end

OccupationNumberFS{M}() where {M} = OccupationNumberFS{M,UInt8}()

# Sparse constructors
OccupationNumberFS(M::Integer, pairs::Pair...) = OccupationNumberFS(M, pairs)
OccupationNumberFS(M::Integer, pairs) = OccupationNumberFS(sparse_to_onr(M, pairs))
OccupationNumberFS{M}(pairs::Pair...) where {M} = OccupationNumberFS{M}(pairs)

function OccupationNumberFS{M}(pairs::NTuple{<:Any,Pair}) where {M}
OccupationNumberFS{M}(sparse_to_onr(M, pairs))
end

OccupationNumberFS(pairs::Pair...) = throw(ArgumentError("number of modes must be provided"))

function print_address(io::IO, ofs::OccupationNumberFS{M,T}; compact=false) where {M,T}
if compact
BITS = sizeof(T) * 8
Expand Down
9 changes: 8 additions & 1 deletion test/BitStringAddresses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ end
@test OccupationNumberFS(i for i in 1:3) == OccupationNumberFS(1, 2, 3)
@test isa(OccupationNumberFS{3,UInt32}(i for i in 1:3), OccupationNumberFS{3,UInt32})
@test isa(OccupationNumberFS(1, 2, 3), OccupationNumberFS{3,UInt8})
@test_throws ArgumentError OccupationNumberFS(1.1, 2, 3)
@test_throws MethodError OccupationNumberFS(1.1, 2, 3)
@test_throws ArgumentError OccupationNumberFS(-1, 2, 3)
@test_throws ArgumentError OccupationNumberFS(1, 2, 300)
end
Expand All @@ -485,6 +485,13 @@ end
@test isa(OccupationNumberFS(fs), OccupationNumberFS{2,UInt8})
end

@testset "OccupationNumberFS with sparse constructor" begin
@test OccupationNumberFS(2, 2=>4) == OccupationNumberFS(0, 4)
@test OccupationNumberFS{2}(2 => 4) == OccupationNumberFS(2, 2 => 4)
@test OccupationNumberFS(5, i => i + 1 for i in 1:3) ==
OccupationNumberFS{5}(Tuple(i => i + 1 for i in 1:3))
end

@testset "Printing and parsing OccupationNumberFS" begin
fs = OccupationNumberFS(1, 2, 3, 0, 1, 20, 3, 2, 5, 0, 1)
@test eval(Meta.parse(repr(fs))) == fs
Expand Down

0 comments on commit c0a2e51

Please sign in to comment.