Skip to content

Commit

Permalink
Merge pull request #28 from MineralsCloud:ReciprocalLattice
Browse files Browse the repository at this point in the history
Fix broadcasting for `ReciprocalLattice`s
  • Loading branch information
singularitti authored Oct 30, 2023
2 parents bbec455 + 874a450 commit 65f2fbf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/lattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,20 @@ Base.firstindex(::Lattice) = 1

Base.lastindex(::Lattice) = 9

# You need this to let the broadcasting work.
Base.:*(lattice::Lattice, x) = Lattice(parent(lattice) * x)
Base.:*(x, lattice::Lattice) = lattice * x

# You need this to let the broadcasting work.
Base.:/(lattice::Lattice, x) = Lattice(parent(lattice) / x)

Base.:+(lattice::Lattice) = lattice
# You need this to let the broadcasting work.
Base.:+(lattice::Lattice, x) = Lattice(parent(lattice) .+ x)
Base.:+(x, lattice::Lattice) = lattice + x

Base.:-(lattice::Lattice) = -one(eltype(lattice)) * lattice
# You need this to let the broadcasting work.
Base.:-(lattice::Lattice, x) = Lattice(parent(lattice) .- x)
Base.:-(x, lattice::Lattice) = -lattice + x

Expand Down
17 changes: 13 additions & 4 deletions src/reciprocal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ Base.firstindex(::ReciprocalLattice) = 1

Base.lastindex(::ReciprocalLattice) = 9

Base.:*(lattice::ReciprocalLattice, x::Number) = ReciprocalLattice(parent(lattice) * x)
Base.:*(x::Number, lattice::ReciprocalLattice) = lattice * x
# You need this to let the broadcasting work.
Base.:*(lattice::ReciprocalLattice, x) = ReciprocalLattice(parent(lattice) * x)
Base.:*(x, lattice::ReciprocalLattice) = lattice * x

Base.:/(lattice::ReciprocalLattice, x::Number) = ReciprocalLattice(parent(lattice) / x)
# You need this to let the broadcasting work.
Base.:/(lattice::ReciprocalLattice, x) = ReciprocalLattice(parent(lattice) / x)

Base.:+(lattice::ReciprocalLattice) = lattice

Expand All @@ -89,4 +91,11 @@ 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)))
ReciprocalLattice(convert(MMatrix{3,3,T,9}, parent(lattice)))

# See https://github.com/JuliaLang/julia/blob/v1.10.0-beta3/base/refpointer.jl#L95-L96
Base.ndims(::Type{<:ReciprocalLattice}) = 2
Base.ndims(::ReciprocalLattice) = 2

# See https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting
Base.broadcastable(lattice::ReciprocalLattice) = Ref(lattice)

0 comments on commit 65f2fbf

Please sign in to comment.