Last active
August 3, 2020 21:01
-
-
Save zyth0s/d51485c97b66fa67d474c101dd2760cd to your computer and use it in GitHub Desktop.
Julia tricks
This file contains 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
# # One-liners | |
low, high = minmax(4,3) | |
low, high = extrema([1,3,5,7]) | |
# double factorial | |
dfactorial(n::Int) = prod(n:-2:1) | |
# Non equivalent pairs | |
_pairs(n::Int) = ((i,j) for i in 1:n for j in 1:(i-1)) # upper triangle without diagonal | |
_pairs_wself(n::Int) = ((i,j) for i in 1:n for j in 1:i) # upper triangle with diagonal | |
_pairs_all(n::Int) = ((i,j) for i in 1:n for j in 1:n) # all indices of matrix | |
triangleneq(i::Int) = div(i*(i+1),2) | |
#triangleneq(i::Int,j::Int) = i < j ? triangleneq(j-1)+i : triangle(i-1)+j | |
triangleneq(i::Int, j::Int) = ifelse(i < j, triangleneq(j-1)+i, triangleneq(i-1)+j ) | |
# Fibonacci | |
# | 1 0 |^n = | Fn+1 Fn | | |
# | 1 0 | | Fn Fn-1 | | |
F0 = [ 1 0; 1 0] | |
fib(n) = (F0^n)[1,2] # or [2,1] | |
n = 3 | |
sin.(rand(8)) | |
true || error("Something wrong") | |
true && @info "Some info" | |
#--------------------------------------- | |
# # Looping | |
v = rand(8) | |
sum(v[1:end .!= 5]) # ∑i≠5 vᵢ | |
prod(v[1:end .!= 5]) # ∏i≠5 vᵢ | |
# cumsum, diff, ... | |
foreach( x -> x^2-3, [1,2,3,5]) | |
collect(1:8) .+ rand(8) .+ range(0,stop=3,length=8) .+ [1,2,3,4,5,6,7,8] | |
# Comprenhensions | |
[i for i in 1:4 if i != 3] # allocates an array | |
(i for i in 1:4 if i != 3) # generator | |
# ∼ Python decorators | |
@time ( | |
for i in 1:4 | |
sleep(0.01) | |
end) | |
@time begin | |
for i in 1:4 | |
sleep(0.01) | |
end end | |
# Nested loops syntactic sugar | |
for i in 1:5, j in 1:5 | |
end | |
for i in 1:5 for j in 1:5 | |
end end | |
# The same as for i,j,k in itertools.product(occupied, repeat=3) in Python | |
occupied = 1:2 | |
for (i,j,k) in Base.product(fill(occupied,3)...) | |
# It is not very clear | |
@show i,j,k | |
end | |
println() | |
function cart_prod(rango,times) | |
# An alias | |
Base.product(fill(rango,times)...) | |
end | |
for (i,j,k) in cart_prod(occupied,3) | |
# Better | |
@show i,j,k | |
end | |
matrix = rand(5,5) | |
for ind in CartesianIndices(matrix) | |
i,j = Tuple(ind) | |
@show i,j | |
end | |
for (i,j) in Tuple.(CartesianIndices(matrix)) | |
@show i,j | |
end | |
index_symat0(i,j) = max(i,j)*(max(i,j)+1)÷2 + min(i,j) # 0 based indexing | |
index_symat1(i,j) = max(i-1,j-1)*(max(i-1,j-1)+1)÷2 + min(i-1,j-1) + 1 # 1 based indexing | |
# Generator | |
function fibonacci(n) | |
Channel() do ch | |
a, b = 1, 1 | |
for i in 1:n | |
put!(ch, a) | |
a, b = b, a + b | |
end | |
end | |
end | |
@time for f in fibonacci(10) | |
println(f) | |
end | |
for (i,elem) in enumerate(rand(8)) | |
elem + i | |
end | |
U, V = rand(5), rand(5) | |
for (u,v) in zip(U,V) | |
u + v | |
end | |
# Flatten nested arrays (e.g. DiffEq sol) | |
# https://gist.github.com/Oblynx/3ccacabf5f873c51eecb49fb6cc136bf | |
a = [[[i * j * k for i in 1:4] for j in 2:5] for k in 1:3] | |
flatten(x::Array{<:Array,1}) = Iterators.flatten(x) |> collect |> flatten | |
flatten(x::Array{<:Number,1}) = x | |
aref = [i * j * k for i in 1:4, j in 2:5, k in 1:3] | |
@assert reshape(flatten(a), (4,4,3)) == aref | |
#--------------------------------------- | |
# # Algebra | |
import LinearAlgebra | |
eye(n) = LinearAlgebra.I(n) | |
# Sometimes these two are denoted with the same symbol but they are not the same | |
⊗(x,y) = kron(x,y) # Kronecker tensor product (vectors & matrices) | |
outer(x::Vector,y::Vector) = x * y' # outer product | |
# Test equivalence | |
u = rand(3); v = rand(3) | |
kron(u,v) == vec(outer(v,u)') | |
kron(u,v') == outer(u,v) | |
# Hadamard product (def. over matrices) | |
# in Julia is broadcast multiplication .* | |
hadamard(x::Matrix,y::Matrix) = x .* y | |
function direct_sum(x::Matrix,y::Matrix) | |
if size(x) == size(y) | |
return hvcat((2,2),x,zero(x),zero(y),y) | |
else | |
nrowx = size(x)[1] | |
ncolx = size(x)[2] | |
ret = zeros(size(x) .+ size(y)) | |
ret[1:nrowx,1:ncolx] = x | |
ret[1+nrowx:end,1+ncolx:end] = y | |
return ret | |
end | |
end | |
A = rand(8,8); v = rand(8) | |
x = A \ v # Ax = v | |
#--------------------------------------- | |
# # Compositon | |
# Piping and anonymous function | |
3.0 |> n -> convert(Int,n) | |
# Function omposition | |
@assert (sin ∘ abs)(-1) === sin(1) #0.8414709848078965 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment