Skip to content

Commit

Permalink
Merge pull request #7 from pabloferz/pz/julia-0.6.0
Browse files Browse the repository at this point in the history
Julia v0.6.0 syntax updates
  • Loading branch information
pabloferz committed Jun 29, 2017
2 parents c1a996e + bab38b0 commit 6b40885
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/NIntegration.jl
Expand Up @@ -10,7 +10,11 @@ using StaticArrays

### Implementation
path = dirname(realpath(@__FILE__)) # works even for symlinks
include(joinpath(path, "types.jl"))
if VERSION < v"0.6.0"
include(joinpath(path, "types-0.5.jl"))
else
include(joinpath(path, "types.jl"))
end
include(joinpath(path, "rules.jl"))
include(joinpath(path, "utils.jl"))
include(joinpath(path, "integration.jl"))
Expand Down
28 changes: 28 additions & 0 deletions src/types-0.5.jl
@@ -0,0 +1,28 @@
type Box{T}
x::T

Box() = new()
Box(x) = new(x)
end
(::Type{Box}){T}(x::T) = Box{T}(x)

immutable Region{N,T<:AbstractFloat,R}
x::MVector{N,T} # center
h::MVector{N,T} # half-width
I::Box{R}
E::Box{R}
axis::Box{Int8}
end
(::Type{Region}){R,N,T}(::Type{R}, x::NTuple{N,T}, h::NTuple{N,T}) =
Region{N,T,R}(MVector(x), MVector(h), Box{R}(), Box{R}(), Box(Int8(0)))
(::Type{Region}){N,T}(x::NTuple{N,T}, h::NTuple{N,T}) =
Region{N,T,Float64}(MVector(x), MVector(h), Box(0.0), Box(0.0), Box(Int8(0)))

immutable Regions{R<:Region}
v::Vector{R}
end

immutable WPoints{N,T,R}
w::Vector{R}
p::Vector{SVector{N,T}}
end
34 changes: 10 additions & 24 deletions src/types.jl
@@ -1,42 +1,28 @@
type Box{T}
mutable struct Box{T}
x::T

Box() = new()
Box(x) = new(x)
Box{T}() where {T} = new()
Box{T}(x) where {T} = new(x)
end
(::Type{Box}){T}(x::T) = Box{T}(x)
Box(x::T) where {T} = Box{T}(x)

immutable Region{N,T<:AbstractFloat,R}
struct Region{N,T<:AbstractFloat,R}
x::MVector{N,T} # center
h::MVector{N,T} # half-width
I::Box{R}
E::Box{R}
axis::Box{Int8}
end
(::Type{Region}){R,N,T}(::Type{R}, x::NTuple{N,T}, h::NTuple{N,T}) =
Region(::Type{R}, x::NTuple{N,T}, h::NTuple{N,T}) where {R,N,T} =
Region{N,T,R}(MVector(x), MVector(h), Box{R}(), Box{R}(), Box(Int8(0)))
(::Type{Region}){N,T}(x::NTuple{N,T}, h::NTuple{N,T}) =
Region{N,T,Float64}(MVector(x), MVector(h),
Box(0.0), Box(0.0), Box(Int8(0)))
Region(x::NTuple{N,T}, h::NTuple{N,T}) where {N,T} =
Region{N,T,Float64}(MVector(x), MVector(h), Box(0.0), Box(0.0), Box(Int8(0)))

Base.isless{N}(r₁::Region{N}, r₂::Region{N}) = isless(r₁.E.x, r₂.E.x)
Base.isequal{N}(r₁::Region{N}, r₂::Region{N}) = isequal(r₁.E.x, r₂.E.x)

Base.show(io::IO, r::Region) = print(io, '(', r.x, ", ", r.h, ')')

immutable Regions{R<:Region}
struct Regions{R<:Region}
v::Vector{R}
end

Base.size(r::Regions) = size(r.v)
Base.getindex(r::Regions, i::Int) = r.v[i]
Base.setindex!(r::Regions, v, i::Int) = (r.v[i] = v)

Base.show(io::IO, m::MIME"text/plain", r::Regions) = show(io, m, r.v)
Base.show(io::IO, r::Regions) =
(n = length(r.v); print(io, n, " subregion", n == 1 ? "" : "s"))

immutable WPoints{N,T,R}
struct WPoints{N,T,R}
w::Vector{R}
p::Vector{SVector{N,T}}
end
37 changes: 33 additions & 4 deletions src/utils.jl
@@ -1,3 +1,7 @@
#############################
# Programming utilities #
#############################

for N = 1:4
# Equivalent to `integral_type(f, x) = typeof(p₀[1] * f(x...)` but inferrable
@eval function integral_type{T}(f, x::NTuple{$N,T})
Expand All @@ -12,14 +16,39 @@ for N = 1:4
end
end

idem(x, t) = (x, t)

### Getters and setters
weights(wp::WPoints) = wp.w
points(wp::WPoints) = wp.p

###############################
# Array related utilities #
###############################

Base.length(r::Regions) = length(r.v)
Base.getindex(r::Regions, i::Int) = r.v[i]

function Base.push!{N,T,R}(wp::WPoints{N,T,R}, t::Tuple{R,SVector{N,T}})
push!(wp.w, t[1])
push!(wp.p, t[2])
end

weights(wp::WPoints) = wp.w
points(wp::WPoints) = wp.p

idem(x, t) = (x, t)
######################
# Math utilities #
######################

chop{T}(x::T) = ifelse(x < eps(T), zero(T), x)

Base.isless{N}(r₁::Region{N}, r₂::Region{N}) = isless(r₁.E.x, r₂.E.x)
Base.isequal{N}(r₁::Region{N}, r₂::Region{N}) = isequal(r₁.E.x, r₂.E.x)

####################
# IO utilities #
####################

Base.show(io::IO, r::Region) = print(io, '(', r.x, ", ", r.h, ')')

Base.show(io::IO, m::MIME"text/plain", r::Regions) = show(io, m, r.v)
Base.show(io::IO, r::Regions) =
(n = length(r.v); print(io, n, " subregion", n == 1 ? "" : "s"))
9 changes: 9 additions & 0 deletions test/integration.jl
Expand Up @@ -62,10 +62,19 @@ let f = (x, y, z) -> x * sin(2y) * cos(3z)

(I, E, n, R) = nintegrate(f, xmin, xmax)
@test I == nintegrate(f, R)
@test length(R) == 1

W = weightedpoints(R)
@test I nintegrate(f, W)

W = weightedpoints(NIntegration.idem, R)
@test I nintegrate(f, W)

p = [0.5, 0.5, 0.5]
io = IOBuffer()
show(io, R)
@test String(take!(io)) == "1 subregion"
show(io, R[1])
@test String(take!(io)) == string("(", p, ", ", p, ")")

end

0 comments on commit 6b40885

Please sign in to comment.