pearselab / r-world-MaggiK

r-world-MaggiK created by GitHub Classroom
0 stars 0 forks source link

terrain #2

Open MaggiK opened 7 years ago

MaggiK commented 7 years ago

I have the diamond step function for the entire matrix. I can fill in the rest of the matrix outside this function. When I try to add the "other_squares" or part where it fills in the matrix I receive this error: Error: evaluation nested too deeply: infinite recursion / options(expressions=)? Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

I tried making the fill in stuff another function and I occasionally get the same error. I am not sure how to fix this error. Thanks! Maggi

MaggiK commented 7 years ago

Actually the fill in the squares does work in a separate function. But I can't add this function into the overall diamond square step. Also, do I need to make the other_squares/fill in the NA part a for loop?

willpearse commented 7 years ago

Have you resolved this? It sounds like you've got yourself into a stack overflow. Come online to https://appear.in/usubioprogramming if you want to talk about it...

MaggiK commented 7 years ago

This doesn't work but should the for loop be something like this?

diamond_square_step<- function(mat){ mat<-diamond_step(mat) mat<- square_step(mat) for i in 1:ncol(mat){ med_x<- median(1:i) med_y<- median(1:i) len_x<- ncol(mat) len_y<- nrow(mat) mat[1:med_x, 1:med_y]<- diamond_square_step(mat[1:med_x, 1:med_y]) mat[1:med_x, med_y:len_y]<- diamond_square_step(mat[1:med_x, med_y:len_y]) mat[med_y:len_y, 1:med_x]<- diamond_square_step(mat[med_y:len_y, 1:med_x]) mat[med_y:len_y, med_x:len_x]<- diamond_square_step(mat[med_y:len_y, med_x:len_x]) } return(mat) }

This doesn't work. I am not quite sure what the for loop should be iterating over. Right now I have the number of columns in the matrix.

willpearse commented 7 years ago

Have you read my recursion section yet?...

What is your edge condition in this recursion? I.e., at what point will your function not just keep calling itself, and will return back a value. Reply back, in this thread, what you think the edge condition is. You don't have one right now, and by thinking in terms of the edge condition you will be able to write recursive code.

If you want to do this recursively, I really would recommend completing those exercises. Recursion is not something to jump in on a hard case - it's quite straightforward (you're thinking recursively naturally, so I don't want to put you off it) but it helps to do an easy one first to get your mind in gear. Once you've read that section, you'll almost certainly want to use Recall as it will speed up the execution in something of this size.

MaggiK commented 7 years ago

I am trying not to make it recursive but I don't understand what I am iterating through in a for loop. So something like this shouldn't be recursive right? But it won't let me run this.

diamond_square_step<- function(mat){ mat<-diamond_step(mat) mat<- square_step(mat) for i in ncol(mat){ med_x<- median(1:i) med_y<- median(1:i) len_x<- ncol(mat) len_y<- nrow(mat) mat[1:med_x, 1:med_y]<- diamond_step(mat[1:med_x, 1:med_y]) mat[1:med_x, med_y:len_y]<- diamond_step(mat[1:med_x, med_y:len_y]) mat[med_y:len_y, 1:med_x]<- diamond_step(mat[med_y:len_y, 1:med_x]) mat[med_y:len_y, med_x:len_x]<- diamond_step(mat[med_y:len_y, med_x:len_x]) mat[1:med_x, 1:med_y]<- square_step(mat[1:med_x, 1:med_y]) mat[1:med_x, med_y:len_y]<- square_step(mat[1:med_x, med_y:len_y]) mat[med_y:len_y, 1:med_x]<- square_step(mat[med_y:len_y, 1:med_x]) mat[med_y:len_y, med_x:len_x]<- square_step(mat[med_y:len_y, med_x:len_x]) return(mat) } }

On Tue, Nov 8, 2016 at 3:03 AM, Will Pearse notifications@github.com wrote:

Have you read my recursion section yet?...

What is your edge condition in this recursion? I.e., at what point will your function not just keep calling itself, and will return back a value. Reply back, in this thread, what you think the edge condition is. You don't have one right now, and by thinking in terms of the edge condition you will be able to write recursive code.

If you want to do this recursively, I really would recommend completing those exercises. Recursion is not something to jump in on a hard case - it's quite straightforward (you're thinking recursively naturally, so I don't want to put you off it) but it helps to do an easy one first to get your mind in gear. Once you've read that section, you'll almost certainly want to use Recall as it will speed up the execution in something of this size.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/pearselab/r-world-MaggiK/issues/2#issuecomment-259095333, or mute the thread https://github.com/notifications/unsubscribe-auth/ARjf-I3dtWYXGn9Jq4KYCswQ-bt8C10kks5q8Ej0gaJpZM4KrPUC .

willpearse commented 7 years ago

What error message are you getting? I can't tell you why it isn't working if you don't tell me what didn't work, if you follow me.

MaggiK commented 7 years ago

Error: unexpected symbol in: " mat<- square_step(mat) for i", E

rror: unexpected '}' in "}",

Error: no function to return from, jumping to top level,

On Tue, Nov 8, 2016 at 8:20 AM, Will Pearse notifications@github.com wrote:

What error message are you getting? I can't tell you why it isn't working if you don't tell me what didn't work, if you follow me.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/pearselab/r-world-MaggiK/issues/2#issuecomment-259165002, or mute the thread https://github.com/notifications/unsubscribe-auth/ARjf-DJ56mNdYC9ltZkdIlARD8tvi7Tgks5q8JNGgaJpZM4KrPUC .

MaggiK commented 7 years ago

Figured out that error. I won't forget the parenthesis around a for statement again in R. But now I have a new issue. The diamond_square_step works on a 5x5 matrix (woohoo!!), BUT it can't make it through the 9x9 one. So I am trying to figure out how to use seq in the for loop. I can cycle through the smaller matrices but it does it diagonally so I end up with filled in patches. I pushed my most recent code onto github. Thanks

willpearse commented 7 years ago

Something like... seq(1,9,by=8) and then seq(1,9,by=4) and then ... notice a pattern?