> For the complete documentation index, see [llms.txt](https://lei-d.gitbook.io/python-for-data-analysis/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lei-d.gitbook.io/python-for-data-analysis/numpy-1/element-wise-functions.md).

# Element-wise Operations

Generally, NumPy arrays are more efficient than lists. One reason is that they allow you to do *element-wise operations*. An element-wise operation allows you to quickly perform an operation, such as addition, on each element in an array.

## Element-wise Mathematical Computation

```python
odds = np.array([1, 3, 5, 7, 9])
evens = odds + 1
print(evens)
# array([2, 4, 6, 8, 10])
```

Multiple arrays can also be used in arithmetic operations, provided that they have the same lengths. When adding or subtracting arrays in NumPy, each element will be added/subtracted to its matching element.

```python
array1 = np.array([1, 2, 3])
array2 = np.array([4, 3, 2])
new_array = array1 + array2
print(new_array)
# array([5, 5, 5])
```

## Element-wise Logical Operations

NumPy Arrays support element-wise logical operations, returning new Arrays populated with `False` or `True` based on their evaluation.

```python
>>> a = np.array([10, 2, 2, 4, 5, 3, 9, 8, 9, 7])
>>> a > 5
array([True, False, False, False, False, 
        False, True, True, True, True], 
        dtype=bool)
```

## Element-wise Functions in NumPy

`abs, fabs` Compute the absolute value element-wise for integer, floating point, or complex values. Use fabs as a faster alternative for non-complex-valued data

`sqrt` Compute the square root of each element. Equivalent to arr \*\* 0.5

`square` Compute the square of each element. Equivalent to arr \*\* 2

`exp` Compute the exponent ex of each element

`log, log10, log2, log1p` Natural logarithm (base e), log base 10, log base 2, and log(1 + x), respectively

`sign` Compute the sign of each element: 1 (positive), 0 (zero), or -1 (negative)

`ceil` Compute the ceiling of each element, i.e. the smallest integer greater than or equal to each element

`floor` Compute the floor of each element, i.e. the largest integer less than or equal to each element

`rint` Round elements to the nearest integer, preserving the dtype

`modf` Return fractional and integral parts of array as separate array

`isnan` Return boolean array indicating whether each value is NaN (Not a Number)

`isfinite, isinf` Return boolean array indicating whether each element is finite (non-inf, non-NaN) or infinite, respectively

`cos, cosh, sin, sinh, tan, tanh` Regular and hyperbolic trigonometric functions

`arccos, arccosh, arcsin, arcsinh, arctan, arctanh` Inverse trigonometric functions

`logical_not` Compute truth value of not x element-wise. Equivalent to -arr.

`add` Add corresponding elements in arrays

`subtract` Subtract elements in second array from first array

`multiply` Multiply array elements

`divide, floor_divide` Divide or floor divide (truncating the remainder)

`power` Raise elements in first array to powers indicated in second array

`maximum, fmax` Element-wise maximum. fmax ignores NaN

`minimum, fmin` Element-wise minimum. fmin ignores NaN

`mod` Element-wise modulus (remainder of division)

`copysign` Copy sign of values in second argument to values in first argument

`greater, greater_equal, less, less_equal, equal, not_equal`Perform element-wise comparison, yielding boolean array. Equivalent to infix operators >, >=, <, <=, ==, !=

`logical_and, logical_or, logical_xor` Compute element-wise truth value of logical operation. Equivalent to infix operators &, |, ^.
