Week 1: Exercises#
Exercises – Long Day#
1: Function or Not?#
Consider the following correspondence between \(a\) and \(b\) values:
\(a\) |
\(b\) |
---|---|
1 |
0 |
2 |
1 |
0 |
3 |
1 |
2 |
We consider functions whose domain is a subset of \(\{0,1,2,3\}\) and whose co-domain is \(\{0,1,2,3\}\).
We need to determine if \(f\) and \(g\) define functions. We let \(f\) follow the rule that the first column (the \(a\) values) contains the input and the second column (the \(b\) values) contains the output of the function \(f\), with the domain being \(\{0,1,2\}\). And we let \(g\) follow the rule that the second column is the input and the first column should be the output of the function \(g\), with the domain being \(\{0,1,2,3\}\).
Does \(f\) define a function? Does \(g\)? If so, determine the range/image of the function, and decide whether the function is injective and/or surjective.
Hint
Read this section in the textbook.
Answer
Only \(g\) is a function because \(f\) tries to define both \(f(1)=0\) and \(f(1)=2\). The function \(g\) has \(\mathrm{im}(g) = \{0,1,2\} \subset \{0,1,2,3\}\), so it is not surjective. Since \(g(0)=1\) and \(g(2)=1\), it is also not injective.
2: Same Functional Expressions?#
We consider functions \(f_i : \mathbb{R} \to \mathbb{R}\) given by:
where \(x \in \mathbb{R}\).
Some of the functions are the same function. Find them all!
Hint
ReLU is defined in the textbook.
Hint
First, calculate \(f_i(x)\) for the different functions for a couple of different values of \(x\).
3: Possible Visualizations#
Discuss whether it is possible to visualize the following functions – if so, plot them with SymPy/dtumathtools:
A scalar function of two variables \(f: \mathbb{R}^2 \to \mathbb{R}, \, f(x_1,x_2) = \sqrt{\vert x_1 x_2 \vert}\)
A scalar function of four variables \(f: \mathbb{R}^4 \to \mathbb{R}, \, f(x_1,x_2,x_3,x_4) = \sqrt{\vert x_1 x_2 x_3 x_4 \vert}\)
A complex scalar function of two variables \(f: \mathbb{R}^2 \to \mathbb{C}, \, f(x_1,x_2) = \sqrt{\vert x_1 x_2 \vert} + i \cos(x_1 + x_2)\)
A vector field in 2D \(\pmb{f}: \mathbb{R}^2 \to \mathbb{R}^2, \, \pmb{f}(x_1,x_2) = (-x_2/3, x_1/3)\)
A vector field in 3D \(\pmb{f}: \mathbb{R}^3 \to \mathbb{R}^3, \, \pmb{f}(x,y,z)= (x^3+yz^2, y^3-xz^2, z^3)\)
A function of the form \(\pmb{r}: [0,10] \to \mathbb{R}^3, \, \pmb{r}(t) = (\cos(t),\sin(t),t)\)
Note
The following Python commands may be useful:
dtuplot.plot3d
, dtuplot.plot_vector
,
dtuplot.plot3d_parametric_line
.
Hint
When plotting a vector field, it may be necessary to scale the vectors (if they are too short or too long). This can be done by multiplying the vector field by a scalar, for example by typing 0.1 * f
instead of just f
.
4: Next-Prime Function#
Let \(f: \mathbb{N} \to \mathbb{N}\) be a function that returns the next prime number (strictly) greater than a given natural number \(n\). In this question, you should first determine the value of the function for two specific inputs, and then show that the function is well-defined before implementing it in Python.
Question a#
Find, with simple reasoning, \(f(10)\) and \(f(13)\).
Hint
Continue this sequence of prime numbers: 2, 3, 5, 7, …
Answer
\(f(10) = 11\) and \(f(13) = 17\), since 11 is the smallest prime greater than 10, and 17 is the smallest prime greater than 13.
Question b#
Argue that the function \(f(n)\) is well-defined.
Hint
This means we need to argue that the mapping \(f: \mathbb{N} \to \mathbb{N}\) is a function, i.e., that it assigns exactly one element \(f(n) \in \mathbb{N}\) to each element \(n \in \mathbb{N}\).
Hint
There are infinitely many primes, and thus infinitely large primes.
Answer
Existence: There are infinitely many primes, so there will always be a prime number greater than any given natural number.
Uniqueness: For any number \(n\), there is a unique smallest prime number greater than \(n\).
Thus, the function is well-defined because there is always a next prime greater than \(n\) (existence), and there is only one smallest prime greater than \(n\) (uniqueness).
Question c#
Can a functional expression for \(f(n)\) be found? Argue why it is or isn’t possible.
Answer
There is no known simple formula that directly computes the next prime number greater than a given number \(n\). Prime numbers do not follow a simple mathematical pattern, so the function must be computed algorithmically by testing numbers after \(n\).
Question d#
Implement the function \(f(n)\) in Python, such that it takes an integer \(n\) as input and returns the next prime number greater than \(n\). Define an auxiliary function (a “helper function”) is_prime(x)
to determine if a number is a prime.
from math import sqrt
def is_prime(x: int) -> bool:
"""Returns True if x is a prime, otherwise False."""
if x < 2:
return False
# Three lines of code missing
return True
def f(n: int) -> int:
"""Returns the next prime number greater than n."""
candidate = n + 1
# Two lines of code missing
return candidate
Hint
For is_prime(x)
:
You can check if
i
is a divisor ofx
withx % i == 0
.Check this for all
i
from2
tosqrt(x)
.Return
False
if this is the case for ani
.
Hint
For f(n)
:
Check if
n+1
is a prime number.Then check if
n+2
is a prime number.Etc. Stop when you find a prime number and return it.
Answer
from math import sqrt
def is_prime(x: int) -> bool:
"""Returns True if x is a prime number, otherwise False."""
if x < 2:
return False
for i in range(2, int(sqrt(x)) + 1): # We only need to check up to the square root of x
if x % i == 0:
return False
return True
def f(n: int) -> int:
"""Returns the next prime number greater than n."""
candidate = n + 1
while not is_prime(candidate):
candidate += 1
return candidate
# Test examples
if __name__ == "__main__":
n = 5
print(f"The next prime number greater than {n} is {f(n)}.") # This should return 7
n = 7
print(f"The next prime number greater than {n} is {f(n)}.") # This should return 11
Question e (optional)#
Can you update your Python function from the previous question so that the domain of \(f\) is extended from \(\mathbb{N}\) to \(\mathbb{R}\)? E.g., \(f(\pi)\) should return 5.
5: Linear Vector Function#
Let \(A \in \mathsf{M}_{3 \times 5}(\mathbb{R})\) be given by
Consider the vector function \(\pmb{f}: \mathbb{R}^5 \to \mathbb{R}^3\) given by \(\pmb{f} = \pmb{x} \mapsto A\pmb{x}\), where \(\pmb{x}\) is a column vector in \(\mathbb{R}\).
Question a#
Provide the 3 coordinate functions for \(\pmb{f}\).
Hint
Remember that the coordinate functions \(f_i\) are \(\pmb{f} = (f_1, f_2, f_3)\).
Question b#
Provide the image set \(\mathrm{im}(\pmb{f})\) for \(\pmb{f}\).
Hint
What is the rank of \(A\)? What is the column space \(\mathrm{col}A\) of \(A\)?
Answer
\(\mathrm{im}(\pmb{f}) = \mathrm{col}A = \mathbb{R}^3 \)
Question c#
Is the vector function \(\pmb{f}\) surjective and/or injective?
Answer
\(\pmb{f}\) is surjective because \(\mathrm{im}(\pmb{f}) = \mathbb{R}^3\). It is not injective since \(\mathrm{dim \, ker}(A) = 5-3=2\) (it is possible to find two different vectors \(\pmb{x}_1, \pmb{x}_2 \in \mathbb{R}^5\) for which \(\pmb{f}(\pmb{x}_1) = \pmb{f}(\pmb{x}_2)\)).
6: Chatbots and (Dis-)continuity#
Ask a chatbot, such as BingChat, OpenAI’s ChatGPT, or Microsoft Copilot, if it can provide an example of a function \(f: \mathbb{R} \to \mathbb{R}\) that is continuous at exactly 3 points and discontinuous at all other points. Evaluate whether the chatbot’s answer is correct or incorrect. Can the function from the chatbot’s example be plotted?
7: Python Function as a Mathematical Function#
Consider the following function:
from sympy import *
from dtumathtools import *
init_printing()
def f(x1, x2):
return Matrix([ln(x1), x1 * x2**2 + 1])
Note
You can define the same function with the following one-liner call: myfun = lambda x1,x2: Matrix([ln(x1), x1 * x2**2 + 1])
. There are technical differences in Python between the two methods, but in usage, they are very similar. We generally recommend using def
functions. However, lambda functions are useful in situations where the function is only needed once and in calls to other functions, e.g., sorted(['a1', 'z0'], key = lambda x: int(x[1]))
.
Note
It is not necessary to define
x1,x2 = symbols("x1 x2")
indef f(x1,x2):
. However, it is good programming practice to tell others what the function expects as input and gives as output. This is elegantly done in Python by:
def f(x1: Symbol('x1', real=True), x2: Symbol('x2', real=True)) -> Matrix:
return Matrix([ln(x1), x1 * x2**2 + 1])
Although we often work with functions in this course, it is often not necessary to define Python functions. For example, the derivative of \(f(x) = x^2 \cos(x)\) is simply given by:
x = symbols("x")
f = x**2 * cos(x)
f.diff()
Question a:#
What is the function value at \(f(2,-3)\)?
Question b:#
Write the Python function as a mathematical function. The co-domain is \(\mathbb{R}^2\). What is the domain?
Hint
In Python, you don’t need to specify domains. Therefore, we are here implying that the domain should be the largest possible.
Hint
What is the domain for the logarithm?
Answer
\(\mathrm{dom}(f) = ]0,\infty [ \times \mathbb{R}\)
Question c:#
Find the image/range.
Hint
The question is for which \((y_1, y_2) \in \mathbb{R}^2\), the equations \(y_1 = \ln(x_1)\) and \(y_2 = x_1 \, x_2^2 + 1\) have a solution. First, look at the equation \(y_1 = \ln(x_1)\).
Hint
If \(x_1\) can only be any positive real number, what values can \(x_1 \, x_2^2 + 1\) take?
Answer
\(\mathrm{im} (f) = \mathbb{R} \times [1, \infty [\)
Exercises – Short Day#
1: Magnitude of Vectors#
Consider the following three vectors in \(\mathbb{R}^3\):
Which vector is the longest? Which vectors are orthogonal to each other? Which two vectors are closest to each other?
Note
We can imagine the vectors as (geometrical) position vectors with their origin at \(\pmb{0}=[0,0,0]^T\) and the endpoint at \(\pmb{v}_i\) for \(i = 1, 2, 3\). Sometimes this is written as \(\overrightarrow{\pmb{0}\pmb{v}_i}\).
2: Partial Derivatives of a Simple Scalar Function#
Find the partial derivatives \(\frac{\partial f}{\partial x_1}\) and \(\frac{\partial f}{\partial x_2}\) for \(f(x_1, x_2) = x_1^3 + 3x_1 x_2 + x_2^3\). Determine the value of the partial derivatives at the point \((x_1, x_2) = (1, 2)\).
Hint
Remember the formula for partial derivatives with respect to each variable.
Treat other variables as constants during differentiation.
Answer
\(\frac{\partial f}{\partial x_1} = 3x_1^2 + 3x_2\)
\(\frac{\partial f}{\partial x_2} = 3x_2^2 + 3x_1\)
At the point \((1,2)\): \(\frac{\partial f}{\partial x_1}(1, 2) = 9\), \(\frac{\partial f}{\partial x_2}(1, 2) = 15\)
3: Different(?) Quadratic Forms#
Let \(\pmb{x} = [x_1,x_2]^T\) be a column vector in \(\mathbb{R}^2\). Define:
and
Let \(q_i: \mathbb{R}^2 \to \mathbb{R}\) be given by:
for \(i=1,2,3\). Such functions are called quadratic forms, see this definition.
Question a#
Expand the expression for \(q_1(x_1,x_2)\). First by hand, then using Python. Also expand the expressions for \(q_2(x_1,x_2)\) and \(q_3(x_1,x_2)\) (either by hand or using Python).
Hint
It may be necessary to use c = Matrix([-60])
because SymPy will not add scalars to 1x1 matrices. On the other hand, a 1x1 SymPy matrix q1
can be converted to a scalar by either q1[0,0]
or q1[0]
.
Answer
\( q_1(\pmb{x}) = 11 x_{1}^{2} - 24 x_{1} x_{2} - 20 x_{1} + 4 x_{2}^{2} + 40 x_{2} - 60\)
Question b#
Is the square matrix \(A\) in a quadratic form (such as \(\pmb{x}^T A \pmb{x}\)) uniquely determined?
Hint
Have a look at \(q_1(\pmb{x})\) and \(q_2(\pmb{x})\).
Answer
No. For example, \(q_1(\pmb{x}) = q_2(\pmb{x})\), while \(A_1 \neq A_2\).
Question c#
Plot the graph of the function \(q_1\). Then plot some level curves. What geometric shape do the level curves have? Do the same for \(q_3\).
Question d#
One of the functions has a minimum. Which one? Where is it approximately located? What is this point called for the functions that do not have a minimum?
Answer
\(q_3\) has a minimum
It is located at \((2,1)\) (just read it from a plot).
\(q_1\) has a so-called saddle point at this point, Wikipedia
4: Quadratic Forms with Symmetric Matrices#
Let \(A\) be an arbitrary \(n \times n\) matrix, and let \(\pmb{x}\) be a column vector in \(\mathbb{R}^n\). Define \(B\) by \(B = (A + A^T)/2\).
Question a#
Show that the matrix \(B\) is symmetric.
Hint
A matrix \(B\) is symmetric if and only if \(B = B^T\). Remember that \(B = \tfrac{1}{2} (A + A^T)\).
Answer
\( B^T = ((A + A^T)/2)^T = \tfrac{1}{2} (A^T + (A^T)^T) = \tfrac{1}{2} (A^T + A) = \tfrac{1}{2} (A + A^T) = B \)
Question b#
Show that \(\pmb{x}^T A \pmb{x} = \pmb{x}^T B \pmb{x}\).
Question c#
Conclude that it always can be assumed that quadratic forms of the type \(q(\pmb{x}) = \pmb{x}^T A \pmb{x} + \pmb{b}^T \pmb{x} + c\) are given by a symmetric matrix \(A\).
5: Level Curves and Gradients#
We consider the function \(f:\mathbb{R}^2\rightarrow \mathbb{R}\) given by the expression
and its level curves
Question a#
Show that the level curve for any \(c\) can be described by an equation in the form \(y = g_c(x)\), where \(g_c\) is a real function of \(x\). Plot the level curves corresponding to \(c \in \{-2, -1, 0, 1, 2\}\).
Answer
The level curves are 5 parabolas that are shifted parallel to each other in the direction of the \(y\) axis. What is the distance between them?
Question b#
Show that the point \(P = (2,1)\) is located on the level curve corresponding to \(c = 2\), and find a parametric representation for this level curve.
Answer
For example, we can choose \(\pmb{r}(u) = \left(u, \frac{1}{2}u^2 - 1\right)\), \(u \in \mathbb{R}\).
Question c#
Determine the tangent vector corresponding to the parametric representation at \(P\), and show that the tangent vector is orthogonal to the gradient of \(f\) at \(P\).
Hint
The tangent vector is obtained by first differentiating each of the two coordinate functions in the parametric representation and then substituting the parameter value for the given point.
Question d#
Create a combined plot in Python showing the level curves and the gradient vector field of \(f\).
Hint
Use SymPy’s dtuplot
.