Skip to content

Instantly share code, notes, and snippets.

@naohaq
naohaq / bucket.rb
Last active December 21, 2022 02:02
Monte Carlo sim for voting
#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8-unix -*-
def rsample(n,m)
l = n+m-1
bins = Array.new(l,0)
(0..(m-2)).each{|k| bins[k]=1}
(0..(l-2)).each do |k|
j = rand(l-k) + k
@naohaq
naohaq / binomlik2.hs
Last active May 25, 2020 06:39
Chart example
module Main where
import Numeric (showFFloat)
import qualified Numeric.SpecFunctions as N
import Numeric.Tools.Integration
import Graphics.Rendering.Chart.Easy
import Graphics.Rendering.Chart.Backend.Cairo
dbeta :: Double -> Double -> Double -> Double
@naohaq
naohaq / dot-irbrc.rb
Created May 17, 2020 14:35
IRB Color configuration
# -*- mode: ruby; coding: utf-8-unix -*-
IRB::Color.instance_eval do
exprs = const_get('TOKEN_SEQ_EXPRS')
t_all = const_get('ALL')
exprs[:on_const] = [[self::YELLOW, self::BOLD, self::UNDERLINE], t_all]
exprs[:on_int] = [[self::YELLOW, self::BOLD], t_all]
end
@naohaq
naohaq / Makefile
Created May 20, 2019 04:08
Cache performance demonstration
CFLAGS = -O3
.PHONY: all clean
all: horizontal vertical
horizontal: cache_test.c
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
@naohaq
naohaq / hoge.rb
Created May 8, 2019 12:15
Sophisticated semantics of Ruby
class Hoge
def a=(x)
puts "Pooh!"
end
private "a=".intern
def a
puts "Bra!"
end
private :a
@naohaq
naohaq / loop_ichg.rb
Created April 25, 2019 12:16
Loop interchange rough analysis
def show_iv(iv)
"(%s)"%(iv.collect{|x|x.to_s}.join(","))
end
def sub_iv(iv0, iv1)
iv0.zip(iv1).collect{|x0,x1| x0 - x1}
end
def add_dependence(iv, ds, us, du_lst)
us.each do |u|
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
module Vec3DList where
import GHC.Generics (Generic)
import Foreign.Ptr (Ptr, nullPtr)
@naohaq
naohaq / dblval.c
Created June 6, 2018 02:37
Output bit string of double value.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef union U_DBL_INT {
double d;
uint64_t i;
} dbl_int_t;
@naohaq
naohaq / Combination.hs
Last active May 28, 2018 05:50
Generate the list of all combinations.
comb_0_1 :: (Num a) => Int -> [[a]]
comb_0_1 n | n >= 0 = f [[]] n
| otherwise = []
where f xs 0 = xs
f xs n = f [(b:x) | b<-[0,1], x<-xs] (n-1)
@naohaq
naohaq / Cyclic.hs
Created February 6, 2018 08:13
Cyclic definition
module Main where
a :: Integer
a = a + 1
main :: IO ()
main = putStrLn (show a)