Skip to content

Instantly share code, notes, and snippets.

@samrat
samrat / Pascal.jl
Created December 22, 2014 15:53
Create a lower triangular n × n matrix filled with the first n rows of Pascal’s triangle.
module Pascal
export pascal
function factorial(n)
if n==0; return(1); end
result = n * factorial(n-1)
return(result)
end
@samrat
samrat / square_lu.c
Last active August 29, 2015 14:11
LU factorization of square matrices. This is an implementation of the algorithm given in Strang's Intro to Linear Algebra textbook
#include <stdio.h>
#include <math.h>
void square_lu(float A[][3], float L[][3], float U[][3])
{
int n = 3;
for (int k = 0; k < n; k++)
{
L[k][k] = 1.0f;
for (int i = k+1; i < n; i++)
@samrat
samrat / ieee.erl
Created September 7, 2014 10:01
Convert a binary representation to IEEE floats
%% K - number of bits to represent `exp`,
%% N - number of bits to represent `frac`.
%% | s | exp(k bits) | frac(n bits) |
make_float_converter(K, N) ->
Bias = math:pow(2, K-1) - 1,
Max_denom = math:pow(2, N),
Max_exp = round(math:pow(2, K) - 1),
fun(<<S:1, Exp:K, Frac:N>>) ->
@samrat
samrat / notPerlin.elm
Created July 5, 2014 16:06
Failed attempt at Perlin- not random enough!
import Generator as Gen
import Generator.Standard as Gs
(w,h) = (100,100)
-- Vectors
type Vec2 = { x : Float
, y : Float
}
@samrat
samrat / randomLeaps.elm
Last active August 29, 2015 14:03
Random walk with "leaps"
import Random
(w,h) = (600, 400)
initialPosition = (0, 0)
step2 seed =
let floats = Random.floatList (always 4 <~ seed)
randCoords [f1,f2,f3,f4] =
let s = if f1 < 0.01 then 50 else 1
n = if f2 < 0.5 then s else -s
@samrat
samrat / randomWalk.elm
Last active August 29, 2015 14:03
Random walk in Elm
import Random
(w,h) = (600, 400)
initialPosition = (0, 0)
intToDir d n =
if d < 0.5
then n
else -n
;; (def related-posts
;; ^{:doc "Returns n posts related to `post`."}
;; (memoize
;; (fn [in-dir post n]
;; (->> (:tags post)
;; (map #(get (tag-buckets (all-pages in-dir)) %))
;; (apply concat)
;; (map #(dissoc % :file :tags))
;; (remove #{post})
;; (frequencies)
@samrat
samrat / gist:8216793
Created January 2, 2014 09:25
max-key-val
(defn max-key-val
[f & args]
(reduce (fn [[max f-of-max] i]
(let [f-of-i (f i)]
(if (> f-of-i f-ofmax)
[i f-of-i]
[max f-of-max])))
[(first args) (f (first args))]
(rest args)))
(defn max-key-val
"Like max-key, but returns [x (f x)] for which (f x) is greatest"
[f & args]
(let [z (zipmap args (map f args))]
(reduce (fn [max i]
(if (> (second i) (second max))
i
max))
(first z)
(rest z))))
@samrat
samrat / gist:5712313
Created June 5, 2013 08:01
Migrate blog posts to YYYY-MM-DD-post-title.md naming.
(defn new-file-name [file]
(let [metadata (metadata file)
slug (or nil (:slug metadata))
parsed-date (or (or (parse (:date metadata)) nil)
(local-now))]
(if slug (-> (clojure.string/replace slug #"\/" "-")
(clojure.string/replace #"-blog-" ""))
(str
(year parsed-date) "-"
(if (= (count (str (month parsed-date))) 2)