Skip to content

Instantly share code, notes, and snippets.

View jwmerrill's full-sized avatar

Jason Merrill jwmerrill

View GitHub Profile
@jwmerrill
jwmerrill / fzero_vs_bisect_benchmark.jl
Created February 15, 2014 01:16
Benchmark simple bisection against Roots.fzero polyalgorithm
using Roots
# Alternative "mean" definition that operates on the binary representation
# of a float. Using this definition, bisection will never take more than
# 64 steps.
function _middle(x::Float64, y::Float64)
# Use the usual float rules for combining non-finite numbers
if !isfinite(x) || !isfinite(y)
return x + y
end
@jwmerrill
jwmerrill / viterbi_2.jl
Created September 21, 2014 18:02
Viterbi algorithm
const states = ["Healthy", "Fever"]
const obsnames = ["normal", "cold", "dizzy"]
const start_probability = [0.6, 0.4]
const transition_probability = [
0.7 0.3
0.4 0.6
]
@jwmerrill
jwmerrill / graythresh_kmeans.jl
Created November 10, 2014 02:18
Kmeans thresholding of a grayscale image
function graythresh_kmeans(img)
# Note, without the type annotation, this is slow because
# mean appears not to be type stable here
thresh = mean(img)::Gray{Float32}
while true
lastthresh = thresh
nplus = 0
nminus = 0
splus = zero(typeof(thresh))
@jwmerrill
jwmerrill / elm-runtime.diff
Last active August 29, 2015 14:09
Modification of elm's runtime to allow drawing more consistently at 60 fps
9345,9351c9345,9374
- var previousDrawId = 0;
- function domUpdate(newScene) {
- previousDrawId = ElmRuntime.draw(previousDrawId, function(_) {
- Render.update(elm.node.firstChild, savedScene, newScene);
- if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
- savedScene = newScene;
- });
+
+ // domUpdate is called whenever the main Signal changes. On domUpdate,
'use strict';
var Elm = {}; Elm.Native = {}; Elm.Native.Graphics = {};
var ElmRuntime = {}; ElmRuntime.Render = {};
Elm.Native.Array = {};
Elm.Native.Array.make = function(elm) {
elm.Native = elm.Native || {};
elm.Native.Array = elm.Native.Array || {};
if (elm.Native.Array.values) return elm.Native.Array.values;
if ('values' in Elm.Native.Array)
return elm.Native.Array.values = Elm.Native.Array.values;
jwm@jason ~/s/elm-platform> runhaskell src/BuildFromSource.hs master
Writing a default package environment file to
/Users/jwm/src/elm-platform/Elm-Platform/master/cabal.sandbox.config
Creating a new sandbox at /Users/jwm/src/elm-platform/Elm-Platform/master
Cloning into 'elm-compiler'...
remote: Counting objects: 17753, done.
remote: Compressing objects: 100% (100/100), done.
remote: Total 17753 (delta 56), reused 0 (delta 0)
Receiving objects: 100% (17753/17753), 5.44 MiB | 1.20 MiB/s, done.
Resolving deltas: 100% (10044/10044), done.
GC(T+97773.6) Reason: TOO_MUCH_MALLOC, Total Time: 16.9ms, Zones Collected: 1, Total Zones: 30, Total Compartments: 417, Minor GCs: 2, Store Buffer Overflows: 0, MMU (20ms): 15%, MMU (50ms): 66%, SCC Sweep Total: 0.3ms, SCC Sweep Max Pause: 0.3ms, Nonincremental Reason: malloc bytes trigger, Allocated: 178MB, +Chunks: 0, -Chunks: 0
Totals:
The connection to http://jsbin.com/piyeye/1?0.44316687606709093 was interrupted while the page was loading. spike.js:232:0
GC(T+97775.4) Reason: TOO_MUCH_MALLOC, Total Time: 26.6ms, Zones Collected: 1, Total Zones: 32, Total Compartments: 420, Minor GCs: 3, Store Buffer Overflows: 0, MMU (20ms): 0%, MMU (50ms): 46%, SCC Sweep Total: 0.5ms, SCC Sweep Max Pause: 0.5ms, Nonincremental Reason: malloc bytes trigger, Allocated: 179MB, +Chunks: 0, -Chunks: 0
Totals:
GC(T+97777.0) Max Pause: 37.6ms, Total Time: 309.2ms, Zones Collected: 32, Total Zones: 32, Total Compartments: 420, Minor GCs: 25, Store Buffer Overflows: 0, MMU (20ms): 0%, MMU (50ms): 16%, SCC Sweep Total:
@jwmerrill
jwmerrill / DigitSets.jl
Created October 21, 2015 22:52
Efficient representation of a set of digits 1-9
# Represent a set of digits 1-9 as an Int16, where the binary digits of the
# integer form a mask determining whether the associated digit is present in
# the set
immutable DigitSet
d::Int16
end
function DigitSet(a::AbstractArray)
d = Int16(0)
for n in a
@jwmerrill
jwmerrill / reinterpret
Created November 9, 2015 17:19
Float int cast native code
julia> function fn(x1::Float64, x2::Float64)
x1_int = reinterpret(UInt64, x1)
x2_int = reinterpret(UInt64, x2)
reinterpret(Float64, (x1_int + x2_int) >> 1)
end
fn (generic function with 1 method)
julia> @code_native fn(3.0, 4.0)
.section __TEXT,__text,regular,pure_instructions
Filename: none
run(`wget http://julialang.org/images/logo.png`)
# Open png file for reading
fp = ccall(:fopen, Ptr{Uint8}, (Ptr{Uint8}, Ptr{Uint8}), "logo.png", "r")
# Read first 8 bytes
initial_bytes = Array(Uint8, 8)
ccall(:fread, Ptr{Uint8}, (Ptr{Uint8}, Uint8, Uint8, Ptr{Uint8}), initial_bytes, 1, 8, fp)
# Check that we actually have a png file