Skip to content

Instantly share code, notes, and snippets.

@jiahao
Last active October 11, 2015 00:29
Show Gist options
  • Save jiahao/4d7515dfa7a5987f74fe to your computer and use it in GitHub Desktop.
Save jiahao/4d7515dfa7a5987f74fe to your computer and use it in GitHub Desktop.
Ref: JuliaLang/julia#13503
function normtypes(S) #For the first post, comparing vector outputs with naive definitions
try
R0 = typeof(abs(one(S))^0+abs(one(S))^0)
R1 = typeof(abs(one(S))+abs(one(S)))
Rp = typeof(sqrt(abs(one(S))^2+abs(one(S))^2))
Ri = typeof(maximum(abs(one(S))))
Ri2 = typeof(abs(one(S)))
R0i = typeof(norm([one(S)], 0))
R1i = typeof(norm([one(S)], 1))
Rpi = typeof(norm([one(S)]))
Rii = typeof(norm([one(S)], Inf))
Rp = promote_type(R0, R1, Rp, Ri)
Rf = typeof(float(real(one(S))))
println(S, " | ",
R0!=R0i ? "**" : "", R0, R0!=R0i ? "**" : "", " | ", R0!=R0i ? "**" : "", R0i, R0!=R0i ? "**" : "", " | ",
R1!=R1i ? "**" : "", R1, R1!=R1i ? "**" : "", " | ", R1!=R1i ? "**" : "", R1i, R1!=R1i ? "**" : "", " | ",
Rp!=Rpi ? "**" : "", Rp, Rp!=Rpi ? "**" : "", " | ", Rp!=Rpi ? "**" : "", Rpi, Rp!=Rpi ? "**" : "", " | ",
Ri!=Rii ? "**" : "", Ri, Ri!=Rii ? "**" : "", " | ", Ri!=Rii ? "**" : "", Rii, Ri!=Rii ? "**" : "", " | ",
Ri2, " | ",
Rp!=Rf ? "**" : "", Rp, Rp!=Rf ? "**" : "", " | ", Rp!=Rf ? "**" : "", Rf, Rp!=Rf ? "**" : "", " | ",
)
end
end
function normtypes(S) #For the second post, comparing scalar and vector outputs
try
Rv0 = typeof(norm([one(S)], 0))
Rs0 = typeof(norm(one(S), 0))
Rv2 = typeof(norm([one(S)], 2))
Rs2 = typeof(norm(one(S), 2))
println(S, " | ",
Rv0!=Rs0 ? "**" : "", Rv0, Rv0!=Rs0 ? "**" : "", " | ", Rv0!=Rs0 ? "**" : "", Rs0, Rv0!=Rs0 ? "**" : "", " | ",
Rv2!=Rs2 ? "**" : "", Rv2, Rv2!=Rs2 ? "**" : "", " | ", Rv2!=Rs2 ? "**" : "", Rs2, Rv2!=Rs2 ? "**" : "", " | ",
)
end
end
function typerecurse(T)
for S in subtypes(T)
if isleaftype(S)
normtypes(S)
else
typerecurse(S)
end
end
#Recurse into type parameters
if isleaftype(T)
normtypes(T)
elseif length(T.parameters) > 0
if isa(T.parameters[1], TypeVar)
Umax = T.parameters[1].ub
normtypes(T{Umax})
if Umax != Any
for U in subtypes(Umax)
typerecurse(T, U)
end
end
else
normtypes(T)
end
end
end
function typerecurse(T, U)
normtypes(T{U})
for S in subtypes(U)
typerecurse(T, S)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment