# NumPy Ufunc Summations

In this article, we will explore NumPy ufunc summations in detail and calculating summations utilizing NumPy, in order to discover which method will be most suitable for you.

## What are NumPy Ufunc Summations?

NumPy ufunc summations are mathematical operations that calculate the sum of ndarrays element-wise.

They are implemented as ufuncs, which allows them to perform fast and efficient calculations on large arrays.

The NumPy ufunc summation functions include:

 Functions Overview numpy.sum() Computes the sum of array elements along a given axis. numpy.cumsum() Returns the cumulative sum of the elements along a given axis. numpy.nansum() Computes the sum of array elements along a given axis, ignoring NaN values. numpy.cumprod() Returns the cumulative product of the elements along a given axis. numpy.prod() Computes the product of array elements along a given axis. numpy.nanprod() Computes the product of array elements along a given axis, ignoring NaN values.

## Difference Between Summation and Addition

According to Numpy Ufunc summations, addition occurs between two arguments, while summation occurs over n elements.

Initialize the two arrays even_arr and odd_arr then apply the add() function to add both arrays together:

#### Example:

import numpy as npy even_arr = npy.array([2, 4, 6, 8, 10]) odd_arr = npy.array([1, 3, 5, 7, 9]) mrx_arr = npy.add(even_arr, odd_arr) print(mrx_arr)

Upon execution you will see below outcome:

Execute the add() function on both the prime_arr and nonprime_arr arrays after creating them:

#### Example:

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11]) nonprime_arr = npy.array([1, 4, 6, 8, 9]) mrx_arr = npy.add(prime_arr, nonprime_arr) print(mrx_arr)

Take the sum of the even_arr and odd_arr arrays:

#### Example:

Exampleimport numpy as npy even_arr = npy.array([2, 4, 6, 8, 10]) odd_arr = npy.array([1, 3, 5, 7, 9]) mrx_arr = npy.sum([even_arr, odd_arr]) print(mrx_arr)

Apply the sum() function to the following arrays:

#### Example:

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11]) nonprime_arr = npy.array([1, 4, 6, 8, 9]) mrx_arr = npy.sum([prime_arr, nonprime_arr]) print(mrx_arr)

## Axis Summation

If you specify axis=1 according to Numpy Ufunc summations, NumPy will sum the numbers in the arrays based on the axis value you provide.

You can compute the summation of the even_arr and odd_arr arrays over the 1st axis as follows:

#### Example:

import numpy as npy even_arr = npy.array([12, 14, 16, 18, 20]) odd_arr = npy.array([11, 13, 15, 17, 19]) mrx_arr = npy.sum([even_arr, odd_arr], axis=1) print(mrx_arr)

Execute the sum() function with axis = 1 on the square_arr and cube_arr arrays:

#### Example:

import numpy as npy square_arr = npy.array([1, 4, 9, 16, 25]) cube_arr = npy.array([1, 8, 27, 64, 125]) mrx_arr = npy.sum([square_arr, cube_arr], axis=1) print(mrx_arr)

## Cummulative Sum

The cumulative sum refers to the part-by-part addition of items in an array.

For example, the partial total of [7, 13, 19] would be [7, 7+13, 7+13+19] = [7, 20, 39].

You can utilize the cumsum() function to calculate partial sums.

Calculate the cumulative sum of the following gap_arr array:

#### Example:

import numpy as npy gap_arr = npy.array([25, 50, 75, 100]) mrx_arr = npy.cumsum(gap_arr) print(mrx_arr)

First create the factorial_arr array then implement the cumsum() function on it:

#### Example:

import numpy as npy factorial_arr = npy.array([1, 2, 6, 24, 120]) mrx_arr = npy.cumsum(factorial_arr) print(mrx_arr)

### Example Explanation

In first line, the NumPy library is imported using the alias “npy” to save time and make the code more concise. Then, an array of factorials is created using the npy.array() function, which takes a list of integers as input and returns an array with those values.

The list [1, 2, 6, 24, 120] represents the factorials of the first five positive integers, i.e., 1!, 2!, 3!, 4!, and 5!.

The next line uses the npy.cumsum() function to compute the cumulative sum of the elements in the factorial_arr array.

This function takes an array as input and returns an array containing the cumulative sum of each element.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0