Skip to content

Instantly share code, notes, and snippets.

@goretkin
Created November 24, 2014 20:19
Show Gist options
  • Save goretkin/5d4fb776b9ef09f4bd1f to your computer and use it in GitHub Desktop.
Save goretkin/5d4fb776b9ef09f4bd1f to your computer and use it in GitHub Desktop.
temporary work
...
function breaks{S,T}(sp::PiecewiseSpace{S,T})
_breaks = T[]
push!(_breaks, sp[1].domain.a)
for i = 1:length(sp)
push!(_breaks, sp[i].domain.b)
end
return _breaks
end
function merge_sorted{T<:Number}(a::Vector{T},b::Vector{T})
@assert issorted(a)
@assert issorted(b)
i::Int = 1
j::Int = 1
k::Int = 1
r = zeros(T, length(a)+length(b))
while i<=length(a) && j<=length(b)
if a[i] < b[j]
r[k] = a[i]
i+=1
else
r[k] = b[j]
j+=1
end
k+=1
end
#copy tail
if i>length(a)
copy!(r,k,b,j,length(b)-j + 1)
else
copy!(r,k,a,i,length(a)-i + 1)
end
end
function merge_pieces(f::PiecewiseSpace,g::PiecewiseSpace)
#check that endpoints of overall domain are compatible
#TODO: maybe use domainscompatible
@assert f.space[1].domain.a == g.space[1].domain.a
@assert f.space[end].domain.b == g.space[end].domain.b
fbreaks = breaks(f.space)
gbreaks = breaks(g.space)
_breaks = unique(merge_sorted(fbreaks, gbreaks))
centers = (breaks[2:end] + breaks[1:end-1]) / 2
space_tup = Any[]
#TODO should probably keep track of the space while doing the merging of intervals
for c in centers
i = searchsortedfirst(fbreaks, c)
j = searchsortedfirst(gbreaks, c)
push!(space_tup, (f.space[i], g.space[j] ))
end
return Interval(_breaks), space_tup
end
## space promotion
canonicalspace(sp::PiecewiseSpace)=PiecewiseSpace(map(canonicalspace,sp.spaces))
for op in (:maxspace,:minspace)
@eval begin
function $op(f::PiecewiseSpace,g::PiecewiseSpace)
if f.space == g.space #TODO maybe allow numerical noise
PiecewiseSpace([$op(f[k],g[k]) for k=1:length(f)])
else
end
end
end
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment