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

Done tasks of day 5

This commit is contained in:
Mo8it 2022-04-01 03:38:24 +02:00
parent e3aa717353
commit 485aa0079c

226
Day_5/Tasks_day_5.jl Normal file
View file

@ -0,0 +1,226 @@
### A Pluto.jl notebook ###
# v0.18.4
using Markdown
using InteractiveUtils
# ╔═╡ b11b2fda-b13e-11ec-0b00-13a7cf45371c
md"""
# Four vectors
We want to define our own struct in Julia to deal with four vectors in physics.
Implement the following four vector operators by completing the code in the next cell in the `begin` block:
$v =
\begin{pmatrix}
ct \\
x \\
y \\
z
\end{pmatrix}$
$u =
\begin{pmatrix}
ct' \\
x' \\
y' \\
z'
\end{pmatrix}$
$v + u =
\begin{pmatrix}
ct + ct' \\
x + x' \\
y + y' \\
z + z'
\end{pmatrix}$
$-v =
\begin{pmatrix}
-ct \\
-x \\
-y \\
-z
\end{pmatrix}$
$v - u =
\begin{pmatrix}
ct - ct' \\
x - x' \\
y - y' \\
z - z'
\end{pmatrix}$
$v * u = ct * ct' - x * x' - y * y' - z * z'$
$|v|^2 = ct^2 - x^2 - y^2 - z^2$
You should implement $|v|^2$ as a function called `length_squared`.
"""
# ╔═╡ 04c7c619-e770-415b-ad52-37e0e0e2d2f4
begin
mutable struct FourVector
# Add fields here
end
# To override a predefined function of a package, the function has to be imported
# In this case, the functions are from the package `Base` which has Julia's builtin functions
import Base: +, -, *, ^
(+)(v::FourVector, u::FourVector) = FourVector(
v.ct + u.ct,
v.x + u.x,
v.y + u.y,
v.z + u.z
)
#(-)(v::FourVector) = ?
#(-)(v::FourVector, u::FourVector) = ?
#(*)(v::FourVector, u::FourVector) = ?
end
# ╔═╡ 4b8da4d9-c880-405e-859e-81f5884d29bf
md"""
After defining everything above successfully, the cells below would not give you any error.
"""
# ╔═╡ 22590bad-ff7e-4189-b816-cb5b6e7232c4
v = FourVector(1.0, 1.0, 1.0, 1.0)
# ╔═╡ e49637f2-e174-4c82-923d-7caf37e5e41a
u = FourVector(2.0, 3.0, 4.0, 5.0)
# ╔═╡ 3a89512a-30a8-4cf6-9d8a-b090cee2e8a7
v == u
# ╔═╡ ab360d3d-0ec4-4718-a3b3-a6b90a167f6b
v == FourVector(1.0, 1.0, 1.0, 1.0)
# ╔═╡ 7fa11520-93a4-48bf-96fa-0f6e134deb96
v + u
# ╔═╡ 0353376b-94d6-40cf-9b28-18287c75ec69
v * u
# ╔═╡ 83be3070-c556-4c3d-aaca-93063ac91c64
-v
# ╔═╡ 8c1ca9ef-6ca4-4a3a-9ffb-8d8f39c9ee48
v - u
# ╔═╡ 5bee57cf-9c34-4e5c-974f-fcd2636f1300
length_squared(v)
# ╔═╡ 4a69ccd4-576c-4d31-b75b-8caa9c7159fa
md"""
# Chess matrices
Implement a function that takes an integer `n` as an arguement and returns a `n × n` matrix with a chess pattern using `1` and `0`.
The matrix elements have to be integers.
Example:
$n = 3 \rightarrow \begin{pmatrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0 \\
\end{pmatrix}$
If you want to have more fun, implement the function again, but this time with the emojies ⬜️ and ⬛️ instead of 0 and 1 😹
"""
# ╔═╡ 070e9dc2-884c-4a3b-873a-9a9ebf40c7c6
# Your code starts here
# ╔═╡ 61970ee5-bb70-465a-a940-257a5437574a
md"""
# Parsing Pauli matrices
In the directory `resources`, there are three files:
- `Matrix_1.txt`
- `Matrix_2.txt`
- `Matrix_3.txt`
These are the pauli matrices.
Implement a function that parses these three files and returns the three matrices.
Verify that for all matrices the trace is 0 and the determinant is -1.
#### Hints:
- You don't need `CSV` or `DataFrames`!
- Instead of
```julia
lines = open(FILEPATH, "r") do io
return readlines(io)
end
```
you can just use this shortcut:
```julia
lines = readlines(FILEPATH)
```
- You should use
```julia
parse(Complex{Float64}, STRING)
```
for parsing a complex number with real and imaginary parts of type `Float64`.
- You should use
```julia
zeros(Complex{Float64}, (2, 2))
```
to generate a `2 × 2` matrix with complex numbers with real and imaginary parts as 0 with type `Float64`.
- The package `LinearAlgebra.jl` might be helpful.
"""
# ╔═╡ eccf0ed9-fe65-4872-8e11-e94a49e642d2
# Your code starts here
# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
"""
# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised
julia_version = "1.7.2"
manifest_format = "2.0"
[deps]
"""
# ╔═╡ Cell order:
# ╟─b11b2fda-b13e-11ec-0b00-13a7cf45371c
# ╠═04c7c619-e770-415b-ad52-37e0e0e2d2f4
# ╟─4b8da4d9-c880-405e-859e-81f5884d29bf
# ╠═22590bad-ff7e-4189-b816-cb5b6e7232c4
# ╠═e49637f2-e174-4c82-923d-7caf37e5e41a
# ╠═3a89512a-30a8-4cf6-9d8a-b090cee2e8a7
# ╠═ab360d3d-0ec4-4718-a3b3-a6b90a167f6b
# ╠═7fa11520-93a4-48bf-96fa-0f6e134deb96
# ╠═0353376b-94d6-40cf-9b28-18287c75ec69
# ╠═83be3070-c556-4c3d-aaca-93063ac91c64
# ╠═8c1ca9ef-6ca4-4a3a-9ffb-8d8f39c9ee48
# ╠═5bee57cf-9c34-4e5c-974f-fcd2636f1300
# ╟─4a69ccd4-576c-4d31-b75b-8caa9c7159fa
# ╠═070e9dc2-884c-4a3b-873a-9a9ebf40c7c6
# ╟─61970ee5-bb70-465a-a940-257a5437574a
# ╠═eccf0ed9-fe65-4872-8e11-e94a49e642d2
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002