Tutorial

numpy.square() in Python

Guide and examples for using numpy.square to compute elementwise squares across integers, floats, complex numbers and 2D arrays, including dtype behavior and differences vs **2.

Drake Nguyen

Founder · System Architect

3 min read
numpy.square() in Python
numpy.square() in Python

numpy.square: elementwise squaring in NumPy

The numpy.square function computes the square of each element in an array and returns a new NumPy array containing those squared values. This elementwise ufunc preserves the input shape; the original array remains unchanged unless you explicitly supply an output array via the out parameter. Use numpy.square when you need a clear, fast, and NumPy-aware way to square values across arrays of any dimensionality.

Python numpy.square() examples

Below are concise examples showing how to use numpy.square with integer, floating-point, complex, and 2D arrays. Each snippet demonstrates how NumPy handles dtype promotion and the fact that np.square returns a new array by default.

Int array example

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print('Source array:\n', arr)
sq = np.square(arr)
print('Squared array:\n', sq)

Floating-point array example

import numpy as np

arr = np.array([1.2, 2.3, 5])
print('Source array:', arr)
sq = np.square(arr)
print('Squared array:', sq)
# Note: integer values in a float array are promoted to float

Complex numbers example

import numpy as np

arr = np.array([1 + 2j, 2 + 3j, 4])
print('Source array:', arr)
sq = np.square(arr)
print('Squared array:', sq)
# Any complex entry promotes the result to complex dtype

2D array (matrix) example

import numpy as np

mat = np.array([[1, -2], [3, 4]], dtype=float)
print('Matrix:\n', mat)
mat_sq = np.square(mat)
print('Elementwise square:\n', mat_sq)

Behavior, dtype and differences

Key points about the numpy square function:

  • Elementwise operation: numpy.square applies to each element independently, so it works on scalars, 1D, 2D, or higher-dimensional arrays.
  • Returns a new array by default: the original array is unchanged unless you provide the out argument to write results in-place.
  • dtype promotion: result dtype follows NumPy's type promotion rules — mixing floats promotes integers to float, and any complex entry yields a complex result.
  • Ufunc advantages: np.square is a NumPy ufunc, so it supports the out and where parameters and can be faster and more memory-efficient than Python-level loops.
  • Operator vs function: arr ** 2 and np.square(arr) both compute elementwise squares, but np.square exposes ufunc features (out, dtype control, broadcasting) and is generally preferred in performance-sensitive code.

Practical tips

  • Use np.square for large arrays when you might want to reuse memory via the out parameter.
  • Check dtype after squaring when precision matters—floating-point inputs keep fractional results, while integer inputs stay integer unless mixed with floats.
  • For complex arithmetic, remember that squaring follows complex algebra and will return complex-valued results.

Summary: numpy.square is a straightforward, elementwise NumPy ufunc to square array elements. It is flexible across dtypes and dimensions and offers performance and memory-control features that the simple ** operator does not.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.