Skip to content

Commit bb55ab7

Browse files
authored
minor reductions in allocation in specialfunctions (#165)
* minor reductions in allocation in specialfunctions * reductions in Point/ConstantSpace
1 parent 739c470 commit bb55ab7

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

src/specialfunctions.jl

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@ splitmap(g,d::Domain,pts) = Fun(g,split(d , pts))
33

44
function split(d::IntervalOrSegment, pts)
55
a,b = endpoints(d)
6-
isendpoint = true
7-
for p in pts
8-
if !(p a) && !(p b)
9-
isendpoint = false
10-
break
11-
end
12-
end
6+
isendpoint = all(p -> p a || p b, pts)
137
isendpoint && return d
148

15-
@assert all(in.(pts, Ref(d)))
9+
@assert all(in(d), pts)
1610
PiecewiseSegment(sort!(union(endpoints(d), pts)))
1711
end
1812

1913
function split(d::PiecewiseSegment, pts)
20-
@assert all(in.(pts, Ref(d)))
14+
@assert all(in(d), pts)
2115
PiecewiseSegment(sort!(union(d.points, pts)))
2216
end
2317

@@ -411,9 +405,9 @@ end
411405
for SP in (:ConstantSpace,:PointSpace)
412406
for OP in (:abs,:sign,:exp,:sqrt,:angle)
413407
@eval begin
414-
$OP(z::Fun{<:$SP,<:Complex}) = Fun(space(z),$OP.(coefficients(z)))
415-
$OP(z::Fun{<:$SP,<:Real}) = Fun(space(z),$OP.(coefficients(z)))
416-
$OP(z::Fun{<:$SP}) = Fun(space(z),$OP.(coefficients(z)))
408+
$OP(z::Fun{<:$SP,<:Complex}) = Fun(space(z),map($OP, coefficients(z)))
409+
$OP(z::Fun{<:$SP,<:Real}) = Fun(space(z),map($OP, coefficients(z)))
410+
$OP(z::Fun{<:$SP}) = Fun(space(z),map($OP, coefficients(z)))
417411
end
418412
end
419413

@@ -500,35 +494,42 @@ for op in (:(argmax),:(argmin))
500494
end
501495
end
502496

497+
if VERSION < v"1.7"
498+
_maybemap(rf, f, pts) = rf(map(f, pts))
499+
else
500+
_maybemap(rf, f, pts) = rf(f, pts)
501+
end
502+
503503
for op in (:(findmax),:(findmin))
504504
@eval begin
505505
function $op(f::Fun{<:RealSpace,<:Real})
506506
# the following avoids warning when differentiate(f)==0
507507
pts = extremal_args(f)
508-
ext,ind = $op(f.(pts))
509-
ext,pts[ind]
508+
ext,ind = _maybemap($op, f, pts)
509+
ext,pts[ind]
510510
end
511511
end
512512
end
513513

514-
extremal_args(f::Fun{<:PiecewiseSpace}) = cat(1,[extremal_args(fp) for fp in components(f)]..., dims=1)
514+
extremal_args(f::Fun{<:PiecewiseSpace}) = cat(1,[extremal_args(fp) for fp in components(f)], dims=1)
515515

516516
function extremal_args(f::Fun)
517517
d = domain(f)
518-
519-
dab = strictconvert(Vector{Number}, collect(components((domain(f)))))
520-
if ncoefficients(f) <=2 #TODO this is only relevant for Polynomial bases
521-
dab
522-
else
523-
[dab;roots(differentiate(f))]
518+
T = prectype(d)
519+
dab = strictconvert(Vector{T}, collect(components((domain(f)))))
520+
if ncoefficients(f) > 2 #TODO this is only relevant for Polynomial bases
521+
r = roots(differentiate(f))
522+
if !isempty(r)
523+
append!(dab, r)
524+
end
524525
end
526+
return dab
525527
end
526528

527529
for op in (:(maximum),:(minimum),:(extrema))
528530
@eval function $op(f::Fun{<:RealSpace,<:Real})
529531
pts = iszero(f') ? [leftendpoint(domain(f))] : extremal_args(f)
530-
v = map(f, pts)
531-
$op(v)
532+
_maybemap($op, f, pts)
532533
end
533534
end
534535

@@ -537,12 +538,12 @@ for op in (:(maximum),:(minimum))
537538
@eval begin
538539
function $op(::typeof(abs), f::Fun{<:RealSpace,<:Real})
539540
pts = iszero(f') ? [leftendpoint(domain(f))] : extremal_args(f)
540-
$op(f.(pts))
541+
_maybemap($op, f, pts)
541542
end
542543
function $op(::typeof(abs), f::Fun)
543544
# complex spaces/types can have different extrema
544545
pts = extremal_args(abs(f))
545-
$op(f.(pts))
546+
_maybemap($op, f, pts)
546547
end
547548
$op(f::Fun{PiecewiseSpace{<:Any,<:UnionDomain,<:Real},<:Real}) =
548549
$op(map($op,components(f)))

0 commit comments

Comments
 (0)