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.

2: Same Functional Expressions?#

We consider functions \(f_i : \mathbb{R} \to \mathbb{R}\) given by:

\[\begin{gather*} f_1(x) = |x| \\ f_2(x) = \begin{cases} x & x > 0 \\ -x & x \le 0 \end{cases} \\ f_3(x) = \max(x,0) \\ f_4(x) = ReLU(x) + ReLU(-x) \end{gather*}\]

where \(x \in \mathbb{R}\).

Some of the functions are the same function. Find them all!

3: Possible Visualizations#

Discuss whether it is possible to visualize the following functions – if so, plot them with SymPy/dtumathtools:

  1. 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}\)

  2. 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}\)

  3. 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)\)

  4. 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)\)

  5. 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)\)

  6. 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.

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)\).

Question b#

Argue that the function \(f(n)\) is well-defined.

Question c#

Can a functional expression for \(f(n)\) be found? Argue why it is or isn’t possible.

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  

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

\[\begin{equation*} A = \begin{bmatrix} 1 & 0 & 2 & 3 & 4 \\ 0 & -1 & 5 & 6 & 7 \\ 0 & 0 & -3 & 8 & 9 \end{bmatrix} \end{equation*}\]

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}\).

Question b#

Provide the image set \(\mathrm{im}(\pmb{f})\) for \(\pmb{f}\).

Question c#

Is the vector function \(\pmb{f}\) surjective and/or injective?

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") in def 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?

Question c:#

Find the image/range.


Exercises – Short Day#

1: Magnitude of Vectors#

Consider the following three vectors in \(\mathbb{R}^3\):

\[\begin{equation*} \pmb{v}_1 = \left[\begin{matrix}-10\\ -10\\ -10\end{matrix}\right], \, \pmb{v}_2 = \left[\begin{matrix}-10\\ -4\\ 14\end{matrix}\right], \, \pmb{v}_3 = \left[\begin{matrix}-10\\ -8\\-12\end{matrix}\right] \end{equation*}\]

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)\).

3: Different(?) Quadratic Forms#

Let \(\pmb{x} = [x_1,x_2]^T\) be a column vector in \(\mathbb{R}^2\). Define:

\[\begin{equation*} A_1 =\left[\begin{matrix}11 & -12 \\ -12 & 4\end{matrix}\right], \, A_2 =\left[\begin{matrix}11 & 0 \\ -24 & 4\end{matrix}\right], \, A_3 =\left[\begin{matrix} 73/5 & -36/5 \\ -36/5 & 52/5 \end{matrix}\right], \, \end{equation*}\]

and

\[\begin{equation*} \pmb{b}_1 = \left[\begin{matrix}-20\\ 40\end{matrix}\right], \, \pmb{b}_2 = \pmb{b}_1, \, \pmb{b}_3 = \left[\begin{matrix}-44\\ 8\end{matrix}\right], \, c = -60 \end{equation*}\]

Let \(q_i: \mathbb{R}^2 \to \mathbb{R}\) be given by:

\[\begin{equation*} q_i(\pmb{x}) = \pmb{x}^T A_i \pmb{x} + \pmb{b}_i^T \pmb{x} + c \end{equation*}\]

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).

Question b#

Is the square matrix \(A\) in a quadratic form (such as \(\pmb{x}^T A \pmb{x}\)) uniquely determined?

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?

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.

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

\[\begin{equation*} f(x,y)=x^2-2y \end{equation*}\]

and its level curves

\[\begin{equation*} f(x,y)=c,\, c\in\mathbb{R}. \end{equation*}\]

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\}\).

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.

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\).

Question d#

Create a combined plot in Python showing the level curves and the gradient vector field of \(f\).