diff options
| author | martial.simon <martial.simon@epita.fr> | 2025-04-13 19:54:19 +0200 |
|---|---|---|
| committer | martial.simon <martial.simon@epita.fr> | 2025-04-13 19:54:19 +0200 |
| commit | 66c3bbfa94d8a41e58adf154be25e6d86fee8e30 (patch) | |
| tree | 9c5e998f324f2f60c1717759144da3f996c5ae1a /PVCM/cama/en/ma1 np90 petits exercices.ipynb | |
init: initial commit
Diffstat (limited to 'PVCM/cama/en/ma1 np90 petits exercices.ipynb')
| -rw-r--r-- | PVCM/cama/en/ma1 np90 petits exercices.ipynb | 430 |
1 files changed, 430 insertions, 0 deletions
diff --git a/PVCM/cama/en/ma1 np90 petits exercices.ipynb b/PVCM/cama/en/ma1 np90 petits exercices.ipynb new file mode 100644 index 0000000..fd79b8d --- /dev/null +++ b/PVCM/cama/en/ma1 np90 petits exercices.ipynb @@ -0,0 +1,430 @@ +{ + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "# Numpy - Exercises\n", + "\n", + "Here is a series of tests to check your knowledge in Numpy. The goal is for this library and the vectorized programming it implies to become natural. You should forget about classic programming with loops (do not use a `for` loop or anything else in this notebook except for list comprehension).\n", + "\n", + "Functions should never modify their arguments.\n", + "\n", + "The exercises indicate the number of lines in the answer. This includes the function header when there is one.\n", + "\n", + "Remember to test your results." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8c0a79d0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 0 1 2 3]\n", + " [ 4 5 6 7]\n", + " [ 8 9 10 11]] \n", + "\n", + "[[ 3 -8 -8 -4 7]\n", + " [ 9 0 -9 -10 7]\n", + " [ 5 -1 -10 4 -10]\n", + " [ 5 9 4 -6 -10]\n", + " [ 6 -6 7 -7 -8]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "rand = np.random.RandomState(123)\n", + "\n", + "A = np.arange(12).reshape(3,4)\n", + "B = rand.randint(-10, 10, size=(5,5))\n", + "print(A, '\\n')\n", + "print(B)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Square Matrix\n", + "\n", + "Create the 5x5 square matrix containing the first 25 integers, from 1 to 25.\n", + "\n", + "1 line" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "14ccdaf5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 1, 2, 3, 4, 5],\n", + " [ 6, 7, 8, 9, 10],\n", + " [11, 12, 13, 14, 15],\n", + " [16, 17, 18, 19, 20],\n", + " [21, 22, 23, 24, 25]])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Vector Norm\n", + "\n", + "Return the Euclidean norm of a vector without using the `norm` function.\n", + "\n", + "2 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "079b9d00", + "metadata": {}, + "outputs": [], + "source": [ + "def norm(v):\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Sub-Matrix\n", + "\n", + "Extract the sub-matrix of size (n-2)x(m-2) that removes the borders of the nxm matrix A.\n", + "\n", + "2 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1c313908", + "metadata": {}, + "outputs": [], + "source": [ + "def submat(A):\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Random Vector\n", + "\n", + "Create a function that takes a size n and returns a random integer vector of size n between -a and +a and centered as close as possible to 0.\n", + "\n", + "3 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "e93d8900", + "metadata": {}, + "outputs": [], + "source": [ + "def rand_vec(n, a):\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Trace\n", + "\n", + "Return the trace of a matrix without using the `trace` function (you have to find the missing elements yourself).\n", + "\n", + "2 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "8df7f55b", + "metadata": {}, + "outputs": [], + "source": [ + "def trace(A):\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Matrix of Multiples of 3\n", + "\n", + "Round the given matrix to the nearest multiple of 3 (for each value).\n", + "\n", + "2 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "55860b91", + "metadata": {}, + "outputs": [], + "source": [ + "def round3(A):\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Count of 9s\n", + "\n", + "Count the number of 9s in an integer matrix A given as a parameter.\n", + "\n", + "2 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "9296a705", + "metadata": {}, + "outputs": [], + "source": [ + "def nb9(A):\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Column with the Smallest Average\n", + "\n", + "Return the index of the column of a matrix with the smallest average.\n", + "\n", + "2 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "c3c9d6a1", + "metadata": {}, + "outputs": [], + "source": [ + "def min_col_mean(A):\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### ChessSum\n", + "\n", + "We consider the matrix as a chessboard with alternating white and black squares. The square at [0,0] is black. \n", + "Calculate the sum of the values on the white squares.\n", + "\n", + "2 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3921063c", + "metadata": {}, + "outputs": [], + "source": [ + "def chess_sum(A):\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### 2 Minimums\n", + "\n", + "Return the 2 smallest values of a matrix using a method with linear complexity. Note that if the matrix has its minimum value duplicated, the answer is twice this minimum value.\n", + "\n", + "6 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b680cc53", + "metadata": {}, + "outputs": [], + "source": [ + "def mins(A):\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Rows in Order\n", + "\n", + "Sort the rows of a matrix in ascending order of their average.\n", + "\n", + "2 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "5784c5be", + "metadata": {}, + "outputs": [], + "source": [ + "def sort_lines(A):\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Unique Values\n", + "\n", + "Provide the list of unique values (appearing only once) in a numpy array (the `unique` function will be useful).\n", + "\n", + "3 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "c95ce1bb", + "metadata": {}, + "outputs": [], + "source": [ + "def val_unique(A):\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Magic Tensor\n", + "\n", + "Construct a 3D tensor of size nxnxn using n times the first n² integers and such that the sum of the elements of each plane (slice) is always the same.\n", + "\n", + "3 lines" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "c1ca40c0", + "metadata": {}, + "outputs": [], + "source": [ + "def mk_magic_tensor(n):\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "lang": "en" + }, + "source": [ + "### Tensor Slices\n", + "\n", + "Extract all the slices of a 3D tensor. We return a list of arrays.\n", + "\n", + "5 lines (2 lines with the `take` function)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "e4d39b2a", + "metadata": {}, + "outputs": [], + "source": [ + "def extract_planes(T):\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "761e6ee4", + "metadata": {}, + "outputs": [], + "source": [] + } + ] +}
\ No newline at end of file |
