Skip to content

Commit

Permalink
disambiguate more undesirable dispatch to StaticArray constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
thchr committed Jul 13, 2021
1 parent 56bdaa3 commit 63b0251
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Crystalline"
uuid = "ae5e2be0-a263-11e9-351e-f94dad1eb351"
authors = ["Thomas Christensen <[email protected]>"]
version = "0.4.2"
version = "0.4.3"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand Down
20 changes: 10 additions & 10 deletions src/bravais.jl
Original file line number Diff line number Diff line change
Expand Up @@ -496,16 +496,16 @@ function conventionalize(Rs′::DirectBasis{D}, cntr::Char) where D
end

function transform(Gs::ReciprocalBasis{D}, P::AbstractMatrix{<:Real}) where D
# While the direct basis (𝐚 𝐛 𝐜) transforms like
# (𝐚′ 𝐛′ 𝐜′) = (𝐚 𝐛 𝐜)𝐏
# under a basis change matrix 𝐏, the reciprocal basis (𝐚* 𝐛* 𝐜*) transforms like
# (𝐚*′ 𝐛*′ 𝐜*′) = (𝐚* 𝐛* 𝐜*)(𝐏⁻¹)ᵀ
# since (𝐚 𝐛 𝐜)(𝐚* 𝐛* 𝐜*)ᵀ = 2π𝐈 must be conserved after the basis change

# Gm′ = Gm*(P⁻¹)ᵀ = Gm*(Pᵀ)⁻¹ (w/ Gm a matrix w/ columns of untransformed reciprocal
# vecs Gᵢ)
Gm′ = basis2matrix(Gs)/P'
return ReciprocalBasis{D}(ntuple(i->Gm′[:,i], Val(D)))
# While the direct basis (𝐚 𝐛 𝐜) transforms like
# (𝐚′ 𝐛′ 𝐜′) = (𝐚 𝐛 𝐜)𝐏
# under a basis change matrix 𝐏, the reciprocal basis (𝐚* 𝐛* 𝐜*) transforms like
# (𝐚*′ 𝐛*′ 𝐜*′) = (𝐚* 𝐛* 𝐜*)(𝐏⁻¹)ᵀ
# since (𝐚 𝐛 𝐜)(𝐚* 𝐛* 𝐜*)ᵀ = 2π𝐈 must be conserved after the basis change

# Gm′ = Gm*(P⁻¹)ᵀ = Gm*(Pᵀ)⁻¹ (w/ Gm a matrix w/ columns of untransformed reciprocal
# vecs Gᵢ)
Gm′ = basis2matrix(Gs)/P'
return ReciprocalBasis{D}(ntuple(i->Gm′[:,i], Val(D)))
end

"""
Expand Down
4 changes: 4 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ for T in (:DirectBasis, :ReciprocalBasis)
@eval $T(Vs::NTuple{D, SVector{D, Float64}}) where D = $T{D}(Vs)
@eval $T{D}(Vs::NTuple{D, NTuple{D,<:Real}}) where D = $T{D}(SVector{D,Float64}.(Vs))
@eval $T(Vs::NTuple{D, NTuple{D,<:Real}}) where D = $T{D}(Vs)
@eval $T{D}(Vs::NTuple{D, <:AbstractVector{<:Real}}) where D = $T{D}(Vs...)
@eval $T(Vs::NTuple{D, <:AbstractVector{<:Real}}) where D = $T{D}(Vs...)
@eval $T{D}(Vs::AbstractVector{<:AbstractVector{<:Real}}) where D = $T{D}(Vs...)
@eval $T(Vs::AbstractVector{<:AbstractVector{<:Real}}) = $T(Vs...)
@eval $T{D}(Vs::AbstractVector{<:Real}...) where D = $T{D}(convert(SVector{D, SVector{D, Float64}}, Vs))
@eval $T(Vs::AbstractVector{<:Real}...) = $T{length(Vs)}(Vs...)
@eval $T{D}(Vs::StaticVector{D,<:Real}...) where D = $T{D}(Vs) # resolve ambiguities w/
Expand Down
20 changes: 19 additions & 1 deletion test/basisvecs.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Crystalline, Test, LinearAlgebra
using Crystalline, Test, LinearAlgebra, StaticArrays

@testset "Basis vectors" begin
@testset "Conventional bases" begin
Expand Down Expand Up @@ -26,4 +26,22 @@ using Crystalline, Test, LinearAlgebra
end
end
end

@testset "Mixed inputs for constructors" begin
Rs = ([1.0, 0.0, 0.0], [-0.5, sqrt(3)/2, 0.0], [0, 0, 1.25])
Rs′ = DirectBasis{3}(Rs)
@test Rs′ == DirectBasis(Rs) # NTuple{3, Vector{3, Float64}}
Rsˢˢ = convert(SVector{3,SVector{3,Float64}}, Rs) # SVector{3, SVector{3, Float64}}
@test Rs′ == DirectBasis{3}(Rsˢˢ) == DirectBasis(Rsˢˢ)
Rsᵛ = [Rs...] # Vector{Vector{Float64}}
@test Rs′ == DirectBasis{3}(Rsᵛ) == DirectBasis(Rsᵛ)
Rsˢ = SVector{3,Float64}.(Rs) # NTuple{3, SVector{3, Float64}}
@test Rs′ == DirectBasis{3}(Rsˢ) == DirectBasis(Rsˢ)
Rsᵗ = ntuple(i->ntuple(j->Rs[i][j], Val(3)), Val(3)) # NTuple{3, NTuple{3, Float64}}
@test Rs′ == DirectBasis{3}(Rsᵗ) == DirectBasis(Rsᵗ)
@test Rs′ == DirectBasis(Rs[1], Rs[2], Rs[3]) == DirectBasis{3}(Rs[1], Rs[2], Rs[3])
# ↑ Varargs / splatting of Vector{Float64}
@test Rs′ == DirectBasis(Rsˢ[1], Rsˢ[2], Rsˢ[3]) == DirectBasis{3}(Rsˢ[1], Rsˢ[2], Rsˢ[3])
# ↑ Varargs / splatting of SVector{3, Float64}
end
end

2 comments on commit 63b0251

@thchr
Copy link
Owner Author

@thchr thchr commented on 63b0251 Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/41729

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.3 -m "<description of version>" 63b02517e6e34f8ec5db8eca44815cabab1080d9
git push origin v0.4.3

Please sign in to comment.