Mathematical Functions in Python

From Sustainability Methods
Revision as of 08:03, 28 February 2023 by Milan (talk | contribs) (Created page with "THIS ARTICLE IS STILL IN EDITING MODE == '''Introduction''' == Python has a built-in module math which defines various mathematical functions. In addition to math module, ther...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

THIS ARTICLE IS STILL IN EDITING MODE

Introduction

Python has a built-in module math which defines various mathematical functions. In addition to math module, there is a fundamental package for scientific computing in Python named NumPy (Numerical Python). It is an open source Python library and contains multidimensional array and matrix data structures. In this section, examples of both methods will be presented.

For using math, we must first import this module:

import math

For using `NumPy`, we must first install it. There is no prerequisite for installing NumPy except Python itself. We can use `pip` or `conda` for this purpose:

'pip'

pip install numpy

'conda'

conda install numpy

We must import `numpy` to access it and its functions. We also shorten the imported name to `np` for better readability of code using NumPy:

import numpy as np

Mathematical Functions

Basic Functions

Some basic functions for my fellow students. Some functions need the module `math`. Please check out the introduction at the top. :)

math Description
math.ceil(x) Rounds the number x up to the next integer
math.floor(x) Rounds the number x down to the next integer
math.com (n,k) Binomial Coefficient: number of possible k choose n without order
math,factorial(n) Returns n factorial if n >= 0
abs(x) Returns the absolute value of x
math.ceil(5.3)

6
math.floor(173.123)

173
math.factorial(4)

24
abs(-17.2)

17.2

Power Functions

built-in function math numpy Description
pow(x, y, mod) math.pow(x, y, mod) np.power(x1, x2,...) x to the power of y. x1, x2 array_like

Examples

pow (2,2)=4
##Square


pow (2,3)=8

##=Cube
pow (3,4, mode: 10)

The value of (3**4) % 10 is = 1


##The exponents of two 1-Darray

ar1 = [3, 5, 7, 2, 4]

ar2 = [6, 2, 5, 3, 5]

arr = np.power(ar1,ar2)

arr: array([  729,    25, 16807,     8,  1024], dtype=int32)


##The power vales of 2-D array

ar1 = np.array([[3,4,3],[6,7,5]])

ar2 =np.array([[4,2,7],[4,2,1]])

arr = np.power(ar1,ar2)

arr: array([[  81,   16, 2187],

       [1296,   49,    5]], dtype=int32)

Root Functions

To implement root funtions in python we can use the built-in power function. Alternatively we can use 'math' or 'numpy'.

built-in power function math numpy Description
x**(1/2) math.sqrt(x) np.sqrt(x) Returns the square root of x
x**(1/3) math.powe(x, 1/3) np.cbrt(x) Returns the cube root of x
x**(1/n) math.pow(x, 1/n) np.power(x, 1/n) Returns the nth root of x

Examples

  • 'built-in function'
0** (1/2)

0.0
9** (1/3)

2.0
120** (1/10)

1.6140542384620635
  • 'math'
math.sqrt(0)

0.0
math.pow(9, 1/3)

2.0
math.pow(120, 1/10)

1.6140542384620635
  • 'numpy'
np.sqrt(0)

0.0
np.cbrt(9)

2.0
np.power(120, 1/10)

1.6140542384620635