Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Base.convert, uconvert, ustrip for ReciprocalLattice & Lattice in ext/UnitfulExt.jl #23

Merged
merged 3 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructEquality = "6ec83bb0-ed9f-11e9-3b4c-2b04cb4e219c"

[weakdeps]
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
UnitfulExt = "Unitful"

[compat]
StaticArrays = "1"
StructEquality = "1, 2"
Expand Down
40 changes: 40 additions & 0 deletions ext/UnitfulExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module UnitfulExt

using CrystallographyCore: Lattice, ReciprocalLattice
using Unitful: Quantity, DimensionError, Units, dimension

import Unitful: uconvert, ustrip

# See https://github.com/PainterQubits/Unitful.jl/blob/v1.17.0/src/conversion.jl#L102-L111
Base.convert(
::Type{Lattice{Quantity{T,D,U}}}, lattice::Lattice{Quantity{T,D,U}}
) where {T,D,U} = lattice
Base.convert(
::Type{ReciprocalLattice{Quantity{T,D,U}}}, lattice::ReciprocalLattice{Quantity{T,D,U}}
) where {T,D,U} = lattice
function Base.convert(::Type{Lattice{Quantity{T,D,U}}}, lattice::Lattice) where {T,D,U}
if dimension(eltype(lattice)) == D
return Lattice(map(Base.Fix1(convert, Quantity{T,D,U}), parent(lattice)))
else
throw(DimensionError(U(), first(lattice)))
end
end
function Base.convert(
::Type{ReciprocalLattice{Quantity{T,D,U}}}, lattice::ReciprocalLattice
) where {T,D,U}
if dimension(eltype(lattice)) == D
return ReciprocalLattice(map(Base.Fix1(convert, Quantity{T,D,U}), parent(lattice)))
else
throw(DimensionError(U(), first(lattice)))
end
end

# See https://github.com/PainterQubits/Unitful.jl/blob/v1.17.0/src/conversion.jl#L66-L74
uconvert(u::Units, lattice::Lattice) = Lattice(map(Base.Fix1(uconvert, u), parent(lattice)))
uconvert(u::Units, lattice::ReciprocalLattice) =
ReciprocalLattice(map(Base.Fix1(uconvert, u), parent(lattice)))

ustrip(lattice::Lattice) = Lattice(map(ustrip, parent(lattice)))
ustrip(lattice::ReciprocalLattice) = ReciprocalLattice(map(ustrip, parent(lattice)))

end
4 changes: 4 additions & 0 deletions src/lattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,7 @@ Base.:+(x::Number, lattice::Lattice) = lattice + x
Base.:-(lattice::Lattice) = -one(eltype(lattice)) * lattice
Base.:-(lattice::Lattice, x::Number) = Lattice(parent(lattice) .- x)
Base.:-(x::Number, lattice::Lattice) = -lattice + x

Base.convert(::Type{Lattice{T}}, lattice::Lattice{T}) where {T} = lattice
Base.convert(::Type{Lattice{T}}, lattice::Lattice{S}) where {S,T} =
Lattice(convert(Matrix{T}, parent(lattice)))
5 changes: 5 additions & 0 deletions src/reciprocal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ Base.:/(lattice::ReciprocalLattice, x::Number) = ReciprocalLattice(parent(lattic
Base.:+(lattice::ReciprocalLattice) = lattice

Base.:-(lattice::ReciprocalLattice) = -one(eltype(lattice)) * lattice

Base.convert(::Type{ReciprocalLattice{T}}, lattice::ReciprocalLattice{T}) where {T} =
lattice
Base.convert(::Type{ReciprocalLattice{T}}, lattice::ReciprocalLattice{S}) where {S,T} =
ReciprocalLattice(convert(Matrix{T}, parent(lattice)))
Loading