[go: up one dir, main page]

0% found this document useful (0 votes)
8 views21 pages

Arithmatic Operators

The document provides an overview of mathematical operations in Python, detailing the basic operators and the order of operations. It introduces built-in functions and the NumPy package, which is essential for advanced mathematical computations. The document also covers various NumPy functions for trigonometry, rounding, exponents, logarithms, and complex numbers, emphasizing their application in engineering computations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

Arithmatic Operators

The document provides an overview of mathematical operations in Python, detailing the basic operators and the order of operations. It introduces built-in functions and the NumPy package, which is essential for advanced mathematical computations. The document also covers various NumPy functions for trigonometry, rounding, exponents, logarithms, and complex numbers, emphasizing their application in engineering computations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CLASS 12:

MATHEMATICAL
OPERATIONS IN PYTHON
ENGR 102 – Introduction to Engineering
2 Mathematical Operations
Python includes the most basic mathematical
operations. Other math functions will be
accessed by importing the NumPy package

Webb ENGR 102


Basic Mathematical Operations
3

 Python itself includes only


seven mathematical operators
 Addition: +
 Subtraction: –

 Multiplication: *

 Division: /

 Modulus: %

 Exponentiation: **

 Floor division: //

Webb ENGR 102


Order of Operations
4

 Python order of operations:


1) () parentheses
2) ^ exponentiation
3) - negation
4) *, / multiplication, division
5) +, - addition, subtraction

 Expressions are evaluated left to right within each


level of the precedence hierarchy

Webb ENGR 102


Other Built-In Python Functions
5

 A few other math-related built-in Python functions:


 abs(x): absolute value
>>> a = abs(-1.76)
1.76
>>> z = abs(2 – 2j)
2.828

 len(x): returns the length of an object


>>> len([2, 4, 5, 3, 1])
5
>>> len('Hello, World!')
13
Webb ENGR 102
Other Built-In Python Functions
6

 A few other math-related built-in Python functions:


 max(x): maximum value in a sequence
>>> x_max = max([2, 4, 5, 3, 1])

 min(x): minimum value in a sequence


>>> x_min = min([2, 4, 5, 3, 1])

 type(x): returns the type of an object


>>> type([2, 4, 5, 3, 1])

list

>>> type('Hello, World!')

str

Webb ENGR 102


7 NumPy
Here we will introduce the concept of packages
and will look specifically at the package we will
use most for mathematical operations, NumPy.

Webb ENGR 102


Packages
8

 Python packages
 Libraries consisting of multiple modules, or individual Python files
 Modules within a package define
 Data types
 Functions
 Must install a package before we can use it
 Anaconda distribution includes all the packages we will need
 Must import a package in our code before we can use it
 Use the import function

 Packages available for


 Array processing and mathematics
 Plotting
 Data analysis
 GUI development
 Much, much more …

Webb ENGR 102


NumPy
9

 We will use the NumPy (Numerical Python)


package extensively
 Fundamental data type:
 Multi-dimensional array object – ndarray
 Useful for engineering computation
 Many built-in functions
 Mathematical operations, e.g.:
 Trigonometric functions
 Exponents and logarithms
 Complex number operations
 Array creation and manipulation routines
 Polynomial creation, manipulation, fitting, etc.
 Much more …

Webb ENGR 102


Using NumPy
10

 To use NumPy functions and data types, we must first


import it:
>>> import numpy as np

 We can assign it a shortened name, np, to keep our code


clean
 To call functions defined in NumPy, precede the
function name with np.
>>> N = np.log2(1024)
>>> x = 3*np.sin(np.pi/2)

 We'll now introduce a small sample of NumPy functions


Webb ENGR 102
NumPy – Trigonometric Functions
11

 sin(x), cos(x), tan(x)


 Input in radians
>>> y = np.sin(x)
>>> y = np.sin(np.radians(x))

 arcsin(x), arccos(x), arctan(x)


 Inversetrig functions
 Output in radians

>>> theta = np.arcsin(0.6)

Webb ENGR 102


NumPy – Trigonometric Functions
12

 arctan2(x) – quadrant-aware inverse tangent


 Accounts for the difference between, e.g. , 45° and 225°
 Output in radians

>>> phi = np.arctan2(-4, 3)


>>> phi_deg = np.degrees(np.arctan2(-4, 3))

 degrees(x) – converts from radians to degrees


>>> ang45 = np.degrees(np.pi/4)

 radians(x) – converts from degrees to radians


>>> angPi = np.radians(180)
Webb ENGR 102
NumPy – Rounding
13

 around(x, decimals=0) – round to the specified


number of decimals (default, 0)
>>> xint = np.around(1.6)
2.0
>>> xrnd = np.around(np.pi, decimals=2)
3.14
 Numbers exactly halfway between rounded decimal values round
to the nearest even value
>>> x0 = np.around(2.5)
2.0
>>> x1 = np.around(1.65, decimals=1)
1.6
>>> y1 = np.around(1.55, decimals=1)
1.6
Webb ENGR 102
NumPy – Rounding
14

 fix(x) – round to the nearest integer toward zero


>>> xfix = np.fix(1.2)
1.0

>>> yfix = np.fix(-2.8)


-2.0

 floor(x) – round to the nearest integer toward negative infinity


>>> xfloor = np.floor(1.6)
1.0

>>> xflr = np.floor(-1.2)


-2.0

 ceil(x) – round to the nearest integer toward positive infinity


>>> xceil = np.fix(1.2)
2.0

>>> yceil = np.fix(-2.8)


-2.0

Webb ENGR 102


NumPy – Exponents
15

 exp(x) – exponential: 𝑒𝑒 𝑥𝑥
>>> y = np.exp(4.1)
60.3403
>>> e = np.exp(1)
2.71828

 exp2(x) – power of 2: 2𝑥𝑥


>>> x = np.exp2(3)
8.0
>>> N = np.exp2(10)
1024.0

Webb ENGR 102


NumPy – Logarithms
16

 log(x) – natural log


>>> y = np.log(5)
1.609

 log10(x) – base-10 logarithm


>>> x = np.log10(1e4)
4.0

 log2(x) – base-2 logarithm


>>> x = np.log2(256)
8.0

Webb ENGR 102


NumPy – Complex Numbers
17

 real(z) – real part of a complex number


>>> x = np.real(3 + 5j)
3.0

 imag(z) – imaginary part of a complex number


>>> y = np.imag(3 + 5j)
5.0

 angle(z) – angle of complex number in radians


>>> x = np.degrees(np.angle(2 + 2j))
45

 conj(z) – complex conjugate


>>> x = np.conj(3 + 5j)
3 – 5j

Webb ENGR 102


NumPy – Miscellaneous
18

 sqrt(x) – square root


>>> y = np.sqrt(2)
1.4142

 sum(x) – sum of all elements in a sequence


>>> total = np.sum([2, 4, 5, 3, 1])
15

 sign(x) – returns: -1 if x < 0, 0 if x == 0, 1 if x > 0


>>> np.sign([-12, 4, 6, 0, -3])
array([-1, 1, 1, 0, -1])

Webb ENGR 102


NumPy – Element-Wise Operations
19

 Numpy functions operate element-by-element on


array (or other sequence) inputs
 Return array outputs (more later)
>>> np.log10([1e4, 0.001, 10, 1e-6])
array([ 4., -3., 1., -6.])

>>> np.sqrt([4, 9, 25, 1e4])


array([ 2., 3., 5., 100.])

 Eliminatesthe need to explicitly perform the operation


on each element in an array
Webb ENGR 102
Built-In Constants
20

 Some built-in Python and Numpy constants:


 𝜋𝜋: np.pi

 Imaginary unit ( −1): j

 Infinity
(∞): inf
 Not-a-number: NaN or nan
 Both inf and nan often result from algorithmic errors

Webb ENGR 102


Math in Python and NumPy
21

 Use Python and NumPy to calculate each of


the following expressions
−0.4𝜋𝜋
 𝑒𝑒 1−0.42 ⋅ 100
Exercise

15
− ln 100

15
𝜋𝜋2 +ln2 100

12
 2 1 − 𝑒𝑒 −0.1⋅8 cos 2𝜋𝜋 ⋅ 12 ⋅ 8
3

Webb ENGR 102

You might also like