Skip to content

Instantly share code, notes, and snippets.

@cossio
cossio / myPkgs.jl
Created January 3, 2018 14:03
Install my Julia packages.
Pkg.clone("https://github.com/cossio/Utils.jl")
Pkg.clone("https://github.com/cossio/COBRAUtils.jl")
Pkg.clone("https://github.com/cossio/TruncatedNormal.jl")
@cossio
cossio / git-alldirs.sh
Created January 8, 2018 15:56
I keep this script around which lets me apply a git command to every subdirectory:
#!/usr/bin/env zsh
submodules=("${(@f)$(find . -type d -depth 1)}")
for submodule in $submodules
do
print "=== $submodule"
git --work-tree=$submodule --git-dir=$submodule/.git $*
print
done
@cossio
cossio / edit-vim.jl
Created January 9, 2018 21:41
Use VIM as default editor in Julia. Raw
ENV["JULIA_EDITOR"] = "vim -R"
@cossio
cossio / pdftoimg.sh
Created April 4, 2018 14:20
Scanned PDF to image
pdfimages -j input.pdf output
@cossio
cossio / ZeroTensor.jl
Created July 11, 2018 11:38
A tensor of zeros (Julia)
"A tensor of zeros"
struct ZeroTensor{T <: Number, N} <: AbstractArray{T,N}
dims::NTuple{N,Int}
function ZeroTensor{T,N}(dims::Vararg{Integer,N}) where {T <: Number, N}
all(dims .≥ 0) || error("invalid ZeroTensor dimensions")
new(dims)
end
end
ZeroTensor(T::DataType, dims::Vararg{Integer,N}) where N = ZeroTensor{T,N}(dims...)
ZeroTensor(dims::Vararg{Integer,N}) where {N} = ZeroTensor{Float64,N}(dims...)
@cossio
cossio / fibonacci_fast_doubling.jl
Created July 23, 2018 14:14
Fast doubling algorithm to compute Fibonacci numbers. This is faster than matrix exponentiation.
function fib(n::Integer)
@assert n ≥ 0
if iszero(n)
return zero(n)
elseif n == 1 || n == 2
return one(n)
end
k = div(n,2)
if iseven(n)
return fib(k) * (2fib(k+1) - fib(k))
function dnacompl(nt::Char)
@assert nt ∈ ('A', 'T', 'C', 'G', 'N', '*')
if nt == 'A'
return 'T'
elseif nt == 'T'
return 'A'
elseif nt == 'G'
return 'C'
elseif nt == 'C'
return 'G'
for i = 1:L, a = 1:A
@assert sub2ind((A,L),a,i) == (i-1)*A + a
end
for j=1:L, i=1:L, b=1:A, a=1:A
@assert sub2ind((A,A,L,L),a,b,i,j) == a + (b-1)*A + (i-1)*A*A + (j-1)*A*A*L
end
srand(2);
L = rand(2:10); A = rand(2:10,L);
@cossio
cossio / find_max_key.jl
Last active August 14, 2018 15:37
Find maximum key=>value pair in a Dict
function findmaxkey(d::Dict)
maxk,maxv = first(d)
for k in keys(d)
if d[k] > maxv
maxk, maxv = k, d[k]
end
end
return maxk=>maxv
end
@cossio
cossio / Distributions_error.jl
Created October 31, 2018 15:46
Distributions.jl numerical error on Multinomial sampling
using Distributions
n = 4969027388018185
p = [4.471459552709845e-5,
0.0021070048390116223,
0.0017485819413324416,
0.0022622153482103544,
0.0019479678673445784,
0.0015864519243622935,
0.00037279832670023585,