Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

Author 1

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.

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}
63.6 ms
+ (generic function with 190 methods)
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.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) = ?

# ==(v::FourVector, u::FourVector) = ?
end
1.1 ms

After defining everything above successfully, the cells below would not give you any error.

268 μs

MethodError: no method matching Main.var"workspace#2".FourVector(::Float64, ::Float64, ::Float64, ::Float64)

Stack trace

Here is what happened, the most recent locations are first:

v = FourVector(1.0, 1.0, 1.0, 1.0)
---

MethodError: no method matching Main.var"workspace#2".FourVector(::Float64, ::Float64, ::Float64, ::Float64)

Stack trace

Here is what happened, the most recent locations are first:

u = FourVector(2.0, 3.0, 4.0, 5.0)
---

UndefVarError: `v` not defined

Stack trace

Here is what happened, the most recent locations are first:

v == u
---

UndefVarError: `v` not defined

Stack trace

Here is what happened, the most recent locations are first:

v == FourVector(1.0, 1.0, 1.0, 1.0)
---

UndefVarError: `v` not defined

Stack trace

Here is what happened, the most recent locations are first:

v + u
---

UndefVarError: `v` not defined

Stack trace

Here is what happened, the most recent locations are first:

v * u
---

UndefVarError: `v` not defined

Stack trace

Here is what happened, the most recent locations are first:

-v
---

UndefVarError: `v` not defined

Stack trace

Here is what happened, the most recent locations are first:

v - u
---

UndefVarError: `length_squared` not defined

Stack trace

Here is what happened, the most recent locations are first:

length_squared(v)
---

Chess matrices

Implement a function that takes an integer n as an argument 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 emojis ⬜️ and ⬛️ instead of 0 and 1 😹

565 μs
# Your code starts here

8.7 μs

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

lines = open(FILEPATH, "r") do io
	return readlines(io)
end

you can just use this shortcut:

lines = readlines(FILEPATH)
  • You should use

parse(Complex{Float64}, STRING)

for parsing a complex number with real and imaginary parts of type Float64.

  • You should use

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.

37.6 ms
# Your code starts here

24.8 μs