Skip to content

Commit

Permalink
Use AbstractFloat types
Browse files Browse the repository at this point in the history
Fixes issue #3.
  • Loading branch information
giordano committed Nov 1, 2016
1 parent 1d37b5f commit 0b4c2b2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
12 changes: 6 additions & 6 deletions bench/bench.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ for n in [1, 10, 100, 1000, 10_000, 100_000, 1_000_000]
println("$n coordinates: ")
if n == 1
c = ICRSCoords(2pi*rand(), pi*(rand() - 0.5))
t1 = @timeit convert(GalCoords, c)
t1 = @timeit convert(GalCoords{Float64}, c)
println(io, "$n,galactic,$t1")
t2 = @timeit convert(FK5Coords{2000}, c)
t2 = @timeit convert(FK5Coords{Float64,2000}, c)
println(io, "$n,fk5j2000,$t2")
t3 = @timeit convert(FK5Coords{1975}, c)
t3 = @timeit convert(FK5Coords{Float64,1975}, c)
println(io, "$n,fk5j1975,$t3")
else
c = [ICRSCoords(2pi*rand(), pi*(rand() - 0.5)) for i=1:n]
t1 = @timeit convert(Vector{GalCoords}, c)
t1 = @timeit convert(Vector{GalCoords{Float64}}, c)
println(io, "$n,galactic,$t1")
t2 = @timeit convert(Vector{FK5Coords{2000}}, c)
t2 = @timeit convert(Vector{FK5Coords{Float64,2000}}, c)
println(io, "$n,fk5j2000,$t2")
t3 = @timeit convert(Vector{FK5Coords{1975}}, c)
t3 = @timeit convert(Vector{FK5Coords{Float64,1975}}, c)
println(io, "$n,fk5j1975,$t3")
end
end
Expand Down
82 changes: 43 additions & 39 deletions src/SkyCoords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,30 @@ import Base: convert, *, transpose

abstract AbstractSkyCoords

immutable ICRSCoords <: AbstractSkyCoords
ra::Float64
dec::Float64
ICRSCoords(ra::Real, dec::Real) = @compat new(mod(Float64(ra), 2pi),
Float64(dec))
immutable ICRSCoords{T<:AbstractFloat} <: AbstractSkyCoords
ra::T
dec::T
ICRSCoords(ra, dec) = new(mod(ra, 2pi), dec)
end
ICRSCoords{T<:AbstractFloat}(ra::T, dec::T) = ICRSCoords{T}(ra, dec)
ICRSCoords(ra::Real, dec::Real) = ICRSCoords(promote(float(ra), float(dec))...)

immutable GalCoords <: AbstractSkyCoords
l::Float64
b::Float64
GalCoords(l::Real, b::Real) = @compat new(mod(Float64(l), 2pi),
Float64(b))
immutable GalCoords{T<:AbstractFloat} <: AbstractSkyCoords
l::T
b::T
GalCoords(l, b) = new(mod(l, 2pi), b)
end
GalCoords{T<:AbstractFloat}(l::T, b::T) = GalCoords{T}(l, b)
GalCoords(l::Real, b::Real) = GalCoords(promote(float(l), float(b))...)

# FK5 is parameterized by equinox (e)
immutable FK5Coords{e} <: AbstractSkyCoords
ra::Float64
dec::Float64
FK5Coords(ra::Real, dec::Real) = @compat new(mod(Float64(ra), 2pi),
Float64(dec))
immutable FK5Coords{T<:AbstractFloat,e} <: AbstractSkyCoords
ra::T
dec::T
FK5Coords(ra, dec) = new(mod(ra, 2pi), dec)
end
FK5Coords{T<:AbstractFloat}(ra::T, dec::T) = FK5Coords{T}(ra, dec)
FK5Coords(ra::Real, dec::Real) = FK5Coords(promote(float(ra), float(dec))...)

# -----------------------------------------------------------------------------
# Helper functions: Immutable array operations
Expand All @@ -42,22 +45,22 @@ end
# small arrays. Eventually, base Julia should support immutable arrays, at
# which point we should switch to using that functionality in base.

immutable Matrix33
a11::Float64
a12::Float64
a13::Float64
a21::Float64
a22::Float64
a23::Float64
a31::Float64
a32::Float64
a33::Float64
immutable Matrix33{T<:AbstractFloat}
a11::T
a12::T
a13::T
a21::T
a22::T
a23::T
a31::T
a32::T
a33::T
end

immutable Vector3
x::Float64
y::Float64
z::Float64
immutable Vector3{T<:AbstractFloat}
x::T
y::T
z::T
end

function *(x::Matrix33, y::Matrix33)
Expand Down Expand Up @@ -144,7 +147,7 @@ const FK5J2000_TO_GAL = (zrotmat(pi - lon0_fk5j2000) *
zrotmat(ngp_fk5j2000_ra))
const GAL_TO_FK5J2000 = FK5J2000_TO_GAL'

# Gal --> ICRS: simply chain through FK5J2000
# Gal --> ICRS: simply chain through FK5J2000
const GAL_TO_ICRS = FK5J2000_TO_ICRS * GAL_TO_FK5J2000
const ICRS_TO_GAL = GAL_TO_ICRS'

Expand Down Expand Up @@ -182,20 +185,21 @@ end
# Abstract away specific field names (ra, dec vs l, b)
coords2cart(c::ICRSCoords) = coords2cart(c.ra, c.dec)
coords2cart(c::GalCoords) = coords2cart(c.l, c.b)
coords2cart{e}(c::FK5Coords{e}) = coords2cart(c.ra, c.dec)
coords2cart{e,T}(c::FK5Coords{T,e}) = coords2cart(c.ra, c.dec)

# Rotation matrix between coordinate systems: `rotmat(to, from)`
rotmat(::Type{GalCoords}, ::Type{ICRSCoords}) = ICRS_TO_GAL
rotmat(::Type{ICRSCoords}, ::Type{GalCoords}) = GAL_TO_ICRS
rotmat{e}(::Type{FK5Coords{e}}, ::Type{ICRSCoords}) =
rotmat{T1<:AbstractFloat,T2<:AbstractFloat}(::Type{GalCoords{T1}},
::Type{ICRSCoords{T2}}) = ICRS_TO_GAL
rotmat{T1,T2}(::Type{ICRSCoords{T1}}, ::Type{GalCoords{T2}}) = GAL_TO_ICRS
rotmat{e,T1,T2}(::Type{FK5Coords{T1,e}}, ::Type{ICRSCoords{T2}}) =
precess_from_j2000(e) * ICRS_TO_FK5J2000
rotmat{e}(::Type{FK5Coords{e}}, ::Type{GalCoords}) =
precess_from_j2000(e) * GAL_TO_FK5J2000
rotmat{e}(::Type{ICRSCoords}, ::Type{FK5Coords{e}}) =
rotmat{e,T1,T2}(::Type{FK5Coords{T1,e}}, ::Type{GalCoords{T2}}) =
precess_from_j2000(e) * GAL_TO_FK5J2000
rotmat{e,T1,T2}(::Type{ICRSCoords{T1}}, ::Type{FK5Coords{T2,e}}) =
FK5J2000_TO_ICRS * precess_from_j2000(e)'
rotmat{e}(::Type{GalCoords}, ::Type{FK5Coords{e}}) =
rotmat{e,T1,T2}(::Type{GalCoords{T1}}, ::Type{FK5Coords{T2,e}}) =
FK5J2000_TO_GAL * precess_from_j2000(e)'
rotmat{e1, e2}(::Type{FK5Coords{e1}}, ::Type{FK5Coords{e2}}) =
rotmat{e1,T1,e2,T2}(::Type{FK5Coords{T1,e1}}, ::Type{FK5Coords{T2,e2}}) =
precess_from_j2000(e1) * precess_from_j2000(e2)'

# Scalar coordinate conversions
Expand Down
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ rad2arcsec(r) = 3600.*rad2deg(r)
fname = joinpath(datapath, "input_coords.csv")
indata, inhdr = readcsv(fname; header=true)

for (insys, T) in (("icrs", ICRSCoords), ("fk5j2000", FK5Coords{2000}),
("fk5j1975", FK5Coords{1975}), ("gal", GalCoords))
for (insys, T) in (("icrs", ICRSCoords{Float64}), ("fk5j2000", FK5Coords{Float64,2000}),
("fk5j1975", FK5Coords{Float64,1975}), ("gal", GalCoords{Float64}))

c_in = T[T(indata[i, 1], indata[i, 2]) for i=1:size(indata,1)]

for (outsys, S) in (("icrs", ICRSCoords), ("fk5j2000", FK5Coords{2000}),
("fk5j1975", FK5Coords{1975}), ("gal", GalCoords))
for (outsys, S) in (("icrs", ICRSCoords{Float64}), ("fk5j2000", FK5Coords{Float64,2000}),
("fk5j1975", FK5Coords{Float64,1975}), ("gal", GalCoords{Float64}))
(outsys == insys) && continue
c_out = convert(Vector{S}, c_in)

Expand Down

0 comments on commit 0b4c2b2

Please sign in to comment.