Introduction to Math Functions in
Python
The real functions used to relate the angle of a right-angled triangle to the ratios of the two
sides’ lengths are called trigonometric functions. Trigonometric functions are used in the field
of science related to geometry, such as navigation, solid mechanics, celestial mechanics,
geodesy, etc. The standard module in python is the math module and is always available. The
math module consists of mathematical functions, and these functions can be imported using
import math. This module does not support complex data types. The Cmath module is used
to support complex data.
Trigonometric calculations can be performed in python by using built-in functions defined in
the math module. By using the math module in python, the number of lines in the code can be
reduced to a minimum. We can either import the math module or use the dot operator with a
math library to use trigonometric functions.
Different Math Functions in Python
All key mathematical functions are deeply described below:
1. Constants
In the case of a mathematical constant, the value for this constant is represented by an
unambiguous definition; these definitions, in some instances, are represented by means of
any special symbols or by many famous mathematicians’ names, or by any other popular
means. Constants occur within numerous areas of mathematics by means of constants such
as π and e happening in miscellaneous circumstances like number theory, geometry,
and calculus. The meaning of a constant to arise “naturally” and makes a constant “interesting”
is in due course material of need, and a number of mathematical constants are prominent
more on chronological grounds than by means of their fundamental mathematical interest.
The more well-liked constants comprise been studied all the way through the ages and
computed to a lot of decimal places.
Constants Description
pi returns 3.141592
E returns 0.718282
nan Not a number
inf infinite
Example :
import math
print( " CONSTANTS IN PYTHON ")
print(" PI value : " , [Link])
print(" E value : " , math.e)
print(" nan value : " , [Link])
print(" E value : " , [Link])
Output :
2. Logarithmic Functions
The inverse for exponentiation is called a logarithm. For any given number x, in order for
determining its respective logarithm value, the exponent of another fixed number with base b
is calculated. In a more straightforward case, the logarithm calculates or counts the numeral
occurrences of the same factor in repeated multiplication;
Ex: 1000 = 10 × 10 × 10 = 103, then the “logarithm to base 10” of 1000 is 3. The logarithm
of x to base b is denoted as logb (x ).
On the other hand, the exponent of a number means the number of times the number is being
used in a multiplication factor.
Ex : 82 = 8 × 8 = 64
In other words, the representation of 82 could be called “8 to the power 2” or simply as “8
squared” On the other hands, the exponent of a number means the number of times the
number is being used in a multiplication factor.
Function Description
exp(x) Returns e**x
expm1(x) Returns e**x – 1
log(x[, base]) x to the base logarithm is returned
log1p(x) Base1 logarithm of x value is returned
log2(x) Base2 logarithm of x value is returned
log10(x) Base10 logarithm of x value is returned
pow(x, y) Returns x raised to the power y
sqrt(x) Square root value for x is returned
Example:
import math
#variable declaration and assignation
Number_1 = 1
Number_2 = 2
Number_3 = 3
Number_4 = 4
# Applying exp() function
print(" EXPONENT VALUE ")
print(" Exponent value: " , [Link](Number_1))
print(" \n ")
# Applying Base1 logarithm function
print(" BASE1 LOGARITHM " )
print(" BASE1 LOGARITHM VALUE of 2 : ", math.log1p(Number_2))
print(" \n " )
# Applying Base2 logarithm function
print(" BASE2 LOGARITHM " )
print(" BASE2 LOGARITHM VALUE of 2 : ", math.log2(Number_2))
print(" \n " )
# Applying Base10 logarithm function
print(" BASE10 LOGARITHM " )
print(" BASE10 LOGARITHM VALUE of 2 : ", math.log10(Number_2))
print(" \n " )
# Applying x to power of Y
print(" X^Y" )
print(" X^Y Value : ", [Link](Number_3,Number_4))
print(" \n " )
# Applying square root determination
print(" SQUARE ROOT " )
print(" SQUARE ROOT of 4 : ", [Link](Number_4))
print(" \n " )
Output :
4. Numeric Functions
The numeric functions allow the calculation of all mathematical inceptions.
Constants Description
ceil(x) The smallest integer which is very much greater than or
equal to the x value is returned
copysign(x, y) Using the sign of y, the value for x is returned
fabs(x) absolute value for the x is returned
factorial(x) factorial value of x is returned
floor(x) the largest integer, which is very much less than or equal
to the x value, is returned
fmod(x, y) the remainder of dividing x by y value is returned
frexp(x) Returns the mantissa and exponent of x as the pair (m, e)
fsum(iterable) Returns an accurate floating-point sum of values in the
iterable
isfinite(x) if x is not an infinity or a Nan, then boolean value true is
returned
isinf(x) if x holds a positive or negative infinity, then true is
returned
isnan(x) Returns True if x is a NaN
gcd(x, y) for x and y value, the most greates common divisor value
is returned
the remainder(x, y) Find the remainder after dividing x by y.
Example :
import math
#variable declaration and assignation
Number_1 = 10.5
Number_2 = 20
Number_3 = -30
Number_4 = -40.24566
Number_5 = 50
Number_6 = 60.94556
Number_7 = 70
Number_8 = 80
# Applying Ceil() function
print( " CEIL : Smallest integer which is very much greater
than or equal to the x value is returned ")
print( " CEIL value : " , [Link](Number_1))
print( " \n " )
# Applying Copysign() function
print( " COPYSIGN : Smallest integer which is very much
greater than or equal to the x value is returned ")
Temp_var1 = [Link](Number_2,Number_3)
print(" VALUE AFTER COPY SIGN : ", Temp_var1)
print(" \n ")
# Applying fabs() function
print( " FABS : absolute value for the x is returned ")
print(" ABSOLUTE VALUE FOR 40.24566 : ",
[Link](Number_4))
print(" \n ")
# Applying Factorial() function
print(" FACTORIAL : factorial value of x is returned ")
print(" Factorial value for 50 : ",
[Link](Number_5))
print(" \n ")
# Applying Floor() function
print(" FLOOR : largest integer which is very much less than
or equal to the x value is returned " )
print(" Floor : ", [Link](Number_6))
print(" \n ")
# Applying Fmod() function
print(" FMOD : remainder of divinding x by y value is
returned ")
print(" Remainder : ", [Link](Number_6,Number_5))
print(" \n ")
# Applying Frexp() function
print( " FREXP : Returns the mantissa and exponent of x as
the pair (m, e) " )
print(" MANTISSA EXPONENT : ", [Link](Number_7))
print( " \n " )
# Applying isfinite() function
print(" isfinite : if x is not an infinity or a Nan then
boolean value true is returned ")
print(" Infinite or Nan (produces boolean output): ",
[Link](Number_8))
print(" \n ")
# Applying gcd() function
print(" GCD : for x and y value the most greates common
divisor value is returned ")
print(" Greatest Common divisor value : ",
[Link](Number_8,Number_7))
print(" \n ")
Output:
4. Trigonometric Functions
In mathematics, trigonometric functions are used to narrate a point of view of a right-angled
triangle in two side lengths. They have a very large set of applications in sciences that are
relative to geometry; such include solid mechanics, celestial mechanics, navigation, a lot of
others. These are considered simple periodic functions and widely known for representing the
periodic phenomena from the beginning to the end of Fourier analysis.
Function Description
sin(x) sine value of x in radians is determined
cos(x) cosine value of x in radians need to be determined
tan(x) tangent value of x in radians need to be determined
degrees(x) radian to degree conversion
radian(x) the degree to radian conversion
Example :
Output :
Introduction to Trigonometric
Functions in Python
The real functions used to relate the angle of a right-angled triangle to the ratios of the two
sides’ lengths are called trigonometric functions. Trigonometric functions are used in the field
of science related to geometry, such as navigation, solid mechanics, celestial mechanics,
geodesy, etc. The standard module in python is the math module and is always available. The
math module consists of mathematical functions, and these functions can be imported using
import math. This module does not support complex data types. The Cmath module is used
to support complex data.
Trigonometric calculations can be performed in python by using built-in functions defined in
the math module. By using the math module in python, the number of lines in the code can be
reduced to a minimum. We can either import the math module or use the dot operator with a
math library to use trigonometric functions.
To import the math module,
Import math
To reference the math library with the dot operator,
Math.function_name(parameter)
Note: All the trigonometric functions in python consider the input angle in terms of radians.
Two helper methods can be used to convert the values in radians to degrees and degrees to
radians. They are:
degrees(value)
The passed value in radians is converted to degrees by using this method.
radians(value)
The passed value in degrees is converted to radians by using this method.
Top Trigonometric Functions in Python
There are several trigonometric functions in python. They are:
1. sin(x) Function
The sine of the value which is passed as an argument to this function is returned. Here
x is passed as an argument. The input value x must be an angle expressed in terms
of radians.
Code:
import math
x = 1
print([Link](x));
Output:
2. cos(x) Function
The cosine of the value, which is passed as an argument to this function, is returned.
Here x is passed as an argument. The input value x must be an angle expressed in
terms of radians.
Code:
import math
x = 1
print([Link](x));
Output:
3. tan(x) Function
The tangent of the value, (i.e. sine/cosine), which is passed as an argument to this
function, is returned. Here x is passed as an argument. The input value x must be an
angle expressed in terms of radians.
Code:
import math
x = 1
print([Link](x));
Output:
4. asin(x) Function
The inverse of the sine is returned by using this function. The inverse of the sine is
also called the arc sine of the complex number. The input value x must be an angle
expressed in terms of radians. The input should be within the range -1 to 1. The output
is returned as a floating-point number.
Code:
import math
print([Link](1))
Output:
5. acos(x) Function
The inverse of the cosine is returned by using this function. The inverse of the cosine
is also called the arc cosine of the complex number. The input value x must be an
angle expressed in terms of radians. The input should be within the range -1 to 1. The
output is returned as a floating-point number.
Code:
import math
print([Link](1))
Output:
6. atan(x) Function
The inverse of the tangent is returned by using this function. The inverse of the cosine
is also called as arc tangent of the complex number. The input value x must be an angle
expressed in terms of radians. The input should be within the range -1 to 1. The output
is returned as a floating-point number.
Code:
import math
print([Link](1))
Output:
7. sin(x) Function
x is a complex number that is passed as an argument to the function. The hyperbolic sine of
the angle of the value passed as an argument is returned by this method.
Code:
import cmath
a = 1.0
b = 1.0
c = complex(a,b)
print("The value of hyperbolic sine is",[Link](c))
Output:
8. cosh(x) Function
x is a complex number that is passed as an argument to the function. The hyperbolic cosine
of the angle of the value passed as an argument is returned by this method.
Code:
import cmath
a = 1.0
b = 1.0
c = complex(a,b)
print("The value of hyperbolic sine is",[Link](c))
Output:
9. tanh(x) Function
x is a complex number that is passed as an argument to the function. The hyperbolic tangent
of the angle of the value passed as an argument is returned by this method.
Code:
import cmath
a = 1.0
b = 1.0
c = complex(a,b)
print("The value of hyperbolic sine is",[Link](c))
Output:
10. asinh(x) Function
The inverse of the hyperbolic sine of the complex number is returned.
Code:
import cmath
a = 1.0
b = 1.0
c = complex(a,b)
print("The value of inverse hyperbolic sine is",[Link](c))
Output:
11. acosh(x) Function
The inverse of the hyperbolic cosine of the complex number is returned.
Code:
import cmath
a = 1.0
b = 1.0
c = complex(a,b)
print("The value of inverse hyperbolic sine is",[Link](c))
Output:
12. atanh(x) Function
The inverse of the hyperbolic tangent of the complex number is returned.
Code:
import cmath
a = 1.0
b = 1.0
c = complex(a,b)
print("The value of inverse hyperbolic sine is",[Link](c))
Output:
Note: Next lab session will have mathematical equations in python
software: