From 56a5fc778db58b066b1227278bfde252c353b8f0 Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 30 Oct 2023 16:21:46 -0400 Subject: [PATCH 1/4] Speed up `convert` for `ReciprocalLattice` --- src/reciprocal.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reciprocal.jl b/src/reciprocal.jl index 278ce13..0e4bbf3 100644 --- a/src/reciprocal.jl +++ b/src/reciprocal.jl @@ -89,4 +89,4 @@ 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))) From 18f58b99a0a95b1dccfb612a0739100ea9c5afc4 Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 30 Oct 2023 16:23:00 -0400 Subject: [PATCH 2/4] Remove type constraint `Number` for `*` `/` for `ReciprocalLattice` --- src/reciprocal.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/reciprocal.jl b/src/reciprocal.jl index 0e4bbf3..510dd46 100644 --- a/src/reciprocal.jl +++ b/src/reciprocal.jl @@ -77,10 +77,10 @@ Base.firstindex(::ReciprocalLattice) = 1 Base.lastindex(::ReciprocalLattice) = 9 -Base.:*(lattice::ReciprocalLattice, x::Number) = ReciprocalLattice(parent(lattice) * x) -Base.:*(x::Number, lattice::ReciprocalLattice) = lattice * x +Base.:*(lattice::ReciprocalLattice, x) = ReciprocalLattice(parent(lattice) * x) +Base.:*(x, lattice::ReciprocalLattice) = lattice * x -Base.:/(lattice::ReciprocalLattice, x::Number) = ReciprocalLattice(parent(lattice) / x) +Base.:/(lattice::ReciprocalLattice, x) = ReciprocalLattice(parent(lattice) / x) Base.:+(lattice::ReciprocalLattice) = lattice From 8a990241205b2cc10fbae340f2573bd876221d1a Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 30 Oct 2023 16:26:15 -0400 Subject: [PATCH 3/4] Extend `Base.ndims` & `Base.broadcastable` for `ReciprocalLattice`s --- src/reciprocal.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/reciprocal.jl b/src/reciprocal.jl index 510dd46..4cf8297 100644 --- a/src/reciprocal.jl +++ b/src/reciprocal.jl @@ -77,9 +77,11 @@ Base.firstindex(::ReciprocalLattice) = 1 Base.lastindex(::ReciprocalLattice) = 9 +# You need this to let the broadcasting work. Base.:*(lattice::ReciprocalLattice, x) = ReciprocalLattice(parent(lattice) * x) Base.:*(x, lattice::ReciprocalLattice) = lattice * x +# You need this to let the broadcasting work. Base.:/(lattice::ReciprocalLattice, x) = ReciprocalLattice(parent(lattice) / x) Base.:+(lattice::ReciprocalLattice) = lattice @@ -90,3 +92,10 @@ Base.convert(::Type{ReciprocalLattice{T}}, lattice::ReciprocalLattice{T}) where lattice Base.convert(::Type{ReciprocalLattice{T}}, lattice::ReciprocalLattice{S}) where {S,T} = 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) From 874a4501d61738a5c3ac504428b6745183e074c2 Mon Sep 17 00:00:00 2001 From: singularitti Date: Mon, 30 Oct 2023 16:26:26 -0400 Subject: [PATCH 4/4] Update comments --- src/lattice.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lattice.jl b/src/lattice.jl index f77ae7c..b8cff34 100644 --- a/src/lattice.jl +++ b/src/lattice.jl @@ -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