Skip to content

Instantly share code, notes, and snippets.

View jrevels's full-sized avatar

Jarrett Revels jrevels

View GitHub Profile
params{T}(::Type{T}) = T.parameters
# Parameterize column types and fields
# by utilizing tuples
type DataFrame{C<:Tuple, F<:Tuple}
columns::C
fields::Type{F}
function DataFrame(columns::C, fields::Type{F})
nrows = length(first(columns))
equallengths = true
using ForwardDiff
function check_failure_ratio(test_hessf::Function, input::Array, original::Array, max_iter=10e6)
bad_versions = 0
for iter = 1:max_iter
if maximum(abs(test_hessf(input) - original)) > 1
bad_versions += 1;
end
end
# This file is a naive implementation of numbers of the form x + y*i where i^2 = p
#######################
# GeneralComplex type #
#######################
immutable GeneralComplex{p,T} <: Number
x::T
y::T
end
@everywhere module A
immutable Foo{T}
x::T
end
cross(a::Foo, b::Foo) = Foo(a.x * b.x)
function reload_cross(a, b)
include(joinpath(pwd(), "parallel-reload-bug.jl"))
return A.cross(a, b)
@jrevels
jrevels / mpi_pagerank_kernel0.jl
Last active July 7, 2016 13:25
An MPI.jl-based implementation of kernel 0 from http://arxiv.org/abs/1603.01876
# execute by running `➜ mpiexec -n $NUM_PROCS julia mpi_pagerank_kernel0.jl`
import MPI
MPI.Init()
######################
# KronGraph500NoPerm #
######################
function tupexpr(f, N)
ex = Expr(:tuple, [f(i) for i=1:N]...)
return quote
@inbounds return $(ex)
end
end
@inline iszero_tuple(::Tuple{}) = true
@inline zero_tuple(::Type{Tuple{}}) = tuple()
@inline one_tuple(::Type{Tuple{}}) = tuple()
using ForwardDiff
using BenchmarkTools
function gen_beta_kl_obj(alpha2, beta2)
lgamma_alpha2 = lgamma(alpha2)
lgamma_beta2 = lgamma(beta2)
return x -> begin
alpha1, beta1 = x
alpha_diff = alpha1 - alpha2
beta_diff = beta1 - beta2
using BenchmarkTools
using ReverseDiff: @forward, GradientTape, gradient!, compile
# Similar to XDiff's @diff_rule, except it gets the derivative automatically via forward mode.
# In future versions, `@forward` will no longer be necessary.
@forward logistic(x::Real) = 1 / (1 + exp(-x))
# This is how I would write this for ReverseDiff usage if parser fusion didn't mess things up.
# In the future, this form will be performant (all the pieces already exist, they just have
# to be hooked up).
using Cassette
function rosenbrock(x)
a = one(eltype(x))
b = 100.0
result = zero(a)
for i in 1:length(x)-1
result += (a - x[i])^2 + b*(x[i+1] - x[i]^2)^2
end
return result
@jrevels
jrevels / recursive_ad.jl
Last active February 23, 2018 16:57
recursive_ad.jl
# This script requires an up-to-date ForwardDiff, ReverseDiff, and Julia v0.6 installation.
using ForwardDiff, ReverseDiff
D_f(f) = x::Number -> ForwardDiff.derivative(f, x)
# ReverseDiff's API only supports array inputs, so we just wrap our scalar input in a
# 1-element array and extract our scalar derivative from the returned gradient array
D_r(f) = x::Number -> ReverseDiff.gradient(y -> f(y[1]), [x])[1]