This file contains hidden or 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
def popcount(v): | |
lu = (0, 1, 1, 2, 1, 2, 2, 3) | |
c = 0 | |
while v > 0: | |
c += lu[v & 0x7] | |
v >>= 3 | |
return c | |
def hamming(v1, v2): |
This file contains hidden or 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
require 'json' | |
require 'sinatra' | |
def tf_idf(word, doc, docs) | |
tf = doc.scan(/#{word}/).length / Float(doc.split.size) | |
docs_with_word = docs.map{ |doc| (doc.include? word) ? 1 : 0 }.reduce(0, :+) | |
idf = Math.log(docs.length / Float(1 + docs_with_word)) | |
tf * idf | |
end |
This file contains hidden or 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 xyz_to_srgb (xyz) { | |
var xsm = [ | |
[3.2406255, -1.537208, -0.4986286], | |
[-0.9689307, 1.8757561, 0.0415175], | |
[0.0557101, -0.2040211, 1.0569959] | |
]; | |
var rLinear = xyz.x * xsm[0][0] + xyz.y * xsm[0][1] + xyz.z * xsm[0][2]; | |
var gLinear = xyz.x * xsm[1][0] + xyz.y * xsm[1][1] + xyz.z * xsm[1][2]; | |
var bLinear = xyz.x * xsm[2][0] + xyz.y * xsm[2][1] + xyz.z * xsm[2][2]; |