[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
Re: [sage-support] R code into Sagemath code
On Mon, 07 Nov 2011 at 03:12PM -0800, Chappman wrote: 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] } } I know nothing about R, but my guess is that the Sage version of the above code is: prob_att_win_dice1 = 0 for y in [2..6]: for z in [1..(y-1)]: prob_att_win_dice1 += probs.y[y] * probs.z[z] You'll have to make sure that probs.y and probs.z have the right probabilities for that to work. Note that there's a much more compact way to do that summation: just write sum(probs.y[y] * probs.z[z] for z in [1..(y-1)] for y in [2..6]) (Not tested; you may need to reverse the for y and for z bits.) I would recommend reading up on basic Python programming, which would cover loops and so on. Dan -- --- Dan Drake - http://mathsci.kaist.ac.kr/~drake --- signature.asc Description: Digital signature
[sage-support] Re: Issues with assigning color values to multiple plots.
The only issue I have with these lines of code is the inability to change the plot names on the legend. right now it shows up as Plot 1, Plot 2, Plot 3, Plot 4. I would like the legend list to show Jerk, Acceleration, Velocity, and Distance. I have messed with the coding, but it looks like the %d has to be in the for the program to work. On Oct 27, 4:09 pm, Jason Grout jason-s...@creativetrax.com wrote: On 10/27/11 6:51 PM, Eric Kangas wrote: Hi, I am working on a standard plot function for multiple plots. I would like to have each plot in a different color to distinguish each function, and also able to show a legend with the color, and function. Here is what I have right now: plot([derivative((f(x), a) for a in [0,1,..3]], (x,0,2*pi), color = ()) Try something like: colors=rainbow(len([0,1,..3])) sum(plot(derivative((f(x), a) , (x,0,2*pi), color = colors[i],label=Plot %d%i) for i,a in enumerate([0,1,..3])) Make each curve an individual plot, and then sum them together. Jason -- 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: Issues with assigning color values to multiple plots.
On 11/7/11 6:16 PM, Eric Kangas wrote: The only issue I have with these lines of code is the inability to change the plot names on the legend. right now it shows up as Plot 1, Plot 2, Plot 3, Plot 4. I would like the legend list to show Jerk, Acceleration, Velocity, and Distance. I have messed with the coding, but it looks like the %d has to be in the for the program to work How about making the names part of the things we iterate over: myplots=[(0,'Distance'), (1,'Velocity'),(2,'Acceleration'),(3,'Jerk')] colors=rainbow(len(myplots)) sum(plot(derivative(f, a) , (x,0,2*pi), color = colors[a],legend_label=name) for a,name in myplots) Thanks, Jason k. On Oct 27, 4:09 pm, Jason Groutjason-s...@creativetrax.com wrote: On 10/27/11 6:51 PM, Eric Kangas wrote: Hi, I am working on a standard plot function for multiple plots. I would like to have each plot in a different color to distinguish each function, and also able to show a legend with the color, and function. Here is what I have right now: plot([derivative((f(x), a) for a in [0,1,..3]], (x,0,2*pi), color = ()) Try something like: colors=rainbow(len([0,1,..3])) sum(plot(derivative((f(x), a) , (x,0,2*pi), color = colors[i],label=Plot %d%i) for i,a in enumerate([0,1,..3])) Make each curve an individual plot, and then sum them together. Jason -- 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: R code into Sagemath code
Another thing you could do, in the notebook, is to evaluate all your R code with %r, and then use the last answer at http://ask.sagemath.org/question/807/converting-r-variables-to-sage to access your variables. But I hope that Dan's idea works too, that would be very nice. - kcrisman -- 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