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

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

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

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 =

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 >

[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)

[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

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)

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 = [

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