What are universal functions for n-dimensional arrays in Python?


In this article, we will explain universal functions in python and how they are applied to n-dimensional arrays.

A universal function (or ufunc) is a function that operates on ndarrays element by element, providing array broadcasting, type casting, and a variety of other standard features. A ufunc, in other words, is a "vectorized" wrapper around a function that accepts a fixed number of scalar inputs and returns a fixed number of scalar outputs.

They are simple mathematical functions in the NumPy library. Numpy includes a number of universal functions that support a wide range of operations.

These functions contain standard trigonometric functions, arithmetic operations, complex number handling, statistical functions, and so forth. The following are some of the characteristics of universal functions −

  • These functions work with ndarray (N-dimensional array), which is Numpy's array class.

  • It provides quick array operations on elements.

  • It provides a variety of functions such as array broadcasting, type casting, and so on.

  • Numpy universal functions are objects in the numpy.ufunc class.

  • Python functions can also be made universal by utilizing the frompyfunc library function.

  • When the corresponding array arithmetic operator is applied, some ufuncs are called automatically. When two arrays are added element by element using the '+' operator, np.add() is called internally.

Trigonometric Functions

Since these trigonometric functions operate in radians, angles must be converted to radians by multiplying by pi/180. Only then can we refer to trigonometric functions. They accept an array of arguments as input.

Trigonometric Function Description
sin, cos, tan Calculates the sine, cosine, and tangent of angles
arcsin, arccos, arctan Calculate the inverse sine, cosine, and tangent angles
hypot Calculates the hypotenuse of the specified right-angled triangle
sinh, cosh, tanh Calculates the hyperbolic sine, cosine and tangent angles
arcsinh, arccosh, arctanh Calculates the inverse hyperbolic sine, cosine and tangent
deg2rad Converts the angle from degree into radians
rad2deg Converts the angle from radians into degree

Example

# importing numpy module with an alias name import numpy as np # creating an array of angles inputAngles = np.array([0, 30, 45, 60, 90, 180]) # converting input angles from degrees into radians # using deg2rad() function radians_angles = np.deg2rad(inputAngles) # printing the sine values of input angles print('Sine values of input array angles:') sineValues = np.sin(radians_angles) print(sineValues) print() # printing the inverse sine of sine values print('Inverse Sine values of sine values:') print(np.rad2deg(np.arcsin(sineValues))) print() # printing the hyperbolic sine of input angles print('Hyperbolic Sine values of input angles:') sineh_value = np.sinh(radians_angles) print(sineh_value) print() # printing the inverse sine hyperbolic print('Inverse Sine hyperbolic values of input angles:') print(np.sin(sineh_value)) print() # printing the hypotenuse of right angled triangle the triangle_base = 4 triangle_height = 3 print('The hypotenuse of right angled triangle = ', np.hypot(triangle_base, triangle_height))

Output

On executing, the above program will generate the following output −

Sine values of input array angles:
[0.00000000e+00 5.00000000e-01 7.07106781e-01 8.66025404e-01
 1.00000000e+00 1.22464680e-16]

Inverse Sine values of sine values:
[0.0000000e+00 3.0000000e+01 4.5000000e+01 6.0000000e+01 9.0000000e+01
 7.0167093e-15]

Hyperbolic Sine values of input angles:
[ 0.          0.54785347  0.86867096  1.24936705  2.3012989  11.54873936]

Inverse Sine hyperbolic values of input angles:
[ 0.          0.52085606  0.76347126  0.94878485  0.74483916 -0.85086591]

The hypotenuse of right angled triangle =  5.0

Here, numpy.array() function- returns an ndarray. The ndarray is an array object that satisfies the given requirements

Statistical Functions

The statistical functions calculates the mean, median, variance, minimum of array elements. Following are the functions it contains −

Statistical Functions Description
amin, amax Returns the minimum, and maximum values of an array along the specified axis
ptp Returns the range of values (maximum-minimum) of an array along the specified axis
median Calculates the median of data along the specified axis
mean Calculates the mean of data along the specified axis
percentile(a, n, axis) Calculates the nth percentile of an array along the specified axis
std Calculates the standard deviation of data along the specified axis
var Calculates the variance of data along the specified axis
average Calculates the average of data along the specified axis

Example

# importing numpy module with an alias name import numpy as np # creating an array inputArray = np.array([20, 45, 50.5, 60, 55.5, 10]) # printing the minimum value in an array print('Minimum value in an input Array:') print(np.amin(inputArray)) # printing the maximum value in an array print('Maximum value in an input Array:') print(np.amax(inputArray)) # printing the peak to peak value i.e # range of inputArray(maximum-minimum) values print('Range of inputArray(maximum-minimum) values:') print(np.ptp(inputArray)) # printing the 50 percentile of input array print('50 percentile of input array: ') print(np.percentile(inputArray, 50)) # printing the mean of input array print('Mean of an input array:') print(np.mean(inputArray)) # printing the median of input array print('Median of an input array:') print(np.median(inputArray)) # printing the standard deviation of input array print('Standard deviation of an input array:') print(np.std(inputArray)) # printing the variance of input array print('Variance of an input array: ') print(np.var(inputArray)) # printing the average of input array print('Average of an input array: ') print(np.average(inputArray))

Output

On executing, the above program will generate the following output −

Minimum value in an input Array:
10.0
Maximum value in an input Array:
60.0
Range of inputArray(maximum-minimum) values:
50.0
50 percentile of input array: 
47.75
Mean of an input array:
40.166666666666664
Median of an input array:
47.75
Standard deviation of an input array:
18.598088312751095
Variance of an input array: 
345.88888888888886
Average of an input array: 
40.166666666666664

Bit-twiddling Functions

Bit-twiddling functions take integer numbers as input arguments and perform bitwise operations on their binary representations. Following are the functions it contains −

Bit-twiddling Functions Description
bitwise_and carries out bitwise “and” operation on two array elements
bitwies_or carries out bitwise “or” operation on two array elements
bitwise_xor carries out bitwise “xor” operation on two array elements
invert carries out bitwise inversion of array elements
left_shift shift bits of elements to the left
right_shift shift bits of elements to the right

Example

# importing numpy module with an alias name import numpy as np # creating two arrays inputArray_1 = np.array([3, 2, 7, 5]) inputArray_2 = np.array([5, 1, 6, 4]) # printing the bitwise "and" operation of two arrays print('Bitwise "and" operation of two arrays:') print(np.bitwise_and(inputArray_1, inputArray_2)) # printing the bitwise "or" operation of two arrays print('Bitwise "or" operation of two arrays:') print(np.bitwise_or(inputArray_1, inputArray_2)) # printing the bitwise "xor" operation of two arrays print('Bitwise "xor" operation of two arrays:') print(np.bitwise_xor(inputArray_1, inputArray_2)) # printing the inversion/not of inputArray_1 print('The inversion/not of inputArray_1:') print(np.invert(inputArray_1)) # left shifting inputArray_1 elements by 1 bit print('left shifting inputArray_1 elements by 1 bit') print(np.left_shift(inputArray_1, 1)) # right shifting inputArray_1 elements by 2 bits print('right shifting inputArray_1 elements by 2 bits:') print(np.right_shift(inputArray_1, 2))

Output

On executing, the above program will generate the following output −

Bitwise "and" operation of two arrays:
[1 0 6 4]
Bitwise "or" operation of two arrays:[7 3 7 5]
Bitwise "xor" operation of two arrays:
[6 3 1 1]
The inversion/not of inputArray_1:
[-4 -3 -8 -6]
left shifting inputArray_1 elements by 1 bit
[ 6  4 14 10]
right shifting inputArray_1 elements by 2 bits:
[0 0 1 1]

Conclusion

In this article, we learned about all of the universal functions in Python's n-dimensional arrays, along with examples.

Updated on: 20-Oct-2022

682 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements