E-learning Python for Ocean Mapping (ePOM) project.
Complementary slides to the Scientific Computing module (part of the Introduction to Ocean Data Science training).
More details at https://www.hydroffice.org/epom
2. WHAT IS SCIENTIFIC COMPUTING?
• Implementation of methods to solve problems with computers
• The problems come from many fields → Interdisciplinary
(source: wikiversity.org)
UNDERWATER
ACOUSTICS
SCIENTIFIC
COMPUTING
Acoustic
Ray Tracing
Z
Solution Method
3. SCIENTIFIC COMPUTING WITH PYTHON
• Efficient storage/manipulation of N-dimensional numerical arrays
• NumPy also provides:
• Linear algebra
• Fourier transform
• Functions to create random numbers
• ...
5. PYTHON LISTS VS NUMPY ARRAYS
• A Python int is not just an integer:
(VanderPlas, Python Data Science Handbook, 2017)
language: C
struct _longobject {
long ob_refcnt;
PyTypeObject *ob_type;
size_t ob_size;
long ob_digit[1];
};
Reference count: used to handle memory allocation
Object type: used to store the type of the variable
Object size: used to specify the size of next member
Actual integer value!!!
language: Python
x = 8
6. PYTHON LISTS VS NUMPY ARRAYS
• A Python int is not just an integer:
language: Python
x = 8
PyObject Info
Value: 8
7. PYTHON LISTS VS NUMPY ARRAYS
• A Python int is not just an integer:
language: Python
x = 8
PyObject Info
Value: 8
• A Python list is more than just a list of values:
language: Python
a = [8, 3.14, "hi"]
PyObject Info
Length
Items
PyObject Info
Value: 3.14
0x88808
0x88810
0x88818
PyObject Info
Value: "hi"
PyObject Info
Value: 8
8. PYTHON LISTS VS NUMPY ARRAYS
• A NumPy array has a fixed type:
language: Python
import numpy as np
b = np.array([8, 4, 1])
8
4
1
PyObject Info
Data
Data Type
Dimensions
Strides
Data Type Description
int8 Integer (-128 to 127)
int16 Integer (-32768 to 32767)
int32 Integer (-2147483648 to 2147483647)
int64 Integer (-9223372036854775808 to 9223372036854775807)
uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
Data Type Description
intp Integer used for indexing
bool Boolean (True or False)
float16 Half precision float
float32 Single precision float
float64 Double precision float
complex64 Complex number, represented by two float32
complex128 Complex number, represented by two float64
... …
9. PYTHON LISTS VS NUMPY ARRAYS
• A NumPy array may have more than 1 dimension:
language: Python
import numpy as np
a = np.random.rand(3)
b = np.random.rand(3, 5)
c = np.random.rand(3, 5, 4)
d = ...
a
0
1
2
b c
10. • NumPy has a huge number of functionalities!
• Consult the Numpy Reference Manual.
• Find your favorite publicly-available cheat sheet → For instance: