[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 541 KB, 1450x1100, R.png [View same] [iqdb] [saucenao] [google]
6030798 No.6030798 [Reply] [Original]

How do I define recursive formulas in R? Very new to programming, and I'm trying construct a recursive formula, but I keep getting error messages. What I've got:

> y <- function(y) {
+ if(y==1)
+ return(1.5)
+ else
+ return(1.022*function(y-1))

At which point I receive:
Error: unexpected '-' in:
"else
return(1.022*function(y-"

I'm just really confused because I created my code my mimicking a recursive formula (the factorial formula) that I copy+pasted into R which worked just fine, so where's the problem with mine?

>> No.6030799

Related: Has anyone else noticed that R is unnecessarily difficult? Would it be worth the cash for me to spring for stata, which I've heard is much more user-friendly?

>> No.6030806 [DELETED] 

>exponential recursive function

The fuck are you doing? Just use the linear iterative formulation

double result=1.5, x=1.022;
while(y){
if (y&1) result*=x;
x=*x;
y>>=1;
}
return result;

>> No.6030810

>>6030798
>exponential recursive function

The fuck are you doing? Just use the linear iterative formulation

double result=1.5, x=1.022;
y--;
while(y){
if (y&1) result*=x;
x=*x;
y>>=1;
}
return result;

>> No.6030817

>>6030810
So many errors in this I don't even know where to begin

>> No.6030823

>>6030817
What errors? It returns 1.5*1.022^(y-1) and only uses linear amounts of space and time unlike OP experiential time and space function.

The only error I see is x=*x; instead of x*=x;

>> No.6030825

I'm completely unfamiliar with R, so can someone explain to me the advantages of R over, say, Matlab or Python? Some people seem to swear by it but I'm not entirely sure why.

>> No.6030828

>>6030823
Did you intend for me to copy and paste this directly into my R window? I can just show you what I got:

> double result=1.5, x=1.022;
Error: unexpected symbol in "double result"
> y--;
Error: unexpected ';' in "y--;"
> while(y){
+ if (y&1) result*=x;
Error: unexpected '=' in:
"while(y){
if (y&1) result*="
> x=*x;
Error: unexpected '*' in "x=*"
> y>>=1;
Error: unexpected '>=' in "y>>="

Also i'm not sure why you thought I was trying to construct an exponential function (is that what my code indicates?). I'm just trying to construct a linear recursion.

>> No.6030829

>>6030828
Clearly it's C++/C pseudo code

>> No.6030830

>>6030829
Given that I'm very new to programming (which is made explicit in the OP), this is not clear at all.

>> No.6030852

>>6030830

result<-1.5
x<-1.022
y<-y-1
while(y){
if (bitAnd(y,1)) result<-result*x
x<-x*x
y<-bitShiftL(y,1)
}
return(result)

>> No.6030951

>>6030825
It's free as in freedom.

>> No.6030981

>>6030951
But so is Python.

>> No.6031054

>>6030798
> y <- function(z) {
+ if(z==1)
+ 1.5
+ else
+ 1.022*y(z-1)
+ }
> y(5)
[1] 1.63642

>> No.6031084

>>6030951
free as in filthy communism

>> No.6031176

>>6031054
God damn you'll probably never know just how much I appreciate this. I tried for so long, posted on so many forums, to find information on how to construct a recursive formula in R. Good information is actually hard to come by.

>> No.6031616

>>6031176
are you kidding ?
http://en.wikibooks.org/wiki/R_Programming/Working_with_functions#Recursive_functions