Skip to content

Commit

Permalink
Merge pull request #23 from MineralsCloud:convert
Browse files Browse the repository at this point in the history
Extend `Base.convert`, `uconvert`, `ustrip` for `ReciprocalLattice` & `Lattice` in ext/UnitfulExt.jl
  • Loading branch information
singularitti authored Oct 8, 2023
2 parents 0ad9f4d + e650e97 commit dc32b16
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
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)))

0 comments on commit dc32b16

Please sign in to comment.