Bogumił Kamiński, September 5, 2018
- Package management in projects
- Handling missing data
- Broadcasting
- Heterogeneous collections
- Start Julia 1.0 REPL
- Type
; mkdir TmpDir - Type
cd("TmpDir") - Type
pwd() - Type
](prompt should change to blue(v1.0) pkg>) - Type
helpto see the list of available package management commands - Type
generate ExampleProject - Inspect contents of
ExampleProjectdirectory - Type
activate ExampleProject(prompt should change to blue(ExampleProject) pkg>)) - Type
status - Type
add DataFrames(nowDataFramespackage will be available ) - Inspect contents of
ExampleProjectdirectory again - Type
statusagain - Type
add DataFrames#masterandstatus - Type
free DataFramesandstatus - Type
pin DataFramesandstatus(now you ware ready for production) - Type
activate(leave project environment) andstatus - Try running
using StaticArrays(or any other package you do not have installed) - Now
activateyour project andadd StaticArrays, try loading it; it should work - Exit Julia and run it again,
using StaticArrayswill fail again (unless you activate your project back)
What grade did you get yesterday during the class?
missing isa Missing: I got a grade, but I will not tell you what.nothing isa Nothing: I did not get any grade yesterday.#undef: the array holding the grades points at mutable objects and was not initialized.undef isa UndefInitializer: it is a concrete value used to create uninitialized arrays.
Useful functions for working with missing: ismissing, coalesce, skipmissing.
Try running this Julia code:
function f()
a, b, c, d = (rand(10^8) for i in 1:4)
extrema(@. (a^2+b^2) * (c^2+d^2) - (a*c+b*d)^2 - (a*d-b*c)^2)
endand this R code:
f <- function() {
a <- runif(10^8)
b <- runif(10^8)
c <- runif(10^8)
d <- runif(10^8)
range((a^2+b^2) * (c^2+d^2) - (a*c+b*d)^2 - (a*d-b*c)^2)
}Have the following definitions in Julia:
f(x) = x + 1
g(x) = f(x) + x
h(x) = g(x) * x
i() = h(1.0)Now run: @code_warntype i() and @code_warntype h(1.0).
Try running this code:
x = ones(2*10^6);
y = repeat(Real[1.0, 1], 10^6);
z = repeat(Union{Int, Float64}[1.0, 1], 10^6);
using BenchmarkTools
@btime sum($x)
@btime sum($y)
@btime sum($z)Now run this code (typical in Agent Based Modeling):
struct A1 end
struct A2 end
const As = Union{A1, A2}
a = As[A1(), A2()]
f(a::A1) = 1
f(a::A2) = 2
function g(a)
s = 0
for v in a
s += f(v)
end
return s
end
@code_warntype g(a)