Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of products of pairs in a list

This is the problem I have. Given a list

xList = [9, 13, 10, 5, 3]

I would like to calculate for sum of each element multiplied by subsequent elements

sum([9*13, 9*10, 9*5 , 9*3]) + 
sum([13*10, 13*5, 13*3]) + 
sum([10*5, 10*3]) + 
sum ([5*3])

in this case the answer is 608.

Is there a way to do this perhaps with itertools or natively with numpy?

Below is a function I came up with. It does the job but it is far from ideal as I would like to add other stuff as well.

    def SumProduct(xList):
        ''' compute the sum of the product 
        of a list 
        e.g. 
        xList = [9, 13, 10, 5, 3]
        the result will be 
        sum([9*13, 9*10, 9*5 , 9*3]) + 
        sum([13*10, 13*5, 13*3]) + 
        sum([10*5, 10*3]) + 
        sum ([5*3])
        '''
        xSum = 0
        for xnr, x in enumerate(xList):
            #print xnr, x
            xList_1 = np.array(xList[xnr+1:])
            #print x * xList_1
            xSum = xSum + sum(x * xList_1)
        return xSum

Any help appreciated.

N.B: In case you wonder, I am trying to implement Krippendorf's alpha with pandas

like image 361
user1043144 Avatar asked May 04 '15 20:05

user1043144


People also ask

How do you find all pairs of elements in an array whose sum is equal to a given number?

Follow the steps below to solve the given problem: Initialize the count variable with 0 which stores the result. Iterate arr and if the sum of ith and jth [i + 1…..n – 1] element is equal to sum i.e. arr[i] + arr[j] == sum, then increment the count variable. Return the count.

How do you find the product of two arrays?

The product of two matrices can be computed by multiplying elements of the first row of the first matrix with the first column of the second matrix then, add all the product of elements. Continue this process until each row of the first matrix is multiplied with each column of the second matrix.

How do you generate all pairs of an array?

In order to find all the possible pairs from the array, we need to traverse the array and select the first element of the pair. Then we need to pair this element with all the elements in the array from index 0 to N-1.


1 Answers

x = array([9, 13, 10, 5, 3])
result = (x.sum()**2 - x.dot(x)) / 2

This takes advantage of some mathematical simplifications to work in linear time and constant space, compared to other solutions that might have quadratic performance.

Here's a diagram of how this works. Suppose x = array([2, 3, 1]). Then if you view the products as the areas of rectangles:

x is this stick: -- --- -

x.sum()**2 is this rectangle:
   -- --- -
  |xx xxx x
  |xx xxx x

  |xx xxx x
  |xx xxx x
  |xx xxx x

  |xx xxx x

x.dot(x) is this diagonal bit:
   -- --- -
  |xx
  |xx

  |   xxx
  |   xxx
  |   xxx

  |       x

(x.sum()**2 - x.dot(x)) is the non-diagonal parts:
   -- --- -
  |   xxx x
  |   xxx x

  |xx     x
  |xx     x
  |xx     x

  |xx xxx

and (x.sum()**2 - x.dot(x)) / 2 is the product you want:
   -- --- -
  |   xxx x
  |   xxx x

  |       x
  |       x
  |       x

  |
like image 128
user2357112 supports Monica Avatar answered Sep 19 '22 12:09

user2357112 supports Monica