Last active
November 1, 2018 14:07
-
-
Save nazywamsiepawel/0a92f4053b31d1f58e719db5c19644a3 to your computer and use it in GitHub Desktop.
Notes and snippets on Julia lang
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
#Make a comment just like in Python | |
#to run > julia testing.jl | |
#Simple arrays | |
println("Hello World") | |
a = [1, 2, 3, 4, 5] | |
println(a) | |
b = [1 2 3; 4 5 6; 7 8 9] | |
println("Array b:", b) | |
c = ["array", "of", "string"] | |
println(c) | |
println(typeof(c)) | |
#Array of functions | |
trigfuncs = [sin, cos, tan] | |
print(trigfuncs) | |
println(typeof(trigfuncs)) | |
#Uninitialized arrays | |
d = Array{Int64}(undef, 2, 2, 2) | |
println(d) | |
#Arrays with mixed types | |
mixedtypes = [1, "2", 3.0, sin, pi] | |
typeof(mixedtypes) | |
println(mixedtypes) | |
#multidim arrays and matrices | |
randomArray = rand(2, 2, 2, 2) | |
println(randomArray) | |
println("One\t", randomArray[1]) | |
println("Two\t", randomArray[1][1][1]) | |
for n in 1:10 println("Just casually looping through a range\t", n) end | |
println("==== collect ====") | |
x = collect(1:10) | |
println(x) | |
#Collect with start:step:stop | |
println(collect(0:10:100)) | |
#Find out the actual step size | |
println("Finding actual step size: ", step(range(1, length=10, stop=100))) | |
#Collecting objects from range | |
println(collect(0:5:100)) | |
for i in collect(1:6) | |
println(i) | |
end | |
#Generator expressions | |
println([n^2 for n in 1:5]) | |
println([r * c for r in 1:5, c in 1:5]) | |
println(collect(x^2 for x in 1:10 if x != 1)) | |
#Creating and filling an array | |
println(zeros(2, 3)) | |
println(ones(2, 3)) | |
println(rand(2, 3)) | |
println(trues(3, 4)) | |
println(fill("DAWG", 2, 2)) | |
#Exclamation mark says that you are about to modify an array with already existing data | |
println(fill!(a, 42)) | |
println(repeat([1, 2, 3], 2)) | |
# Creating similar arrays - getting the same shape | |
b = similar(a) | |
a = rand(0:10,10, 10) | |
#reshaping arrays | |
id = reshape([1, 2, 3, 4], 2, 2) | |
println(id) | |
#Max numbers | |
println(max(1, 2, 3)) | |
#Julia Type System | |
println("Supertype of Number is " , supertype(Number)) | |
#println("Subtypes of Number are ", subtypes(Number)) - y u no work? | |
println(sizeof(BigFloat)) | |
# | |
# function showtypetree(T, level=0) | |
# println("\t" ^ level, T) | |
# for t in subtypes(T) | |
# if t != Any | |
# showtypetree(t, level+1) | |
# end | |
# end | |
# end | |
# | |
# showtypetree(Number) | |
isodd(1000003) && @warn("That's odd!") | |
name = "wat" | |
if name == "Julia" | |
println("I like Julia") | |
elseif name == "Python" | |
println("I like Python.") | |
println("But I prefer Julia.") | |
else | |
println("I don't know what I like") | |
end | |
for i in 0:10:100 | |
println(i) | |
end | |
for color in ["red", "green", "blue"] # an array | |
print(color, " ") | |
end | |
for i in 1:10 | |
@show i | |
end | |
# To remember the value of a variable outside of the for loop scope it needs to be marked as global | |
for i in 1:10 | |
global howfar | |
if i % 4 == 0 | |
howfar = i | |
end | |
end | |
#Comprehensions | |
#"Let S be the set of all elements n where n is greater than or equal to 1 and less than or equal to 10" | |
S = Set([n for n in 1:10]) | |
println(S) | |
#When in need of working with two or god knows how many arrays there is an option to zip'em | |
#Over and under supply is handled. | |
for i in zip(0:10, 100:110, 200:210) | |
println(i) | |
end | |
#In order to iterate through an array with an index, | |
A = zeros(10, 1) | |
for i = 1:length(A) | |
@show i | |
end | |
#While loops | |
while true | |
println(x) | |
x += 1 | |
x >= 4 && break | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment