NumPy Mathematical Functions

Learn via video courses
Topics Covered

Overview

Numerous mathematical operations can be carried out using the many mathematical functions contained in Numpy. Trigonometric, arithmetic, and complex number-handling functions are among the mathematical operations. Let's talk about the mathematical functions that one might use.

Introduction

Although computation is a significant element in mathematical problem-solving, mathematics is a fundamental intellectual tool in computing.

In mathematics, a function is an expression, rule, or law that establishes the relationship between an independent variable and a dependent variable (the dependent variable).

Mathematics is used in computer science in a variety of ways, including discrete computational sciences, data science, machine learning, etc. Algorithms, which are at the heart of computer science, primarily rely on mathematics as well.

The essential Python library for numerical computing is called NumPy.

Numerous mathematical operations can be carried out using the many mathematical functions contained in Numpy. Trigonometric, arithmetic, and complex number handling functions are among the mathematical operations. Let's talk about the functions of mathematics.

Creating an Array Within a Specific Range

Almost everything in NumPy deals with the use of vectors and arrays.

Let's first look at how to create a NumPy array within a specified range.

We can use many functions in NumPy to do this, but we will discuss only the two important and popular functions to do so.

  • numpy.arange() One of the array-creation methods in NumPy that uses numerical ranges are called arange(). It returns the reference to an instance of ndarray that has values that are uniformly spaced out.

    Syntax: numpy.arange([start, ]stop, [step, ], dtype=None)

    Let's take a look at it.

    Output:

    We have successfully created a NumPy that ranges from 3 to 10 without individually providing the input and statically pre-defining it.

  • numpy.linspace() You can create equally spaced samples for the x-axis with the linspace function. For instance, using the numpy.linspace function, we can quickly create samples for the x-axis if we want to plot a mathematical function.

    Syntax: numpy.linspace(start, stop, num)

    Let's see how to do this.

    Output:

    Here we created an array that contains 7 elements that range from 3 to 10 and are linearly spaced, which means the distance between two consecutive numbers is the same.

Arithmetic Functions in Numpy

Numerous common arithmetic procedures are available in Python. The standard addition, subtraction, multiplication, and division operations are aided by these operations. For executing arithmetic operations, NumPy has dedicated functions. These NumPy arithmetic operations and functions will be discussed here.

  • numpy.add() This mathematical procedure assists the user in computing added sum of arguments element-wise.

    Output:

  • numpy.subtract() This mathematical procedure assists the user to subtract the arguments, element-wise.

    Output:

  • numpy.multiply() This mathematical procedure assists the user to multiply the arguments, element-wise.

    Output:

  • numpy.divide() This mathematical procedure assists the user to divide the arguments, element-wise.

    Output:

  • numpy.reciprocal This mathematical procedure returns the reciprocal of the arguments, element-wise.

    Output:

There are a lot of other arithmetic functions available in NumPy.

Here is a list of some of them.

FunctionDescription
add(x1, x2)Add arguments element-wise.
subtract(x1, x2)Subtract arguments, element-wise.
multiply(x1, x2)Multiply arguments element-wise.
divide(x1, x2)Divides arguments element-wise.
reciprocal(x)Return the reciprocal of the argument, element-wise.
positive(x)Numerical positive, element-wise.
negative(x)Numerical negative, element-wise.
power(x1, x2)First array elements raised to powers from second array, element-wise.
true_divide(x1, x2)Divide arguments element-wise.
floor_divide(x1, x2)Return the largest integer smaller or equal to the division of the inputs.
mod(x1, x2)Returns the element-wise remainder of division.
modf(x1, x2)Return the fractional and integral parts of an array, element-wise.
remainder(x1, x2)Returns the element-wise remainder of division.
divmod(x1, x2)Return element-wise quotient and remainder simultaneously.

Trigonometric Functions in Numpy

For a given angle in radians, the usual trigonometric functions in NumPy return trigonometric ratios.

  • numpy.sin() This mathematical procedure assists the user in computing the trigonometric sine for all x. (being the array elements).

    Output:

  • numpy.cos() This mathematical procedure assists the user in computing the trigonometric cosine for all x. (being the array elements).

    Output:

  • numpy.tan() This mathematical procedure assists the user in computing the trigonometric tangent for all x. (being the array elements).

    Output:

  • numpy.deg2rad() This mathematical procedure assists the user in converting the value in the degree to radians.

    Output:

There are a lot of other trigonometric functions available in NumPy.

Here is a list of some of them.

FunctionDescription
sin(x)Trigonometric sine, element-wise.
cos(x)Trigonometric cosine, element-wise.
tan(x)Trigonometric tangent, element-wise.
arcsin(x)Inverse sine, element-wise.
arccos(x)Inverse cosine, element-wise.
arctan(x)Inverse tangent, element-wise.
hypot(x1, x2)Given the two sides of a right triangle, return its hypotenuse.
degrees(x)Convert angles from radians to degrees.
radians(x)Convert angles from degrees to radians.
rad2deg(x)Convert angles from radians to degrees.

Different Rounding Functions in Numpy

To truncate the value of a decimal float number rounded to a specific precision of decimal numbers, the NumPy package offers several routines. Let's talk about how rounding functions work.

  • numpy.around() The user can equally round array elements to the specified number of decimals with the aid of this mathematical function.

    Output:

  • numpy.round() This mathematical function converts the element to the specified number of decimals.

    Output:

  • numpy.ceil() This mathematical function returns the ceiling of the input, element-wise.

    Output:

  • numpy.floor() This mathematical function returns the floor value of the input, element-wise.

    Output:

There are a lot of other rounding functions available in NumPy.

Here is a list of some of them.

FunctionDescription
around(x)Evenly round to the given number of decimals.
round(x)Round an array to the given number of decimals.
rint(x)Round elements of the array to the nearest integer.
fix(x)Round to the nearest integer towards zero.
trunc(x)Return the truncated value of the input, element-wise.

Hyperbolic Functions in Numpy

We will go over how to create a Python program to output values of the numpy.sinh(), numpy.cosh(), and numpy.tanh() library functions for hyperbolic functions.

  • numpy.sinh() This mathematical function computes the hyperbolic sine values of the arguments, element-wise.
    Output:
  • numpy.cosh() This mathematical function computes the hyperbolic cosine values of the arguments, element-wise.
    Output:
  • numpy.tanh() This mathematical function computes the hyperbolic tangent values of the arguments, element-wise.
    Output:

There are some other hyperbolic functions available in NumPy.

Here is a list of some of them.

FunctionDescription
arcsinh(x)Inverse hyperbolic sine element-wise.
arccosh(x)Inverse hyperbolic cosine element-wise.
arctanh(x)Inverse hyperbolic tangent element-wise.

Complex Number Functions in Numpy

We can work with both actual and imaginary numbers in Python.

We only append j to the end to signify a complex number.

  • numpy.angle() This mathematical function returns the angle of the complex argument.

    Output:

  • numpy.isreal() This mathematical function returns a boolean output stating whether the given argument is a real number or not.

    Output:

  • numpy.conjugate() This mathematical function returns the complex conjugate of the complex argument, element-wise.

    Output:

There are some other hyperbolic functions available in NumPy.

Here is a list of some of them.

FunctionDescription
real(x)Return the real part of the complex argument.
imag(x)Return the imaginary part of the complex argument.
conj(x)Return the complex conjugate, element-wise.

Other Important Functions in Numpy

There are some other important functions in NumPy used for mathematical computations.

  • numpy.i0(x) This mathematical function returns the modified Bessel function of the first kind, order 0.

    The Bessel function or the cylinder function is a set of mathematical functions used to help solve problems in physics involving the flow of heat or electricity in a solid cylinder, the propagation of electromagnetic waves along wires, the diffraction of light, the motions of fluids, and the deformations of elastic bodies. Using these principles along with the power of computation allows one to achieve results faster and easier.

    You can refer to more on Bessel Function here.

    Output:

  • numpy.sinc(x) The normalized sinc function is the output of this mathematical operation. sinc is an abbreviation for sine cardinals. The Lanczos resampling filter, interpolation, and anti-aliasing, signal processing are some of the tasks for which the sinc function is used.

    Output:

Conclusion

Let's see what we have learned here today.

  • Numerous mathematical operations can be carried out using the many mathematical functions contained in Numpy.

  • How NumPy uses mathematical functions to ease the difficulty in computation.

  • Various Arithmetic, Trigonometric, hyperbolic, and complex number functions can be used in NumPy to reduce effort and increase computation efficiency.

  • How the various NumPy Rounding functions work.

  • Some important mathematical special functions in NumPy like i0 and sinc and how to make use of them.