Skip to content

Instantly share code, notes, and snippets.

@BoZenKhaa
Last active February 21, 2020 15:28
Show Gist options
  • Save BoZenKhaa/21e309dd9d56467ea7c710aceeef4a60 to your computer and use it in GitHub Desktop.
Save BoZenKhaa/21e309dd9d56467ea7c710aceeef4a60 to your computer and use it in GitHub Desktop.
Julia notes
#Array syntax
[x,y,z]
vcat(x,y,z) == [x; y; z]
hcat(x,y,z) == [x y z]
#Square matrix
[a b; c d]
# Solve Ax=b
A\b
# For speed, avoid temporary allcoations
A = B[1:3] # Creates a copy
A = view(B,1:3) # Creates a view
B[1:3] = A # No temp alloc here
# Sparse matrix: sparse(r_cords, c_coordds, vals)
using SparseArrays
A = sparse([1;2;3],[2;2;1],[3;4;5])
# Uniform matrix (does not actually initialize matrix)
I
using Test
@test ex # check if this evaluates to True
@test f(args...) key=val ... # test expressions usch as "π ≈ 3.14 atol=0.01"
# slicing arrays creates copies. To get a view, do:
v = view(x, 1:5)
# or for function
@views fview(x) = sum(x[2:end-1])
# mutating fucntion - modifies the firts argument, usually marked by !
copyto!(b,a)
# Symnol SYMB, type Symbol
symbol = :SYMB
# Lambda fun
fun = (x,y) -> 2x+y
# Types
mutable struct Car
make
model
end
# Fast iterating
for i in eachindex(A)
something(i)
end
# Help and inspection tools
?fun # get help
methods(fun) # get implementations
fieldnames(thing)
@which fun() #f fidn out method that was used
typeof(thing)
# Benchmark
using BenchmarkTools
@btime fun()
# Arrays
# Array has an alias, Vector
# Array operations can be chained, mem allocation in multiple operations is optimized away.
# Views
# Null values
# https://docs.julialang.org/en/v1/manual/faq/#faq-nothing-1
nothing
Union{T, Nothing}
isdefined()
isassigned()
# Type methods
isa(val, type::DataType)
typeof(val)
supertype(type::DataType)
subtype(type::DataType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment