{ "cells": [ { "cell_type": "markdown", "id": "b4c48d87-84f4-4b30-9941-928e7dff57ad", "metadata": {}, "source": [ "# Introduction to Computing" ] }, { "cell_type": "markdown", "id": "7b25a24a-8aa6-4457-aaab-2a029edcfa4c", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## Course Objective" ] }, { "cell_type": "markdown", "id": "89b1ae19-0400-40c4-9d70-072b7f144a81", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Introduce computer programming via _Python_ as a tool for solving engineering problems" ] }, { "cell_type": "markdown", "id": "90015e89-b89f-4764-a842-8c88b2df6374", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Straddles between a full-fledged introductory computer programming course and a numerical methods course" ] }, { "attachments": {}, "cell_type": "markdown", "id": "12926ec0-f4a7-448b-92e8-d503b6bb596c", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- Reference textbooks\n", " - [_Algorithmic Problem Solving with Python_](www.eecs.wsu.edu/~schneidj/swan) by John B. Schneider, Shira Lynn\u000b\n", "Broschat, and Jess Dahmen. 2013.\n", " - [_Foundations of Python Programming_](https://runestone.academy/ns/books/published//fopp/index.html) by the Runestone Interactive Project\n", " - [_Think Python: How to Think Like a Computer Scientist_](https://greenteapress.com/wp/think-python-2e/) by Allan B. Downey\n", " - [_Python Programming and Numerical Methods: A Guide for Engineers and Scientists_](https://pythonnumericalmethods.berkeley.edu/notebooks/Index.html) by Qingkai Kong, Timmy Siauw, and Alexandre Bayen. 2020." ] }, { "cell_type": "markdown", "id": "1311b370-dc4f-4b4c-b42e-7def792f5043", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## Computers" ] }, { "cell_type": "markdown", "id": "931b6108-817b-4f99-a125-878856e38552", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- What is a computer?\n", " - A device or machine capable of processing instructions and performing arithmetic or logical operations\n", " - Note: before the invention of electronic computers, computers referred to humans that carried out repetitive arithmetic calculations" ] }, { "cell_type": "markdown", "id": "b340f2bd-1103-431b-ae3f-09f613242ce7", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" } }, "source": [ "### Mechanical Computers" ] }, { "cell_type": "markdown", "id": "fe65568f-38cb-426f-b100-0acb8f57b44d", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- Antikythera mechanism\n", " - Used for predicting astronomical events\n", " - Considered the first known computer" ] }, { "cell_type": "markdown", "id": "b03d6096-c3f2-4bbd-8666-57b226b3d8e3", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "![antikythera](figures/antikythera_reconstruction.png)\n", "\n", "

\n", "Antikythera Mechanism
\n", "Source: \n", "Using computation to decode the first known computer, Edmunds & Feeth, 2011\n", "

" ] }, { "cell_type": "markdown", "id": "7e685625-1c7a-4bea-a5f4-941c45531dfc", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- Difference and analytical engine \n", " - Invented by Charles Babbage\n", " - Difference engine is a mechanical calculator designed to tabulate logarithms and trigonometric functions\n", " - Analytical engine is designed to be a general-purpose mechanical computer that was never built" ] }, { "cell_type": "markdown", "id": "f72ad3d4-02b6-4124-b9fc-562fb1130f2d", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Tide-predicting machine – invented by Lord Kelvin\n", "- Slide rule\n", "- Differential analyzer\n", "- ..." ] }, { "cell_type": "markdown", "id": "7834ee27-3233-4362-b86d-fd3535b38973", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "![tide_prediction](figures/tide_prediction_Kelvin.webp)\n", "\n", "

\n", "Tide Predicting Machine
\n", "Source: \n", "Lord Kelvin and his analog computer, Allison Marsh, 2024\n", "

" ] }, { "cell_type": "markdown", "id": "12cfe199-5c90-4e31-bc25-a34a38c1d2bd", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" } }, "source": [ "### Electronic Computers" ] }, { "cell_type": "markdown", "id": "3ecb6c6f-a0c3-488b-b43b-0592a3c91012", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- Earlier computers using _analog_ electric signals\n", " - Inflexible - need to be reprogrammed for solving a specific program" ] }, { "cell_type": "markdown", "id": "edcca5ad-b347-455b-aec2-c3b4c8836ca8", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Modern computers use _digital_ signals and operate on _binary_ data (0's and 1's)\n", "- Bit = **B**inary Dig**it**" ] }, { "cell_type": "markdown", "id": "ef2a7543-3d70-4c37-9e77-59735365c3c4", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "![analog_binary](figures/analog_binary.png)" ] }, { "cell_type": "markdown", "id": "508f09d7-310e-4828-a22e-e0d5e1ba822b", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Bits to Bytes conversion\n", " \n", "| | | |\n", "| ---: | :--- | :--- |\n", "| 8 | Bits | = 1 Byte |\n", "| 1024 | Bytes | = 1 Kilobyte (KB) |\n", "| 1024 | KB | = 1 Megabyte (MB) |\n", "| 1024 | MB | = 1 Gigabyte (MB) |\n", "| 1024 | GB | = 1 Terabyte (MB) |" ] }, { "cell_type": "markdown", "id": "634746c4-aa27-40e5-9240-f34f8210418b", "metadata": { "editable": true, "slideshow": { "slide_type": "notes" } }, "source": [ "- Electronic computers do not need precision mechanisms like mechanical computers\n", "- 1 KB = 1000 bytes is the SI convention; kibi, mebi, etc. were proposed but not commonly used" ] }, { "cell_type": "markdown", "id": "d6fa94aa-e6f6-4745-9a52-f24df6a6795b", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" } }, "source": [ "#### Digital Computers" ] }, { "attachments": {}, "cell_type": "markdown", "id": "90c758d1-3c3c-4d0f-86a5-0197f056c3de", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- Fixed program computers\n", " - Perform only a set of tasks (e.g., calculators)" ] }, { "cell_type": "markdown", "id": "e6ab1ace-116d-4542-8f16-944784aa9f7d", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Stored program computers\n", " - General-purpose devices like PCs, smartphones, and servers" ] }, { "cell_type": "markdown", "id": "6fcaddaf-9f17-41d9-b694-2da8fabb9b6c", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Modern general-purpose computers constitute:\n", " - Input and output devices: mouse, keyboard, printer, monitor\n", " - Memory: RAM, hard drive(s)\n", " - Processors: CPU(s) and GPU(s)" ] }, { "cell_type": "markdown", "id": "0c4f975d-9cc3-47b1-87f3-576da59adc87", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "![computer_architecture](figures/Von_Neumann_Architecture.svg)\n", "\n", "

\n", "The basic architecture of a modern computer
\n", "Source: \n", "Wikipedia.org, Kaphoot\n", "

" ] }, { "cell_type": "markdown", "id": "fdf96914-0978-4d16-bde2-435d994f5d87", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- Current CPUs consist billions of transistors that operate on digital signals\n", " - Transistor is the basic unit of computation" ] }, { "cell_type": "markdown", "id": "37aa8d72-2706-45eb-aa76-128b9064266c", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "![first_transistor](figures/Replica-of-first-transistor.jpg)\n", "\n", "

\n", "Replica of the first transistor
\n", "Source: \n", "Wikipedia.org, Ragesoss\n", "

" ] }, { "cell_type": "markdown", "id": "9e7658ec-74b4-4f11-b09a-f5cb88ec739b", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "![mosfet](figures/Lateral_mosfet.svg)\n", "\n", "

\n", "Metal-Oxide-Semiconductor Field Effect Transistor (MOSFET)
\n", "Source: \n", "Wikipedia.org, Cyril BUTTAY\n", "

" ] }, { "cell_type": "markdown", "id": "2a6b5226-f207-4bc0-b199-655eae06ebe4", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- The transistors form digital circuits that perform logic and arithmetic operations on binary data given specific instructions (also in binary format)\n", " - CPUs understand only binary instructions\n", " - These binary instructions are referred to as machine code or machine language which is specific to the CPU architecture" ] }, { "cell_type": "markdown", "id": "34a4ff41-b244-463e-9acc-3487051d0a33", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "![ryzen_cpu](figures/CPU-Front-and-Back.jpg)\n", "\n", "

\n", "Current CPUs pack ~10 billion transistors
\n", "Source: \n", "igorslab.de, Igor Wallossek, 2023\n", "

" ] }, { "cell_type": "markdown", "id": "9f909305-1b4a-40c3-a266-0daeffdf400d", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## Computer Programming" ] }, { "cell_type": "markdown", "id": "dff195f6-e5a2-4202-822f-827b53f4e190", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- A computer program (or simply program) is a sequence of instructions to the computer with the goal of automating a specific task\n", " - Examples: e-mail clients, word processors, browsers, engineering software" ] }, { "cell_type": "markdown", "id": "d4b4bd8a-0aff-4669-b129-c40528b14aba", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Computer programs are written in computer programming languages\n", "- Programming languages can be classified as:\n", " - Low, medium, and high-level languages" ] }, { "cell_type": "markdown", "id": "a478b12e-df7a-4aae-bf82-25ec15565b7d", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Low-level languages\n", " - Machine language, assembly language\n", " - Specific to the CPU architecture" ] }, { "cell_type": "markdown", "id": "562b4cde-c33d-4f4c-a3a9-7d0bca9ae880", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- “Medium-level” languages\n", " - Fortran, C, C++, Rust\n", " - Independent of the machine architecture; abstract away the architectural details\n", " - Human readable\n", " - Need a _compiler_ program to convert the _source code_ into executable _machine code_" ] }, { "cell_type": "markdown", "id": "156b3d12-c8c8-4034-8d24-8322c7978be7", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- High-level languages\n", " - Python, Matlab language, Mathematica language, Perl, Ruby, tkl\n", " - Human readable and independent of the machine architecture\n", " - Not compiled but instead use an _interpreter_ to process one instruction at a time" ] }, { "cell_type": "markdown", "id": "a35aae1b-e1f6-4791-aea4-569b33890571", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Programs written in compiled languages are typically faster than those written in interpreted languages" ] }, { "cell_type": "markdown", "id": "47db4ad6-9c00-4345-b0c0-91782be69b1c", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- Programming languages\n", " - General purpose languages \n", " - C, C++, Python\n", " - Applications such as browsers, games, communication tools\n", " - Scientific computing \n", " - Fortran, Matlab, Octave, Julia, R\n", " - Applications\n", " - Weather prediction\n", " - Computer-aided design (CAD)\n", " - Computer-aided engineering (CAE)\n", " - Simulation of solids and fluids\n", " - Data analysis and visualization\n", " - Image analysis\n", " - Signal processing\n", " - ..." ] }, { "cell_type": "markdown", "id": "347f8f7e-18c6-4c2e-915a-84dbd8a117bd", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- Programming paradigms\n", " - Procedural programming\n", " - Makes use of reusable procedures or subroutines that operate on given data to achieve specific tasks\n", " - Object-oriented programming\n", " - Uses objects to combine data and procedures (called methods) to do the same\n", " - ..." ] }, { "cell_type": "markdown", "id": "b2dd6537-bd4b-4acc-b77b-3ed195b8666c", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Fortran and C are strictly procedural programming languages\n", "- Python and C++ support both procedural and object-oriented programming paradigms" ] }, { "cell_type": "markdown", "id": "5c35f503-d3a8-463f-87e9-7fd3372e9b47", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" } }, "source": [ "### Programming vs Spoken Languages" ] }, { "cell_type": "markdown", "id": "ffbc1b9e-bfd8-4ebe-b8a3-083b1ac18cac", "metadata": { "editable": true, "slideshow": { "slide_type": "" } }, "source": [ "- Spoken languages\n", " - Differ in grammar and semantics (meaning of the words/phrases in a specific context)\n", " - May use different scripts and sounds" ] }, { "cell_type": "markdown", "id": "2f475f83-67f8-4a81-834c-874ccbd8d3df", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "- Programming languages\n", " - Differ in syntax (rules for providing instructions to the computer) \n", " - Semantics: usually have either a valid or invalid meaning for an expression" ] }, { "cell_type": "markdown", "id": "6014d524-078b-4680-9b00-b96a68b60f27", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" } }, "source": [ "| Spoken languages | Programming languages |\n", "| :---: | :---: |\n", "| Words | Keywords (typically plain English words) and mathematical operators |\n", "| Grammar | Syntax |\n", "| Communication between people | Communication with computers |\n", "| Flexible and evolve over time | Rigid; new syntax and features are added |" ] }, { "cell_type": "markdown", "id": "710120df", "metadata": {}, "source": [ "### Programming Languages" ] }, { "cell_type": "markdown", "id": "ba8a7310", "metadata": {}, "source": [ "- All programming languages have some common features. They define:\n", " - Keywords, syntax, and semantics of the language\n", " - Data types and data structures\n", " - How these can be operated on and interact with each other" ] }, { "cell_type": "markdown", "id": "2365044a", "metadata": {}, "source": [ "- Standard data types\n", " - Numbers (integers, real, and complex)\n", " - Characters \n", " - Boolean (True or False)" ] }, { "cell_type": "markdown", "id": "fd95505d", "metadata": {}, "source": [ "- Data structures – groupings of various data types\n", " - Lists, arrays, maps, strings\n", " - Trees, hashes, graphs\n", " - Objects\n", " - Combine data with procedures that operate on the stored data (referred to as methods)\n", " - A class defines, implements and enables instantiation of objects" ] }, { "cell_type": "markdown", "id": "7da6f0c8", "metadata": {}, "source": [ "#### C vs Python Programming Language" ] }, { "cell_type": "markdown", "id": "eb2e5b9a", "metadata": {}, "source": [ "- The C programming language is a _statically_ and _weakly_ typed language\n", " - Static typing - the data type must be specified before execution at the compile time\n", " - Weak typing - allows _type conversion_\n", " - E.g., a character or real number can be converted to the integer type during execution\n", " - Can lead to unpredicatable behavior\n", " - To be specific, C is relatively weakly typed compared to certain languages\n", " - Data types have fixed size in memory" ] }, { "cell_type": "markdown", "id": "1b269128", "metadata": {}, "source": [ "```C\n", "/* Simple C program */\n", "#include \n", "int main()\n", "{\n", "    float varPi = 3.14159;\n", "    printf(\"Hello World!\\n\"); \n", "    printf(\"The value of pi is %f\\n\", varPi);\n", "    return 0;\n", "}\n", "```" ] }, { "cell_type": "markdown", "id": "ebfd013f", "metadata": {}, "source": [ "- Python is _dynamically_ and _strongly_ typed language\n", " - _Dynamic typing_ - the data type is typically determined during execution\n", " - _Strong typing_ - type conversion is typically not done unless explicitly requested\n", " - All data types are objects\n", " - Memory automatically allocated for integers and can be arbitrarily large\n", " - Real numbers behave the same way as `double` data type in C" ] }, { "cell_type": "code", "execution_count": null, "id": "69300597", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n", "The value of pi is 3.14159\n" ] } ], "source": [ "\n", "# Simple Python program\n", "varPi = 3.14159\n", "print(\"Hello World!\")\n", "print(\"The value of pi is {}\".format(varPi))\n" ] }, { "cell_type": "markdown", "id": "4f1f1d9b", "metadata": {}, "source": [ "#### Python: Pros and Cons" ] }, { "cell_type": "markdown", "id": "ebda147f", "metadata": {}, "source": [ "- Advantages of Python over C (and other similar languages)\n", " - Simple syntax, highly readable, and easy to learn/use\n", " - Automatic memory management\n", " - Free and open source\n", " - Portable and extremely popular\n", " - Ranked No. 1 over C and C++ by [TIOBE index](https://www.tiobe.com/tiobe-index/) and [IEEE](https://spectrum.ieee.org/the-top-programming-languages-2023)" ] }, { "cell_type": "markdown", "id": "f31ee9d7", "metadata": {}, "source": [ "- Advantages of Python over C (and other similar languages)\n", "" ] }, { "cell_type": "markdown", "id": "8645e829", "metadata": {}, "source": [ "![Antigravity](https://imgs.xkcd.com/comics/python.png)" ] }, { "cell_type": "markdown", "id": "337f7460", "metadata": {}, "source": [ "- Disadvantages\n", " - Slow and less memory efficient compared to compiled languages like C, Fortran, and C++" ] }, { "cell_type": "markdown", "id": "d6c3c0f6", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Total
Energy (J)Time (ms)Mb
(c) C1.00(c) C1.00(c) Pascal1.00
(c) Rust1.03(c) Rust1.04(c) Go1.05
(c) C++1.34(c) C++1.56(c) C1.17
(c) Ada1.70(c) Ada1.85(c) Fortran1.24
(v) Java1.98(v) Java1.89(c) C++1.34
(c) Pascal2.14(c) Chapel2.14(c) Ada1.47
(c) Chapel2.18(c) Go2.83(c) Rust1.54
(v) Lisp2.27(c) Pascal3.02(v) Lisp1.92
(c) Ocaml2.40(c) Ocaml3.09(c) Haskell2.45
(c) Fortran2.52(v) C#3.14(i) PHP2.57
(c) Swift2.79(v) Lisp3.40(c) Swift2.71
(c) Haskell3.10(c) Haskell3.55(i) Python2.80
(v) C#3.14(c) Swift4.20(c) Ocaml2.82
(c) Go3.23(c) Fortran4.20(v) C#2.85
(i) Dart3.83(v) F#6.30(i) Hack3.34
(v) F#4.13(i) JavaScript6.52(v) Racket3.52
(i) JavaScript4.45(i) Dart6.67(i) Ruby3.97
(v) Racket7.91(v) Racket11.27(c) Chapel4.00
(i) TypeScript21.50(i) Hack26.99(v) F#4.25
(i) Hack24.02(i) PHP27.64(i) JavaScript4.59
(i) PHP29.30(v) Erlang36.71(i) TypeScript4.69
(v) Erlang42.23(i) Jruby43.44(v) Java6.01
(i) Lua45.98(i) TypeScript46.20(i) Perl6.62
(i) Jruby46.54(i) Ruby59.34(i) Lua6.72
(i) Ruby69.91(i) Perl65.79(v) Erlang7.20
(i) Python75.88(i) Python71.90(i) Dart8.64
(i) Perl79.58(i) Lua82.91(i) Jruby19.84
\n", "\n", "

\n", " Source: Ranking programming languages by energy efficiency, Pereira et al., 2021.\n", "

" ] }, { "cell_type": "markdown", "id": "44c3340b", "metadata": {}, "source": [ "#### Working with Python" ] }, { "cell_type": "markdown", "id": "152b00d8", "metadata": {}, "source": [ "- To learn and write programs in Python, we need:\n", " - A Python interpreter\n", " - An integrated development environment (IDE)\n", " - A software tool that helps:\n", " - Create and edit programs\n", " - With syntax highlighting and code autocompletion\n", " - _Debug_ programs to resolve unexpected/unintended behavior\n", " - A package manager\n", " - A program to obtain additional modules/packages as required" ] } ], "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.11.4" }, "scenes_data": { "active_scene": "Default Scene", "init_scene": "", "scenes": [ "Default Scene" ] } }, "nbformat": 4, "nbformat_minor": 5 }