Re: [R] Odp: Programming: loop versus vector oriented
Hi Jan r-help-boun...@r-project.org napsal dne 17.09.2010 12:43:40: Hello Petr, but I think this is how your code really works. Did you try it? it does, but the R documentation says somewhere: Warning: for() loops are used in R code much less often than in compiled languages. Code that takes a `whole object' view is likely to be both clearer and faster in R. Yes and no. For loops are not per se slower. AFAIK lapply or apply is only wrapped loop. But especially people coming from modern compiled languages are used to loops and try to use R like C, which usually ends up in doing things which can be done by built in function by for loop. Consider counting numbers from 1 to 10. x - 1:10 # loop system.time(for(j in 1:100) { + r1-0 + for (i in 1:10) r1-r1+x[i] + }) user system elapsed 15.420.01 15.49 # sum system.time(for(j in 1:100) r2-sum(as.numeric(x))) user system elapsed 0.160.050.20 #simple math system.time(for(j in 1:100) r3-10*11/2) user system elapsed 0 0 0 all.equal(r1, r2, r3) [1] TRUE So if you do not care about built in functions and simple math you can use loop but you have to wait. So I am wondering in what way the whole object view could be applied to my function. I offered an option to split vectors according to thresholds to list and use only for cycle with three loops instead of performing as many loops as is length of your vector and checking each number n times inside of a loop. If it is quicker depends on how long are your vectors. If you want to profile your functions see ?Rprof Regards Petr Best regards, Jan The function should work on the nth elements of the two input vectors and put the result into the nth element of the output vector. So it would work like c - a + b, only instead of '+' there are more complex calculations. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Odp: Programming: loop versus vector oriented
Hi Jan Jan private jrheinlaen...@gmx.de napsal dne 18.09.2010 12:12:29: Hello Petr, thank you for your ideas. The split() looks most realistic. What about this idea: 1. Define three functions Refun1, Refun2, Refun3 for the three different sections of the calculations (same as you suggested) 2. lambda = (Re = 2320) * Refun1(Re) + ((Re 2320) (Re 65 * dk)) * Refun2(Re) etc. But my thought is that probably the values of RefunXYZ will be calculated for every value of Re, even if the condition (Re = 2320) is FALSE (= 0). So that would give a lot of unnecessary function evaluations. Probably yes but it could be only small overhead and you can check speed by system.time(result1 - your function1) system.time(result2 - your function2) all.equal(result1, result2) If you do not perceive differences you can wrap function into some loop and let it perform several times inside system.time call Regards Petr Regards, Jan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Odp: Programming: loop versus vector oriented
Hello Petr, thank you for your ideas. The split() looks most realistic. What about this idea: 1. Define three functions Refun1, Refun2, Refun3 for the three different sections of the calculations (same as you suggested) 2. lambda = (Re = 2320) * Refun1(Re) + ((Re 2320) (Re 65 * dk)) * Refun2(Re) etc. But my thought is that probably the values of RefunXYZ will be calculated for every value of Re, even if the condition (Re = 2320) is FALSE (= 0). So that would give a lot of unnecessary function evaluations. Regards, Jan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Odp: Programming: loop versus vector oriented
Hi Jan but I think this is how your code really works. Did you try it? Regards Petr Jan private jrheinlaen...@gmx.de napsal dne 16.09.2010 13:22:05: Hello Petr, If you want to get results of your function for a vector of reynolds and dk you can use function outer and probably get rid of for cycle in the function. outer(c(100, 530,2410), c(10, 150,200),lambda_wall) [,1] [,2] [,3] [1,] 0.640 0.6400 0.6400 [2,] 0.1207547 0.12075472 0.12075472 [3,] 0.1081338 0.04515774 0.04515774 that gives me an array as an answer (and does more calculations than necessary, in this case 9 instead of 3). The result should be a vector. The function should work on the nth elements of the two input vectors and put the result into the nth element of the output vector. So it would work like c - a + b, only instead of '+' there are more complex calculations. Best regards, Jan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Odp: Programming: loop versus vector oriented
Hi Jan Jan private jrheinlaen...@gmx.de napsal dne 17.09.2010 12:43:40: Hello Petr, but I think this is how your code really works. Did you try it? it does, but the R documentation says somewhere: Warning: for() loops are used in R code much less often than in compiled languages. Code that takes a `whole object' view is likely to be both clearer and faster in R. Yes and no. Try to go through R-Inferno from Patrick Burns. For loop is not necessarily slower if properly used. Your problem was specified not very clearly because only in last mail you presented that what you have is 2 equal length vectors which shall give result of the same length. The problem is that (if I remember correctly as I do not keep original mails) you have 3 distinct computations based on values in vector1. So if you want some vectorised computation you has to use either switch or ifelse or cycle. However it is not necessary to do computation one by one as in your function but only 3 times based on threshold criteria. I would do something like: 1. make 3 distinct separate functions which compute relevant values from 2 vectors in a list 2. split vectors according to thresholds to list see ?split 3. make cycle in which for each part of list proper function is used something like }not very elegant. for (i in 1:3) result[[1]] - if (i==1) fun1(mylist1[[i]], mylist2[[1]]) else if(i==2) fun2(mylist1[[i]], ylist2[[1]]) else fun3(mylist1[[i]], ylist2[[1]]) 4. reconstruct result in original vector e.g. cbind(unlist(mylist1), unlist(mylist2), unlist(result)) Regards Petr So I am wondering in what way the whole object view could be applied to my function. Best regards, Jan The function should work on the nth elements of the two input vectors and put the result into the nth element of the output vector. So it would work like c - a + b, only instead of '+' there are more complex calculations. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Odp: Programming: loop versus vector oriented
Hello Petr, but I think this is how your code really works. Did you try it? it does, but the R documentation says somewhere: Warning: for() loops are used in R code much less often than in compiled languages. Code that takes a `whole object' view is likely to be both clearer and faster in R. So I am wondering in what way the whole object view could be applied to my function. Best regards, Jan The function should work on the nth elements of the two input vectors and put the result into the nth element of the output vector. So it would work like c - a + b, only instead of '+' there are more complex calculations. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Odp: Programming: loop versus vector oriented
Hello Petr, If you want to get results of your function for a vector of reynolds and dk you can use function outer and probably get rid of for cycle in the function. outer(c(100, 530,2410), c(10, 150,200),lambda_wall) [,1] [,2] [,3] [1,] 0.640 0.6400 0.6400 [2,] 0.1207547 0.12075472 0.12075472 [3,] 0.1081338 0.04515774 0.04515774 that gives me an array as an answer (and does more calculations than necessary, in this case 9 instead of 3). The result should be a vector. The function should work on the nth elements of the two input vectors and put the result into the nth element of the output vector. So it would work like c - a + b, only instead of '+' there are more complex calculations. Best regards, Jan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Odp: Programming: loop versus vector oriented
Hi I do not want to go too much deep to internals of your function. What do you suppose to get as the result. If you want to get results of your function for a vector of reynolds and dk you can use function outer and probably get rid of for cycle in the function. outer(c(100, 530,2410), c(10, 150,200),lambda_wall) [,1] [,2] [,3] [1,] 0.640 0.6400 0.6400 [2,] 0.1207547 0.12075472 0.12075472 [3,] 0.1081338 0.04515774 0.04515774 If you want to feed vector(s) to your function it would not be so easy as you need to check some predefined conditions for laminar or turbulent or any other flow. In this case you cold check function ?switch together with separate function definitions for each type of flow. But three if's could be as good as switch. Basically your function probably works for vectors but only in certain conditions. Laminar OK lambda_wall(10:20) [1] 6.40 5.818182 5.33 4.923077 4.571429 4.27 4.00 3.764706 [9] 3.56 3.368421 3.20 Turbulent needs dk lambda_wall(2400:2410) Error: argument dk is missing, with no default But not any dk lambda_wall(2400:2410, 10) Error in if (Re 65 * dk[z]) { : missing value where TRUE/FALSE needed only a vector of the same length as reynolds lambda_wall(2400:2410, 10:21) [1] 0.10815993 0.10325278 0.09912072 0.09558754 0.09252750 0.08984834 [7] 0.08748069 0.08537137 0.08347884 0.08177018 0.08021892 So before trying to elaborate your function further what shall be inputs and what is desired output? Regards Petr r-help-boun...@r-project.org napsal dne 15.09.2010 11:57:28: Dear all, I am new to R and to it's programming philosophy. The following function is supposed to work on a vector, but I can't figure out how to do that without looping through every element of it. Is there a more elegant way? Note: I have shortened it, so it is NOT correct from the pipe hydraulics point of view # Calculate wall friction factor # reynolds: Reynolds number # dk: relative roughness of pipe lambda_wall - function (reynolds, dk) { z - 1 result - 0 for (Re in reynolds) { if (Re = 2320) { # Laminar flow lambda - 64/Re } else if (Re 65 * dk[z]) { # Turbulent flow if (Re 1e+5) { lambda - 0.3164 / sqrt(sqrt(Re)) } else { lambda - 0.309/(log10(Re/7))^2 } } else { # Intermediate area lambdanew - 1 / (2 * log10(3.71 * dk[z]))^2 # Start value iter - 0 repeat { lambda - lambdanew lambdanew - 1 / (2 * log10(2.51/(Re * sqrt(lambda)) + 0.27/dk[z]))^2 iter - iter + 1 if ((abs(lambdanew - lambda) 0.001) || (iter 100)) break } lambda = lambdanew } result[z] - lambda z - z + 1 } result } # lambda_wall() __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.