Skip to content

Instantly share code, notes, and snippets.

@phipsgabler
Created October 12, 2022 07:28
Show Gist options
  • Save phipsgabler/0c9f6a9d9e2ebec897e48f7be4522f76 to your computer and use it in GitHub Desktop.
Save phipsgabler/0c9f6a9d9e2ebec897e48f7be4522f76 to your computer and use it in GitHub Desktop.
# Background:
# - Release in ≈ 2012
# - Intended for scientific programming, but solving 2-language-problem
# - Replace Matlab/Fortran/C++ with free, dynamic language (inspired by
# Matlab, Python, Ruby, LISP, Perl,...)
# - Has become really good alternative to Matlab & Python
# - GC, dynamic, but strong nominal parametric type system with
# multiple dispatch
# - Extensive standard library (numerics, glue code), loads of packages
# - JIT-compiled
# - Very good documentation (usually)
# - Really cool REPL and package manager
# ] activate --temp
# numbers, strings, etc.
1
2.3
"slkd"
"⨁"
collect("slkd")
[1,2,3]
[1, true, 'c']
# variables
α = 2
𝐱₂ = 3 # \bfx
a, b = (α, 𝐱₂)
# Matlab replacement/arrays
2 * [1,2,3]
[1,2,3] * [1,2,3]
[1,2,3] .* [1,2,3]
[1,2,3]' * [1,2,3]
z = [1 2 3] # \bfz
x = [1, 2, 3]' # \bfx
x * ones(3)
z * ones(3)
M = [1 2; 3 4]
M \ [4, 3]
using LinearAlgebra
eigen(M)
normalize(x)
sin(im .+ rand(2, 2))
T = rand(2, 3, 3)
T[1]
T[1, :, :]
T[:, 1, 1:2]
vec([1 2 3; 4 5 6; 7 8 9])
reshape(1:9, 3, 3)
1:9
(1:9).start
:
size(T)
size(T, 2)
axes(T, 2)
collect(axes(T, 2))
2T
sin(T)
sin.(T)
T / T
T ./ T
I(3)
T + I(3)
reshape(I(3), 1, 3, 3)
T .+ reshape(I(3), 1, 3, 3)
T .> 0.5
T[T .> 0.5]
T[[2, 1], :, :]
T′ = @view T[[2, 1], :, :]
T[1] += 10
T′
# functions & compilation
f(T) = T ./ sin.(T .+ 2)
@code_native f(T)
@code_warntype f(T)
Meta.@lower T ./ sin.(T .+ 2)
g(x) = x + 1
g(1)
g(2.3)
g(big"123123123123123123123123123")
g("1")
g
g(s::String) = "$s + 1"
g("1")
methods(g)
+
methods(+)
# control flow etc.
for i = 1:3
println(i)
end
for c in "abc"
println(c)
end
for i in eachindex(T)
T[i] *= 2
end
function foo(s)
m = match(r"\d{3}", s)
@show m
return m.match
end
foo("123,456")
@macroexpand @show x
# types
struct Point
x::Float64
y::Float64
end
p = Point(2, 3)
Base.:+(p::Point, q::Point) = Point(p.x + q.x, p.y + q.y)
Point(2, 3) + Point(0, 3)
Point(0, 0) == Point(0, 0)
p.x += 1
# other things
ccall(:clock, Int32, ())
@ccall clock()::Int32
`date --iso-8601=minutes`
run(`date --iso-8601=minutes`)
readchomp(`date --iso-8601=minutes`)
# REPL
# ? selectdim
# ; pwd
# x = [1 2
# 3 4]
# @less sleep(1)
# parallelism
Threads.@threads for i = 1:10
sleep(1)
@show(i, Threads.threadid())
end
using Distributed
addprocs(2)
N = 100000000
@time (1/N) * sum(1:N) do i
x, y = rand(2)
x^2 + y^2 < 1
end
@time (1/N) * @distributed (+) for i = 1:N
x, y = rand(2)
x^2 + y^2 < 1
end
π / 4
# Packages
# ] st
# ] update
# ]add Distributions
using Distributions
Distributions.Weibull(0.4)
logpdf(Distributions.Weibull(0.4), 10)
rand(Distributions.Weibull(0.4), 2, 3)
# ?Weibull
# ] add UnicodePlots
using UnicodePlots
lineplot(x -> pdf(Weibull(1), x), 0, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment