Skip to content

Commit

Permalink
improve algebra and show implementation for (Abstract)SymmetryVectors
Browse files Browse the repository at this point in the history
  • Loading branch information
thchr committed Sep 24, 2024
1 parent f8ae0ba commit 24f1f89
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,12 @@ end
function Base.show(io :: IO, n :: SymmetryVector)
print(io, "[")
for (i, (mults_k, lgirs_k)) in enumerate(zip(n.multsv, n.lgirsv))
printstyled(io,
Crystalline.symvec2string(mults_k, label.(lgirs_k); braces=false);
color=iseven(i) ? :normal : :light_blue)
str = if !iszero(mults_k)
Crystalline.symvec2string(mults_k, label.(lgirs_k); braces=false)
else # if there are no occupied irreps at the considered k-point print "0kᵢ"
"0" * klabel(first(lgirs_k)) * ""
end
printstyled(io, str; color=iseven(i) ? :normal : :light_blue)
i length(n.multsv) && print(io, ", ")
end
print(io, "]")
Expand Down
5 changes: 5 additions & 0 deletions src/types_symmetryvectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,14 @@ Base.:-(n::AbstractSymmetryVector{D}, m::AbstractSymmetryVector{D}) where D = n
function Base.:*(n::AbstractSymmetryVector{D}, k::Integer) where D
SymmetryVector{D}(irreps(n), [ms .* k for ms in multiplicities(n)], occupation(n) * k)
end
Base.:*(k::Integer, n::AbstractSymmetryVector) = n * k
function Base.zero(n::AbstractSymmetryVector{D}) where D
SymmetryVector{D}(irreps(n), zero.(multiplicities(n)), 0)
end
# make sure `sum(::AbstractSymmetryVector)` is type-stable (necessary since the + operation
# now may change the type of an `AbstractSymmetryVector` - so `n[1]` and `n[1]+n[2]` may
# be of different types) and always returns a `SymmetryVector`
Base.reduce_first(::typeof(+), n::AbstractSymmetryVector) = SymmetryVector(n)

# ::: Utilities & misc :::
dim(::AbstractSymmetryVector{D}) where D = D
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ using Crystalline, Test
include("kvecs.jl")
include("wyckoff.jl")

# symmetry vectors
include("symmetryvectors.jl")

# show, notation, and cached info
include("show.jl")
include("notation.jl")
Expand Down
25 changes: 25 additions & 0 deletions test/symmetryvectors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Test
using Crystalline

@testset "(Abstract)SymmetryVectors" begin
brs = calc_bandreps(221) # ::Collection{NewBandRep{3}}

# addition of symmetry vectors
@test brs[1] + brs[2] == SymmetryVector(brs[1]) + SymmetryVector(brs[2])
@test Vector(brs[1] + brs[2]) == Vector(brs[1]) + Vector(brs[2])

# multiplication of symmetry vectors
@test brs[1] + brs[1] == 2brs[1]
@test -brs[1] == 2brs[1] - 3brs[1]
@test zero(brs[1]) == 0*brs[1]
@test brs[3]*7 == 7*brs[3]

# type-stable summation
@test sum(brs[1:1]) isa SymmetryVector{3}
@test sum(brs[1:2]) isa SymmetryVector{3}

# printing SymmetryVector that have zero-contents
n = SymmetryVector(brs[1])
@test n*0 == zero(n)
@test string(n) == "[0Mᵢ, 0Xᵢ, 0Γᵢ, 0Rᵢ] (0 bands)"
end # @testset "(Abstract)SymmetryVectors"

0 comments on commit 24f1f89

Please sign in to comment.