Python

NumPy np.square()

As the name suggests, the square() function in NumPy allows you to calculate the mathematical square of each element in the array.

We will discuss the function syntax, parameters, and return value using this tutorial.

NumPy Square() Function Syntax

The function syntax is expressed below:

numpy.square(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'square'>

Function Parameters
The function supports the following parameters:

  1. x – defines the input array or an array-like object
  2. where – the condition that is broadcasted over the input array
  3. casting – defines the casting type
  4. dtype – the data type of the output array

Function Return Value
The function returns a new array with the elements as the square of each component in the input array.

Since the function creates a new array, it does not alter the original array.

Examples:

Let us illustrate how to use the NumPy square function with practical examples.

Squaring a 1D Array

To square a one-dimensional array, apply the following code:

# import numpy
import numpy as np
arr = [29, 34, 22, 100, 40, 3, 2]
print(f"square array: {np.square(arr)}")

The previous code takes each element in the input array and returns an array with their respective squares.

Note: The resulting array is of the same shape as the input array, as shown below:

square array: [  841  1156   484 10000  1600     9     4]

Squaring a 2D Array

The same case applies to a two-dimensional array. An example of the code snippet is as shown:

arr_2d = np.array([[29, 34, 22], [100, 40, 3]])
print(f"Squared array: {np.square(arr_2d)}")

The following is the resulting output:

Squared array: [[  841  1156   484]
               [10000  1600     9]]

Squaring Floating-Point Values

The operation does not change when working with floats.

arr_floats = np.array([[2.9, 3.4, 2.2], [10.3, 4.0, 3.1]])
print(f"Squared array: {np.square(arr_floats)}")

The previous operation returns to the following array:

Squared array: [[  8.41  11.56   4.84]
            [106.09  16.     9.61]]

NOTE: If you include an integer in an array containing floating-point values, its resulting square will be a float.

Squaring Complex Numbers

You can also use complex numbers with the square function. Take a look at the example below:

arr_complex = np.array([[2, 3j, 2j], [10j, 4j, 4]])
print(f"Squared array: {np.square(arr_complex)}")

This returns to the following array:

Squared array: [[   4.+0.j   -9.+0.j   -4.+0.j]
             [-100.+0.j  -16.+0.j   16.+0.j]]

NOTE: Similarly, an integer in an array containing complex numbers is converted to a complex number.

Conclusion

Thank you for reading through this tutorial where we discussed how to use the NumPy square function by understanding the function parameters and return value, along with illustrations of practical examples. Read more related articles at Linux Hint website.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list