Skip to content

Instantly share code, notes, and snippets.

@jmdeldin
jmdeldin / bench.rb
Created September 8, 2012 04:10
Split vs. count
require 'benchmark'
N = 10_000_000
S = 'string,' * N
Benchmark.bmbm do |x|
x.report('#count') do
(length = S.count(',')) && (length + 1).times{|i| "line #{i} of #{length}" }
end
@jmdeldin
jmdeldin / Matrix.java
Created September 25, 2012 04:32
Matrix rotation
// -*- compile-command: "javac -Xlint Matrix.java && java Matrix" -*-
import java.lang.Math;
/**
* Square matrix class.
*
* @author Jon-Michael Deldin <[email protected]>
*/
public class Matrix {
private int N;
@jmdeldin
jmdeldin / sampling.rb
Created September 25, 2012 06:05
Likelihood weighting and sampling
# sampling.rb -- approximate the probability distribution of Pr[A,B|-C,+F]
#
# Author: Jon-Michael Deldin
def label_table(label, n, fields)
puts "#{label} (n = #{n}):"
hdr = "%-15s %-10s" % fields
puts hdr
puts "-" * hdr.length
end
def add_vectors(x, y); x.zip(y).map { |xj, yj| xj + yj }; end
def mult_scalar_to_vector(x, vec); vec.map { |v| v * x }; end
def dot_product(vec1, vec2)
vec1.zip(vec2).map { |v1, v2| v1 * v2 }.reduce(:+)
end
# Hypothesis function
#
@jmdeldin
jmdeldin / reader.cc
Created October 21, 2012 07:49
Line Endings in C++
/**
* reader.cc -- demonstrate the inanity of line endings
*
* compile with
* g++ -Wall -ansi -pedantic reader.cc -o reader
* run it with
* ./reader some_file.txt
*
* You can create Windows line endings in Vim with :se ff=dos and Unix files
* with :se ff=unix (or run dos2unix and unix2dox on a text file).
#!/usr/bin/env ruby
#
# Clears all IPC queues.
#
# Author: Jon-Michael Deldin <[email protected]>
#
# different position of the queue id on Linux
`ipcs -q`.scan(/^q (\d+)/) do |m|
puts "removing #{m.first}"
@jmdeldin
jmdeldin / bench.rb
Created October 30, 2012 23:58
Benchmark is_num methods
# from http://stackoverflow.com/questions/13149146/ruby-rails-determine-if-variable-is-a-number
require 'benchmark'
def is_num_e(num_given)
!!Integer(num_given) rescue false
end
def is_num_r(num_given)
num_given =~ /\d+(\.\d+)?/
end
@jmdeldin
jmdeldin / misc-file.el
Created February 13, 2013 05:16
Some Elisp for getting the last modified date of files and comparing two files. The original idea was to use this for comparing whether a tangled .el file from org-babel was as new as the source .org file, but it turns out org-babel-load-file already does this!
(defun get-last-modifed (file)
"Returns the last modified date of a FILE."
(interactive)
(format-time-string "%Y-%m-%d %T"
(nth 5 (file-attributes file))))
(defun most-recent-p (file1 file2)
"Returns t if FILE1 was modified more recently than FILE2."
(string< (get-last-modifed file1) (get-last-modifed file2)))
@jmdeldin
jmdeldin / disable_ar_helper.rb
Created February 21, 2013 18:40
A few methods to disable and restore ActiveRecord in a spec helper
def disable_ar
class << ActiveRecord::Base
def fail_connection; fail 'Tried to access the DB'; end
alias_method :old_connection, :connection
alias_method :connection, :fail_connection
end
end
def restore_ar
class << ActiveRecord::Base
## Jon-Michael Deldin
## Pattern Recognition, SP 2013
## LDA Project
##
## Usage:
## Rscript lda.R
##
## clear existing vars
rm(list = ls(all=TRUE))