[sage-support] associativity of addition on ell. curves

2008-01-09 Thread achrzesz

Hello
My question is
connected with   file http://modular.fas.harvard.edu/ent/ent_py
and especially with checking the associativity law of addition on ell.
curves.
When I was trying to perform the same calculations in Sage I observed
some surprising (me) behaviour.
Enclosed is the corresponding Sage code.

# the first 12 lines of code  is modyfied version of
# http://modular.fas.harvard.edu/ent/ent_py
# necessary changes seem to be inessential

x1 = var('x1'); x2 = var('x2'); x3 = var('x3')
y1 = var('y1'); y2 = var('y2'); y3 = var('y3')
a  = var('a'); b  = var('b')
lambda12 = (y1 - y2)/(x1 - x2)
x4   = (lambda12*lambda12 - x1 - x2)
nu12 = (y1 - lambda12*x1)
y4   = (-lambda12*x4 - nu12)
lambda23 = ((y2 - y3)/(x2 - x3))
x5   = (lambda23*lambda23 - x2 - x3)
nu23 = (y2 - lambda23*x2)
y5   = (-lambda23*x5 - nu23)
s1 =(x1 - x5)*(x1 - x5)*((y3 - y4)*(y3 - y4) - (x3 + x4)*(x3 - x4)*(x3
- x4))
s2 = (x3 - x4)*(x3 - x4)*((y1 - y5)*(y1 - y5) - (x1 + x5)*(x1 -
x5)*(x1 - x5))


#the code below looks reasonable but gives wrong result; I wonder why?

t1=s1.factor()
t2=s2.factor()
R=QQ[x1,x2,x3,y1,y2,y3]
print R(t1.denominator())==R(t2.denominator())   # True
#denominators of s1,s2 are equal, so let's compare numerators
print R(t1.numerator())==R(t2.numerator())# False (in Sage 2.9)


I would be grateful for any comments.
Is there an easy way to check the mentioned associativity in Sage?
A. Chrzeszczyk


--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] associativity of addition on elliptic curves

2008-01-09 Thread Andrzej Chrzęszczyk

Hello
My question is
connected with   file http://modular.fas.harvard.edu/ent/ent_py
and especially with checking the associativity law of addition on ell. 
curves.
When I was trying to perform the same calculations in Sage I observed
some surprising (me) behaviour.
Enclosed is the corresponding Sage code.

# the first 12 lines of code  is modyfied version of
# http://modular.fas.harvard.edu/ent/ent_py
# necessary changes seem to be inessential

x1 = var('x1'); x2 = var('x2'); x3 = var('x3')
y1 = var('y1'); y2 = var('y2'); y3 = var('y3')
a  = var('a'); b  = var('b')
lambda12 = (y1 - y2)/(x1 - x2)
x4   = (lambda12*lambda12 - x1 - x2)
nu12 = (y1 - lambda12*x1)
y4   = (-lambda12*x4 - nu12)
lambda23 = ((y2 - y3)/(x2 - x3))
x5   = (lambda23*lambda23 - x2 - x3)
nu23 = (y2 - lambda23*x2)
y5   = (-lambda23*x5 - nu23)
s1 =(x1 - x5)*(x1 - x5)*((y3 - y4)*(y3 - y4) - (x3 + x4)*(x3 - x4)*(x3 - 
x4))
s2 = (x3 - x4)*(x3 - x4)*((y1 - y5)*(y1 - y5) - (x1 + x5)*(x1 - x5)*(x1 
- x5))


#the code below looks reasonable but gives wrong result; I wonder why?

t1=s1.factor()
t2=s2.factor()
R=QQ[x1,x2,x3,y1,y2,y3]
print R(t1.denominator())==R(t2.denominator())   # True
#denominators of s1,s2 are equal, so let's compare numerators
print R(t1.numerator())==R(t2.numerator())# False (in Sage 2.9)
   

I would be grateful for any comments.
Is there an easy way to check the mentioned associativity in Sage?
A. Chrzeszczyk


Zagłosuj i zgarnij 10.000 złotych! 
Wybierz z nami Internetowego SportoWWWca Roku.
Oddaj swój głos na najlepszego. - Kliknij:
http://klik.wp.pl/?adr=http%3A%2F%2Fcorto.www.wp.pl%2Fas%2Fsportowiec2007.htmlsid=166



--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support]

2008-01-09 Thread Andrzej Chrzęszczyk

Hello,
I'm sory for repeating the same post 2 times (associativity...)
Please remove one of them if possible.
achrzesz


Zagłosuj i zgarnij 10.000 złotych! 
Wybierz z nami Internetowego SportoWWWca Roku.
Oddaj swój głos na najlepszego. - Kliknij:
http://klik.wp.pl/?adr=http%3A%2F%2Fcorto.www.wp.pl%2Fas%2Fsportowiec2007.htmlsid=166



--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Sage LAN server

2008-01-09 Thread Magnus

First let me say that I am a sage-newbie. I work at a high-school in
Sweden as a math teacher. Our curriculum states that we are to teach
our students in the using of computer programs when solving math
problems.

My problem is that our technicians (sp?) have succeded in installing
sage at an internal server at our school, but we have found no
information about how to create accounts other than the one admin
account that is created automaticly. Does anyone know any solution to
this problem?

Hoping for a good reply,
Magnus Dahlström

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: Sage LAN server

2008-01-09 Thread William Stein

On Jan 9, 2008 6:53 AM, Magnus [EMAIL PROTECTED] wrote:

 First let me say that I am a sage-newbie. I work at a high-school in
 Sweden as a math teacher. Our curriculum states that we are to teach
 our students in the using of computer programs when solving math
 problems.

 My problem is that our technicians (sp?) have succeded in installing
 sage at an internal server at our school, but we have found no
 information about how to create accounts other than the one admin
 account that is created automaticly. Does anyone know any solution to
 this problem?

If you type

  sage:  notebook?

and read the help there is an option accounts=True
which turns on letting users create accounts, e.g.,

   sage: notebook(..., accounts=True)

You could either (1) just leave that on, or (2) turn it on, create all
accounts you
want to have, and turn it off.

This is obviously not the best solution -- we haven't had time to implement
something better.  Volunteers welcome!

William

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: Sage LAN server

2008-01-09 Thread bill purvis

On Wednesday 09 January 2008, William Stein wrote:
 On Jan 9, 2008 6:53 AM, Magnus [EMAIL PROTECTED] wrote:
  First let me say that I am a sage-newbie. I work at a high-school in
  Sweden as a math teacher. Our curriculum states that we are to teach
  our students in the using of computer programs when solving math
  problems.
 
  My problem is that our technicians (sp?) have succeded in installing
  sage at an internal server at our school, but we have found no
  information about how to create accounts other than the one admin
  account that is created automaticly. Does anyone know any solution to
  this problem?

 If you type

   sage:  notebook?

 and read the help there is an option accounts=True
 which turns on letting users create accounts, e.g.,

sage: notebook(..., accounts=True)

 You could either (1) just leave that on, or (2) turn it on, create all
 accounts you
 want to have, and turn it off.

 This is obviously not the best solution -- we haven't had time to implement
 something better.  Volunteers welcome!

 William

I was about to ask the same question. Having read the info supplied by
'notebook?' I decided to have a go. Logged in as Admin, I typed:

nb = load('sage_notebook/nb.sobj')

and got file not found. After some searching around I found 'sage_notebook'
in my ~/.sage directory, so I tried:

nb = load('/home/bill/.sage/sage_notebook/nb.sobj')

and got:
-
Exception (click to the left for traceback):
...
zlib.error: Error -3 while decompressing data: incorrect header check

Traceback (most recent call last):
  File stdin, line 1, in module
  File /home/bill/.sage/sage_notebook/worksheets/admin/4/code/2.py, line 4, 
in module
nb=load(\u0027/home/bill/.sage/sage_notebook/nb.sobj\u0027)
  
File 
/home/bill/maths/sage-2.9/local/lib/python2.5/site-packages/sympy/plotting/, 
line 1, in module

  File sage_object.pyx, line 443, in sage.structure.sage_object.load
  File sage_object.pyx, line 532, in sage.structure.sage_object.loads
  File sage_object.pyx, line 530, in sage.structure.sage_object.loads
zlib.error: Error -3 while decompressing data: incorrect header check

Any suggestion as to what has gone wrong and how to get out of it?

Bill
-- 
+---+
| Bill Purvis, Amateur Mathematician|
|  email: [EMAIL PROTECTED]  |
|  http://bil.members.beeb.net  |
+---+

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: associativity of addition on ell. curves

2008-01-09 Thread John Cremona

As far as I know you cannot check associativity in this naive way.

For a start, nowhere in your code do you use the equation of the
curve.  If that is (say) y^2=x^3+a*x+b, then your equation will only
be correct modulo the relations y1^2=x1^3+a*x1+b and so on.

However, even with that I don't think that this can be verified using
pure computer algebra.  Recall that the actual group law is not
defined only by the rule you are using (addition of points with
distinct x coordinates) since you need to take into account the fact
that your equation is really projective, and the group law is defined
differently on different patches.

I once had an interesting conversation about exactly this with Don
Zagier.  He told me something interesting and relevant to this
discussionm but it was 10 years ago and I cannot remember.  It would
be fun to look into exactly why the approach does not work.

John Cremona

On 09/01/2008, achrzesz [EMAIL PROTECTED] wrote:

 Hello
 My question is
 connected with   file http://modular.fas.harvard.edu/ent/ent_py
 and especially with checking the associativity law of addition on ell.
 curves.
 When I was trying to perform the same calculations in Sage I observed
 some surprising (me) behaviour.
 Enclosed is the corresponding Sage code.

 # the first 12 lines of code  is modyfied version of
 # http://modular.fas.harvard.edu/ent/ent_py
 # necessary changes seem to be inessential

 x1 = var('x1'); x2 = var('x2'); x3 = var('x3')
 y1 = var('y1'); y2 = var('y2'); y3 = var('y3')
 a  = var('a'); b  = var('b')
 lambda12 = (y1 - y2)/(x1 - x2)
 x4   = (lambda12*lambda12 - x1 - x2)
 nu12 = (y1 - lambda12*x1)
 y4   = (-lambda12*x4 - nu12)
 lambda23 = ((y2 - y3)/(x2 - x3))
 x5   = (lambda23*lambda23 - x2 - x3)
 nu23 = (y2 - lambda23*x2)
 y5   = (-lambda23*x5 - nu23)
 s1 =(x1 - x5)*(x1 - x5)*((y3 - y4)*(y3 - y4) - (x3 + x4)*(x3 - x4)*(x3
 - x4))
 s2 = (x3 - x4)*(x3 - x4)*((y1 - y5)*(y1 - y5) - (x1 + x5)*(x1 -
 x5)*(x1 - x5))


 #the code below looks reasonable but gives wrong result; I wonder why?

 t1=s1.factor()
 t2=s2.factor()
 R=QQ[x1,x2,x3,y1,y2,y3]
 print R(t1.denominator())==R(t2.denominator())   # True
 #denominators of s1,s2 are equal, so let's compare numerators
 print R(t1.numerator())==R(t2.numerator())# False (in Sage 2.9)


 I would be grateful for any comments.
 Is there an easy way to check the mentioned associativity in Sage?
 A. Chrzeszczyk


 



-- 
John Cremona

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: Sage LAN server

2008-01-09 Thread bill purvis

On Wednesday 09 January 2008, bill purvis wrote:
 On Wednesday 09 January 2008, William Stein wrote:
  On Jan 9, 2008 6:53 AM, Magnus [EMAIL PROTECTED] wrote:
   First let me say that I am a sage-newbie. I work at a high-school in
   Sweden as a math teacher. Our curriculum states that we are to teach
   our students in the using of computer programs when solving math
   problems.
  
   My problem is that our technicians (sp?) have succeded in installing
   sage at an internal server at our school, but we have found no
   information about how to create accounts other than the one admin
   account that is created automaticly. Does anyone know any solution to
   this problem?
 
  If you type
 
sage:  notebook?
 
  and read the help there is an option accounts=True
  which turns on letting users create accounts, e.g.,
 
 sage: notebook(..., accounts=True)
 
  You could either (1) just leave that on, or (2) turn it on, create all
  accounts you
  want to have, and turn it off.
 
  This is obviously not the best solution -- we haven't had time to
  implement something better.  Volunteers welcome!
 
  William

 I was about to ask the same question. Having read the info supplied by
 'notebook?' I decided to have a go. Logged in as Admin, I typed:

 nb = load('sage_notebook/nb.sobj')

 and got file not found. After some searching around I found 'sage_notebook'
 in my ~/.sage directory, so I tried:

 nb = load('/home/bill/.sage/sage_notebook/nb.sobj')

 and got:
 -
 Exception (click to the left for traceback):
 ...
 zlib.error: Error -3 while decompressing data: incorrect header check

 Traceback (most recent call last):
   File stdin, line 1, in module
   File /home/bill/.sage/sage_notebook/worksheets/admin/4/code/2.py, line
 4, in module
 nb=load(\u0027/home/bill/.sage/sage_notebook/nb.sobj\u0027)

 File
 /home/bill/maths/sage-2.9/local/lib/python2.5/site-packages/sympy/plotting
/, line 1, in module

   File sage_object.pyx, line 443, in sage.structure.sage_object.load
   File sage_object.pyx, line 532, in sage.structure.sage_object.loads
   File sage_object.pyx, line 530, in sage.structure.sage_object.loads
 zlib.error: Error -3 while decompressing data: incorrect header check
 
 Any suggestion as to what has gone wrong and how to get out of it?

 Bill

Oh, yes, and I meant to add - what is the last argument to add_user?
I tried typingadd_user?but as I don't have the right object
to hang it onto it says it's undefined. The help info on notebook?
just says
nb.add_user(username,password,email-address,user)
with no further explanation. The first 3 are fairly obvious but the
last isn't.

Bill

-- 
+---+
| Bill Purvis, Amateur Mathematician|
|  email: [EMAIL PROTECTED]  |
|  http://bil.members.beeb.net  |
+---+

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: associativity of addition on ell. curves

2008-01-09 Thread John Cremona

Thanks Paul, I was too lazy to try that myself.  Now I am puzzled by
what I remembered being possible and impossible, but never mind -- we
have answered the original question!

John

On 09/01/2008, Paul Zimmermann [EMAIL PROTECTED] wrote:

John,

  As far as I know you cannot check associativity in this naive way.
 
  For a start, nowhere in your code do you use the equation of the
  curve.  If that is (say) y^2=x^3+a*x+b, then your equation will only
  be correct modulo the relations y1^2=x1^3+a*x1+b and so on.

 this is correct, but the first statement is not, since Maple can do it
 (sorry for that :-)

 |\^/| Maple 10 (IBM INTEL LINUX)
 ._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2005
  \  MAPLE  /  All rights reserved. Maple is a trademark of
     Waterloo Maple Inc.
   |   Type ? for help.
  lambda12 := (y1 - y2)/(x1 - x2):
  x4   := (lambda12*lambda12 - x1 - x2):
  nu12 := (y1 - lambda12*x1):
  y4   := (-lambda12*x4 - nu12):
  lambda23 := ((y2 - y3)/(x2 - x3)):
  x5   := (lambda23*lambda23 - x2 - x3):
  nu23 := (y2 - lambda23*x2):
  y5   := (-lambda23*x5 - nu23):
  s1 :=(x1 - x5)*(x1 - x5)*((y3 - y4)*(y\
  3 - y4) - (x3 + x4)*(x3 - x4)*(x3 - x4)):
  s2 :=(x3 - x4)*(x3 - x4)*((y1 - y5)*(y\
  1 - y5) - (x1 + x5)*(x1 - x5)*(x1 - x5)):
  numer(s1-s2):
  simplify(%, {y1^2=x1^3+a*x1+b,y2^2=x2^3+a*x2+b,y3^2=x3^3+a*x3+b});
0

 Note the simplify(..., {eqs}) command, which computes the normal form of a
 polynomial with respect to a set of polynomial equations. This is quite useful
 for the user who is not aware of Gröbner bases (or the aware-user who prefers
 a simple command). Does a similar command exist in SAGE?

 Paul Zimmermann

 



-- 
John Cremona

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] vmware for Sage 2.9.2

2008-01-09 Thread Peter

Hi,

I tried the Sage 2.9.2 vmware for Windows version (downloaded from
www.sagemath.org), and it does not seems to have the jmol 3d graphics
(this is on a Dell laptop with XP).

version()

reports 2.9.1, so perhaps it is a previous version? The vmware 2.9.2
distributed by DVD at the JMM08 conference works fine with 3d
graphics (it has size 732MB vs 721MB for the file on www.sagemath.org)

Another (minor) issue is that

%latex
$\sqrt 2$

does not seem to work for vmware Sage 2.9.2 (DVD version)

Just mentioning these issues since Sage got a lot of interest at JMM,
so the vmware 2.9.2 version will be downloaded by some people who want
to try the cool 3d graphics

Peter
--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] strange list behavior

2008-01-09 Thread benjamin antieau

Code for the worksheet attached below.

There must surely be a simple answer to this problem, but I have not
been able to figure it out. I loop through i,j print the list
[i,j], and append the list to pts. However, once appended to points
something goes wrong, and all that points sees are the constant lists
[3,3]. I have changed the various constants and such, but I am at a
loss to explain why this is happening.

What I need is for pts to contain the correct information. Any help
would really be appreciated.

Ben

{{{id=125|
P=[0,0]
i=0
pts=[]
while i4:
P[0]=i

j=0
while j4:
P[1]=j
print P
pts.append(P)
j+=1
i+=1
pts
///
[0, 0]
[0, 1]
[0, 2]
[0, 3]
[1, 0]
[1, 1]
[1, 2]
[1, 3]
[2, 0]
[2, 1]
[2, 2]
[2, 3]
[3, 0]
[3, 1]
[3, 2]
[3, 3]
[[3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3,
3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3]]
}}}

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: strange list behavior

2008-01-09 Thread Jonathan Bober

Hi.

short answer: immediately after the line

while i  4:

add the line

P = [0,0]

Your problem seems to be that when you put something in a list, python
does not make a copy of it, but instead stores that actual object in the
list. So the line

pts.append(P)

appends the object P (or, rather, the object that P references) to the
list pts. Thus, when you change P, you also 'change' the list. (A better
way to think of it, though, is that you don't actually change the list
by changing P. Instead, you might think of the list as eventually
looking like

[P, P, P, ..., P]

because all you did was append the same object a bunch of times, so of
course all of the entries in pts are the same at the end.)

The line

P = [0,0]

creates a new list (which P now references). Putting that line in the
while loop should fix the problem because you will be adding a different
object to the list each time.

A simpler example is

sage: x = ['something']
sage: y = x # now x and y are the SAME OBJECT
sage: x
['something']
sage: y
['something']
sage: x[0] = 'something else'
sage: y # since x and y are the same object, they have the same contents
['something else']
sage: x = ['something else again'] # now x references a new object,
sage: y# so y doesn't change
['something else']

-bober

On Wed, 2008-01-09 at 21:25 -0800, benjamin antieau wrote:
 Code for the worksheet attached below.
 
 There must surely be a simple answer to this problem, but I have not
 been able to figure it out. I loop through i,j print the list
 [i,j], and append the list to pts. However, once appended to points
 something goes wrong, and all that points sees are the constant lists
 [3,3]. I have changed the various constants and such, but I am at a
 loss to explain why this is happening.
 
 What I need is for pts to contain the correct information. Any help
 would really be appreciated.
 
 Ben
 
 {{{id=125|
 P=[0,0]
 i=0
 pts=[]
 while i4:
 P[0]=i
 
 j=0
 while j4:
 P[1]=j
 print P
 pts.append(P)
 j+=1
 i+=1
 pts
 ///
 [0, 0]
 [0, 1]
 [0, 2]
 [0, 3]
 [1, 0]
 [1, 1]
 [1, 2]
 [1, 3]
 [2, 0]
 [2, 1]
 [2, 2]
 [2, 3]
 [3, 0]
 [3, 1]
 [3, 2]
 [3, 3]
 [[3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3,
 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3]]
 }}}
 
  
 
 


--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---



[sage-support] Re: strange list behavior

2008-01-09 Thread benjamin antieau

This indeed fixes the problem, and clarifies some other things for me
as well.

Thanks for your quick response Jonathan.

Ben

On Jan 9, 11:59 pm, Jonathan Bober [EMAIL PROTECTED] wrote:
 Hi.

 short answer: immediately after the line

 while i  4:

 add the line

 P = [0,0]

 Your problem seems to be that when you put something in a list, python
 does not make a copy of it, but instead stores that actual object in the
 list. So the line

 pts.append(P)

 appends the object P (or, rather, the object that P references) to the
 list pts. Thus, when you change P, you also 'change' the list. (A better
 way to think of it, though, is that you don't actually change the list
 by changing P. Instead, you might think of the list as eventually
 looking like

 [P, P, P, ..., P]

 because all you did was append the same object a bunch of times, so of
 course all of the entries in pts are the same at the end.)

 The line

 P = [0,0]

 creates a new list (which P now references). Putting that line in the
 while loop should fix the problem because you will be adding a different
 object to the list each time.

 A simpler example is

 sage: x = ['something']
 sage: y = x # now x and y are the SAME OBJECT
 sage: x
 ['something']
 sage: y
 ['something']
 sage: x[0] = 'something else'
 sage: y # since x and y are the same object, they have the same contents
 ['something else']
 sage: x = ['something else again'] # now x references a new object,
 sage: y# so y doesn't change
 ['something else']

 -bober

 On Wed, 2008-01-09 at 21:25 -0800, benjamin antieau wrote:
  Code for the worksheet attached below.

  There must surely be a simple answer to this problem, but I have not
  been able to figure it out. I loop through i,j print the list
  [i,j], and append the list to pts. However, once appended to points
  something goes wrong, and all that points sees are the constant lists
  [3,3]. I have changed the various constants and such, but I am at a
  loss to explain why this is happening.

  What I need is for pts to contain the correct information. Any help
  would really be appreciated.

  Ben

  {{{id=125|
  P=[0,0]
  i=0
  pts=[]
  while i4:
  P[0]=i

  j=0
  while j4:
  P[1]=j
  print P
  pts.append(P)
  j+=1
  i+=1
  pts
  ///
  [0, 0]
  [0, 1]
  [0, 2]
  [0, 3]
  [1, 0]
  [1, 1]
  [1, 2]
  [1, 3]
  [2, 0]
  [2, 1]
  [2, 2]
  [2, 3]
  [3, 0]
  [3, 1]
  [3, 2]
  [3, 3]
  [[3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3,
  3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3], [3, 3]]
  }}}
--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~--~~~~--~~--~--~---