Trending October 2023 # Frequently Used Mathematical Functions In Numpy # Suggested November 2023 # Top 12 Popular | Nhunghuounewzealand.com

Trending October 2023 # Frequently Used Mathematical Functions In Numpy # Suggested November 2023 # Top 12 Popular

You are reading the article Frequently Used Mathematical Functions In Numpy updated in October 2023 on the website Nhunghuounewzealand.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Frequently Used Mathematical Functions In Numpy

Overview of NumPy Functions

Numpy is a python package used for scientific computing. So certainly, it supports a vast variety of functions used for computation. The various functions supported by numpy are mathematical, financial, universal, windows, and logical functions. Universal functions are used for array broadcasting, typecasting, and several other standard features. While windows functions are used in signal processing. We will be learning mathematical functions in detail in this article.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Mathematical Functions in NumPy

Numpy is written purely in C language. Hence, it’s mathematical functions are closely associated with functions present is math.h library in C.

1. Arithmetic Functions

Description

add(arr1, arr2,..) Add arrays element wise

reciprocal(arr) Returns reciprocal of elements of the argument array

negative(arr) Returns numerical negative of elements of an array

multiply(arr1,arr2,…) Multiply arrays element wise

divide(arr1,arr2) Divide arrays element wise

power(arr1,arr2) Return the first array with its each of its elements raised to the power of elements in the second array (element wise)

subtract(arr1,arr2,…) Subtract arrays element wise

true_divide(arr1,arr2) Returns true_divide of an array element wise

floor_divide(arr1,arr2) Returns floor after dividing an array element wise

float_power(arr1,arr2) Return the first array with its each of its elements raised to the power of elements in the second array (elementwise)

fmod(arr1,arr2) Returns floor of the remainder after division elementwise

mod(arr1,arr2) Returns remainder after division elementwise

remainder(arr1,arr2) Returns remainder after division elementwise

divmod(arr1,arr2) Returns remainder and quotient after division elementwise

The above-mentioned operations can be performed in the following examples:

Example #1

In the given code snippet, we try to do some basic operations on the arguments, array a and array b.

Code:

import numpy as np a = np.array([10,20,30]) b= np.array([1,2,3]) print("addition of a and b :",np.add(a,b)) print("multiplication of a and b :",np.multiply(a,b)) print("subtraction of a and b :",np.subtract(a,b)) print("a raised to b is:",np.power(a,b))

Output:

Example #2

In this code snippet, we try to perform division and related operations on the arguments, array a and array b. We can notice the difference between mod, remainder, divmod and simple division.

Code:

import numpy as np a = np.array([10,20,30]) b= np.array([2,3,4]) print("division of a and b :",np.divide(a,b)) print("true division of a  :",np.true_divide(a,b)) print("floor_division of a and b :",np.floor_divide(a,b)) print("float_power of a raised to b :",np.float_power(a,b)) print("fmod of a and b :",np.fmod(a,b)) print("mod of a and b :",np.mod(a,b)) print("quotient and remainder of a and b :",np.divmod(a,b)) print("remainders when a/b :",np.remainder(a,b))

Output:

2. Trigonometric Functions

Function Description

sin(arr) Returns trigonometric sine element wise

cos(arr) Returns trigonometric cos element wise

tan(arr) Returns trigonometric tan element wise

arcsin(arr) Returns trigonometric inverse sine element wise

arccos(arr) Returns trigonometric inverse cosine element wise

arctan(arr) Returns trigonometric inverse tan element wise

hypot(a,b) Returns hypotenuse of a right triangle with perpendicular and base as arguments

degrees(arr)

rad2deg(arr)

Covert input angles from radians to degrees

radians(arr)

deg2rad(arr)

Covert input angles from degrees to radians

Here is an example of how to use trigonometric functions.

Example

Code:

import numpy as np sin_angles = np.sin(angles) cosine_angles = np.cos(angles) tan_angles = np.tan(angles) rad2degree = np.degrees(angles) print("sin of angles:",sin_angles) print("cosine of angles:",cosine_angles) print("tan of angles:",tan_angles) print("angles in radians",rad2degree)

3. Logarithmic and Exponential Functions

Function Description

exp(arr) Returns exponential of an input array element wise

expm1(arr) Returns exponential exp(x)-1 of an input array element wise

exp2(arr) Returns exponential 2**x of all elements in an array

log(arr) Returns natural log of an input array element wise

log10(arr) Returns log base 10 of an input array element wise

log2(arr) Returns log base 2 of an input array element wise

logaddexp(arr) Returns logarithm of the sum of exponentiations of all inputs

logaddexp2(arr) Returns logarithm of the sum of exponentiations of the inputs in base 2

Here is an example of using logarithmic functions:

Example

Code:

import numpy as np a = np.array([1,2,3,4,5]) a_log = np.log(a) a_exp = np.exp(a) print("log of input array a is:",a_log) print("exponent of input array a is:",a_exp)

Output:

4. Rounding Functions

Function Description

around(arr,decimal) Rounds the elements of an input array upto given decimal places

round_(arr,decimal) Rounds the elements of an input array upto given decimal places

rint(arr) Round the elements of an input array to the nearest integer towards zero

fix(arr) Round the elements of an input array to the nearest integer towards zero

floor(arr) Returns floor of input array element wise

ceil(arr) Returns ceiling of input array element wise

trunc(arr) Return the truncated value of an input array element wise

Example of using rounding functions with numpy arrays:

Example #1

Code:

import numpy as np a = np.array([1.23,4.165,3.8245]) rounded_a = np.round_(a,2) print(rounded_a)

Output:

Example #2

Code:

floor_a = np.floor(a) print(floor_a)

5. Miscellaneous Functions

Function Description

sqrt(arr) Returns the square root of an input array element wise

cbrt(arr) Returns cube root of an input array element wise

absolute(arr) Returns absolute value each element in an input array

maximum(arr1,arr2,…) Returns element wise maximum of the input arrays

minimum(arr1,arr2,…) Returns element wise minimum of the input arrays

interp(arr, xp, fp) Calculates one-dimensional linear interpolation

convolve(arr, v) Returns linear convolution of two one-dimensional sequences

clip(arr, arr_min, arr_max) Limits the values in an input array

Some examples using the above functions.

Example #1 – Finding the Maxima

Code:

import numpy as np a = [1,2,3] b = [3,1,2] maximum_elementwise = np.maximum(a,b) print("maxima are:",maximum_elementwise)

Output:

Example #2 – Clipping an array between max_limit and min_limit

Code:

import numpy as np a = [1,2,3] b = [3,1,2] limiting_a = np.clip(a,0,2) print("limiting a between 0 and 2:",limiting_a)

Output:

Conclusion

In this post, we have tried to cover the most frequently used mathematical functions in numpy. However, there are some other functions like complex functions which helps in working with real and imaginary parts of a number, floating-point routines which helps in performing decimal operations and hyperbolic functions which helps in calculating hyperbolic sine, tan, cos, etc.

Recommended Articles

This is a guide to NumPy Functions. Here we discuss the basic concept and different mathematical functions in NumPy. You may also look at the following articles to learn more –

You're reading Frequently Used Mathematical Functions In Numpy

Update the detailed information about Frequently Used Mathematical Functions In Numpy on the Nhunghuounewzealand.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!