summaryrefslogtreecommitdiff
path: root/PVCM/cama/en/ma1 np90 petits exercices.ipynb
blob: fd79b8d3a76eed33fcba04041c6720b4483a8afa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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": []
  }
 ]
}