Previous Up Next

5.43.29  Sum of list (or matrix) elements transformed by a function: count

count takes as argument: a real function f and a list l of length n (or a matrix A of dimension p*q).
count applies the function to the list (or matrix) elements and returns their sum, i.e.:
count(f,l) returns f(l[0])+f(l[1])+...+f(l[n-1]) or
count(f,A) returns f(A[0,0])+....+f(A[p-1,q-1]).
If f is a boolean function count returns the number of elements of the list (or of the matrix) for which the boolean function is true.
Input:

count((x)->x,[2,12,45,3,7,78])

Output:

147

because: 2+12+45+3+7+78=147.
Input:

count((x)->x<12,[2,12,45,3,7,78])

Output:

3

Input:

count((x)->x==12,[2,12,45,3,7,78])

Output:

1

Input:

count((x)->x>12,[2,12,45,3,7,78])

Output:

2

Input:

count(x->x^2,[3,5,1])

Output:

35

Indeed 32+52+11=35.
Input:

count(id,[3,5,1])

Output:

9

Indeed, id is the identity functions and 3+5+1=9.
Input:

count(1,[3,5,1])

Output:

3

Indeed, 1 is the constant function equal to 1 and 1+1+1=3.


Previous Up Next