Yes I do want a function of some sort here, but I do not want a "def chaps(u,v)" like what anton has done for now, both for my simplified and larger problem. Is there a solution/method to this?
Kind Regards Chappman On Feb 7, 9:35 pm, Robert Bradshaw <rober...@math.washington.edu> wrote: > On Tue, Feb 7, 2012 at 1:07 PM, Chappman <chappman....@gmail.com> wrote: > > Hi Rob, > > I am pretty new to Sage and am not used to the syntex, so even though > > I write [y1,y2] , i do not mean it as a list. > > Basically what I am trying to do is try and get the folllowing code to > > work, if the summation does come out correct ly > > x would be equal to 5 , i.e. x=5. > > Sage will interpret [y1, y2] as a list (as will others reading your code). > > > > > > > > > > > I would not need to specify when y1<y2 because I have made a rule in > > my summation that y2 in [1..y1], so y2 can never be bigger than y1. > > > So the way I want my code to work is basically, when doing the > > summation, the first one would be [y1,y2] = [1,1], then using my > > previously set criteries > > > if y1=y2: > > [y1,y2]=2 > > elif y1>y2: > > [y1,y2]=1 > > > this would make [y1,y2] = [1,1]=2 > > my second summation would be [y1,y2] = [2,1]=1 > > my last summation would be [y1,y2] = [2,2]=2 > > > so then x += [y1,y2] = 5 > > > ------------------------------------- > > if y1=y2: > > [y1,y2]=2 > > elif y1>y2: > > [y1,y2]=1 > > > x=0 > > for y1 in [1..2]: > > for y2 in [1..y1]: > > x += [y1,y2] > > print x > > ------------------------------------- > > > Is there a method of not using a function like "def chap(u,v)" for > > this right now, because this is just a simplified problem, of my > > larger problem. Thank you for taking your time looking at this. > > If I understand your intent correctly, you do want a function here, > both for the simplified and larger problem. Is there a reason that > this doesn't work for you? > > > > > > > > > On Feb 7, 8:07 pm, Anton Sherwood <bro...@pobox.com> wrote: > >> On 2012-2-07 01:18, Chappman wrote: > > >> > Hi Rob, > > >> > with this syntex: > > >> >> x=0 > >> >> for y_1 in [1..2]: > >> >> for y_2 in [1..y_1]: > >> >> x += [y_1,y_2] > >> >> print x > > >> > what I am trying to do is, trying to use the two numbers y_1 and y_2 > >> > in x +=[y_1,y_2] > >> > to assign it a number from previously set conditions > > >> >> if y_1 = y_2: > >> >> y_1 = y_2 = 2 > >> >> elif y_1>y_2: > >> >> y_1 = y_2 = 1 > > >> > but currently my code is having trouble doing that. > >> > Is there a way to do this please? > > >> Are you trying to define [u,v] as a function whose value is 2 if the > >> arguments are equal and 1 if u>v? (What if v<u?) Among other syntactic > >> problems, you can't do that with [], because that symbol is reserved for > >> lists. > > >> Here's how I'd do what I think you're trying to do: > > >> # define a function of two inputs > >> def chap(u,v): > >> if u==v: return 2 > >> # no 'else' needed, because 'return' breaks out of the function > >> if u>v: return 1 > >> return None # ought to be a numeric value > > >> x=0 > >> for y1 in range(1,3): > >> for y2 in range(1,y1+1): > >> x += chap(y1,y2) > >> print x > > >> -- > >> Anton Sherwood *\\*www.bendwavy.org*\\*www.zazzle.com/tamfang > > > -- > > To post to this group, send email to sage-support@googlegroups.com > > To unsubscribe from this group, send email to > > sage-support+unsubscr...@googlegroups.com > > For more options, visit this group > > athttp://groups.google.com/group/sage-support > > URL:http://www.sagemath.org -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org