{{ message }}

Instantly share code, notes, and snippets.

# David Gold davidagold

• San Francisco, CA
Last active Aug 29, 2015
Perform the simplex method on a linear programming problem in canonical form
View simplexmethod.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 function simplex() # (A::Array{Float64, 2}, b::Array{Float64, 1}, c::Array{Float64, 1}) # Initialize the constraint matrix A, constraint vector b, objective function c # In the future these will be passed as arguments to simplex() A = Float64[4 2 5 15 4; 1 4 5 1 2; 1 0 3 1 11] b = Float64[17, 9, 16] c = Float64[15, 4, 10, 11, 27] println("We first find a basic feasible solution to the given problem (or show that none exists).") # Perform Phase I on given problem
Last active Aug 29, 2015
Merge two functions from different modules
View metamerge.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 function metamerge(f::Function, modulef::Module, g::Function, moduleg::Module, h::Function) # Generate arrays of Method objects for f, g const fmethods = methods(f, (Any...)) const gmethods = methods(g, (Any...)) # println(gmethods) # Generate arrays of method signatures for f, g. # Note that the 'sig' field of a Method is a tuple of types.
Created Jun 20, 2015
map
View gist:145e0a0f41f6d1baa915
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 function Base.map!{F}(f::F, dest::NullableArray, A::AbstractArray) # -> NullableArray{T, N} local func function func(dest, A) for i in 1:length(dest) dest[i] = f(A[i]) end end func(dest, A)
Created Jul 2, 2015
Reduce methods testing/profiling
View profile_reduce.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 using NullableArrays A = rand(5_000_000) X = NullableArray(A) f(x) = 5 * x f{T<:Number}(x::Nullable{T}) = Nullable(5 * x.value, x.isnull) function Base.mapreduce_impl{T}(f, op::Base.MinFun, X::NullableArray{T}, first::Int, last::Int) # locate the first non-null entry
Last active Aug 29, 2015
NullableArrays `map` perf
View gist:0de522ac52973936ef9d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 using NullableArrays srand(1) N = 5_000_000 A = rand(N) B = rand(Bool, N) C = Array(Float64, N) X = NullableArray(A)
Created Jul 25, 2015
`NullityIndex` -- indexing non-empty entries of a `NullableArray`
View nullityindex.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 immutable NullityIndex non_nulls::Vector{Int} end function NullityIndex(X::NullableArray) inds = Vector{Int}() sizehint!(inds, length(X.isnull)) for i in eachindex(X.isnull) !X.isnull[i] && push!(inds, i) end
Last active Aug 29, 2015
`NullityIndex` -- indexing non-empty entries of a `NullableArray`
View .jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 immutable NullityIndex ranges::Vector{UnitRange{Int}} singletons::Vector{Int} end function NullityIndex(X::NullableArray) ranging = false push = false first = 0 last = 0
Created Sep 25, 2015
average pairwise distance over array of Float 2-tuples
View pwise_meandist.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 function pwise_meandist(A::Array{Tuple{FLoat64, Float64}}) s = 0.0 i = 1 n = length(A) while i < n (x0, y0) = A[i] for j in (i+1):length(A) (x1, y1) = A[j] s += sqrt((x0 - x1)^2 + (y0 - y1)^2) end
Created Dec 21, 2015
Revised lift macro
View liftmacro.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 Base.isnull(x) = false Base.get(x) = x macro ^(call, T...) arg_cache = Dict{Union{Symbol, Expr}, Expr}() # if !(isa(call, Expr)) || call.head != :call # throw(ArgumentError("@^: argument must be a function call")) # end if length(T) == 1 e_type = T[1]
Created Jul 7, 2016
View mymap.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 macro fcall(Xs, nargs) res = Expr(:call, :f) for i in 1:nargs push!(res.args, :(\$(Xs)[\$i][i])) end return res end mymap(f, Xs...) = _map(f, Xs...)