├── README.md
├── assignment-statements
└── cs325-f21-a-01.pdf
├── notebooks
├── 00-python-refresher.ipynb
├── 01-vectors-matrices.html
├── 02-numpy.html
├── 04-numbers.html
├── 05-hdf5.html
├── 06-linear-algebra.html
├── 07-root-finding.html
└── 08-interpolation-basics.html
└── notes-pdf
├── 01-intro-a.pdf
├── 02-floating-point.pdf
├── 03-distance-measures.pdf
├── 04-solns-linear-eqns-a.pdf
├── 05-root-finding-a.pdf
└── 06-interpolation-a.pdf
/README.md:
--------------------------------------------------------------------------------
1 | # Numerical Computing (Full Course in Urdu)
2 |
3 | recluze.net
4 |
5 | See full video course here: https://www.youtube.com/playlist?list=PLnd7R4Mcw3rIpsOe-lijX3jtpUwPmK8n6
6 |
--------------------------------------------------------------------------------
/assignment-statements/cs325-f21-a-01.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/recluze/numerical-computing-cs/06a4e9e4d957163a5689aa1698bcc2cbbf47145d/assignment-statements/cs325-f21-a-01.pdf
--------------------------------------------------------------------------------
/notebooks/00-python-refresher.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Refresher"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 2,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "x = 5 "
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 3,
22 | "metadata": {},
23 | "outputs": [],
24 | "source": [
25 | "def good_enough(x, guess): \n",
26 | " return abs((guess * guess) - x) < 0.00000000001\n",
27 | "\n",
28 | "def improve_guess(x, guess): \n",
29 | " return (guess + x/guess)/2 \n",
30 | "\n",
31 | "def sqrt(x, guess=0.0001): \n",
32 | " if good_enough(x, guess): \n",
33 | " return guess \n",
34 | " else: \n",
35 | " return sqrt(x, improve_guess(x, guess)) "
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": 4,
41 | "metadata": {},
42 | "outputs": [
43 | {
44 | "data": {
45 | "text/plain": [
46 | "6.000000000000008"
47 | ]
48 | },
49 | "execution_count": 4,
50 | "metadata": {},
51 | "output_type": "execute_result"
52 | }
53 | ],
54 | "source": [
55 | "sqrt(36)"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": 5,
61 | "metadata": {},
62 | "outputs": [
63 | {
64 | "name": "stdout",
65 | "output_type": "stream",
66 | "text": [
67 | "0\n",
68 | "1\n",
69 | "2\n",
70 | "3\n",
71 | "4\n",
72 | "5\n",
73 | "6\n",
74 | "7\n",
75 | "8\n",
76 | "9\n"
77 | ]
78 | }
79 | ],
80 | "source": [
81 | "p = 0\n",
82 | "while (p < 10): \n",
83 | " print(p)\n",
84 | " p += 1"
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": 6,
90 | "metadata": {},
91 | "outputs": [
92 | {
93 | "name": "stdout",
94 | "output_type": "stream",
95 | "text": [
96 | "0\n",
97 | "1\n",
98 | "2\n",
99 | "3\n",
100 | "4\n",
101 | "5\n",
102 | "6\n",
103 | "7\n",
104 | "8\n",
105 | "9\n"
106 | ]
107 | }
108 | ],
109 | "source": [
110 | "for i in range(10): \n",
111 | " print(i)"
112 | ]
113 | },
114 | {
115 | "cell_type": "code",
116 | "execution_count": 7,
117 | "metadata": {},
118 | "outputs": [],
119 | "source": [
120 | "l = [1, 2, 4] "
121 | ]
122 | },
123 | {
124 | "cell_type": "code",
125 | "execution_count": 8,
126 | "metadata": {},
127 | "outputs": [
128 | {
129 | "name": "stdout",
130 | "output_type": "stream",
131 | "text": [
132 | "Square root of 1: 1.0\n",
133 | "Square root of 2: 1.4142135623730954\n",
134 | "Square root of 5: 2.23606797749979\n"
135 | ]
136 | }
137 | ],
138 | "source": [
139 | "for i in [1, 2, 5]: \n",
140 | " print(\"Square root of \" + str(i) + \": \", end=\"\") \n",
141 | " print(sqrt(i))"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": 10,
147 | "metadata": {},
148 | "outputs": [
149 | {
150 | "name": "stdout",
151 | "output_type": "stream",
152 | "text": [
153 | "one\n",
154 | "dict_items([(1, 'one'), (2, 'two')])\n"
155 | ]
156 | }
157 | ],
158 | "source": [
159 | "d = {1: \"one\", \n",
160 | " 2: \"two\"\n",
161 | " }\n",
162 | "\n",
163 | "\n",
164 | "print(d[1])\n",
165 | "print(d.items())"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": 11,
171 | "metadata": {},
172 | "outputs": [
173 | {
174 | "name": "stdout",
175 | "output_type": "stream",
176 | "text": [
177 | "1 one\n",
178 | "2 two\n"
179 | ]
180 | }
181 | ],
182 | "source": [
183 | "for k, v in d.items(): \n",
184 | " print(k, v)"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": 12,
190 | "metadata": {},
191 | "outputs": [
192 | {
193 | "data": {
194 | "text/plain": [
195 | "[1, 2]"
196 | ]
197 | },
198 | "execution_count": 12,
199 | "metadata": {},
200 | "output_type": "execute_result"
201 | }
202 | ],
203 | "source": [
204 | "list(d.keys())"
205 | ]
206 | },
207 | {
208 | "cell_type": "code",
209 | "execution_count": 13,
210 | "metadata": {},
211 | "outputs": [],
212 | "source": [
213 | "a = [1, 2, 3, 4, 5]\n",
214 | "b = [6, 7, 8, 9, 0]\n",
215 | "\n",
216 | "merged = list(zip(a, b))"
217 | ]
218 | },
219 | {
220 | "cell_type": "code",
221 | "execution_count": 14,
222 | "metadata": {},
223 | "outputs": [
224 | {
225 | "data": {
226 | "text/plain": [
227 | "[(1, 6), (2, 7), (3, 8), (4, 9), (5, 0)]"
228 | ]
229 | },
230 | "execution_count": 14,
231 | "metadata": {},
232 | "output_type": "execute_result"
233 | }
234 | ],
235 | "source": [
236 | "merged"
237 | ]
238 | },
239 | {
240 | "cell_type": "code",
241 | "execution_count": 15,
242 | "metadata": {},
243 | "outputs": [
244 | {
245 | "data": {
246 | "text/plain": [
247 | "{'id': '13', 'name': 'bill', 'location': 'redmond'}"
248 | ]
249 | },
250 | "execution_count": 15,
251 | "metadata": {},
252 | "output_type": "execute_result"
253 | }
254 | ],
255 | "source": [
256 | "# Real world usecase of ZIP: combining column headers with row values \n",
257 | "# much more, along with usecases, here: \n",
258 | "# https://stackoverflow.com/questions/2429692\n",
259 | "\n",
260 | "fields = [\"id\", \"name\", \"location\"]\n",
261 | "values = [\"13\", \"bill\", \"redmond\"]\n",
262 | "dict(zip(fields, values))"
263 | ]
264 | },
265 | {
266 | "cell_type": "markdown",
267 | "metadata": {},
268 | "source": [
269 | "**Very important:** When trying to solve any problem, first see if you can do it with lists or dictionaries (dynamic arrays and maps). These will solve a majority of your problems. "
270 | ]
271 | },
272 | {
273 | "cell_type": "markdown",
274 | "metadata": {},
275 | "source": [
276 | "# OOP Concepts in Python"
277 | ]
278 | },
279 | {
280 | "cell_type": "markdown",
281 | "metadata": {},
282 | "source": [
283 | "Creating a class with a constructor is simple with a minor difference: self
"
284 | ]
285 | },
286 | {
287 | "cell_type": "markdown",
288 | "metadata": {},
289 | "source": [
290 | "To access member variables and methods, you must always use self
. This is not optional as in other languages. "
291 | ]
292 | },
293 | {
294 | "cell_type": "code",
295 | "execution_count": 16,
296 | "metadata": {},
297 | "outputs": [],
298 | "source": [
299 | "class Point:\n",
300 | " # constructors are defined using a special method, which must be named __init__ \n",
301 | " \n",
302 | " def __init__(self, x=0, y=0): \n",
303 | " self.x = x\n",
304 | " self.y = y \n",
305 | " \n",
306 | " def __str__(self): \n",
307 | " return \"[\" + str(self.x) + \",\" + str(self.y) + \"]\""
308 | ]
309 | },
310 | {
311 | "cell_type": "code",
312 | "execution_count": 17,
313 | "metadata": {},
314 | "outputs": [
315 | {
316 | "name": "stdout",
317 | "output_type": "stream",
318 | "text": [
319 | "p1 = 0\n",
320 | "p1 = [0,0]\n",
321 | "p2 = 2\n",
322 | "p2 = [2,4]\n"
323 | ]
324 | }
325 | ],
326 | "source": [
327 | "p1 = Point() # p1 is a reference variable \n",
328 | "print(\"p1 = \", p1.x)\n",
329 | "print(\"p1 = \", p1)\n",
330 | "\n",
331 | "\n",
332 | "p2 = Point(2, 4) # notice that we do NOT pass in self. That is automatically done for us! \n",
333 | "\n",
334 | "\n",
335 | "print(\"p2 = \", p2.x)\n",
336 | "print(\"p2 = \", p2)"
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": null,
342 | "metadata": {},
343 | "outputs": [],
344 | "source": [
345 | "print(p1)"
346 | ]
347 | },
348 | {
349 | "cell_type": "markdown",
350 | "metadata": {},
351 | "source": [
352 | "## Composition "
353 | ]
354 | },
355 | {
356 | "cell_type": "markdown",
357 | "metadata": {},
358 | "source": [
359 | "Composition is simple as always. "
360 | ]
361 | },
362 | {
363 | "cell_type": "code",
364 | "execution_count": 18,
365 | "metadata": {},
366 | "outputs": [],
367 | "source": [
368 | "class Shape: \n",
369 | " def __init__(self, points): \n",
370 | " self.points = points \n",
371 | " \n",
372 | " def __str__(self): \n",
373 | " ret = \"\" \n",
374 | " \n",
375 | " for i in self.points:\n",
376 | " ret += str(i) + \" - \"\n",
377 | " \n",
378 | " return ret "
379 | ]
380 | },
381 | {
382 | "cell_type": "code",
383 | "execution_count": 19,
384 | "metadata": {},
385 | "outputs": [],
386 | "source": [
387 | "p1 = Point(5, 5) \n",
388 | "p2 = Point(10, 5) \n",
389 | "p3 = Point(5, 10) \n",
390 | "p = [p1, p2, p3]\n",
391 | "\n",
392 | "sh = Shape(p)"
393 | ]
394 | },
395 | {
396 | "cell_type": "code",
397 | "execution_count": 20,
398 | "metadata": {},
399 | "outputs": [
400 | {
401 | "name": "stdout",
402 | "output_type": "stream",
403 | "text": [
404 | "[5,5] - [10,5] - [5,10] - \n"
405 | ]
406 | }
407 | ],
408 | "source": [
409 | "print(sh)"
410 | ]
411 | },
412 | {
413 | "cell_type": "markdown",
414 | "metadata": {},
415 | "source": [
416 | "We can add methods to class even after it has been defined! "
417 | ]
418 | },
419 | {
420 | "cell_type": "code",
421 | "execution_count": 24,
422 | "metadata": {},
423 | "outputs": [],
424 | "source": [
425 | "def print_points(self): \n",
426 | " for i in self.points: \n",
427 | " print(i) \n",
428 | " \n",
429 | "Shape.print_points = print_points "
430 | ]
431 | },
432 | {
433 | "cell_type": "code",
434 | "execution_count": 25,
435 | "metadata": {},
436 | "outputs": [
437 | {
438 | "name": "stdout",
439 | "output_type": "stream",
440 | "text": [
441 | "[5,5]\n",
442 | "[10,5]\n",
443 | "[5,10]\n"
444 | ]
445 | }
446 | ],
447 | "source": [
448 | "sh.print_points()"
449 | ]
450 | },
451 | {
452 | "cell_type": "markdown",
453 | "metadata": {},
454 | "source": [
455 | "## Inheritance "
456 | ]
457 | },
458 | {
459 | "cell_type": "markdown",
460 | "metadata": {},
461 | "source": [
462 | "Inhertiance syntax is also slightly different but quite easy."
463 | ]
464 | },
465 | {
466 | "cell_type": "code",
467 | "execution_count": 26,
468 | "metadata": {},
469 | "outputs": [],
470 | "source": [
471 | "class Triangle(Shape): # Triangle inhertis from Shape \n",
472 | " pass # pass means I'm not going to have anything in this block "
473 | ]
474 | },
475 | {
476 | "cell_type": "code",
477 | "execution_count": 27,
478 | "metadata": {},
479 | "outputs": [],
480 | "source": [
481 | "t = Triangle(p)"
482 | ]
483 | },
484 | {
485 | "cell_type": "code",
486 | "execution_count": 28,
487 | "metadata": {},
488 | "outputs": [
489 | {
490 | "name": "stdout",
491 | "output_type": "stream",
492 | "text": [
493 | "[5,5]\n",
494 | "[10,5]\n",
495 | "[5,10]\n"
496 | ]
497 | }
498 | ],
499 | "source": [
500 | "t.print_points() # automatically inherited "
501 | ]
502 | },
503 | {
504 | "cell_type": "code",
505 | "execution_count": 29,
506 | "metadata": {},
507 | "outputs": [],
508 | "source": [
509 | "def get_area(self): \n",
510 | " vertices = self.points\n",
511 | " n = len(vertices) # of corners\n",
512 | " a = 0.0\n",
513 | " for i in range(n):\n",
514 | " j = (i + 1) % n\n",
515 | " a += abs(vertices[i].x * vertices[j].y - vertices[j].x * vertices[i].y)\n",
516 | " return a / 2.0\n",
517 | "\n",
518 | "Triangle.get_area = get_area "
519 | ]
520 | },
521 | {
522 | "cell_type": "code",
523 | "execution_count": 30,
524 | "metadata": {},
525 | "outputs": [
526 | {
527 | "data": {
528 | "text/plain": [
529 | "62.5"
530 | ]
531 | },
532 | "execution_count": 30,
533 | "metadata": {},
534 | "output_type": "execute_result"
535 | }
536 | ],
537 | "source": [
538 | "t.get_area()"
539 | ]
540 | },
541 | {
542 | "cell_type": "markdown",
543 | "metadata": {},
544 | "source": [
545 | "## Access Parent Class' Overridden Methods "
546 | ]
547 | },
548 | {
549 | "cell_type": "code",
550 | "execution_count": 31,
551 | "metadata": {},
552 | "outputs": [],
553 | "source": [
554 | "class Rectangle:\n",
555 | " def __init__(self, length, width):\n",
556 | " self.length = length\n",
557 | " self.width = width\n",
558 | "\n",
559 | " def area(self):\n",
560 | " return self.length * self.width\n",
561 | "\n",
562 | " def perimeter(self):\n",
563 | " return 2 * self.length + 2 * self.width\n",
564 | "\n",
565 | " def __str__(self): \n",
566 | " return \"L: \" + str(self.length) + \" W: \" + str(self.width)"
567 | ]
568 | },
569 | {
570 | "cell_type": "code",
571 | "execution_count": 32,
572 | "metadata": {},
573 | "outputs": [
574 | {
575 | "name": "stdout",
576 | "output_type": "stream",
577 | "text": [
578 | "L: 2 W: 4\n"
579 | ]
580 | }
581 | ],
582 | "source": [
583 | "rect = Rectangle(2, 4) \n",
584 | "print(rect)"
585 | ]
586 | },
587 | {
588 | "cell_type": "code",
589 | "execution_count": null,
590 | "metadata": {},
591 | "outputs": [],
592 | "source": [
593 | "# Here we declare that the Square class inherits from the Rectangle class\n",
594 | "class Square(Rectangle):\n",
595 | " def __init__(self, length):\n",
596 | " super().__init__(length, length)\n",
597 | " \n",
598 | " def __str__(self): \n",
599 | " return \"Square: \" + super().__str__()"
600 | ]
601 | },
602 | {
603 | "cell_type": "code",
604 | "execution_count": null,
605 | "metadata": {},
606 | "outputs": [],
607 | "source": [
608 | "square = Square(4)\n",
609 | "square.area()"
610 | ]
611 | },
612 | {
613 | "cell_type": "code",
614 | "execution_count": null,
615 | "metadata": {},
616 | "outputs": [],
617 | "source": [
618 | "print(square)"
619 | ]
620 | },
621 | {
622 | "cell_type": "markdown",
623 | "metadata": {},
624 | "source": [
625 | "## Polymorphism \n",
626 | "\n",
627 | "You get that for free in Python. Just call the method. If it's overridden, the child class' method will be executed. "
628 | ]
629 | },
630 | {
631 | "cell_type": "markdown",
632 | "metadata": {},
633 | "source": [
634 | "## Access Modifiers \n",
635 | "\n",
636 | "There are none. By convention, methods starting with _
are considered private and shouldn't be called from the outside. If you still want to do it, the response is, \"good luck!\". "
637 | ]
638 | },
639 | {
640 | "cell_type": "markdown",
641 | "metadata": {},
642 | "source": [
643 | "See more details about classes on https://docs.python.org/3/tutorial/classes.html"
644 | ]
645 | },
646 | {
647 | "cell_type": "code",
648 | "execution_count": null,
649 | "metadata": {},
650 | "outputs": [],
651 | "source": []
652 | }
653 | ],
654 | "metadata": {
655 | "kernelspec": {
656 | "display_name": "Python 3",
657 | "language": "python",
658 | "name": "python3"
659 | },
660 | "language_info": {
661 | "codemirror_mode": {
662 | "name": "ipython",
663 | "version": 3
664 | },
665 | "file_extension": ".py",
666 | "mimetype": "text/x-python",
667 | "name": "python",
668 | "nbconvert_exporter": "python",
669 | "pygments_lexer": "ipython3",
670 | "version": "3.8.6"
671 | }
672 | },
673 | "nbformat": 4,
674 | "nbformat_minor": 4
675 | }
676 |
--------------------------------------------------------------------------------
/notes-pdf/01-intro-a.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/recluze/numerical-computing-cs/06a4e9e4d957163a5689aa1698bcc2cbbf47145d/notes-pdf/01-intro-a.pdf
--------------------------------------------------------------------------------
/notes-pdf/02-floating-point.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/recluze/numerical-computing-cs/06a4e9e4d957163a5689aa1698bcc2cbbf47145d/notes-pdf/02-floating-point.pdf
--------------------------------------------------------------------------------
/notes-pdf/03-distance-measures.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/recluze/numerical-computing-cs/06a4e9e4d957163a5689aa1698bcc2cbbf47145d/notes-pdf/03-distance-measures.pdf
--------------------------------------------------------------------------------
/notes-pdf/04-solns-linear-eqns-a.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/recluze/numerical-computing-cs/06a4e9e4d957163a5689aa1698bcc2cbbf47145d/notes-pdf/04-solns-linear-eqns-a.pdf
--------------------------------------------------------------------------------
/notes-pdf/05-root-finding-a.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/recluze/numerical-computing-cs/06a4e9e4d957163a5689aa1698bcc2cbbf47145d/notes-pdf/05-root-finding-a.pdf
--------------------------------------------------------------------------------
/notes-pdf/06-interpolation-a.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/recluze/numerical-computing-cs/06a4e9e4d957163a5689aa1698bcc2cbbf47145d/notes-pdf/06-interpolation-a.pdf
--------------------------------------------------------------------------------