Re: [Tutor] Summing arrays

2017-03-16 Thread Peter Otten
Peter Otten wrote:

> (Specifying dtype=float forces floating point arithmetic which is inexact)

I found a way to avoid the error:

>>> import numpy as np
>>> def sum2(N):
... a = np.arange(1, N+1, dtype=object)
... return (2*(1+3**(a-1))).sum()
... 
>>> sum2(50)
717897987691852588770348

As this operates on int objects rather than C/machine language numbers this  
will probably be significantly slower than dtype=int or dtype=float.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Summing arrays

2017-03-16 Thread Wolfgang Maier

On 03/16/2017 10:38 AM, Wolfgang Maier wrote:


In addition, I'd agree with Alan that the advantage of using numpy
functionality in this function seems quite questionable. You'd probably
get pretty much the same performance with just:

def sum2(N):
return sum(2*(1+3**(i-1)) for i in range(1,N+1,1))



Or, if you really want to leverage the power of numpy (and are willing 
to pay the price of possibly inexact calculations) rewrite your function 
as Peter shows in his reply.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Summing arrays

2017-03-16 Thread Wolfgang Maier

On 03/16/2017 10:12 AM, Alan Gauld via Tutor wrote:

On 16/03/17 05:11, Aaliyah Ebrahim wrote:


def sum2(N):

b = np.arange(1,N+1,1)
mylist = [ ]
for i in b:
terms = 2*(1+3**(i-1))
a = mylist.append[terms]
return np.sum(mylist)



terms = 2*(1+3**(i-1))> 9 mylist.append[terms] 10
return np.sum(mylist) 11
TypeError: 'builtin_function_or_method' object is not subscriptable


You are using [] to call append instead of ().
It should be
a = mylist.append(terms)

However, most of your function could be replaced by
a list comprehension:

def sum2(N):
mylist = [2*(1+3**(i-1)) for i in np.arange(1,N+1,1) ]
return np.sum(mylist)

And I'm not sure the np versions of the functions will
give much advantage over the standard range() and sum()

Any time you see a structure like

aList = []
for 
   aList.append()

You should consider whether a comprehension would
be more suitable



Alternatively, you could even use a generator expression (essentially, 
just omitting the brackets from Alan's suggestion) and avoid keeping 
mylist in memory like this:


def sum2(N):
return np.sum(2*(1+3**(i-1)) for i in np.arange(1,N+1,1))

In addition, I'd agree with Alan that the advantage of using numpy 
functionality in this function seems quite questionable. You'd probably 
get pretty much the same performance with just:


def sum2(N):
return sum(2*(1+3**(i-1)) for i in range(1,N+1,1))

Best,
Wolfgang

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Summing arrays

2017-03-16 Thread Peter Otten
Aaliyah Ebrahim wrote:

> Hi guys! I hope you can assist me with understanding and fixing my error
> below. Thanks for this amazing forum :)
> 
> 
> def sum2(N):
> 
> b = np.arange(1,N+1,1)
> mylist = [ ]
> for i in b:
> 
> 
> terms = 2*(1+3**(i-1))
> a = mylist.append[terms]

The immediate problem is that append is a method, the line above should be

mylist.append(terms)

As the append() method always returns None the assignment a = ...
does not do anything useful and should be avoided.

> return np.sum(mylist)
> 
> print(sum2(N=50))

However, with the fix above you will still run into overflow errors with 
your code. I suggest that you use Python's integer arithmetic and forget 
about numpy for now:

$ cat tmp1.py
import numpy as np

def sum2(N):
b = np.arange(1,N+1,1)
mylist = []
for i in b:
terms = 2*(1+3**(i-1))
mylist.append(terms)
return np.sum(mylist)

def sum2a(N):
mylist = []
for i in range(1, N+1):
mylist.append(2*(1+3**(i-1)))
return sum(mylist)

print("N=5:", sum2(5), sum2a(5))
print("N=50:", sum2(50), sum2a(50))
$ python3 tmp1.py
N=5: 252 252
tmp1.py:7: RuntimeWarning: overflow encountered in long_scalars
  terms = 2*(1+3**(i-1))
N=50: 6048575297968530476 717897987691852588770348

Both the numpy and the non-numpy version can be written more concisely (and 
for the numpy version the rewritten version is more efficient, too):

$ cat tmp2.py
import numpy as np

def sum2(N):
b = np.arange(1, N+1, 1, dtype=float)
a = 2*(1+3**(b-1))
return a.sum()

def sum2a(N):
return sum(2*(1+3**(i-1)) for i in range(1, N+1))

print("N=5:", sum2(5), sum2a(5))
print("N=50:", sum2(50), sum2a(50))
$ python3 tmp2.py 
N=5: 252.0 252
N=50: 7.17897987692e+23 717897987691852588770348

(Specifying dtype=float forces floating point arithmetic which is inexact)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Summing arrays

2017-03-16 Thread Alan Gauld via Tutor
On 16/03/17 05:11, Aaliyah Ebrahim wrote:

> def sum2(N):
> 
> b = np.arange(1,N+1,1)
> mylist = [ ]
> for i in b:
> terms = 2*(1+3**(i-1))
> a = mylist.append[terms]
> return np.sum(mylist)

> terms = 2*(1+3**(i-1))> 9 mylist.append[terms] 10
> return np.sum(mylist) 11
> TypeError: 'builtin_function_or_method' object is not subscriptable

You are using [] to call append instead of ().
It should be
a = mylist.append(terms)

However, most of your function could be replaced by
a list comprehension:

def sum2(N):
mylist = [2*(1+3**(i-1)) for i in np.arange(1,N+1,1) ]
return np.sum(mylist)

And I'm not sure the np versions of the functions will
give much advantage over the standard range() and sum()

Any time you see a structure like

aList = []
for 
   aList.append()

You should consider whether a comprehension would
be more suitable

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Summing arrays

2017-03-16 Thread Aaliyah Ebrahim
Hi guys! I hope you can assist me with understanding and fixing my error
below. Thanks for this amazing forum :)


def sum2(N):

b = np.arange(1,N+1,1)
mylist = [ ]
for i in b:


terms = 2*(1+3**(i-1))
a = mylist.append[terms]
return np.sum(mylist)

print(sum2(N=50))




---TypeError
Traceback (most recent call
last) in () 10 return
np.sum(mylist) 11 ---> 12 print(sum2(N=50))
 in sum2(N)  7   8
terms = 2*(1+3**(i-1))> 9 mylist.append[terms] 10
return np.sum(mylist) 11
TypeError: 'builtin_function_or_method' object is not subscriptable




Aaliyah
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] summing arrays, along a dimension

2007-09-14 Thread John
 d
array([[0, 0, 1],
   [1, 2, 3],
   [2, 2, 4],
   [3, 6, 8]])
 e=reshape((d[:,-2]+d[:,-1]),(4,1))
 e
array([[ 1],
   [ 5],
   [ 6],
   [14]])

is there a better way to accomplish this?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] summing arrays, along a dimension

2007-09-14 Thread Ricardo Aráoz
John wrote:
 d
 array([[0, 0, 1],
[1, 2, 3],
[2, 2, 4],
[3, 6, 8]])
 e=reshape((d[:,-2]+d[:,-1]),(4,1))
 e
 array([[ 1],
[ 5],
[ 6],
[14]])
  
 is there a better way to accomplish this?
 

 d
[[0, 0, 1], [1, 2, 3], [2, 2, 4], [3, 6, 8]]
 e = [sum(i) for i in d]
 e
[1, 6, 8, 17]
 f = [sum([L[i] for L in d]) for i in xrange(len(d[0]))]
 f
[6, 10, 16]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] summing arrays, along a dimension

2007-09-14 Thread Alan Gauld

John [EMAIL PROTECTED] wrote 

 d
 array([[0, 0, 1],
   [1, 2, 3],
   [2, 2, 4],
   [3, 6, 8]])
 e=reshape((d[:,-2]+d[:,-1]),(4,1))
 e
 array([[ 1],
   [ 5],
   [ 6],
   [14]])
 
 is there a better way to accomplish this?

Better? Maybe. More readable I think is:

e1 = [ [row[-2]+row[-1]]  for row in d ]

You can convert the 2D list to an array object if you want later


Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] summing arrays, along a dimension

2007-09-14 Thread bob gailer
John wrote:
  d
 array([[0, 0, 1],
[1, 2, 3],
[2, 2, 4],
[3, 6, 8]])
  e=reshape((d[:,-2]+d[:,-1]),(4,1))
  e
 array([[ 1],
[ 5],
[ 6],
[14]])
  
 is there a better way to accomplish this?
Which module are you using?

In APL we'd write +/d to reduce along the rows using +. Does the array 
object have a similar reduce method?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor