I know this should not be difficult, probably I've been too lazy to figure it out. When I execute the python script below, I get this message: <bound method Forces.Sums of <main.Forces instance at 0x7fcecfb56a28>>. What is wrong?

``````import numpy

class Forces:
""" Forces between particles"""

def __init__(self, l1, l2):
self.l1 = l1
self.l2 = l2
N = len(l1)
k = list(xrange(N*N))
result = numpy.zeros(N*N) #No repetition N*(N-1)
def Sums(self):
l1, l2 = self.l1, self.l2
for i in range(0, (len(l1)-1)):
for j in range(0, (len(l2)-1)):
result[i+j] += l1[i]*l2[j]
k += 1
return result

#Demo
if __name__ == '__main__':
list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]
p = Forces(list1, list2)
print p.Sums``````
2
Contributors
3
Replies
54
Views
2 Years
Discussion Span
Last Post by Gribouillis

Nothing is wrong. Only you must call the method if you want it to be executed. So write `p.Sums()`.

Thank you Griboullis. I wanted the code to look something like this:

``````import numpy

class Particles:
""" Forces between particles"""

def __init__(self, l1, l2):
self.l1 = l1
self.l2 = l2
N = len(l1)
self.result = numpy.zeros(N) #No repetition N*(N-1)
self.Total = 0
def First_sums(self):
l1, l2, ith_sums, Total = self.l1, self.l2, self.result, self.Total
for i in range(0, (len(l1))):
for j in range(0, len(l2)):
ith_sums[i] += l1[i]*l2[j]
for k in range(0, len(ith_sums)):  # We can use Total = sum(ith_sums) here
Total += ith_sums[k]

def Second_sums(self):
l1, l2, ith_sums, Total = self.l1, self.l2, self.result, self.Total
for i in range(0, (len(l1))):
for j in range(0, len(l2)):
if i == j:
ith_sums[i] += l1[i]*l2[j]
for k in range(0, len(ith_sums)):
Total += ith_sums[k]

#def Differences(self): You can create another method to compute the difference
#Demo
if __name__ == '__main__':
N = 1000000 #Define N
list1 = list2 = numpy.random.randn(N) #list = [1,2,3,4,5], Try something simple
p = Particles(list1, list2)
y = Particles(list1, list2)
sum1 = p.First_sums()
sum2 = y.Second_sums()
diff = sum1 - sum2
print "The difference between the two sums:", diff

#Try the following, and see what happens:

#print "The difference between the two sums:", p.First_sums(), p.Second_sums() ``````

With numpy, it is very mportant to vectorize the computations. I think the following code does the same thing but avoids a `10**6 * 10**6` pure python loop which takes an infinite amount of time

``````import numpy

class Particles:
""" Forces between particles"""

def __init__(self, l1, l2):
self.l1 = l1
self.l2 = l2
N = len(l1)
self.result = numpy.zeros(N) #No repetition N*(N-1)
self.Total = 0
def First_sums(self):
l1, l2, ith_sums, Total = self.l1, self.l2, self.result, self.Total
ith_sums += l1 * numpy.sum(l2)
Total = numpy.sum(ith_sums)

def Second_sums(self):
l1, l2, ith_sums, Total = self.l1, self.l2, self.result, self.Total
ith_sums += l1 * l2
Total = numpy.sum(ith_sums)

#def Differences(self): You can create another method to compute the difference
#Demo
if __name__ == '__main__':
N = 1000000 #Define N
list1 = list2 = numpy.random.randn(N) #list = [1,2,3,4,5], Try something simple
p = Particles(list1, list2)
y = Particles(list1, list2)
sum1 = p.First_sums()
sum2 = y.Second_sums()
diff = sum1 - sum2
print "The difference between the two sums:", diff``````

Edited by Gribouillis

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.