Skip to content

Instantly share code, notes, and snippets.

View bicycle1885's full-sized avatar

Kenta Sato bicycle1885

  • Karolinska Institute
  • Stockholm, Sweden
View GitHub Profile
@bicycle1885
bicycle1885 / variance.c
Last active August 29, 2015 14:02
Haskell vs. C - variance calculation
#include <stdlib.h>
#include <stdio.h>
// Two-pass algorithm:
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Two-pass_algorithm
double var(double* v, size_t n)
{
double mean = 0.0;
for (int i = 0; i < n; i++) {
mean += v[i];
{-# LANGUAGE FlexibleContexts #-}
import qualified Data.Vector.Generic as G
fooG :: (G.Vector v Int) => Int -> v Int -> v Bool
fooG x = G.map (> x)
-- hello.hs:5:10:Could not deduce (G.Vector v Bool) arising from a use of ‘G.map’
-- from the context (G.Vector v Int)
-- bound by the type signature for
-- fooG :: G.Vector v Int => Int -> v Int -> v Bool
@bicycle1885
bicycle1885 / out.txt
Created December 29, 2014 16:14
scope leak?
UndefVarError(:x)
x: 10
@bicycle1885
bicycle1885 / anms.patch
Created March 7, 2015 18:36
Benchmark of Nelder-Mead algorithms
diff --git a/src/ANMS.jl b/src/ANMS.jl
index b235efd..5f93caa 100644
--- a/src/ANMS.jl
+++ b/src/ANMS.jl
@@ -72,6 +72,7 @@ function nelder_mead(f::Function, x₀::Vector{Float64}; iterations::Int=1_000_0
u = zeros(n)
for i in 1:n
τ = x₀[i] == 0.0 ? 0.00025 : 0.05
+ τ = 1.0
u[i] = 1.0
# 間違い!
module Hoge
export length
function length()
0
end
end
using Hoge
@bicycle1885
bicycle1885 / conv.jl
Last active September 13, 2015 04:23
New v0.4 features.
type Foo
x
Foo(x::Int) = new(x)
end
Base.convert(::Type{Foo}, x::Integer) = Foo(convert(Int, x))
@show Foo(1) # ok both on v0.3 and v0.4
@show Foo(0x01) # ok on v0.4, but not on v0.3
diff --git a/src/TinySegmenter.jl b/src/TinySegmenter.jl
index 7fcb27a..7e73fbb 100644
--- a/src/TinySegmenter.jl
+++ b/src/TinySegmenter.jl
@@ -5,6 +5,11 @@ export tokenize
typealias US UTF8String
+Base.hash(x::Tuple{Char,Char}) = hash((UInt64(x[2]) << 32) | UInt64(x[1]))
+Base.hash(x::Tuple{UInt8,UInt8}) = hash((UInt64(x[2]) << 8) | UInt64(x[1]))
open(`ls`) do stream
for line in eachline(stream)
@show line
end
end
@bicycle1885
bicycle1885 / nelder_mead.jl
Created December 19, 2015 02:59
JuliaTokyo#5 Nelder-Meadアルゴリズム
"""
Nelder-Mead algorithm
nelder_mead(f, x0, n)
Arguments
---------
* `f`: target function to optimize
* `x0`: initial value
@bicycle1885
bicycle1885 / JuliaTuning.ipynb
Created December 18, 2015 23:05
JuliaTokyo#5 ハンズオン (佐藤建太)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.