Re: [R] Odp: Programming: loop versus vector oriented

2010-09-20 Thread Petr PIKAL
Hi Jan

Jan private  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

2010-09-20 Thread Petr PIKAL
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

2010-09-18 Thread Jan private
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

2010-09-17 Thread Jan private
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

2010-09-17 Thread Petr PIKAL
Hi Jan

Jan private  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

2010-09-16 Thread Petr PIKAL
Hi Jan

but I think this is how your code really works. Did you try it?

Regards
Petr

Jan private  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

2010-09-16 Thread Jan private
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.