mirror of
https://gitlab.rlp.net/mobitar/julia_course.git
synced 2024-11-16 13:28:10 +00:00
Add first sections of day 4
This commit is contained in:
parent
5342da212d
commit
f748185f62
1 changed files with 351 additions and 6 deletions
357
Day_4/Day_4.jl
357
Day_4/Day_4.jl
|
@ -4,15 +4,158 @@
|
||||||
using Markdown
|
using Markdown
|
||||||
using InteractiveUtils
|
using InteractiveUtils
|
||||||
|
|
||||||
# ╔═╡ 81b6211e-b065-11ec-0a86-375721c85b07
|
|
||||||
|
|
||||||
|
|
||||||
# ╔═╡ 1fb7d9af-333e-44f2-b693-09ff97937d4c
|
# ╔═╡ 1fb7d9af-333e-44f2-b693-09ff97937d4c
|
||||||
|
# Oh, no, you found my secret! 😱
|
||||||
|
# Don't change this hidden cell!
|
||||||
|
begin
|
||||||
|
using PlutoUI
|
||||||
|
|
||||||
|
TableOfContents()
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 81b6211e-b065-11ec-0a86-375721c85b07
|
||||||
|
md"""
|
||||||
|
# Jupyter notebooks
|
||||||
|
Although Pluto notebooks are very interactive and revolutionary, there are some cases where they provide you with too much interactivity. This is especially a problem when you are mutating a variable in a different cell. You can always use `begin` blocks, but sometimes, this is not practical.
|
||||||
|
|
||||||
|
Jupyter notebooks don't rerun every cell depending on a variable that was just updated. Cells are only run manually.
|
||||||
|
|
||||||
|
Jupyter supports Julia! Indeed, Jupyter is named after **Ju**lia + **Pyt**hon (+ **e**) + **R**.
|
||||||
|
|
||||||
|
To use Jupyter notebooks with Julia, you just have to install the `IJulia` package globally (not in an environment).
|
||||||
|
|
||||||
|
To install the package globally, open a terminal and launch Julia:
|
||||||
|
|
||||||
|
julia
|
||||||
|
|
||||||
|
In Julia, press `]` to switch to the package manager prompt (see `README.adoc`).
|
||||||
|
|
||||||
|
You should NOT activate an environment!
|
||||||
|
|
||||||
|
Now, run the following:
|
||||||
|
|
||||||
|
add IJulia
|
||||||
|
|
||||||
|
After the installation is done, you can verify that the package is installed by running the following (still in the packagae manager prompt):
|
||||||
|
|
||||||
|
status
|
||||||
|
|
||||||
|
Then you should see `IJulia` listed.
|
||||||
|
|
||||||
|
Now, you can exit Julia. You have now the Julia kernel for Jupyter.
|
||||||
|
|
||||||
|
If you don't already have JupyterLab (recommended) or Jupyter isntalled, install JupyterLab.
|
||||||
|
|
||||||
|
Launch JupyterLab or Jupyter. You should see the Julia kernel listed now! 🎉
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 7f45c502-0909-42df-b93d-384f743df6a9
|
||||||
|
md"""
|
||||||
|
# VS Code/Codium
|
||||||
|
If you are working on a big project, then splitting code up into different files does help maintaining it. Therefore, notebooks should not be used for big projects.
|
||||||
|
|
||||||
|
Because you should avoid using global variables in Julia and instead only call functions, you will not achieve the maximum performance using a notebook because of its workflow that does not support working only with functions. See the section about performance in this notebook.
|
||||||
|
|
||||||
|
Julia has a very good integration in VS Code. VS Codium is the free open source version of VS Code without proprietary code and telemetary. Both will do the job. Just start one of them after installation, go the extensions tab on the sidebar, type in Julia and install the Julia extension.
|
||||||
|
|
||||||
|
Now, you can edit Julia files with autocomplition, real-time feedback, formatting (you might need to set up formatting on save) and other features that you can find about in the documentation of the extension:
|
||||||
|
|
||||||
|
https://www.julia-vscode.org/docs/stable/
|
||||||
|
|
||||||
|
Code written in normal Julia files can be run using one of these three methods:
|
||||||
|
|
||||||
|
1. You can let the VS Code/Codium extension run a line, a code section or an entire file.
|
||||||
|
|
||||||
|
2. You can run it using the following command in the terminal:
|
||||||
|
|
||||||
|
julia --project=. FILENAME.jl
|
||||||
|
|
||||||
|
The `--project` argument tells Julia to use the environment in the current path. See section about environments in this notebook.
|
||||||
|
|
||||||
|
3. You can launch Julia, activate the project environment and run the functions defined in the Julia file after importing using the following in Julia:
|
||||||
|
|
||||||
|
include("FILENAME.jl")
|
||||||
|
|
||||||
|
I you are using the last method, consider using [Revise](https://timholy.github.io/Revise.jl/stable/)! It does reinclude functions of an included file if they where updated in this file. This way, you don't have to reinclude the file after every change or restart your Julia session (remember the startup time because of compilation on running code for the first time).
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ f23ad33d-af1d-40c2-9efc-17ef8c4d1fb8
|
||||||
|
md"""
|
||||||
|
# Environments
|
||||||
|
If you are working on a project and not using Pluto notebooks, you should be using environments.
|
||||||
|
|
||||||
|
Environments separate project dependencies (packages). Therefore, you are less likely to have any conflicts between two packages. They also allow you to use a different version of a package for every project.
|
||||||
|
|
||||||
|
Each Pluto notebook has its own environment. After running
|
||||||
|
|
||||||
|
using PACKAGENAME
|
||||||
|
|
||||||
|
in a Pluto notebook, Pluto takes care of downloading and adding the package to the environment of the notebook. Therefore, if you are not working with Pluto notebooks, you should manually create and use a project environment and you have to add Packages manually to your environment.
|
||||||
|
|
||||||
|
To enter a project environment, you create a project directory and `cd` into it in the terminal. Then, you launch julia
|
||||||
|
|
||||||
|
julia --project=.
|
||||||
|
|
||||||
|
The argument `--project=.` activates a project environment in your current path (project directory). To add packages to your project environment, press `]` to activate the package manager prompt and then run the following:
|
||||||
|
|
||||||
|
add PACKAGENAME
|
||||||
|
|
||||||
|
where you replace `PACKAGENAME` with the name of the package that you want to use.
|
||||||
|
|
||||||
|
In the package manager prompt, run the following to see a list of the packages added to your environment:
|
||||||
|
|
||||||
|
status
|
||||||
|
|
||||||
|
In the package manager prompt, run the following to update (or install if not already installed) all the packages in your environment:
|
||||||
|
|
||||||
|
update
|
||||||
|
|
||||||
|
In the package manager prompt, run the following to remove a package from your environment:
|
||||||
|
|
||||||
|
remove PACKAGENAME
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 6340aec8-6f77-4a30-8815-ce76ddecd6e8
|
||||||
|
md"""
|
||||||
|
# REPL
|
||||||
|
The Julia REPL is what you get when you launch Julia in the terminal.
|
||||||
|
|
||||||
|
The REPL is very useful if you want to quickly experiment with something or test if a function works how you do imagine.
|
||||||
|
|
||||||
|
It is also helpful if you want to look up documentation. To do so, press `?` in the normal prompt. This will cause the prompt to change from the normal prompt `julia>` to the help prompt `help?>`. Now you can enter what you are looking for and you will get its documentation.
|
||||||
|
|
||||||
|
The REPL does support tab autocompletion. Just press tab one time to autocomplete. Press tab two times to see what Julia would suggest to you. This also works in the package manager prompt.
|
||||||
|
|
||||||
|
It REPL also supports history. If you press arrow up or down, you can navigate the execution history. You can also press `CTRL+R` to see the a list of all last commands. You can search in this history and then press enter.
|
||||||
|
|
||||||
|
If you want syntax highlighting in your REPL, add the package `OhMyREPL` to your **global** environment and let this package be imported always on startup as descriped in the documentation:
|
||||||
|
|
||||||
|
https://kristofferc.github.io/OhMyREPL.jl/latest/installation/
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ d21771de-272e-4d57-8c76-c75be709ad0a
|
||||||
|
|
||||||
|
|
||||||
|
# ╔═╡ 09f71a9e-6798-492f-98df-45087d0c4c8b
|
||||||
|
md"""
|
||||||
|
# Performance
|
||||||
|
Julia has a focus on high performance. But if you don't pay attention, you might produce code that either can not be optimized by the compiler or results in a lot of allocations. In both cases, your code will be slow.
|
||||||
|
|
||||||
|
In this section, tools and tips for performance optimization in Julia are presented.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 2a24aebc-0654-4d00-bdab-627a8e1a75f2
|
||||||
|
|
||||||
|
|
||||||
# ╔═╡ 00000000-0000-0000-0000-000000000001
|
# ╔═╡ 00000000-0000-0000-0000-000000000001
|
||||||
PLUTO_PROJECT_TOML_CONTENTS = """
|
PLUTO_PROJECT_TOML_CONTENTS = """
|
||||||
[deps]
|
[deps]
|
||||||
|
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
|
||||||
|
|
||||||
|
[compat]
|
||||||
|
PlutoUI = "~0.7.38"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# ╔═╡ 00000000-0000-0000-0000-000000000002
|
# ╔═╡ 00000000-0000-0000-0000-000000000002
|
||||||
|
@ -22,11 +165,213 @@ PLUTO_MANIFEST_TOML_CONTENTS = """
|
||||||
julia_version = "1.7.2"
|
julia_version = "1.7.2"
|
||||||
manifest_format = "2.0"
|
manifest_format = "2.0"
|
||||||
|
|
||||||
[deps]
|
[[deps.AbstractPlutoDingetjes]]
|
||||||
|
deps = ["Pkg"]
|
||||||
|
git-tree-sha1 = "8eaf9f1b4921132a4cff3f36a1d9ba923b14a481"
|
||||||
|
uuid = "6e696c72-6542-2067-7265-42206c756150"
|
||||||
|
version = "1.1.4"
|
||||||
|
|
||||||
|
[[deps.ArgTools]]
|
||||||
|
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
|
||||||
|
|
||||||
|
[[deps.Artifacts]]
|
||||||
|
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
|
||||||
|
|
||||||
|
[[deps.Base64]]
|
||||||
|
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
|
||||||
|
|
||||||
|
[[deps.ColorTypes]]
|
||||||
|
deps = ["FixedPointNumbers", "Random"]
|
||||||
|
git-tree-sha1 = "024fe24d83e4a5bf5fc80501a314ce0d1aa35597"
|
||||||
|
uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
|
||||||
|
version = "0.11.0"
|
||||||
|
|
||||||
|
[[deps.CompilerSupportLibraries_jll]]
|
||||||
|
deps = ["Artifacts", "Libdl"]
|
||||||
|
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
|
||||||
|
|
||||||
|
[[deps.Dates]]
|
||||||
|
deps = ["Printf"]
|
||||||
|
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
|
||||||
|
|
||||||
|
[[deps.Downloads]]
|
||||||
|
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
|
||||||
|
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
|
||||||
|
|
||||||
|
[[deps.FixedPointNumbers]]
|
||||||
|
deps = ["Statistics"]
|
||||||
|
git-tree-sha1 = "335bfdceacc84c5cdf16aadc768aa5ddfc5383cc"
|
||||||
|
uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
|
||||||
|
version = "0.8.4"
|
||||||
|
|
||||||
|
[[deps.Hyperscript]]
|
||||||
|
deps = ["Test"]
|
||||||
|
git-tree-sha1 = "8d511d5b81240fc8e6802386302675bdf47737b9"
|
||||||
|
uuid = "47d2ed2b-36de-50cf-bf87-49c2cf4b8b91"
|
||||||
|
version = "0.0.4"
|
||||||
|
|
||||||
|
[[deps.HypertextLiteral]]
|
||||||
|
git-tree-sha1 = "2b078b5a615c6c0396c77810d92ee8c6f470d238"
|
||||||
|
uuid = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
|
||||||
|
version = "0.9.3"
|
||||||
|
|
||||||
|
[[deps.IOCapture]]
|
||||||
|
deps = ["Logging", "Random"]
|
||||||
|
git-tree-sha1 = "f7be53659ab06ddc986428d3a9dcc95f6fa6705a"
|
||||||
|
uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
|
||||||
|
version = "0.2.2"
|
||||||
|
|
||||||
|
[[deps.InteractiveUtils]]
|
||||||
|
deps = ["Markdown"]
|
||||||
|
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
|
||||||
|
|
||||||
|
[[deps.JSON]]
|
||||||
|
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
|
||||||
|
git-tree-sha1 = "3c837543ddb02250ef42f4738347454f95079d4e"
|
||||||
|
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
|
||||||
|
version = "0.21.3"
|
||||||
|
|
||||||
|
[[deps.LibCURL]]
|
||||||
|
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
|
||||||
|
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
|
||||||
|
|
||||||
|
[[deps.LibCURL_jll]]
|
||||||
|
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
|
||||||
|
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
|
||||||
|
|
||||||
|
[[deps.LibGit2]]
|
||||||
|
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
|
||||||
|
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
|
||||||
|
|
||||||
|
[[deps.LibSSH2_jll]]
|
||||||
|
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
|
||||||
|
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"
|
||||||
|
|
||||||
|
[[deps.Libdl]]
|
||||||
|
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
|
||||||
|
|
||||||
|
[[deps.LinearAlgebra]]
|
||||||
|
deps = ["Libdl", "libblastrampoline_jll"]
|
||||||
|
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
|
||||||
|
|
||||||
|
[[deps.Logging]]
|
||||||
|
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
|
||||||
|
|
||||||
|
[[deps.Markdown]]
|
||||||
|
deps = ["Base64"]
|
||||||
|
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
|
||||||
|
|
||||||
|
[[deps.MbedTLS_jll]]
|
||||||
|
deps = ["Artifacts", "Libdl"]
|
||||||
|
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
|
||||||
|
|
||||||
|
[[deps.Mmap]]
|
||||||
|
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
|
||||||
|
|
||||||
|
[[deps.MozillaCACerts_jll]]
|
||||||
|
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
|
||||||
|
|
||||||
|
[[deps.NetworkOptions]]
|
||||||
|
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
|
||||||
|
|
||||||
|
[[deps.OpenBLAS_jll]]
|
||||||
|
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
|
||||||
|
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"
|
||||||
|
|
||||||
|
[[deps.Parsers]]
|
||||||
|
deps = ["Dates"]
|
||||||
|
git-tree-sha1 = "85b5da0fa43588c75bb1ff986493443f821c70b7"
|
||||||
|
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
|
||||||
|
version = "2.2.3"
|
||||||
|
|
||||||
|
[[deps.Pkg]]
|
||||||
|
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
|
||||||
|
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
|
||||||
|
|
||||||
|
[[deps.PlutoUI]]
|
||||||
|
deps = ["AbstractPlutoDingetjes", "Base64", "ColorTypes", "Dates", "Hyperscript", "HypertextLiteral", "IOCapture", "InteractiveUtils", "JSON", "Logging", "Markdown", "Random", "Reexport", "UUIDs"]
|
||||||
|
git-tree-sha1 = "670e559e5c8e191ded66fa9ea89c97f10376bb4c"
|
||||||
|
uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
|
||||||
|
version = "0.7.38"
|
||||||
|
|
||||||
|
[[deps.Printf]]
|
||||||
|
deps = ["Unicode"]
|
||||||
|
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
|
||||||
|
|
||||||
|
[[deps.REPL]]
|
||||||
|
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
|
||||||
|
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
|
||||||
|
|
||||||
|
[[deps.Random]]
|
||||||
|
deps = ["SHA", "Serialization"]
|
||||||
|
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
|
||||||
|
|
||||||
|
[[deps.Reexport]]
|
||||||
|
git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b"
|
||||||
|
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
|
||||||
|
version = "1.2.2"
|
||||||
|
|
||||||
|
[[deps.SHA]]
|
||||||
|
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
|
||||||
|
|
||||||
|
[[deps.Serialization]]
|
||||||
|
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
|
||||||
|
|
||||||
|
[[deps.Sockets]]
|
||||||
|
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
|
||||||
|
|
||||||
|
[[deps.SparseArrays]]
|
||||||
|
deps = ["LinearAlgebra", "Random"]
|
||||||
|
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
|
||||||
|
|
||||||
|
[[deps.Statistics]]
|
||||||
|
deps = ["LinearAlgebra", "SparseArrays"]
|
||||||
|
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
|
||||||
|
|
||||||
|
[[deps.TOML]]
|
||||||
|
deps = ["Dates"]
|
||||||
|
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
|
||||||
|
|
||||||
|
[[deps.Tar]]
|
||||||
|
deps = ["ArgTools", "SHA"]
|
||||||
|
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
|
||||||
|
|
||||||
|
[[deps.Test]]
|
||||||
|
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
|
||||||
|
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
|
||||||
|
|
||||||
|
[[deps.UUIDs]]
|
||||||
|
deps = ["Random", "SHA"]
|
||||||
|
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
|
||||||
|
|
||||||
|
[[deps.Unicode]]
|
||||||
|
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
|
||||||
|
|
||||||
|
[[deps.Zlib_jll]]
|
||||||
|
deps = ["Libdl"]
|
||||||
|
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
|
||||||
|
|
||||||
|
[[deps.libblastrampoline_jll]]
|
||||||
|
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"]
|
||||||
|
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"
|
||||||
|
|
||||||
|
[[deps.nghttp2_jll]]
|
||||||
|
deps = ["Artifacts", "Libdl"]
|
||||||
|
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
|
||||||
|
|
||||||
|
[[deps.p7zip_jll]]
|
||||||
|
deps = ["Artifacts", "Libdl"]
|
||||||
|
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# ╔═╡ Cell order:
|
# ╔═╡ Cell order:
|
||||||
# ╠═81b6211e-b065-11ec-0a86-375721c85b07
|
# ╟─81b6211e-b065-11ec-0a86-375721c85b07
|
||||||
# ╠═1fb7d9af-333e-44f2-b693-09ff97937d4c
|
# ╟─7f45c502-0909-42df-b93d-384f743df6a9
|
||||||
|
# ╟─f23ad33d-af1d-40c2-9efc-17ef8c4d1fb8
|
||||||
|
# ╟─6340aec8-6f77-4a30-8815-ce76ddecd6e8
|
||||||
|
# ╠═d21771de-272e-4d57-8c76-c75be709ad0a
|
||||||
|
# ╟─09f71a9e-6798-492f-98df-45087d0c4c8b
|
||||||
|
# ╠═2a24aebc-0654-4d00-bdab-627a8e1a75f2
|
||||||
|
# ╟─1fb7d9af-333e-44f2-b693-09ff97937d4c
|
||||||
# ╟─00000000-0000-0000-0000-000000000001
|
# ╟─00000000-0000-0000-0000-000000000001
|
||||||
# ╟─00000000-0000-0000-0000-000000000002
|
# ╟─00000000-0000-0000-0000-000000000002
|
||||||
|
|
Loading…
Reference in a new issue