Skip to content

Commit

Permalink
Allow conversions between vector types with different scalar types.
Browse files Browse the repository at this point in the history
+ bump version and update author email.
  • Loading branch information
JLTastet committed May 16, 2023
1 parent 5a6db4d commit 14ebb61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LorentzVectors"
uuid = "3f54b04b-17fc-5cd4-9758-90c048d965e3"
authors = ["Jean-Loup Tastet <jean-loup.tastet@cern.ch>"]
version = "0.4.2"
authors = ["Jean-Loup Tastet <jean-loup.tastet@uam.es>"]
version = "0.4.3"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
18 changes: 17 additions & 1 deletion src/LorentzVectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ module LorentzVectors
import LinearAlgebra: dot, , cross, ×, norm, normalize
import Random: rand, AbstractRNG

import Base: +, -, *, /, ==, isapprox, , zero
import Base: +, -, *, /, ==, isapprox, , zero, convert

export LorentzVector, SpatialVector, Vec4, Vec3
export dot, , cross, ×, norm, normalize
export boost, rotate
export convert

"""
LorentzVector(t, x, y, z)
Expand Down Expand Up @@ -70,6 +71,21 @@ Construct a 4-vector from a time component and a 3-vector.
LorentzVector(t::T, u::SpatialVector{U}) where {T,U} =
LorentzVector(t, u.x, u.y, u.z)

"""
convert(LorentzVector{T}, u)
Converts between LorentzVector types of different precisions.
"""
convert(::Type{LorentzVector{T}}, u::LorentzVector) where {T <: AbstractFloat} = LorentzVector{T}(u.t, u.x, u.y, u.z)


"""
convert(SpatialVector{T}, u)
Converts between SpatialVector types of different precisions.
"""
convert(::Type{SpatialVector{T}}, u::SpatialVector) where {T <: AbstractFloat} = SpatialVector{T}(u.x, u.y, u.z)

"""
zero(LorentzVector{T})
zero(LorentzVector)
Expand Down
11 changes: 11 additions & 0 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
@test Vec3(π, π, π) Vec3(float(π), float(π), float(π))
end;

@testset "Conversions" begin
x = Vec3(1., 2., 3.)
x′ = convert(Vec3{Float32}, x)
@test x′ == Vec3(1f0, 2f0, 3f0)
@test typeof(x′) == Vec3{Float32}
u = Vec4(0., 1., 2., 3.)
u′ = convert(Vec4{Float32}, u)
@test u′ == Vec4(0f0, 1f0, 2f0, 3f0)
@test typeof(u′) == Vec4{Float32}
end

@testset "Misc. constructors" begin
x = Vec3(1, 2, 3)
@test Vec4(0.5, x) == Vec4(0.5, 1., 2., 3.)
Expand Down

4 comments on commit 14ebb61

@JLTastet
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register

Release notes:

Allow conversions between vectors backed by different scalar types (e.g. Float32 and Float64).

@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/83699

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>" 14ebb61f6c26b36d28fd96dfa38ba51727906d18
git push origin v0.4.3

Also, note the warning: Version 0.4.3 skips over 0.4.1
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

@JLTastet
Copy link
Owner Author

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 updated: JuliaRegistries/General/83699

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>" 14ebb61f6c26b36d28fd96dfa38ba51727906d18
git push origin v0.4.3

Please sign in to comment.