Skip to content

Instantly share code, notes, and snippets.

@jiahao
jiahao / cg_hs.jl
Last active August 29, 2015 13:56
Conjugate gradients (Hestenes-Stiefel algorithm) implemented as a Julia iterator
import Base: start, next, done
immutable Terminator #Stores information related to when the algorithm should terminate
maxiter :: Int
resabsthresh :: Real
resrelthresh :: Real
end
Terminator(maxiter::Integer) = Terminator(maxiter, eps(), eps())
Terminator() = Terminator(0) #By default, always terminate.
@jiahao
jiahao / multipledispatchstats.jl
Last active August 29, 2015 13:58
Implement some statistics from 'Multiple Dispatch in Practice' in Julia
#Computes the dispatch ratio for each generic function defined in a module
#all: iterate over exported methods, or all methods?
function dispatchratio(M::Module=Base, all=false)
methodcount = Dict{Symbol, Int}()
for name in names(M, all)
try #name may not represent a function or function-able type
methodcount[name] = length(methods(eval(M, name)))
catch continue end
end
methodcount
@jiahao
jiahao / CheckUnicodeWidths.jl
Last active August 29, 2015 14:02
Check Unicode widths reported in Julia
RawUnicodeData=readdlm(download("http://www.unicode.org/Public/UCD/latest/ucd/EastAsianWidth.txt"), ';', String)
#UAX11 - http://www.unicode.org/reports/tr11/
widthmap={
"A "=>0, #ambiguous
"W "=>2, #wide
"F "=>2, #full
"N "=>0, #neutral
"Na "=>1, #narrow
"H "=>1, #half
@jiahao
jiahao / Computing character widths.ipynb
Last active August 29, 2015 14:04
Computing character widths.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jiahao
jiahao / methodsbyrank.jl
Created August 24, 2014 14:45
Which functions have methods defined only for scalars, vectors, or 2d-arrays?
el = Float64
types=(el, Vector{el}, Matrix{el})
functions=Dict()
for mytype in types
functions[mytype]={}
for method in methodswith(mytype, true)
(length(method.sig) == 1) || #Unary functions
(length(method.sig) == 2 && method.va) || #Binary function with varargs
continue
method.func.code.name in functions[mytype] && continue #Don't duplicate
@jiahao
jiahao / doublelength.jl
Last active August 29, 2015 14:05
An implementation of doublelength floating point arithmetic as described in Dekker, 1971. http://link.springer.com/article/10.1007%2FBF01397083 (See also https://github.com/jiahao/DoublelengthFloat.jl)
import Base: +, -, ⋅, *, /, √, convert
export DoublelengthFloat
immutable DoublelengthFloat{T<:FloatingPoint} <: FloatingPoint
head :: T
tail :: T
end
DoublelengthFloat(x::FloatingPoint) = DoublelengthFloat(x, zero(x))
convert{T<:FloatingPoint}(::Type{DoublelengthFloat{T}}, x::T) = DoublelengthFloat(x)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#function axpy!{T}(a::T, b::StridedVector{T}, c::StridedVector{T})
# @simd for i=1:size(b, 1)
# c[i] += a*b[i]
# end
#end
mgs(A)=mgs!(copy(A))
function mgs!(Q) #Does not store R
m, n = size(Q)
Qc= [slice(Q,:,k) for k=1:n]
@jiahao
jiahao / 12899.jl
Last active September 1, 2015 04:09
This code triggers a weird bug on OSX https://github.com/JuliaLang/julia/issues/12899
type BrokenArrowBidiagonal{T} <: AbstractMatrix{T}
dv::Vector{T}
av::Vector{T}
ev::Vector{T}
end
Base.size(B::BrokenArrowBidiagonal) = (n=length(B.dv); (n, n))
function Base.size(B::BrokenArrowBidiagonal, n::Int)
if n==1 || n==2
return length(B.dv)
else