include("Phi.jl")
using Phi
using PyPlot
For scalar
For matrix case, see Expokit paper.
# Scalar
z = 0.1
K = 4
P = phi(z,K)
Palt = phi_alt(z,K)
P ≈ Palt
true
# Matrix
A = randn(2,2)
K = 4
P = phim(A,K)
Palt = phim_alt(A,K)
P ≈ Palt
true
# Float64
K = 6
zero_points = phi(0.0, K)
zs = logspace(0, -12, 13)
residuals = zeros(K+1, length(zs))
for i in 1:length(zs)
# phis = phi(zs[i], K)
phis = phi_alt(zs[i], K)
@. residuals[:,i] = abs(phis - zero_points)
end
for k = 0:K
plot(zs, residuals[k+1,:], "-o", label="|ϕ$k(z) - ϕ$k(0)|")
end
loglog()
legend()
xlim(xlim()[2], xlim()[1])
xlabel("z")
title("Float64, scalar, Sidje");
# Float64
K = 4
dim = 5
zero_points = phim(zeros(dim,dim), K)
srand(0); A = randn(dim,dim)
zs = logspace(0, -12, 13)
residuals = zeros(K+1, length(zs))
for i in 1:length(zs)
# phims = phim(zs[i]*A, K)
phims = phim_alt(zs[i]*A, K)
for k in 1:K+1
residuals[k,i] = norm(phims[k] - zero_points[k], Inf)
end
end
for k = 0:K
plot(zs, residuals[k+1,:], "-o", label="|ϕ$k(zA) - ϕ$k(0)|")
end
loglog()
legend()
xlim(xlim()[2], xlim()[1])
xlabel("z")
title("Float64, $dim x $dim matrix, Sidje");
Scalar phi
vs phi_alt
:
z = 0.1
@time begin
for i = 1:1000
phi(z, 4)
end
end
0.000130 seconds (2.00 k allocations: 250.000 KiB)
@time begin
for i = 1:1000
phi_alt(z, 4)
end
end
0.043846 seconds (41.00 k allocations: 10.269 MiB, 5.13% gc time)
z = big(0.1)
@time begin
for i = 1:1000
phi(z, 4)
end
end
0.015232 seconds (74.00 k allocations: 2.869 MiB)
Matrix phim
vs phim_alt
dim = 10
A = randn(dim,dim);
@time begin
for i = 1:1000
phim(A, 4)
end
end
0.202246 seconds (87.00 k allocations: 49.683 MiB, 8.80% gc time)
@time begin
for i = 1:1000
phim_alt(A, 4)
end
end
1.011265 seconds (877.00 k allocations: 737.366 MiB, 18.17% gc time)
A = big.(A);
@time begin
for i = 1:1000
phim(A, 4)
end
end
9.409701 seconds (94.35 M allocations: 3.829 GiB, 29.62% gc time)