Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 16

Sums, Products, and Rounding

Use NumPy universal functions for sums, running totals, products, differences, rounding, and the LCM and GCD of whole arrays.

Intermediate25 min
Sums, Products, and Rounding hero Module 23 Lesson 16

A universal function, or ufunc, is a NumPy function that runs element by element in compiled code. np.add and np.floor are ufuncs. Some ufuncs also have a reduce form that combines a whole array into one value.

An array of four values and its three differences

Addition versus summation

np.add works on pairs of elements. np.sum adds up many values into one, or into one per row or column with axis. np.cumsum gives the running total.

python
import numpy as np

a = np.array([1, 2, 3])
b = np.array([1, 2, 3])
print(np.add(a, b))
print(np.sum([a, b]))
print(np.sum([a, b], axis=1))
print(np.cumsum(np.array([1, 2, 3, 4])))

Output:

text
[2 4 6]
12
[6 6]
[ 1  3  6 10]

Products

np.prod multiplies the values together. np.cumprod gives the running product. Both take an axis.

python
import numpy as np

print(np.prod(np.array([1, 2, 3, 4])))
m = np.array([[1, 2, 3], [3, 4, 5]])
print(np.prod(m, axis=0))
print(np.prod(m, axis=1))
print(np.cumprod(np.array([5, 6, 7, 8])))

Output:

text
24
[ 3  8 15]
[ 6 60]
[   5   30  210 1680]

Discrete differences

np.diff subtracts each value from the next one, so the result is one shorter. n=2 repeats the step on the result. On 2-D arrays, axis=0 compares rows and axis=1 compares columns.

python
import numpy as np

a = np.array([10, 15, 25, 5])
print(np.diff(a))
print(np.diff(a, n=2))

m = np.array([[1, 2, 3], [3, 4, 5], [2, 3, 5]])
print(np.diff(m, axis=0))
print(np.diff(m, axis=1))

Output:

text
[  5  10 -20]
[  5 -30]
[[ 2  2  2]
 [-1 -1  0]]
[[1 1]
 [1 1]
 [1 2]]

Rounding

trunc and fix drop the decimals, moving toward zero. floor moves down and ceil moves up. np.round, also called around, rounds to a number of decimals. An exact half rounds to the nearest even digit, so 2.5 becomes 2.

python
import numpy as np

values = np.array([-3.1666, 3.6667])
print(np.trunc(values), np.fix(values))
print(np.floor(values), np.ceil(values))
print(np.round(3.1666, 2))
print(np.round(np.array([0.5, 1.5, 2.5, 3.5])))

Output:

text
[-3.  3.] [-3.  3.]
[-4.  3.] [-3.  4.]
3.17
[0. 2. 2. 4.]

LCM and GCD

np.lcm gives the lowest common multiple of two whole numbers and np.gcd the greatest common divisor. reduce applies either one across a whole array.

python
import numpy as np

print(np.lcm(4, 6), np.gcd(4, 6))
print(np.lcm.reduce(np.array([3, 6, 9])))
print(np.lcm.reduce(np.arange(1, 11)))
print(np.gcd.reduce(np.array([12, 18, 24])))

Output:

text
12 2
18
2520
6

Key takeaways

  • add pairs values. sum totals them. cumsum gives running totals

  • diff is one shorter than its input

  • floor goes down, ceil up, trunc toward zero

  • np.round sends exact halves to the even digit

  • Next: Unique Values and Sets

  • Previous: Statistics

Loading playground…

Check your understanding

Sums, Products, and Rounding Quiz

What is np.add([1, 2, 3], [1, 2, 3])?

1 / 10