Skip to content

Commit 967d26d

Browse files
committed
Finished lecture_04
1 parent 57d0198 commit 967d26d

File tree

5 files changed

+44
-33
lines changed

5 files changed

+44
-33
lines changed

docs/src/lecture_04/DataFrames.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"], C = rand(4))
1616

1717
Since each column is stored in a `DataFrame` as a separate vector, it is possible to combine columns of different element types. Columns can be accessed directly without copying.
1818

19-
```@example dfbasics
19+
```@repl dfbasics
2020
df.A
2121
```
2222

2323
Another way is to use the indexing syntax similar to the one for arrays.
2424

25-
```@example dfbasics
25+
```@repl dfbasics
2626
df[!, :A]
2727
```
2828

@@ -77,7 +77,7 @@ Alternatively, we can use the `insertcols!` function. This function can insert m
7777
insertcols!(df, 3, :B => rand(4), :B => 11:14; makeunique = true)
7878
```
7979

80-
New rows can be added to an existing `DataFrame` by the `push!` function. It is possible to append a new row in the form of a vector or a tuple of the correct length or in the form of a dictionary with the correct keys.
80+
New rows can be added to an existing `DataFrame` by the `push!` function. It is possible to append a new row in the form of a vector or a tuple of the correct length or in the form of a dictionary or `DataFrame` with the correct keys.
8181

8282
```@example dfbasics
8383
push!(df, [10, "F", 0.1, 15, 0.235, :f])
@@ -122,7 +122,7 @@ rename!(df, [:a, :b, :c, :d, :e, :f])
122122
df
123123
```
124124

125-
We can use it to rename only specific columns.
125+
Another option is to rename only some of the columns specified by their names.
126126

127127
```@example dfbasics
128128
rename!(df, :a => :A, :f => :F)
@@ -208,9 +208,9 @@ using StatsPlots
208208
@df iris scatter(
209209
:SepalLength,
210210
:SepalWidth;
211+
group = :Species,
211212
xlabel = "SepalLength",
212213
ylabel = "SepalWidth",
213-
group = :Species,
214214
marker = ([:d :h :star7], 8),
215215
)
216216
```

docs/src/lecture_04/Plots.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ where ``t \in [0, 2\pi]``. Create a plot of the curve described by the equations
134134
2. The line color should change with the changing line width.
135135
Use `:viridis` color scheme or any other [color scheme](http://docs.juliaplots.org/latest/generated/colorschemes/) supported by the Plots package. Use additional plot attributes to get a nice looking graph.
136136

137-
**Hint:** use the `pallete` function combined with the `collect` function to generate a vector of colors from the `:viridis` color scheme.
138-
139-
**Hint:** remove all decorators by using: `axis = nothing`, `border = :none`.
137+
**Hints:**
138+
- use the `pallete` function combined with the `collect` function to generate a vector of colors from the `:viridis` color scheme.
139+
- remove all decorators by using: `axis = nothing`, `border = :none`.
140140

141141
```@raw html
142142
</p></div>
@@ -221,7 +221,7 @@ savefig("plot_exercise1.svg") # hide
221221
using Plots
222222
```
223223

224-
The previous section showed basic functionality of `plot`. We first calculated the values to be plotted and then created the graphs. However, it is possible to pass functions directly to `plot`.
224+
The previous section showed basic functionality of the `plot` function. We first calculated the values to be plotted and then created the graphs. However, it is possible to pass functions directly to the `plot` function.
225225

226226
```@example plots_fce
227227
t = range(0, 2π; length = 100)
@@ -322,8 +322,8 @@ f(x, y) = \frac{x^2 \cdot y^2}{x^4 + y^4}.
322322
```
323323

324324
Draw this function for ``x, y \in [-5, 5]``. Use the following three plot series `contourf`, `heatmap`, and `surface` with the following settings:
325-
- `:viridis` color scheme;
326-
- camera angle `(25, 65)`;
325+
- `:viridis` color scheme,
326+
- camera angle `(25, 65)`,
327327
- no legend, color bar, or decorators (`axis`, `frame` and `ticks`).
328328

329329
```@raw html
@@ -353,7 +353,7 @@ kwargs = (
353353
nothing # hide
354354
```
355355

356-
We use the `plot` function with the `seriestype = :contourf` keyword to draw a filled contour plot. The simpler option is to use the `contourf` function.
356+
We can use the `plot` function with the `seriestype = :contourf` keyword to draw a filled contour plot. The simpler option is to use the `contourf` function.
357357

358358
```julia
359359
contourf(x, x, fz; kwargs...) # or plot(x, x, fz; seriestype = :contourf, kwargs...)
@@ -369,7 +369,7 @@ heatmap(x, x, fz; kwargs...)
369369

370370
![](plots_srs_ex2.svg)
371371

372-
For the `surface` plot, we can change the camera angle.
372+
For the `surface` plot, we can change the camera angle by setting the `camera` attribute.
373373

374374
```julia
375375
surface(x, x, fz; camera = (25, 65), kwargs...)
@@ -406,7 +406,7 @@ plot(x, [sin, cos, tan, sinc];
406406
)
407407
```
408408

409-
It is possible to create more advanced layouts with the `@layout` macro. In the example below, we create a non-symmetric layout with one subplot in the first row and two subplots in the second row. Moreover, we set the width of the first subplot in the second row to be `0.3` the whole plot width.
409+
It is possible to create more advanced layouts with the `@layout` macro. In the example below, we create a non-symmetric layout with one subplot in the first row and two subplots in the second row. Moreover, we set the width of the first subplot in the second row to be `0.3` of the whole plot width.
410410

411411
```@example plots_srs
412412
l = @layout [a ; b{0.3w} c]

docs/src/lecture_04/interaction.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
# Interaction with other languages
22

3-
One of the biggest advantages of Julia is its speed. As we discussed in the section [Why julia?](@ref Why-julia?), Julia is fast out-of-box without the necessity to do any additional steps. As a result, Julia solves the so-called Two-Language problem: *users are programming in a high-level language such as R and Python, but the performance-critical parts of the code have to be rewritten in C/Fortran for performance.* Since Julia is fast enough, most of the libraries are written in pure Julia and there is no need to use C/Fortran for performance. However, there are many high-quality, mature libraries for numerical computing already written in C and Fortran. It would be a shame if it will not be possible to use them in Julia.
3+
One of the most significant advantages of Julia is its speed. As we discussed in the section [Why julia?](@ref Why-julia?), Julia is fast out-of-box without the necessity to do any additional steps. As a result, Julia solves the so-called Two-Language problem:
44

5-
To allow easy use of this existing code, Julia makes it simple and efficient to call C and Fortran functions. Julia has a **no boilerplate** philosophy: functions can be called directly from Julia without any glue code generation, or compilation – even from the interactive prompt. This is accomplished just by making an appropriate call with `ccall` syntax, which looks like an ordinary function call. Moreover, it is possible to pass Julia functions to native C functions that accept function pointer arguments. In this section, we will show one example of the interaction between Julia and C. Extensive description of all provided functionality can be found in the [official manual](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/).
5+
> Users are programming in a high-level language such as R and Python, but the performance-critical parts of the code have to be rewritten in C/Fortran for performance.
66
7-
The following example is taken from the official manual. Consider the situation, that we want to use the `qsort` function from the standard C library. The `qsort` function sortrs an array and is edclared as follows
7+
Since Julia is fast enough, most of the libraries are written in pure Julia, and there is no need to use C/Fortran for performance. However, there are many high-quality, mature libraries for numerical computing already written in C and Fortran. It would be a shame if it will not be possible to use them in Julia.
8+
9+
To allow easy use of this existing code, Julia makes it simple and efficient to call C and Fortran functions. Julia has a **no boilerplate** philosophy: functions can be called directly from Julia without any glue code generation or compilation – even from the interactive prompt. This is accomplished just by making an appropriate call with the `ccall` syntax, which looks like an ordinary function call. Moreover, it is possible to pass Julia functions to native C functions that accept function pointer arguments. This section will show one example of the interaction between Julia and C. Extensive description of all provided functionality can be found in the [official manual](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/).
10+
11+
The following example is taken from the official manual. Consider the situation that we want to use the `qsort` function from the standard C library. The `qsort` function sorts an array and is declared as follows.
812

913
```c
1014
void qsort(void *base, size_t nitems, size_t size,
1115
int (*compare)(const void*, const void*))
1216
```
1317
14-
The `base` is the pointer to the first element of the array to be sorted. The `nitems` is the number of elements in the array pointed by `base` and the `size` is the size in bytes of each element in the array. Finally, the `compare` is the function that compares two elements. The `compare` function should return a negative integer if the first argument is less than the second, a positive integer if the first argument is greater than the second, and otherwise zero. Such a function can be in Julia defined as follows.
18+
The `base` is the pointer to the first element of the array to be sorted. The `nitems` is the number of elements in the array pointed by `base`. The `size` is the size in bytes of each element in the array. Finally, the `compare` is the function that compares two elements. The `compare` function should return a negative integer if the first argument is less than the second, a positive integer if the first argument is greater than the second, and otherwise zero. Such a Julia function can be defined as follows.
1519
1620
```julia
1721
mycompare(a, b)::Cint = sign(a - b)
1822
```
1923

20-
Since the `qsort` function expects, that the return type of the `compare` function is C `int`, we annotate the return type to be `Cint`. In order to pass this function to C, we obtain its address using the macro `@cfunction`.
24+
Since the `qsort` function expects that the return type of the `compare` function is C `int`, we annotate the return type to be `Cint`. In order to pass this function to C, we obtain its address using the macro `@cfunction`.
2125

2226
```julia
2327
mycompare_c = @cfunction(mycompare, Cint, (Ref{Cdouble}, Ref{Cdouble}))
2428
```
2529

26-
The `@cfunction` macro requires three arguments: the Julia function, the return type, and tuple of the input argument types. Finally we can use the `ccall` function to call the `qsort` function.
30+
The `@cfunction` macro requires three arguments: the Julia function, the return type, and the tuple of the input argument types. Finally, we can use the `ccall` function to call the `qsort` function.
2731

2832
```julia
2933
julia> A = [1.3, -2.7, 4.4, 3.1];
@@ -39,7 +43,7 @@ julia> A
3943
4.4
4044
```
4145

42-
Besides C and Fortran that can be called directly using `ccall` function, it is possible to interact with other languages. There are many packages that provide an interface to interact with other languages. The following table shows an overview of those packagess
46+
Besides C and Fortran that can be called directly using `ccall` function, it is possible to interact with other languages using third-party packages. The following table shows an overview of those packages.
4347

4448
| Language | Calling from Julia | Calling Julia |
4549
| :--- | :--- | :--- |
@@ -50,11 +54,11 @@ Besides C and Fortran that can be called directly using `ccall` function, it is
5054
| MATLAB | [MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl) | [Mex.jl](https://github.com/jebej/Mex.jl/) |
5155
| Java | [JavaCall.jl](https://github.com/JuliaInterop/JavaCall.jl) | [JuliaCaller](https://github.com/jbytecode/juliacaller) |
5256

53-
Moreover, there are other Julia packages that provide Julia interface for some well-known libraries from other languages. As an example, we can mention ScikitLear.jl which provides an interface for the [scikit-learn](https://scikit-learn.org/stable/) library from Python or the [RDatasets.jl](https://github.com/JuliaStats/RDatasets.jls) that provides an easy way to load famous R datasets.
57+
Moreover, other Julia packages provide Julia interface for some well-known libraries from other languages. As an example, we can mention [ScikitLear.jl](https://github.com/cstjean/ScikitLearn.jl), which provides an interface for the [scikit-learn](https://scikit-learn.org/stable/) library from Python or the [RDatasets.jl](https://github.com/JuliaStats/RDatasets.jls) that provides an easy way to load famous R datasets.
5458

5559
## RCall.jl
5660

57-
The [RCall.jl](https://github.com/JuliaInterop/RCall.jl) package provides an interface for calling R functions from Julia and passing data between these two languages. The package provides an interactive REPL for the R language that can be accessed from the Julia REPL by typing `$` symbol. As a consequence, it is possible to easily switch between these languages and use functionality provided by both languages simultaneously.
61+
The [RCall.jl](https://github.com/JuliaInterop/RCall.jl) package provides an interface for calling R functions from Julia and passing data between these two languages. The package provides an interactive REPL for the R language that can be accessed from the Julia REPL by typing the `$` symbol. Consequently, it is possible to easily switch between these languages and use functionality provided by both languages simultaneously.
5862

5963
```julia
6064
julia> using RCall, RDatasets
@@ -68,7 +72,7 @@ R> ggplot($mtcars, aes(x = WT, y = MPG)) + geom_point()
6872

6973
![](ggplot.svg)
7074

71-
The package also provides string syntax, that allows non-interactive usage. The previous example can be rewritten as follows.
75+
The package also provides string syntax that allows non-interactive usage. The previous example can be rewritten as follows.
7276

7377
```julia
7478
using RCall, RDatasets
@@ -80,11 +84,11 @@ ggplot($mtcars, aes(x = WT, y = MPG)) + geom_point()
8084
"""
8185
```
8286

83-
Note that we use multiline string syntax, but it is also possible to use standard string syntax as well. This multiline string syntax is very useful especially when we want to perform multiple operations in R at once and then just return the result to Julia.
87+
Note that we use multiline string syntax, but it is also possible to use standard string syntax. This multiline string syntax is very useful, especially when we want to perform multiple operations in R at once and then just return the result to Julia.
8488

8589
## MATLAB.jl
8690

87-
The [MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl) provides an easy interface for calling Matlab functions and passing data between Julia and Matlab. Consider the situation, that you wrote a Matlab function, that uses some special functionality that is not available in Julia. Using the MATLAB.jl package, it is very easy to call this function directly from Julia as can be seen in the following example
91+
The [MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl) provides an easy interface for calling Matlab functions and passing data between Julia and Matlab. Consider the situation that you wrote a Matlab function that uses some special functionality that is not available in Julia. MATLAB.jl package provides an interface to call this function directly from Julia, as can be seen in the following example.
8892

8993
```julia
9094
using MATLAB, BSON
@@ -97,7 +101,7 @@ The `mxcall` function accepts the name of the function as the first argument and
97101

98102
![](../data/Video.gif)
99103

100-
The package also provides string syntax, that allows writing the code in Matlab syntax. The previous example can be rewritten as follows.
104+
Like the RCall.jl package, the MATLAB.jl package also provides string syntax that allows for Matlab syntax. The previous example can be rewritten as follows.
101105

102106
```julia
103107
using MATLAB, BSON
@@ -107,5 +111,3 @@ mat"""
107111
MakeVideo($(X), 30, "Video2.gif");
108112
"""
109113
```
110-
111-
Note that we use multiline string syntax as in the case of R.

docs/src/lecture_04/otherpackages.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,22 @@ In combination with the `StatsPlots` package, it is possible to plot probability
3131

3232
```@example distr
3333
using StatsPlots
34-
plot(D; linewidth = 2, xlabel = "x", ylabel = "pdf(x)", legend = false)
34+
plot(
35+
plot(D; title = "pdf"),
36+
plot(D; func = cdf, title = "cdf");
37+
legend = false,
38+
xlabel = "x",
39+
ylabel = "f(x)",
40+
ylims = (0,1),
41+
linewidth = 2,
42+
layout = (1,2),
43+
size = (800, 400)
44+
)
3545
```
3646

3747
The `Distributions` package also provides methods to fit a distribution to a given set of samples.
3848

39-
```@example distr
49+
```@repl distr
4050
x = rand(Normal(2, 0.5), 10000); # generate 10000 random numbers from Normal(2, 0.5)
4151
D = fit(Normal, x)
4252
```
@@ -113,7 +123,6 @@ The previous expression returns a vector of functions. Now we can use the `plot`
113123

114124
```@example distr
115125
plot(cdfs, 0, 20;
116-
func = cdf,
117126
xaxis = ("x", (0, 20)),
118127
yaxis = ("cdf(x)", (0, 1.05)),
119128
labels = labels,

docs/src/lecture_04/standardlibrary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Note that for `D+I` and `D-I`, the matrix `D` must be square.
6464

6565
## Random
6666

67-
The last package that we will describe in more detail is the [Random](https://docs.julialang.org/en/v1/stdlib/Random/) package. This package provides more functionality for generating random numbers in Julia. The package allows setting the seed for the random generator using the `seed!` function. The `seed!` function is used to create a reproducible code that contains randomly generated values.
67+
The last package that we will describe in more detail is the [Random](https://docs.julialang.org/en/v1/stdlib/Random/) package. This package provides advanced functionality for generating random numbers in Julia. The package allows setting the seed for the random generator using the `seed!` function. The `seed!` function is used to create a reproducible code that contains randomly generated values.
6868

6969
```@repl rand
7070
using Random

0 commit comments

Comments
 (0)