[sage-support] Re: CSV file
Thank you very much for that code. Just to clarify, I'm using the sage notebook (running on my machine), will the code still work? I.e. be able to read where the files are on my machine even when using the notebook interface? Thank you very much for your help. Chappman -- 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
[sage-support] Re: CSV file
Hi Harald, You answered I should of phrased that question better, but lets say I created a matrix in Sage, and I want to save it as a csv file how do I do that? The matrix is made from the following function below Def function(A,D) (syntex for making matrix P) return P Kind Regards Chappman On Feb 24, 6:03 pm, Harald Schilly wrote: > On Friday, February 24, 2012 11:13:53 AM UTC+1, Chappman wrote: > > > and then using a function which opens up the CSV file and utilizes the > > entires in the matrix P, from the CSV file. > > Is there a method for this? > > uhm, i'm not sure if you ask about reading or writing. also, your "d" in > def is uppercase. > > reading from this file matrix.csv: > 1,2,3 > 2,2,-1.1 > 0,0,1 > > works like this: > sage: import csv > sage: data = list(csv.reader(file("matrix.csv"))) > sage: m = matrix([[ float(_) for _ in line] for line in data]) > sage: m > [ 1.0 2.0 3.0] > [ 2.0 2.0 -1.1] > [ 0.0 0.0 1.0] > > the other way around works like this (i print this, writing to the file is > trivial) > > for line in m.rows(): > print ','.join((str(_) for _ in line)) > > gives > > 1.0,2.0,3.0 > 2.0,2.0,-1.1 > 0.0,0.0,1.0 > > h -- 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
[sage-support] CSV file
Hi folks, I am having trouble trying to save a matrix P as a CSV file in Sage: Def function(A,D): (sage syntax for creating a matrix P) return P and then using a function which opens up the CSV file and utilizes the entires in the matrix P, from the CSV file. Is there a method for this? Kind Regards Chappman -- 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
[sage-support] Re: functions
What y ou suggested all I tried it again today, thank you very much for the help, I did use the def function that you suggest. All the help and input is being put to good use. Kind Regards Chappman On Feb 8, 6:48 pm, Anton Sherwood wrote: > Chappman wrote: > > >> 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, [...] > > On 2012-2-08 02:39, Robert Bradshaw wrote: > > > It sounds like what you're saying is "I want a function, > > but I don't want a function." [...] > > Or "I want a function, but I don't like Python's function syntax." > > -- > 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 at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
[sage-support] Re: functions
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 wrote: > On Tue, Feb 7, 2012 at 1:07 PM, Chappman 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 > 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 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 >> 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
[sage-support] Re: functions
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. I would not need to specify when y1y2: [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. Kind Regards Chappman On Feb 7, 8:07 pm, Anton Sherwood 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 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 at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
[sage-support] Re: functions
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? Kind Regards Chappman On Feb 6, 11:23 pm, Robert Bradshaw wrote: > On Mon, Feb 6, 2012 at 3:12 PM, Chappman wrote: > > Hi, I am currently trying to write a program for my project and am > > trying to make the following syntax work: > > > if y_1=y_2: > > [y_1,y_2]=2 > > elif y_1>y_2: > > [y_1,y_2]=1 > > I think you want is > > > if y_1 = y_2: > > y_1 = y_2 = 2 > > elif y_1>y_2: > > y_1 = y_2 = 1 > > "[a,b] = x" essentially means "tmp = list(x); a = tmp[0]; b = tmp[1]" > > > x=0 > > for y_1 in [1..2]: > > for y_2 in [1..y_1]: > > x += [y_1,y_2] > > print x > > What are you trying to do here? x is an integer, [y_1, y_2] is a list > of integers. It doesn't really make sense to add an integer to a list. > > > > > > > > > The error messages I am getting is " TypeError: > > 'sage.rings.integer.Integer' object is not iterable ". > > > When this code is fixed and does work, the answer would be x=5. > > Can anybody see where my code is going wrong please? > > > Kind Regards > > Chappman > > > -- > > 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
[sage-support] functions
Hi, I am currently trying to write a program for my project and am trying to make the following syntax work: if y_1=y_2: [y_1,y_2]=2 elif y_1>y_2: [y_1,y_2]=1 x=0 for y_1 in [1..2]: for y_2 in [1..y_1]: x += [y_1,y_2] print x The error messages I am getting is " TypeError: 'sage.rings.integer.Integer' object is not iterable ". When this code is fixed and does work, the answer would be x=5. Can anybody see where my code is going wrong please? Kind Regards Chappman -- 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
[sage-support] Re: matrix help
Is it possible to make an entry in Q[i,j] equal to a entry in U[i,j]? I have to have the two functions separately for each matrix though just like my code above i.e. def U(N,M): ... ... ... return U def Q(N,M) ... ... ... Q[i,j]=U(N,M) return Q Kind Regards Chappman On Dec 3, 4:26 am, "D. S. McNeil" wrote: > > def U(N,M): > > U=matrix(ZZ,N*M) > > for i in range(N*M): > > for j in range(N*M): > > U[i,j]=1 > > return U > > def Q(N,M): > > Q=matrix(ZZ,N*M) > > for i in range(N*M): > > for j in range(N*M): > > Q[i,j]=U(N,M) > > return Q > > U(N,M) is a function which returns a matrix. The line Q[i,j]=U(N,M) > attempts to set the (i,j)-th entry of the Q matrix, defined over ZZ, > to an NxM matrix, which isn't going to work. (Hence the "cannot > coerce a matrix to an integer" error message -- it's trying to fit > U(N,M) into an entry of Q, and it can't figure out how to do it.) > > BTW, you might also be interested in the command ones_matrix: > > sage: ones_matrix(ZZ,3,4) > [1 1 1 1] > [1 1 1 1] > [1 1 1 1] > > Doug -- 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
[sage-support] matrix help
Hi folks, I am currently writing a code which uses a matrix in a previously defined function written in the same sage window, but I keep on getting this error message : TypeError: unable to coerce to an integer. thank you for your time. Kind Regards Chappman my code is this : def U(N,M): U=matrix(ZZ,N*M) for i in range(N*M): for j in range(N*M): U[i,j]=1 return U def Q(N,M): Q=matrix(ZZ,N*M) for i in range(N*M): for j in range(N*M): Q[i,j]=U(N,M) return Q -- 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
[sage-support] unsupported operand type(s) ????????
HI folks, In Sagemath I am trying to compute this a=[1,1] b=[4,3] b-a=[3,2] the answer I want is b-a=[3,2] but in sage i come up with a type Error: unsupported operand type(s) for -: 'list' and 'list'. Is there any way of rectifying this problem and so that I can compute b-a ? Kind Regards Chappman -- 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
[sage-support] if statements and arguments in sage
Hi folks, When I try and do to two arguments in the same lines in sagemath it does not compute, this is what I have written: if A==1 and D==1 then case <- 1 else if A==2 and D==1 then case <- 2 and if case==1 then probsy <- list1 and probsz <- list1 else if case==2 then probsy <- list2 and probsz <- list1 I keep on getting syntax errors when I write these, can anybody shed some light on this please. Kind Regards Chappman -- 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
[sage-support] R code into Sagemath code
HI folks, after much reading and trial and error I have came to a brick wall with this. I have made a code in R GUI which had "for loops and summations equations" that links back to sets/lines of arrays/matrix data. But here is the problem, I have to do this in SageMath programming, so I have to change the syntax completely Here is the syntax, it is a shortened version of the real thing, but the main crux of the problem i'm having is in the for loop i'm having trouble trying to link my data in my matrix to that iterative equation. Thank you for all for your time and patience. C **I start by defining my function with two variables army.A (Attackers) , army.D (defenders)** tp <- function(army.A,army.D){ **so depending on how many army I got , then it assigns to the variable a number of dice** **, and gives it a case, in this code I gave it case 4 and 5. ** if (arm.A==1 & arm.D>=2) {dice.A <- 1; dice.D <- 2; case <- 4} else if (arm.A==2 & arm.D>=2) {dice.A <- 2; dice.D <- 2; case <- 5} **These are the dice probabilities stored as a matrix** global.probs <- matrix(NA,ncol=6,nrow=2) global.probs[1,] <- rep(1,6)/6 global.probs[2,] <-c(1,3,5,7,9,11)/36 **based on how many dice the attacker and defender have, get the** **probabilities out of the matrix, i.e. if case 4, probs,y <- global probs[1,]** **( the dice probability stored as a matrix)** if (case==4){probs.y <- global.probs[1,]; probs.z <- global.probs[2,]} else if (case==5){probs.y <- global.probs[2,]; probs.z <- global.probs[2,]} else **here is my for loop, a first make prob.att.win.dice1 equal to 0 ** ** prob.att.win.dice1 <- 0 for (y in 2:6){ for (z in 1:(y-1)){ prob.att.win.dice1 <- prob.att.win.dice1 + probs.y[y]*probs.z[z] } } -- 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
[sage-support] Re: Creating Arrays in sage, and then writing a programme which refers back to the array and uses it.
yes I am just trying to reasign totol prob again so that a new value of total prob comes out . On Nov 1, 12:24 am, Dan Drake wrote: > On Mon, 31 Oct 2011 at 03:09PM -0700, Chappman wrote: > > Hi folks, I am relatively new to programming in Sagemath, and I am > > trying > > to find a method in of creating a n x n array, with probabilities as > > the entries of this array . > > Use a matrix, or even simpler, a list of lists. > > sage: m = matrix(3, [[1, 2, 3], [4,5,6], [7,8,9]]) > > > Here's the tricky part, then I have to create a coding program which > > uses the values in this array. > > > For i=1 to 5; > > For j=1 to i-1; > > total_prob= (some type of simple formula which refers back > > to entries in the array) > > end; > > end; > > It looks like you're just reassigning total_prob again and again. Is > that what you want? > > Dan > > -- > --- Dan Drake > - http://mathsci.kaist.ac.kr/~drake > --- > > signature.asc > < 1KViewDownload -- 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
[sage-support] Creating Arrays in sage, and then writing a programme which refers back to the array and uses it.
Hi folks, I am relatively new to programming in Sagemath, and I am trying to find a method in of creating a n x n array, with probabilities as the entries of this array . Here's the tricky part, then I have to create a coding program which uses the values in this array. For i=1 to 5; For j=1 to i-1; total_prob= (some type of simple formula which refers back to entries in the array) end; end; Is it possible to do this in Sage? -- 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