mirror of
https://gitlab.rlp.net/mobitar/julia_course.git
synced 2024-11-16 13:28:10 +00:00
Addition to first day
This commit is contained in:
parent
535b7f079b
commit
e77d9a2fd1
1 changed files with 311 additions and 0 deletions
311
Day_1/Day_1.jl
311
Day_1/Day_1.jl
|
@ -120,6 +120,41 @@ fl = 42.0
|
||||||
# ╔═╡ 750bba32-e695-48f1-af70-70c94d13366b
|
# ╔═╡ 750bba32-e695-48f1-af70-70c94d13366b
|
||||||
meaning_of_life = 42
|
meaning_of_life = 42
|
||||||
|
|
||||||
|
# ╔═╡ 608d4433-6e68-4f95-8581-437234b58e87
|
||||||
|
md"""
|
||||||
|
## Converting
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ beadbfd3-0015-449a-b6e7-b5182b396c1d
|
||||||
|
convert(Int64, 3.0)
|
||||||
|
|
||||||
|
# ╔═╡ 552fafa4-fad5-4efe-895f-255b3ec5c858
|
||||||
|
convert(Complex, 3.0)
|
||||||
|
|
||||||
|
# ╔═╡ d11fde7f-3238-4013-bd2d-546aab0d9f9c
|
||||||
|
# This does not work!
|
||||||
|
# convert(Int64, 3.2)
|
||||||
|
|
||||||
|
# ╔═╡ 036a2c43-dbc9-487c-96aa-94324eeb4a52
|
||||||
|
md"""
|
||||||
|
## Rounding
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 1e954726-254e-41bb-a62f-17bdc9884bee
|
||||||
|
# We have to tell Julia explicitely what to do
|
||||||
|
round(Int64, 3.2)
|
||||||
|
|
||||||
|
# ╔═╡ d74f6c46-f5a8-4720-bcaf-936f1508efda
|
||||||
|
round(π)
|
||||||
|
|
||||||
|
# ╔═╡ 3e5daca6-5aa8-42bf-988b-c09fb17388df
|
||||||
|
# ; marks the start of keyword arguments. More about it later!
|
||||||
|
round(π; digits=2)
|
||||||
|
|
||||||
|
# ╔═╡ 7ff20c67-58d5-4095-bb8e-7ab7522791c7
|
||||||
|
# You can provide a rounding mode, see the docs!
|
||||||
|
round(π, RoundUp)
|
||||||
|
|
||||||
# ╔═╡ 4a00035f-a1d1-409f-b73b-07f9073dc9d5
|
# ╔═╡ 4a00035f-a1d1-409f-b73b-07f9073dc9d5
|
||||||
md"""
|
md"""
|
||||||
## Boolean operators
|
## Boolean operators
|
||||||
|
@ -164,6 +199,230 @@ split(new_to_julia, ",")
|
||||||
# ╔═╡ a96f3ae9-12df-4df8-85da-09b9b1e47de1
|
# ╔═╡ a96f3ae9-12df-4df8-85da-09b9b1e47de1
|
||||||
join(["Hello", "brave", "new", "world", "!"], " ", "")
|
join(["Hello", "brave", "new", "world", "!"], " ", "")
|
||||||
|
|
||||||
|
# ╔═╡ 8d106ad2-5f92-4138-bfff-56ee21e098fa
|
||||||
|
multi_line_string = """Line 1
|
||||||
|
Line 2
|
||||||
|
Line 3"""
|
||||||
|
|
||||||
|
# ╔═╡ f8bc4051-93c6-4376-abaa-7b4cb4b8f607
|
||||||
|
println(multi_line_string)
|
||||||
|
|
||||||
|
# ╔═╡ b25dc5f2-e186-4da1-b045-47b22c93799b
|
||||||
|
md"""
|
||||||
|
## String formatting
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ e0d7fbc8-39fc-4b70-9b92-0c19fffb0c05
|
||||||
|
a
|
||||||
|
|
||||||
|
# ╔═╡ 398648e8-358e-4289-ae95-957e77d0c46f
|
||||||
|
b
|
||||||
|
|
||||||
|
# ╔═╡ 28fa32e7-4e50-4890-a765-5cfb1d3f791b
|
||||||
|
a_mult_b = a * b
|
||||||
|
|
||||||
|
# ╔═╡ d2607457-1794-4a0f-af41-cb80aadb598f
|
||||||
|
"The result of $a * $b is $a_mult_b"
|
||||||
|
|
||||||
|
# ╔═╡ 23dbbe13-d997-4f9f-a300-7cb78c4fb8ee
|
||||||
|
"The result of $a * $b is $(a * b)"
|
||||||
|
|
||||||
|
# ╔═╡ e767971a-7e1d-4a78-88d7-03e4ae4d51db
|
||||||
|
# Using the macro @show, helpful for usage in scripts
|
||||||
|
@show a * b
|
||||||
|
|
||||||
|
# ╔═╡ 9e3f698d-e57b-46c2-98e0-157fa7b06ae6
|
||||||
|
md"""
|
||||||
|
## Arrays
|
||||||
|
An array is a **mutable** **ordered** collection of elements.
|
||||||
|
|
||||||
|
Arrays can have one dimension (vector), two dimensions (matrix) or more (tensor).
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ d1680205-a8eb-4ef6-ae4f-059e7a30f5c1
|
||||||
|
md"""
|
||||||
|
### Vectors
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ b54ace0e-8947-46a3-842a-05b5cbfc4e87
|
||||||
|
first_vector = [2, 4, 6, 8, 10]
|
||||||
|
|
||||||
|
# ╔═╡ 8c47710f-1ed2-40fd-9290-374b498380e3
|
||||||
|
first_vector[1]
|
||||||
|
|
||||||
|
# ╔═╡ a03b46cc-1b26-44c2-b83d-884e3dbbe4fa
|
||||||
|
md"""
|
||||||
|
Yes, it is not the second element of the vector! 😯
|
||||||
|
|
||||||
|
In Julia, indexing starts with 1️⃣
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 68658408-188e-43f7-ad74-251172dec0a8
|
||||||
|
# ⚠️ This results in an error!
|
||||||
|
# first_vector[0]
|
||||||
|
|
||||||
|
# ╔═╡ 45b64c7a-850b-402c-b7ce-2a0bf6d77060
|
||||||
|
# ⚠️ No negative indexing, too!
|
||||||
|
# first_vector[-1]
|
||||||
|
|
||||||
|
# ╔═╡ d1ed1515-cd59-4e10-a15c-b64325bc44c2
|
||||||
|
# Instead, this can be used
|
||||||
|
first_vector[end-1]
|
||||||
|
|
||||||
|
# ╔═╡ 7c00f22c-860f-4bb1-b4b4-74c5c3c70f45
|
||||||
|
# Last element
|
||||||
|
first_vector[end]
|
||||||
|
|
||||||
|
# ╔═╡ 6c80e009-30de-4232-9a1b-ac954242a5a6
|
||||||
|
md"""
|
||||||
|
### Slicing
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ b1426df5-a083-4977-a72c-81e03fd7719d
|
||||||
|
# start:end
|
||||||
|
first_vector[2:end]
|
||||||
|
|
||||||
|
# ╔═╡ 5b16ca43-1f56-4934-a420-5ffa5ed437ec
|
||||||
|
# start:step:end
|
||||||
|
first_vector[1:2:end]
|
||||||
|
|
||||||
|
# ╔═╡ 628852dc-16e5-4a03-93a9-be209b1e8fb4
|
||||||
|
# List of indices
|
||||||
|
first_vector[[1, end, 2]]
|
||||||
|
|
||||||
|
# ╔═╡ 3ea54f0d-2aa5-47a3-bbc3-92023a56b834
|
||||||
|
md"""
|
||||||
|
### Mutation
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ e9e117af-1194-4d64-94a8-3e9fd51498aa
|
||||||
|
# Setting the first element to 3
|
||||||
|
begin
|
||||||
|
second_vector = [1, 2, 3]
|
||||||
|
second_vector[1] = 42
|
||||||
|
second_vector
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 027313d6-c247-43e9-872b-c3f0fe71b733
|
||||||
|
third_vector = [1, 2, 3]
|
||||||
|
|
||||||
|
# ╔═╡ e77e7ceb-31e3-4231-9923-f62b1382a2d1
|
||||||
|
# ⚠️ This does not work!
|
||||||
|
# third_vector[2] = 42.1
|
||||||
|
|
||||||
|
# ╔═╡ 65d3ddc2-36ed-4126-9211-e838ffc0d859
|
||||||
|
typeof(third_vector)
|
||||||
|
|
||||||
|
# ╔═╡ 641e8c05-3e80-47e5-be77-91090a5f799a
|
||||||
|
begin
|
||||||
|
mixed_vector = ["hi", 2.1, 55.7]
|
||||||
|
mixed_vector[2] = 1 + 2im
|
||||||
|
mixed_vector
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 2b9a5867-ca82-4a27-a700-bd0bd6c89bbe
|
||||||
|
typeof(mixed_vector)
|
||||||
|
|
||||||
|
# ╔═╡ 87b43f26-7437-4ee9-9b83-5b21e86dd0c9
|
||||||
|
md"""
|
||||||
|
⚠️ Try to avoid using mixed arrays (type `Any`)!
|
||||||
|
|
||||||
|
It hurts the performance! 🐢
|
||||||
|
|
||||||
|
This is because the array can contain anything and the compiler can not optimize for specific types.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 22f5ebc1-fd5c-4ee7-b169-8144fbd9b570
|
||||||
|
# ⚠️ This does not work! Run it and see the helpful error message
|
||||||
|
# first_vector[1:2] = 5
|
||||||
|
|
||||||
|
# ╔═╡ 7479c420-2e04-4fe7-823c-3fde9efb54ca
|
||||||
|
# Instead use this to change many slices in the original vector
|
||||||
|
first_vector[1:2] .= 5
|
||||||
|
|
||||||
|
# ╔═╡ 4cd82256-be63-4db1-b2af-82a1358f4881
|
||||||
|
# Compare the output of the cell above with this. What is the difference?
|
||||||
|
typeof(first_vector[1:2])
|
||||||
|
|
||||||
|
# ╔═╡ 605ee405-ec83-4064-adc8-861d95513e5e
|
||||||
|
md"""
|
||||||
|
### Views
|
||||||
|
Views are not a copy of an array, but a reference to a part of it.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ 22a7baee-6533-43f7-8503-e5d5537a8c78
|
||||||
|
begin
|
||||||
|
v = [3, 6, 9, 12]
|
||||||
|
@show v
|
||||||
|
|
||||||
|
@show copy_of_v = v[1:3]
|
||||||
|
|
||||||
|
@show view_of_v = view(v, 1:3)
|
||||||
|
|
||||||
|
println()
|
||||||
|
|
||||||
|
new_value = 100
|
||||||
|
v[1] = new_value
|
||||||
|
println("Changed first element of v to $new_value")
|
||||||
|
|
||||||
|
@show v
|
||||||
|
@show copy_of_v
|
||||||
|
@show view_of_v
|
||||||
|
|
||||||
|
println()
|
||||||
|
|
||||||
|
new_value = 42
|
||||||
|
copy_of_v[2] = new_value
|
||||||
|
println("Changed second element of copy_of_v to $new_value")
|
||||||
|
|
||||||
|
@show v
|
||||||
|
@show copy_of_v
|
||||||
|
@show view_of_v
|
||||||
|
|
||||||
|
println()
|
||||||
|
|
||||||
|
new_value = 55
|
||||||
|
view_of_v[3] = new_value
|
||||||
|
println("Changed third element of view_of_v to $new_value")
|
||||||
|
|
||||||
|
@show v
|
||||||
|
@show copy_of_v
|
||||||
|
@show view_of_v
|
||||||
|
end
|
||||||
|
|
||||||
|
# ╔═╡ 2ac5d431-1a4d-4db2-8954-97e011cd2175
|
||||||
|
md"""
|
||||||
|
### Matrices
|
||||||
|
"""
|
||||||
|
|
||||||
|
# ╔═╡ d30b3a5f-e14c-45ea-89a4-cf710733a2ee
|
||||||
|
# Readable method to define a matrix
|
||||||
|
first_matrix = [
|
||||||
|
1 2
|
||||||
|
3 4
|
||||||
|
]
|
||||||
|
|
||||||
|
# ╔═╡ 6bb730e4-b5aa-4e7b-9ccd-9298db061e7f
|
||||||
|
# Easier to write method
|
||||||
|
second_matrix = [1 2; 3 4]
|
||||||
|
|
||||||
|
# ╔═╡ 83eca43d-2280-40f1-bf2a-016a843362a3
|
||||||
|
first_tensor = zeros(3, 3, 3)
|
||||||
|
|
||||||
|
# ╔═╡ 64e47c03-356c-4a71-b463-4c247ea861cb
|
||||||
|
first_tensor[1, 1, 1] = 1.0
|
||||||
|
|
||||||
|
# ╔═╡ 23bdd308-3efa-49fd-9dcd-d4d1989383ee
|
||||||
|
first_tensor[2, :, :] .= 4.0
|
||||||
|
|
||||||
|
# ╔═╡ 1a0b5604-fa4b-4291-baf0-97340aff9bca
|
||||||
|
first_tensor
|
||||||
|
|
||||||
|
# ╔═╡ f4c48701-d90e-48d6-bf9d-539c7fb7c7a5
|
||||||
|
md"""
|
||||||
|
Of course, you can have more than 3 dimensions!
|
||||||
|
"""
|
||||||
|
|
||||||
# ╔═╡ 00000000-0000-0000-0000-000000000001
|
# ╔═╡ 00000000-0000-0000-0000-000000000001
|
||||||
PLUTO_PROJECT_TOML_CONTENTS = """
|
PLUTO_PROJECT_TOML_CONTENTS = """
|
||||||
[deps]
|
[deps]
|
||||||
|
@ -405,6 +664,15 @@ uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
|
||||||
# ╠═10fdb32f-b66f-4c4e-abd9-e856549941b8
|
# ╠═10fdb32f-b66f-4c4e-abd9-e856549941b8
|
||||||
# ╠═00ba151b-a741-448d-b8bf-775217250915
|
# ╠═00ba151b-a741-448d-b8bf-775217250915
|
||||||
# ╠═750bba32-e695-48f1-af70-70c94d13366b
|
# ╠═750bba32-e695-48f1-af70-70c94d13366b
|
||||||
|
# ╟─608d4433-6e68-4f95-8581-437234b58e87
|
||||||
|
# ╠═beadbfd3-0015-449a-b6e7-b5182b396c1d
|
||||||
|
# ╠═552fafa4-fad5-4efe-895f-255b3ec5c858
|
||||||
|
# ╠═d11fde7f-3238-4013-bd2d-546aab0d9f9c
|
||||||
|
# ╟─036a2c43-dbc9-487c-96aa-94324eeb4a52
|
||||||
|
# ╠═1e954726-254e-41bb-a62f-17bdc9884bee
|
||||||
|
# ╠═d74f6c46-f5a8-4720-bcaf-936f1508efda
|
||||||
|
# ╠═3e5daca6-5aa8-42bf-988b-c09fb17388df
|
||||||
|
# ╠═7ff20c67-58d5-4095-bb8e-7ab7522791c7
|
||||||
# ╟─4a00035f-a1d1-409f-b73b-07f9073dc9d5
|
# ╟─4a00035f-a1d1-409f-b73b-07f9073dc9d5
|
||||||
# ╠═d4ebb324-fa31-4058-9da1-35e07a971106
|
# ╠═d4ebb324-fa31-4058-9da1-35e07a971106
|
||||||
# ╠═f8259580-5a29-4a13-811f-c91d6811a291
|
# ╠═f8259580-5a29-4a13-811f-c91d6811a291
|
||||||
|
@ -416,6 +684,49 @@ uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
|
||||||
# ╠═da5fb1f9-2a2d-4148-8ba5-8c4a529829e9
|
# ╠═da5fb1f9-2a2d-4148-8ba5-8c4a529829e9
|
||||||
# ╠═943da836-384d-4774-aaf4-54c27feb53d8
|
# ╠═943da836-384d-4774-aaf4-54c27feb53d8
|
||||||
# ╠═a96f3ae9-12df-4df8-85da-09b9b1e47de1
|
# ╠═a96f3ae9-12df-4df8-85da-09b9b1e47de1
|
||||||
|
# ╠═8d106ad2-5f92-4138-bfff-56ee21e098fa
|
||||||
|
# ╠═f8bc4051-93c6-4376-abaa-7b4cb4b8f607
|
||||||
|
# ╟─b25dc5f2-e186-4da1-b045-47b22c93799b
|
||||||
|
# ╠═e0d7fbc8-39fc-4b70-9b92-0c19fffb0c05
|
||||||
|
# ╠═398648e8-358e-4289-ae95-957e77d0c46f
|
||||||
|
# ╠═28fa32e7-4e50-4890-a765-5cfb1d3f791b
|
||||||
|
# ╠═d2607457-1794-4a0f-af41-cb80aadb598f
|
||||||
|
# ╠═23dbbe13-d997-4f9f-a300-7cb78c4fb8ee
|
||||||
|
# ╠═e767971a-7e1d-4a78-88d7-03e4ae4d51db
|
||||||
|
# ╠═9e3f698d-e57b-46c2-98e0-157fa7b06ae6
|
||||||
|
# ╟─d1680205-a8eb-4ef6-ae4f-059e7a30f5c1
|
||||||
|
# ╠═b54ace0e-8947-46a3-842a-05b5cbfc4e87
|
||||||
|
# ╠═8c47710f-1ed2-40fd-9290-374b498380e3
|
||||||
|
# ╟─a03b46cc-1b26-44c2-b83d-884e3dbbe4fa
|
||||||
|
# ╠═68658408-188e-43f7-ad74-251172dec0a8
|
||||||
|
# ╠═45b64c7a-850b-402c-b7ce-2a0bf6d77060
|
||||||
|
# ╠═d1ed1515-cd59-4e10-a15c-b64325bc44c2
|
||||||
|
# ╠═7c00f22c-860f-4bb1-b4b4-74c5c3c70f45
|
||||||
|
# ╟─6c80e009-30de-4232-9a1b-ac954242a5a6
|
||||||
|
# ╠═b1426df5-a083-4977-a72c-81e03fd7719d
|
||||||
|
# ╠═5b16ca43-1f56-4934-a420-5ffa5ed437ec
|
||||||
|
# ╠═628852dc-16e5-4a03-93a9-be209b1e8fb4
|
||||||
|
# ╟─3ea54f0d-2aa5-47a3-bbc3-92023a56b834
|
||||||
|
# ╠═e9e117af-1194-4d64-94a8-3e9fd51498aa
|
||||||
|
# ╠═027313d6-c247-43e9-872b-c3f0fe71b733
|
||||||
|
# ╠═e77e7ceb-31e3-4231-9923-f62b1382a2d1
|
||||||
|
# ╠═65d3ddc2-36ed-4126-9211-e838ffc0d859
|
||||||
|
# ╠═641e8c05-3e80-47e5-be77-91090a5f799a
|
||||||
|
# ╠═2b9a5867-ca82-4a27-a700-bd0bd6c89bbe
|
||||||
|
# ╠═87b43f26-7437-4ee9-9b83-5b21e86dd0c9
|
||||||
|
# ╠═22f5ebc1-fd5c-4ee7-b169-8144fbd9b570
|
||||||
|
# ╠═7479c420-2e04-4fe7-823c-3fde9efb54ca
|
||||||
|
# ╠═4cd82256-be63-4db1-b2af-82a1358f4881
|
||||||
|
# ╠═605ee405-ec83-4064-adc8-861d95513e5e
|
||||||
|
# ╠═22a7baee-6533-43f7-8503-e5d5537a8c78
|
||||||
|
# ╟─2ac5d431-1a4d-4db2-8954-97e011cd2175
|
||||||
|
# ╠═d30b3a5f-e14c-45ea-89a4-cf710733a2ee
|
||||||
|
# ╠═6bb730e4-b5aa-4e7b-9ccd-9298db061e7f
|
||||||
|
# ╠═83eca43d-2280-40f1-bf2a-016a843362a3
|
||||||
|
# ╠═64e47c03-356c-4a71-b463-4c247ea861cb
|
||||||
|
# ╠═23bdd308-3efa-49fd-9dcd-d4d1989383ee
|
||||||
|
# ╠═1a0b5604-fa4b-4291-baf0-97340aff9bca
|
||||||
|
# ╟─f4c48701-d90e-48d6-bf9d-539c7fb7c7a5
|
||||||
# ╟─d1a4ef8b-8e7d-4d34-80d8-cee195e237ae
|
# ╟─d1a4ef8b-8e7d-4d34-80d8-cee195e237ae
|
||||||
# ╟─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