Mathematical Functions in Python

From Sustainability Methods
Revision as of 05:45, 30 June 2023 by Milan (talk | contribs) (This article provides an overview over mathematical functions in Pyhton with examples.)

THIS ARTICLE IS STILL IN EDITING MODE

Introduction

Python has a built-in module math which defines various mathematical functions. In addition to the 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-D arrays

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 values of a 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 functions 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

Exponential Functions

math numpy Description
math.exp(x) np.exp() The math.exp() method returns E raised to the power of x (Ex).‘E’ is the base of the natural system of logarithms and x is the number passed to it, return value: Float

Examples

math.exp(66) or np.exp(66)

4.607186634331292e+28

DELETE?

math.exp(66) or np.exp(66)

214643579785916.06

Log Functions

math numpy Description
math.log(x, base) np.log(x) The natural logarithm log is the inverse of the exponential function. The math.log() method returns the natural logarithm of a number, or the logarithm of number to base. Base value is optional and the default is e.

Examples

# get the log value of an array with base 2

arr = np.array([1, 4, 6])

arr1 = np.log2(arr)

print(arr1)

​

# Output :

# [0.        2.        2.5849625]
# get the log value of an array with base 10

arr = np.array([1, 4, 6])

arr1 = np.log10(arr)

print(arr1)

​

# Output :

# [0.         0.60205999 0.77815125]

Trigonometric Functions

math numpy Description
math.cos(x) np.cos(x) Return the cosine of x radians.
math.sin(x) np.sin(x) Return the sine of x radians.
math.tan(x) np.tan(x) Return the tangent of x radians.

Examples

We use math.pi or np.pi methods for defining pi(π) in Python.

  • math
math.cos(math.pi)

-1.0
math.sin(math.pi/2)

1.0
  • numpy
np.cos(math.pi)

-1.0
np.sin(math.pi/2)

1.0

References

1.https://docs.python.org/3/library/math.html

2.https://numpy.org/doc/stable/reference/routines.math.html#extrema-finding

The author of this entry is XX. Edited by Milan Maushart