Introduction to Computing#

Course Objective#

  • Introduce computer programming via Python as a tool for solving engineering problems

  • Straddles between a full-fledged introductory computer programming course and a numerical methods course

Computers#

  • What is a computer?

    • A device or machine capable of processing instructions and performing arithmetic or logical operations

      • Note: before the invention of electronic computers, computers referred to humans that carried out repetitive arithmetic calculations

Mechanical Computers#

  • Antikythera mechanism

    • Used for predicting astronomical events

    • Considered the first known computer

antikythera

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

  • Difference and analytical engine

    • Invented by Charles Babbage

    • Difference engine is a mechanical calculator designed to tabulate logarithms and trigonometric functions

    • Analytical engine is designed to be a general-purpose mechanical computer that was never built

  • Tide-predicting machine – invented by Lord Kelvin

  • Slide rule

  • Differential analyzer

tide_prediction

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

Electronic Computers#

  • Earlier computers using analog electric signals

    • Inflexible - need to be reprogrammed for solving a specific program

  • Modern computers use digital signals and operate on binary data (0’s and 1’s)

  • Bit = Binary Digit

analog_binary

  • Bits to Bytes conversion

8

Bits

= 1 Byte

1024

Bytes

= 1 Kilobyte (KB)

1024

KB

= 1 Megabyte (MB)

1024

MB

= 1 Gigabyte (MB)

1024

GB

= 1 Terabyte (MB)

  • Electronic computers do not need precision mechanisms like mechanical computers

  • 1 KB = 1000 bytes is the SI convention; kibi, mebi, etc. were proposed but not commonly used

Digital Computers#

  • Fixed program computers

    • Perform only a set of tasks (e.g., calculators)

  • Stored program computers

    • General-purpose devices like PCs, smartphones, and servers

  • Modern general-purpose computers constitute:

    • Input and output devices: mouse, keyboard, printer, monitor

    • Memory: RAM, hard drive(s)

    • Processors: CPU(s) and GPU(s)

computer_architecture

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

  • Current CPUs consist billions of transistors that operate on digital signals

    • Transistor is the basic unit of computation

first_transistor

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

mosfet

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

  • The transistors form digital circuits that perform logic and arithmetic operations on binary data given specific instructions (also in binary format)

    • CPUs understand only binary instructions

    • These binary instructions are referred to as machine code or machine language which is specific to the CPU architecture

ryzen_cpu

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

Computer Programming#

  • A computer program (or simply program) is a sequence of instructions to the computer with the goal of automating a specific task

    • Examples: e-mail clients, word processors, browsers, engineering software

  • Computer programs are written in computer programming languages

  • Programming languages can be classified as:

    • Low, medium, and high-level languages

  • Low-level languages

    • Machine language, assembly language

      • Specific to the CPU architecture

  • “Medium-level” languages

    • Fortran, C, C++, Rust

      • Independent of the machine architecture; abstract away the architectural details

      • Human readable

      • Need a compiler program to convert the source code into executable machine code

  • High-level languages

    • Python, Matlab language, Mathematica language, Perl, Ruby, tkl

      • Human readable and independent of the machine architecture

      • Not compiled but instead use an interpreter to process one instruction at a time

  • Programs written in compiled languages are typically faster than those written in interpreted languages

  • Programming languages

    • General purpose languages

      • C, C++, Python

      • Applications such as browsers, games, communication tools

    • Scientific computing

      • Fortran, Matlab, Octave, Julia, R

      • Applications

        • Weather prediction

        • Computer-aided design (CAD)

        • Computer-aided engineering (CAE)

        • Simulation of solids and fluids

        • Data analysis and visualization

        • Image analysis

        • Signal processing

  • Programming paradigms

    • Procedural programming

      • Makes use of reusable procedures or subroutines that operate on given data to achieve specific tasks

    • Object-oriented programming

      • Uses objects to combine data and procedures (called methods) to do the same

  • Fortran and C are strictly procedural programming languages

  • Python and C++ support both procedural and object-oriented programming paradigms

Programming vs Spoken Languages#

  • Spoken languages

    • Differ in grammar and semantics (meaning of the words/phrases in a specific context)

    • May use different scripts and sounds

  • Programming languages

    • Differ in syntax (rules for providing instructions to the computer)

    • Semantics: usually have either a valid or invalid meaning for an expression

Spoken languages

Programming languages

Words

Keywords (typically plain English words) and mathematical operators

Grammar

Syntax

Communication between people

Communication with computers

Flexible and evolve over time

Rigid; new syntax and features are added

Programming Languages#

  • All programming languages have some common features. They define:

    • Keywords, syntax, and semantics of the language

    • Data types and data structures

    • How these can be operated on and interact with each other

  • Standard data types

    • Numbers (integers, real, and complex)

    • Characters

    • Boolean (True or False)

  • Data structures – groupings of various data types

    • Lists, arrays, maps, strings

    • Trees, hashes, graphs

    • Objects

      • Combine data with procedures that operate on the stored data (referred to as methods)

      • A class defines, implements and enables instantiation of objects

C vs Python Programming Language#

  • The C programming language is a statically and weakly typed language

    • Static typing - the data type must be specified before execution at the compile time

    • Weak typing - allows type conversion

      • E.g., a character or real number can be converted to the integer type during execution

      • Can lead to unpredicatable behavior

      • To be specific, C is relatively weakly typed compared to certain languages

    • Data types have fixed size in memory

/* Simple C program */
#include <stdio.h>
int main()
{
    float varPi = 3.14159;
    printf("Hello World!\n"); 
    printf("The value of pi is %f\n", varPi);
    return 0;
}
  • Python is dynamically and strongly typed language

    • Dynamic typing - the data type is typically determined during execution

    • Strong typing - type conversion is typically not done unless explicitly requested

    • All data types are objects

      • Memory automatically allocated for integers and can be arbitrarily large

      • Real numbers behave the same way as double data type in C

#  Simple Python program
varPi = 3.14159
print("Hello World!")
print("The value of pi is {}".format(varPi))
Hello World!
The value of pi is 3.14159

Python: Pros and Cons#

  • Advantages of Python over C (and other similar languages)

    • Simple syntax, highly readable, and easy to learn/use

    • Automatic memory management

    • Free and open source

    • Portable and extremely popular

  • Advantages of Python over C (and other similar languages)

  • Extensive standard library and many extensions in the form of modules and packages
    • Numerical analysis - numpy and scipy
    • Plotting and visualization - matplotlib
    • Math and physics - sympy_ and sagemath
    • Image processing - scikit-image
    • Data science, statistics, and machine learning - pandas, scikit-learn, PyTorch
    • Astrophysics - astropy
    • Bioinformatics - biopython

Antigravity

  • Disadvantages

    • Slow and less memory efficient compared to compiled languages like C, Fortran, and C++

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

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

Working with Python#

  • To learn and write programs in Python, we need:

    • A Python interpreter

    • An integrated development environment (IDE)

      • A software tool that helps:

        • Create and edit programs

        • With syntax highlighting and code autocompletion

        • Debug programs to resolve unexpected/unintended behavior

    • A package manager

      • A program to obtain additional modules/packages as required