Skip to content

Instantly share code, notes, and snippets.

# Compute the nth elementary symmetric function (elementary symmetric polynomial) of a a 1D iterable x
# This is the THIRD esf
function esf(n, x)
summand = zero(eltype(x))
for j1 = 1:n, j2 = 1:j1-1, j3 = 1:j2-1
summand += x[j1]*x[j2]*x[j3]
end
return summand
end
@jiahao
jiahao / ipm.jl
Created July 7, 2016 19:41
A simple interior point method implementation. Ref: http://www.maths.ed.ac.uk/~gondzio/reports/mfCS.pdf
#Problem-specific thing
dualitygap(x...) = nothing
solveeq(x...) = rand()
############################
#Simple backtracking line search
function linesearch(v, dv, α₀= 1.0, δ = 0.6)
α = α₀
while v + α*dv ≤ 0
@jiahao
jiahao / movielens-20m-dl.jl
Created May 15, 2016 05:24
Julia script for downloading the MovieLens 20M dataset from http://grouplens.org/datasets/movielens/
using CSV
using Nettle
using ZipFile
zfilename = download("http://files.grouplens.org/datasets/movielens/ml-20m.zip")
#TODO check hashes
#md5 = readchomp(open(download("http://files.grouplens.org/datasets/movielens/ml-20m.zip.md5")))
#md5dl = open(zfilename) do f hexdigest(readall(f)) end
@jiahao
jiahao / m012.jl
Last active May 21, 2017 06:02
Matvecs with matrices in PLINK v1 BEM format
import Base: convert, rand, size, getindex, A_mul_B!
immutable PLINK1Matrix <: AbstractMatrix{UInt8}
m :: Int
n :: Int
data :: Array{UInt8}
end
size(M::PLINK1Matrix, i::Int) = i==1 ? M.m : i==2 ? M.n : error()
size(M::PLINK1Matrix) = (M.m, M.n)
@jiahao
jiahao / nrules.svg
Last active May 17, 2022 08:30
What do .gitignore files tell us about the inherent complexity of programming languages? This gist can be run on data from GitHub's own collection of gitignore templates, whose repository is at https://github.com/github/gitignore, and counts how many rules are present in the template for each programming language.
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.
@jiahao
jiahao / reservoir.jl
Created November 25, 2015 16:03
An implementation of Vitter's Method D for sampling a random subset from an arbitrary iterable in doi:10.1145/23002.23003
function randsubset(iterable, n::Int, T::Int=22)
state = start(iterable)
sample = eltype(iterable)[]
sizehint!(sample, n)
for j=1:n #Make first n record candidates for the sample
x, state = next(iterable, state)
done(iterable, state) && j<n && error("Requested sample of size $n but only $j items available")
push!(sample, x)
end
t = n #Number of records processed so far
@jiahao
jiahao / julia.jl
Created November 24, 2015 02:58
Rank calculus for JuliaLang/julia#4774
include("rankstability.jl")
info("Julia semantics")
axiomsused = Dict()
@axiom matmul Mat * Mat --> Mat
@axiom mattrans transpose(Mat) --> Mat
@axiom matdiv Mat / Mat --> Mat
@axiom matvec Mat * Vec --> Vec
@jiahao
jiahao / savitzkygolay.jl
Last active April 26, 2024 12:03
An implementation of the Savitzky-Golay filter using generated functions. Accompanies https://medium.com/@acidflask/smoothing-data-with-julia-s-generated-functions-c80e240e05f3
"""
Savitzky-Golay filter of window half-width M and degree N
M is the number of points before and after to interpolate, i.e. the full width
of the window is 2M+1
"""
immutable SavitzkyGolayFilter{M,N} end
@generated function Base.call{M,N,T}(::Type{SavitzkyGolayFilter{M,N}},
data::AbstractVector{T})
"""
estimate_coeffs
August, 2015
Anna C. Gilbert
input: xs = samples of signal
Λ = list of (freq,coeff) pairs in current approx
Ω = list of (freq,coeff) pairs found in this iteration
k = length of short filter