Re: Strange behavior related to value / reference

2009-10-30 Thread Aahz
In article <626f24e5-4d8e-416c-b3ed-dc56a88dc...@s21g2000prm.googlegroups.com>, Lambda wrote: > >def matrix_power(m, n): > result = m[:] > print result is m Use copy.deepcopy() -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "You could make Eskimos emigrate t

Re: Strange behavior related to value / reference

2009-10-28 Thread Dave Angel
Mark Dickinson wrote: On Oct 28, 8:24 am, Lambda wrote: Thank you! Following is my final code: Looks good, but are you sure about that word 'final'? ;-) def matrix_power(m, n): """ Raise 2x2 matrix m to nth power. """ if n =0: return [[1, 0], [0, 1]] x =atrix_power(m,

Re: Strange behavior related to value / reference

2009-10-28 Thread Peter Otten
Lambda wrote: > I defined a function to raise a 2x2 matrix to nth power: > BTW, numpy has such a function, but it doesn't support really large > number. > Does numpy has some functions that support arbitrarily large number? You can tell it to use Python instead of C integers: >>> import numpy >

Re: Strange behavior related to value / reference

2009-10-28 Thread Mark Dickinson
On Oct 28, 8:24 am, Lambda wrote: > Thank you! > Following is my final code: Looks good, but are you sure about that word 'final'? ;-) > > def matrix_power(m, n): >   """ >   Raise 2x2 matrix m to nth power. >   """ >   if n == 0: return [[1, 0], [0, 1]] > >   x = matrix_power(m, n / 2) I sugg

Re: Strange behavior related to value / reference

2009-10-28 Thread Lambda
On Oct 28, 10:40 am, Chris Rebert wrote: > On Tue, Oct 27, 2009 at 7:15 PM, Lambda wrote: > > I defined a function to raise a 2x2 matrix to nth power: > > > def matrix_power(m, n): > >  result = m[:] > > Note that this only copies the *outer* list. It does NOT copy any of > the inner, nested list

Re: Strange behavior related to value / reference

2009-10-27 Thread Terry Reedy
Lambda wrote: I defined a function to raise a 2x2 matrix to nth power: There is a much faster way to raise x to a count power n than the definitional but naive method of multipling 1 by x n times. It is based on the binary representation of n. Example: x**29 = x**(16+8+4+1) = x**16 * x**8 *

Re: Strange behavior related to value / reference

2009-10-27 Thread Chris Rebert
On Tue, Oct 27, 2009 at 7:15 PM, Lambda wrote: > I defined a function to raise a 2x2 matrix to nth power: > > def matrix_power(m, n): >  result = m[:] Note that this only copies the *outer* list. It does NOT copy any of the inner, nested lists, it just makes fresh *references* to them, which is w

Strange behavior related to value / reference

2009-10-27 Thread Lambda
I defined a function to raise a 2x2 matrix to nth power: def matrix_power(m, n): result = m[:] print result is m for i in range(n - 1): result[0][0] = result[0][0] * m[0][0] + result[0][1] * m[1][0] result[0][1] = result[0][0] * m[0][1] + result[0][1] * m[1][1] result[1][0] = re