Skip to content

Commit c869c7f

Browse files
authored
avoid recursion in intpow (#105)
* avoid recursion in intpow * Add explicit tests for intpow
1 parent e4922b7 commit c869c7f

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Fun.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,13 @@ function intpow(f::Fun,k::Integer)
363363
ones(space(f))
364364
elseif k==1
365365
f
366-
elseif k > 1
367-
f*f^(k-1)
368366
else
369-
1/f^(-k)
367+
t = reduce(*, fill(f, abs(k)))
368+
if k > 0
369+
return t
370+
else
371+
return 1/t
372+
end
370373
end
371374
end
372375

test/SpacesTest.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ import ApproxFunBase: PointSpace, HeavisideSpace, PiecewiseSegment, dimension, V
3636
end
3737
end
3838
end
39+
@testset "intpow" begin
40+
@test ApproxFunBase.intpow(f, 0) == f^0 == Fun(space(f), ones(ncoefficients(f)))
41+
for n in 1:3
42+
@test ApproxFunBase.intpow(f, n) == f^n == reduce(*, fill(f, n))
43+
end
44+
@test ApproxFunBase.intpow(f,-2) == f^-2 == 1/(f*f)
45+
end
3946
end
4047

4148
@testset "Derivative operator for HeavisideSpace" begin

0 commit comments

Comments
 (0)