Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy fabs() Function



The NumPy fabs() function is used to compute the absolute values element-wise for floating-point numbers.

Unlike the absolute() function, which works on integers, floats, and complex numbers, fabs() is specifically designed for floating-point numbers.

Syntax

Following is the syntax of the NumPy fabs() function −

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

Parameters

This function accepts the following parameters −

  • x: The input array containing floating-point numbers whose absolute values are to be calculated.
  • out (optional): A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
  • where (optional): This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Otherwise, it will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are summed. The dtype of x is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array containing the absolute values of each element in the input array x. If out is provided, it returns a reference to out.

Example: Basic Usage of fabs() Function

In the following example, we create a 1-dimensional array with floating-point numbers and use the fabs() function to calculate their absolute values −

import numpy as np

# Creating a 1-dimensional array
arr = np.array([-1.1, -2.2, 3.3, -4.4])

# Calculating the absolute values
result = np.fabs(arr)
print(result)

Following is the output obtained −

[1.1 2.2 3.3 4.4]

Example: fabs() Function with Broadcasting

In this example, we demonstrate the use of broadcasting with the fabs() function. We create a 2-dimensional array and calculate the absolute values element-wise −

import numpy as np

# Creating a 2-dimensional array
arr = np.array([[1.1, -2.2, 3.3], [-4.4, 5.5, -6.6]])

# Calculating the absolute values
result = np.fabs(arr)
print(result)

This will produce the following result −

[[1.1 2.2 3.3]
 [4.4 5.5 6.6]]

Example: fabs() Function with Mixed Sign Numbers

In this example, we create an array with both positive and negative floating-point numbers and use the fabs() function to get their absolute values −

import numpy as np

# Creating an array with mixed sign numbers
arr = np.array([-5.5, 6.6, -7.7, 8.8])

# Calculating the absolute values
result = np.fabs(arr)
print(result)

Following is the output of the above code −

[5.5 6.6 7.7 8.8]
numpy_arithmetic_operations.htm
Advertisements