numpy.square(arr, out = None, ufunc 'square') : This mathematical function helps user to calculate square value of each element in the array. Parameters :
arr : [array_like] Input array or object
whose elements, we need to square.
Return :
An array with square value of each array.
Code #1 : Working
Python3
# Python program explaining
# square () function
import numpy as np
arr1 = [1, -3, 15, -466]
print ("Square Value of arr1 : \n", np.square(arr1))
arr2 = [23 ,-56]
print ("\nSquare Value of arr2 : ", np.square(arr2))
Output :
Square Value of arr1 :
[ 1 9 225 217156]
Square Value of arr2 : [ 529 3136]
Code #2 : Working with complex numbers
Python3
# Python program explaining
# square () function
import numpy as np
a = 4 + 3j
print("Square(4 + 3j) : ", np.square(a))
b = 16 + 13j
print("\nSquare value(16 + 13j) : ", np.square(b))
Output :
Square(4 + 3j) : (7+24j)
Square value(16 + 13j) : (87+416j)
Code #3 : Graphical Representation of numpy.square()
Python3
# Python program explaining
# square () function
import numpy as np
import matplotlib.pyplot as plt
a = np.linspace(start = -5, stop = 5,
num = 6, endpoint = True)
print("Graphical Representation : \n", np.square(a))
plt.title("blue : with square\nred : without square")
plt.plot(a, np.square(a))
plt.plot(a, a, color = 'red')
plt.show()
Output :
Graphical Representation :
[ 25. 9. 1. 1. 9. 25.]
References : https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.absolute.html .
using for loop:
In this example, we define a list of numbers called numbers. We then use a for loop to iterate over each number in the list, and calculate the square of each number using the exponentiation operator **. We store each square in a new list called squares. Finally, we print the list of squares to the console.
Note that this approach is not as efficient or concise as using the numpy.square() function, especially if you are working with large arrays of numbers. However, it can be useful if you need to implement your own custom square function or want to understand how the numpy.square() function works internally.
Python3
# Define a list of numbers
numbers = [1, 2, 3, 4, 5]
# Calculate the square of each number using a for loop
squares = []
for number in numbers:
square = number ** 2
squares.append(square)
# Print the squares of the numbers
print("The squares of the numbers are:", squares)
OutputThe squares of the numbers are: [1, 4, 9, 16, 25]
Approach:
The code defines a list of integers numbers. It then calculates the square of each number in numbers using a for loop, and stores the results in a new list called squares. Finally, the squares of the numbers are printed.
Time Complexity:
The time complexity of the list comprehension is also O(n), where n is the length of the input list numbers. Therefore, the time complexity of both the original code and the alternative code is O(n).
Space Complexity:
The space complexity of the original code and the alternative code is also O(n), because they both create a new list squares to store the results of the square operation, which has the same length as numbers. Additionally, they both use some auxiliary space to store the loop variable and the temporary result of each square operation.
Similar Reads
numpy.sqrt() in Python numpy.sqrt() in Python is a function from the NumPy library used to compute the square root of each element in an array or a single number. It returns a new array of the same shape with the square roots of the input values. The function handles both positive and negative numbers, returning NaN for n
2 min read
numpy.eye() in Python numpy.eye() is a function in the NumPy library that creates a 2D array with ones on the diagonal and zeros elsewhere. This function is often used to generate identity matrices with ones along the diagonal and zeros in all other positions.Let's understand with the help of an example:Pythonimport nump
2 min read
Number System in Python The arithmetic value that is used for representing the quantity and used in making calculations is defined as NUMBERS. The writing system for denoting numbers logically using digits or symbols is defined as a Number system. Number System is a system that defines numbers in different ways to represen
6 min read
numpy.exp2() in Python numpy.exp2(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate 2**x for all x being the array elements. Parameters : array : [array_like]Input array or object whose elements, we need to test. out : [ndarray, optional
2 min read
NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C
2 min read
numpy.multiply() in Python The numpy.multiply() is a numpy function in Python which is used to find element-wise multiplication of two arrays or scalar (single value). It returns the product of two input array element by element.Syntax:numpy.multiply(arr1, arr2, out=None, where=True, casting='same_kind', order='K', dtype=None
3 min read
Circle of Squares using Python Turtle library enables users to draw pictures or shapes using commands, providing them with a virtual canvas. turtle comes with Python's Standard Library. It needs a version of Python with Tk support, as it uses tkinter for the graphics. In this article, we will generate a circular pattern out of sq
2 min read
Python | Decimal sqrt() method Decimal#sqrt() : sqrt() is a Decimal class method which returns the Square root of a non-negative number to context precision. Syntax: Decimal.sqrt() Parameter: Decimal values Return: the Square root of a non-negative number to context precision. Code #1 : Example for sqrt() method Python3 # Python
2 min read
numpy.fromiter() function â Python NumPy's fromiter() function is a handy tool for creating a NumPy array from an iterable object. This iterable can be any Python object that provides elements one at a time. The function is especially useful when you need to convert data from a custom data source, like a file or generator, into a Num
2 min read
Python math.sqrt() function | Find Square Root in Python math.sqrt() returns the square root of a number. It is an inbuilt function in the Python programming language, provided by the math module. In this article, we will learn about how to find the square root using this function.Example:Pythonimport math # square root of 0 print(math.sqrt(0)) # square r
2 min read