From 1b716e337a763870ec771d29a74b2ff74095bbd6 Mon Sep 17 00:00:00 2001 From: br86redu <br86redu@idiv.de> Date: Thu, 7 Feb 2019 11:16:38 +0100 Subject: [PATCH] add cheatsheet --- FirstSteps.jl | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 FirstSteps.jl diff --git a/FirstSteps.jl b/FirstSteps.jl new file mode 100644 index 0000000..671f95f --- /dev/null +++ b/FirstSteps.jl @@ -0,0 +1,155 @@ +""" +Setting a working directory with abc +""" +cd("/home/Wherever/You/Want") # Linux +cd("C:\\Users\\Nickname\\Just\\put\\it\\there") # Windows +""" +Show the working directory +""" +pwd() + +""" +Save your files with a .jl to make atom recognize that its Julia +and make the colour formatting appear +""" + +""" +Documentation +Any string appearing at the top-level right before an object +is interpreted as documentation (Markdown) +""" +# This is a comment :) + +""" +type ? in the REPL to switch to the help menu +type ; in the REPL to switch to the shell +type ] in the REPL to switch to the package management + ]st # status of the packages + ]update + ]add "Plots" + +or you can do it like this +""" +import Pkg +Pkg.add("StatsBase") # add the package +using StatsBase # load the package + +Pkg.add("Distributions") # add the package +using Distributions # load the package + +""" +The define operator is = +To use the unicode math symbols (e.g. Greek) type \alpha and press Tab +You cannot redefine a built-in constant or function +""" +pi +a,b,c = 1,2,3 +x = 3 +z = x + a +z1 = 2x^2 - 3x + 1 +z2 = 2^2x +x += 1 +d = rand() # choose a random number between 0 and 1 +# randn(10) 10 random numbers- normal distributed + +### "===" compare the number and the type of a variable + +""" +Define an array +""" +a = [x,y,z] #Array +b = rand(1:10,6) +b = rand(2,2) #2×2 Array{Float64,2} +a = Array{Float64,1} # Array{T,N} N-dimensional dense array with elements of type T. +# N can be Any, Float64, Int64, String +# T can be 1 - Vector, 2 - Matrix, 3 - Array +c = Vector{Float64}(undef,6) # create a vector with random numbers (undefined, row, column) +c = fill!(c,3) +d = fill(rand(1:10), (5,5)) + +g = [1:10;] #create a vector +g[9] = -9 # change number at pos. 9 +g[3:5] = fill!(g[3:5], -8) + +print(g) +length(g) +size(b) +println("text"^2) + +""" +Arithmetic Operators +(+) (-) (/) (*) (^) (%) (\) +For every binary operation there is a "dot"-operation that + automatically performs element-by-element on arrays +""" +x = 3 +-x #mathematical inverse ++x +y % x # remainder +x += 3 # is like x = x + 3 ++(1,2,3) +*(1,2,3,4) +a = [1,2,3] +b = a .+ 1 +c = a .^2 + +""" +build-in mathematical functions +""" +Random.seed!(123) # Setting the seed; using Random, Distributions +d = Normal() #Normal distribution std=1 +x = rand(d,2) + +g = [1,2,3,4,5,6,7,8,9] +sum(g) +mean(g) # in the Pkg StatsBase +maximum(g) +extrema(g) + +""" +- Numeric comparison +- (==) (!=) (<=) ... +""" +x = 3 +x < 5 ? "yes" : "no" +x = 7 +x < 5 ? "yes" : "no" + +# Boolean operators +x > 5 && "Cool!" +x < 5 && "Cool!" +x > 5 || "Cool!" +x < 5 || "Cool!" + +# Loop and if-else statments +for i in 1:length(g) + println("Hallo World ($i)"^2) +end + +name = "Brinkley" +if name == "Jeeves" + println("Very Good Jeeves") +elseif name == "Brinkley" + println("Thank you, Brinkley") + println("and shut the door behind you") +else + println("Fine, just ignore me") +end + +""" +functions in julia +""" +# one-line function +myfunc(value) = 20*value +myfunc(20) +# more complicated functions +function First(x, y) + x + y +end +First(3,4) + +function Sec(x, y) + return x*y + x + y +end +Sec(3,4) -- GitLab