1
0
Fork 0
mirror of https://gitlab.rlp.net/mobitar/julia_course.git synced 2024-11-16 13:28:10 +00:00

Day 1 almost done

This commit is contained in:
Mo8it 2022-03-26 16:03:54 +01:00
parent 63fee57cbd
commit bb116d68ef

View file

@ -4,6 +4,11 @@
using Markdown
using InteractiveUtils
# ╔═╡ 1f347724-1db2-48f0-87df-4e63ad6e8820
# Importing a builtin library that provides more functions for linear algebra.
# The keyword `using` imports the package and exports (public) functions automatically.
using LinearAlgebra
# ╔═╡ d1a4ef8b-8e7d-4d34-80d8-cee195e237ae
begin
using PlutoUI
@ -13,7 +18,7 @@ end
# ╔═╡ 2c5e32f4-1d7d-4494-b025-a90d17919756
md"""
# Introduction to Julia
# Introduction
"""
# ╔═╡ 21590bf1-1e1c-46b4-a2b6-7eb915e121ab
@ -43,6 +48,11 @@ md"""
- 📚️ Take a look at the **live docs** to the right.
"""
# ╔═╡ b559ef1a-76cf-4464-b5ec-f7d6bfa892e6
md"""
# Basics
"""
# ╔═╡ 938adcfe-8d1b-4c77-8d82-c48415f5673e
md"""
## Calculation
@ -164,6 +174,62 @@ meaning_of_life = 42
# Int64 is the default
typeof(meaning_of_life)
# ╔═╡ 944e2d37-8280-47b8-b874-97221955d048
md"""
## Type hierarchy
Julia does have abstract types which are helpful for multiple dispatch.
More about multiple dispatch later 😉
"""
# ╔═╡ 23d4ac67-05ec-4b3d-8368-86256076be62
# The type hierarchy of Int64
supertypes(Int64)
# ╔═╡ e8d7de2f-7c7e-47cb-9364-27d583652167
md"""
All types showed in the output of the above cell except `Int64` are abstract.
This means that you can not have a variable with an abstract type.
You can only derive from an abstract type, but more about this when discussing structs and mutliple dispatch 😉
`Any` is the abstract type of everything.
"""
# ╔═╡ 866edf5e-a76c-448d-98e8-925eaed5eba5
# A number can either be real or complex
subtypes(Number)
# ╔═╡ a8ea4ac1-7f62-4485-a8c5-8ccf00c45720
# There are some types of real numbers.
subtypes(Real)
# ╔═╡ 786a96be-16cd-4f1b-9b5f-138e232d3183
# Integers can have a sign or no sign (only positive).
# A Bool is also treated as an integer with the value 0 or 1.
subtypes(Integer)
# ╔═╡ dd8dad86-6bc2-4489-8469-7eac80fc41bb
# Integers can have different number of bits.
subtypes(Signed)
# ╔═╡ fac04aa7-28e9-4f93-9312-a8f8f93c0877
# Minimum and maximum value of a type
typemin(Int8), typemax(Int8)
# ╔═╡ 86bc0ff0-b6bf-4700-a741-36323be58391
typemin(Int128), typemax(Int128)
# ╔═╡ ef35a3a7-c1df-4952-aa41-1ed22d7f3981
# BigInt does not have a minimum or maximum!
BigInt(10)^1000 + 1
# ╔═╡ c336d5f6-80ee-4994-a55f-2d6b3aa3d559
# Hierarchy of Float64
supertypes(Float64)
# ╔═╡ 608d4433-6e68-4f95-8581-437234b58e87
md"""
## Convertion
@ -288,22 +354,110 @@ a_mult_b = a * b
# Using the macro @show, helpful for usage in scripts
@show a * b
# ╔═╡ 3102810f-3467-4ed8-86c0-16e9177fa69d
md"""
## For loop
"""
# ╔═╡ c1c705d2-7e46-4811-9fb1-6b88b5a4140e
for i in 1:3
println(i)
end
# ╔═╡ 00456b15-5d1c-4c74-a875-31ff9c8e1789
md"""
## While loop
"""
# ╔═╡ 91c4c623-5680-4b35-a694-2bd2612def94
begin
value = 5
while value > 0
value -= 1
println(value)
end
end
# ╔═╡ 2a85d95b-51d2-4ea0-a2a2-43307a725f2a
md"""
## If, else, elseif
"""
# ╔═╡ 9a78bf14-7fb4-448a-a8dd-69e244a0a297
test_value = 1
# ╔═╡ 5281ef32-5de6-4488-8430-e5652cbf8299
if test_value == 1
println("The value is 1")
end
# ╔═╡ eec41279-e038-4415-81b3-ad5d4c396011
# change the value of the variable `test_value` and see how the input changes
if test_value == 1
println("The value is 1")
else
println("The value is not 1")
end
# ╔═╡ d2333817-e941-429b-b8e3-2ff07669096b
# change the value of the variable `test_value` and see how the input changes
if test_value == 1
println("The value is 1")
elseif test_value == 2
println("The value is 2")
elseif test_value == 3
println("The value is 3")
else
println("The value is not 1, 2 or 3")
end
# ╔═╡ be0ff87b-229a-433e-a49e-2f1ced5bb9aa
# You can combine conditions
if (test_value == 1) || (test_value == 2)
println("Value is 1 or 2")
end
# ╔═╡ 96803a1e-0779-4eab-b120-b5569a44ac7b
second_test_value = 2
# ╔═╡ 5ab233d2-f360-4362-b1f8-3f3ae2a4fee1
# change the value of the variable `second_test_value` and see how the input changes
if (test_value == 1) && (second_test_value == 1)
println("Both values are 1")
end
# ╔═╡ 0d100501-de84-4a5c-beb7-8ff9e83c473d
# change the value of the variable `test_value` and see how the input changes
if !(test_value == 1)
println("Value is not 1")
end
# ╔═╡ 9e3f698d-e57b-46c2-98e0-157fa7b06ae6
md"""
## Arrays
# Arrays
An array is a **mutable ordered** collection of elements of the same type.
Arrays can have one dimension (vector), two dimensions (matrix) or more (tensor).
Arrays can have different dimensions.
An array with (`n × 1`) dimensions is called a vector, like in mathematics.
An array with (`n × n`) dimensions is called a matrix, also like in mathematics.
But arrays can also have other dimensions (`n₁ × n₂ × n₃ × ...`) with `nᵢ` as natural numbers.
"""
# ╔═╡ d1680205-a8eb-4ef6-ae4f-059e7a30f5c1
md"""
### Vectors
## Vectors
"""
# ╔═╡ b54ace0e-8947-46a3-842a-05b5cbfc4e87
first_vector = [2, 4, 6, 8, 10]
# ╔═╡ 0d5bfd45-79da-435b-9a98-8ed996bbc7b4
# Show the dimensions of an array
size(first_vector)
# ╔═╡ 8c47710f-1ed2-40fd-9290-374b498380e3
first_vector[1]
@ -332,7 +486,7 @@ first_vector[end-1]
# ╔═╡ 6c80e009-30de-4232-9a1b-ac954242a5a6
md"""
### Slicing
## Slicing
"""
# ╔═╡ b1426df5-a083-4977-a72c-81e03fd7719d
@ -349,7 +503,7 @@ first_vector[[1, end, 2]]
# ╔═╡ 3ea54f0d-2aa5-47a3-bbc3-92023a56b834
md"""
### Mutation
## Mutation
"""
# ╔═╡ 6fdc6add-a478-4707-876b-cf6d660870ba
@ -390,33 +544,6 @@ end
# But then, the type of the array is Any
typeof(mixed_vector)
# ╔═╡ 944e2d37-8280-47b8-b874-97221955d048
md"""
`Any` is the *abstract* type of everything.
Julia does have abstract types which are helpful for multiple dispatch. More about multiple dispatch later 😉
"""
# ╔═╡ 23d4ac67-05ec-4b3d-8368-86256076be62
# The type hierarchy
supertypes(Int64)
# ╔═╡ 866edf5e-a76c-448d-98e8-925eaed5eba5
subtypes(Number)
# ╔═╡ a8ea4ac1-7f62-4485-a8c5-8ccf00c45720
subtypes(Real)
# ╔═╡ 786a96be-16cd-4f1b-9b5f-138e232d3183
subtypes(Integer)
# ╔═╡ dd8dad86-6bc2-4489-8469-7eac80fc41bb
subtypes(Signed)
# ╔═╡ c336d5f6-80ee-4994-a55f-2d6b3aa3d559
# Hierarchy of Float64
supertypes(Float64)
# ╔═╡ 87b43f26-7437-4ee9-9b83-5b21e86dd0c9
md"""
⚠️ Try to avoid using mixed arrays (type `Any`)!
@ -440,20 +567,26 @@ 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.
## Views
Views are not a copy of an array, but a *reference* to a part of it. It is best explained by an example:
"""
# ╔═╡ 22a7baee-6533-43f7-8503-e5d5537a8c78
# Don't panic!
# You don't have to understand everything in this "long" piece of code.
# It is only meant for concept explanation.
# The output is important!
begin
v = [3, 6, 9, 12]
@show v
# Copy
@show copy_of_v = v[1:3]
# View
@show view_of_v = view(v, 1:3)
println()
println() # Generate a new empty line
new_value = 100
v[1] = new_value
@ -484,42 +617,119 @@ begin
@show view_of_v
end
# ╔═╡ 90b0fb2f-eb2d-4d06-96da-a4605ce61c41
md"""
Using views is important for performance. Copying or initializing an array is expensive!
This is because a free place in the memory has to be found and assigned to the new array. This process is called *memory allocation*. More about allocations and performance improvements in the days 😉
"""
# ╔═╡ 786682f6-692d-488d-8dab-231b0111d07f
md"""
## Vector operations
"""
# ╔═╡ 16a9cb53-2812-4ed3-afe4-96c0b116ad9a
v1 = [1, 2, 3]
# ╔═╡ dfd91d6b-65a5-454b-a0f9-6ed267def022
v2 = [0, 5, 10]
# ╔═╡ e68c54d8-3fb8-4aae-a334-665fdb8db1f0
v1 + v2
# ╔═╡ 8d74d994-3d4e-40ba-97cb-6dac1003fb8f
# Now we have access to the function `dot` (and many others)
# Dot product
dot(v1, v2)
# ╔═╡ af87251f-a37c-4088-8f4d-3803778bd97e
# Cross product
cross(v1, v2)
# ╔═╡ 2ac5d431-1a4d-4db2-8954-97e011cd2175
md"""
### Matrices
## Matrices
"""
# ╔═╡ d30b3a5f-e14c-45ea-89a4-cf710733a2ee
# Readable method to define a matrix
first_matrix = [
1 2
3 4
1 2
3 4
]
# ╔═╡ 6bb730e4-b5aa-4e7b-9ccd-9298db061e7f
# Easier to write method
second_matrix = [1 2; 3 4]
# ╔═╡ 2ef34862-0578-41fe-adad-0e894c287dd5
third_matrix = [
1 2 3
4 5 6
]
# ╔═╡ 09f2d0f9-cd0e-45e4-a159-cb360292dac1
# Dimensions: n × m
size(third_matrix)
# ╔═╡ 6f6a875e-fe60-47ba-8837-60edef1b20e0
# Determinant, from LinearAlgebra
det(first_matrix)
# ╔═╡ e99a89ab-af3a-42f5-b1c1-22e13a761eeb
# Inverse matrix, from LinearAlgebra
inv_first_matrix = inv(first_matrix)
# ╔═╡ f45b7774-df6f-4019-9217-e88d99babdb3
# Matrix multiplication
inv_first_matrix * first_matrix
# ╔═╡ db1b6e53-3116-48df-b098-1c3045be0dad
# Calculate eigenvalues and vectors, from LinearAlgebra
vals, vecs = eigen(first_matrix)
# ╔═╡ cd5abd71-1bf8-484f-a46a-99cc8b994b91
# Eigenvalues
vals
# ╔═╡ 7cef46dc-803a-4a7a-9663-148b6de4a267
# First eigenvector
vecs[:, 1]
# ╔═╡ 70710989-9139-4970-a7b0-5702571e59a4
# Second eigenvector
vecs[:, 2]
# ╔═╡ a9f39e34-4c2c-48f2-9353-babe1bc3cd05
md"""
## More dimensions
One of the best ways to initialize arrays is to use `zeros`, `ones` or `fill` providing the dimensions.
After initialization, you can populate the array (inplace).
"""
# ╔═╡ 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
# Tensor with the following dimensions: 3 × 3 × 3
begin
first_tensor = zeros(3, 3, 3)
first_tensor[1, 1, 1] = 1.0
first_tensor[2, :, :] .= 4.0 # The dot is important! (Remember views)
first_tensor
end
# ╔═╡ f4c48701-d90e-48d6-bf9d-539c7fb7c7a5
md"""
Of course, you can have more than 3 dimensions!
"""
ones(3, 2)
# ╔═╡ 875cb2c2-e78d-41e3-808b-c6948f215b76
# Fill with a value other than 0 or 1
fill(42, (2, 2, 3))
# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
[compat]
@ -736,6 +946,7 @@ uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
# ╟─2c5e32f4-1d7d-4494-b025-a90d17919756
# ╟─21590bf1-1e1c-46b4-a2b6-7eb915e121ab
# ╟─d04af0fd-5ced-4f4f-b157-dd170e2ef8c8
# ╟─b559ef1a-76cf-4464-b5ec-f7d6bfa892e6
# ╟─938adcfe-8d1b-4c77-8d82-c48415f5673e
# ╠═73190799-fd03-4cc4-9b4e-c523bc310468
# ╠═4c242a67-6445-48e7-a6c3-418a489b89ba
@ -763,6 +974,17 @@ uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
# ╠═204cf77f-bf37-4110-9c9f-1f9236301ba9
# ╠═750bba32-e695-48f1-af70-70c94d13366b
# ╠═0b663bcb-4ff4-4597-b28b-b58c9cbfa181
# ╟─944e2d37-8280-47b8-b874-97221955d048
# ╠═23d4ac67-05ec-4b3d-8368-86256076be62
# ╟─e8d7de2f-7c7e-47cb-9364-27d583652167
# ╠═866edf5e-a76c-448d-98e8-925eaed5eba5
# ╠═a8ea4ac1-7f62-4485-a8c5-8ccf00c45720
# ╠═786a96be-16cd-4f1b-9b5f-138e232d3183
# ╠═dd8dad86-6bc2-4489-8469-7eac80fc41bb
# ╠═fac04aa7-28e9-4f93-9312-a8f8f93c0877
# ╠═86bc0ff0-b6bf-4700-a741-36323be58391
# ╠═ef35a3a7-c1df-4952-aa41-1ed22d7f3981
# ╠═c336d5f6-80ee-4994-a55f-2d6b3aa3d559
# ╟─608d4433-6e68-4f95-8581-437234b58e87
# ╠═beadbfd3-0015-449a-b6e7-b5182b396c1d
# ╠═552fafa4-fad5-4efe-895f-255b3ec5c858
@ -792,9 +1014,23 @@ uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
# ╠═d2607457-1794-4a0f-af41-cb80aadb598f
# ╠═23dbbe13-d997-4f9f-a300-7cb78c4fb8ee
# ╠═e767971a-7e1d-4a78-88d7-03e4ae4d51db
# ╟─3102810f-3467-4ed8-86c0-16e9177fa69d
# ╠═c1c705d2-7e46-4811-9fb1-6b88b5a4140e
# ╟─00456b15-5d1c-4c74-a875-31ff9c8e1789
# ╠═91c4c623-5680-4b35-a694-2bd2612def94
# ╠═2a85d95b-51d2-4ea0-a2a2-43307a725f2a
# ╠═9a78bf14-7fb4-448a-a8dd-69e244a0a297
# ╠═5281ef32-5de6-4488-8430-e5652cbf8299
# ╠═eec41279-e038-4415-81b3-ad5d4c396011
# ╠═d2333817-e941-429b-b8e3-2ff07669096b
# ╠═be0ff87b-229a-433e-a49e-2f1ced5bb9aa
# ╠═96803a1e-0779-4eab-b120-b5569a44ac7b
# ╠═5ab233d2-f360-4362-b1f8-3f3ae2a4fee1
# ╠═0d100501-de84-4a5c-beb7-8ff9e83c473d
# ╟─9e3f698d-e57b-46c2-98e0-157fa7b06ae6
# ╟─d1680205-a8eb-4ef6-ae4f-059e7a30f5c1
# ╠═b54ace0e-8947-46a3-842a-05b5cbfc4e87
# ╠═0d5bfd45-79da-435b-9a98-8ed996bbc7b4
# ╠═8c47710f-1ed2-40fd-9290-374b498380e3
# ╟─a03b46cc-1b26-44c2-b83d-884e3dbbe4fa
# ╠═68658408-188e-43f7-ad74-251172dec0a8
@ -813,27 +1049,36 @@ uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
# ╠═65d3ddc2-36ed-4126-9211-e838ffc0d859
# ╠═641e8c05-3e80-47e5-be77-91090a5f799a
# ╠═2b9a5867-ca82-4a27-a700-bd0bd6c89bbe
# ╟─944e2d37-8280-47b8-b874-97221955d048
# ╠═23d4ac67-05ec-4b3d-8368-86256076be62
# ╠═866edf5e-a76c-448d-98e8-925eaed5eba5
# ╠═a8ea4ac1-7f62-4485-a8c5-8ccf00c45720
# ╠═786a96be-16cd-4f1b-9b5f-138e232d3183
# ╠═dd8dad86-6bc2-4489-8469-7eac80fc41bb
# ╠═c336d5f6-80ee-4994-a55f-2d6b3aa3d559
# ╟─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
# ╟─605ee405-ec83-4064-adc8-861d95513e5e
# ╠═22a7baee-6533-43f7-8503-e5d5537a8c78
# ╟─90b0fb2f-eb2d-4d06-96da-a4605ce61c41
# ╟─786682f6-692d-488d-8dab-231b0111d07f
# ╠═16a9cb53-2812-4ed3-afe4-96c0b116ad9a
# ╠═dfd91d6b-65a5-454b-a0f9-6ed267def022
# ╠═e68c54d8-3fb8-4aae-a334-665fdb8db1f0
# ╠═1f347724-1db2-48f0-87df-4e63ad6e8820
# ╠═8d74d994-3d4e-40ba-97cb-6dac1003fb8f
# ╠═af87251f-a37c-4088-8f4d-3803778bd97e
# ╟─2ac5d431-1a4d-4db2-8954-97e011cd2175
# ╠═d30b3a5f-e14c-45ea-89a4-cf710733a2ee
# ╠═6bb730e4-b5aa-4e7b-9ccd-9298db061e7f
# ╠═2ef34862-0578-41fe-adad-0e894c287dd5
# ╠═09f2d0f9-cd0e-45e4-a159-cb360292dac1
# ╠═6f6a875e-fe60-47ba-8837-60edef1b20e0
# ╠═e99a89ab-af3a-42f5-b1c1-22e13a761eeb
# ╠═f45b7774-df6f-4019-9217-e88d99babdb3
# ╠═db1b6e53-3116-48df-b098-1c3045be0dad
# ╠═cd5abd71-1bf8-484f-a46a-99cc8b994b91
# ╠═7cef46dc-803a-4a7a-9663-148b6de4a267
# ╠═70710989-9139-4970-a7b0-5702571e59a4
# ╟─a9f39e34-4c2c-48f2-9353-babe1bc3cd05
# ╠═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
# ╠═f4c48701-d90e-48d6-bf9d-539c7fb7c7a5
# ╠═875cb2c2-e78d-41e3-808b-c6948f215b76
# ╟─d1a4ef8b-8e7d-4d34-80d8-cee195e237ae
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002