Sigma Notation in Python

12,290

Solution 1

Did you try something like this instead of using Numpy.sum() function?

def new_sum(i,j): 
    hi=0
    for n in range(j+1):
       hi+= A[i][n]*Network[n]
    return hi

The numpy sum() function just return the sum of all the elements in an array. The parameter you are giving to it is just a case and not an array to sum. So you are returning the sum of one element : this element.

Solution 2

It looks like a vector product followed by a sum along the resulting array. You could do something like this:

sigma = lambda x, y: np.sum(np.dot(x,y))

hi = sigma(A, Network)
Share:
12,290
user2509830
Author by

user2509830

Updated on June 04, 2022

Comments

  • user2509830
    user2509830 almost 2 years

    I am trying to create a sigma sum in python.

    I have a 100 by 100 matrix (created with numpy) and I have a list of 100 values. My matrix is the variable A, and my list is the variable Network.

    The sum should look like so.

    hi= Sigma( (A[i][j])* Network[j])
    

    i and j in the matrix refer to the specific value, and j in Network refers to the value in the list.

    so, if I wanted h67, the sum would be:

    (A[67][67]*Network[67]) + (A[67][66]*Network[66]) + (A[67][65*Network[65]) + ...
    (A[67][0]*Network[0]).
    

    My code is as follows, but I don't think it is right.

    def new_sum(i,j):
        hi=0
        hi+= numpy.sum((A[i][j]*Network[j]))
        return hi
    

    What should I do?