can anyone write this using recursion (java)

Status
Not open for further replies.

happyhippie

Seniorius Lurkius
8
JAVA
public double BisectPra()
{
double TOL = 0.0001;
int ii = 0;

double min = (CO.calcIndPrAO (isFemale));


double max = (VR.calcIndPsf (isFemale));


double left;
double mid;

CO.calcIndCOmax(isFemale);
CO.calcIndK(isFemale);
VR.calcIndRvr(isFemale);

while( Math.abs(max - min) > TOL )
{

left = (CO.calcCardOut(min) - VR.calcVenReturn(min));
midPra = (min + max) / 2;
mid = (CO.calcCardOut(midPra) - VR.calcVenReturn(midPra));

if(( left * mid ) < 0 )
{
max = midPra;
ii++;
}

else
{
min = midPra;
ii++;
}

}

if(ii>=100000)
{
midPra = -1;
}

return midPra;


}
 

happyhippie

Seniorius Lurkius
8
code:<HR>
Code:
public double BisectPra()
{
	double TOL = 0.0001; 
	int ii = 0; 
	double min = (CO.calcIndPrAO (isFemale)); <P>
	double max = (VR.calcIndPsf (isFemale)); <P>
	double left; 
	double mid; <P>	CO.calcIndCOmax(isFemale);
	CO.calcIndK(isFemale);
	VR.calcIndRvr(isFemale);<P>	while( Math.abs(max - min) > TOL ) 
	{ <P>	left = (CO.calcCardOut(min) - VR.calcVenReturn(min));
	midPra = (min + max) / 2;
	mid = (CO.calcCardOut(midPra) - VR.calcVenReturn(midPra));<P>		if(( left * mid ) < 0 )
		{
			max = midPra;
			ii++;
		}<P>		else
		{
			min = midPra;
			ii++;
		} //end if/else<P>	} //end while<P>	if(ii>=100000) 
	{
	midPra = -1; 
	} //end if<P>	return midPra;<P>
}end method
 

MrMills

Ars Praefectus
3,727
Moderator
I don't really feel like writing it out, but you could have a shell that calls a recursive function consisting mainly of the body of the while. It would basically split its part in half and then call itself with half the set, depending on the condition.

Kind of like the following simple binary search function:

Code:
int search(int *array, int key, int low, int high)
{
  int mid = (low + high) / 2;
 
  if(low > high)
    return -1;
  else if(key == array[mid])
    return mid;
  else if(key < array[mid])
    return search(array, key, low, mid - 1);
  else
    return search(array, key, mid + 1, high);
}
 

newton_meter

Ars Scholae Palatinae
1,424
Originally posted by happyhippie:
Believe me I've tried...but I thought recursion worked best with if else loops, it seems to me that this time it should be done in the while loop.

how about that?

It's not too hard. In fact, it can be done automatically. Looping is a special syntax for a limited form of recursion. Rather than just give you the fish, though, I'm going to try to teach you to fish.

Here's your original source code (cleaned up to my taste a little):

code:<HR>
Code:
public double bisectPra() {
  double TOL = 0.0001;
  int ii = 0;
  double min = (CO.calcIndPrAO(isFemale));
  double max = (VR.calcIndPsf(isFemale));
  double left;
  double mid;
  
  CO.calcIndCOmax(isFemale);
  CO.calcIndK(isFemale);
  VR.calcIndRvr(isFemale);
  
  while (Math.abs(max - min) > TOL) {
    left = (CO.calcCardOut(min) - VR.calcVenReturn(min));
    midPra = (min + max) / 2;
    mid = (CO.calcCardOut(midPra) - VR.calcVenReturn(midPra));
    
    if ((left * mid) < 0) {
      max = midPra;
      ii++;
    } else {
      min = midPra;
      ii++;
    }
  }
  
  if (ii >= 100000) {
    midPra = -1;
  }
  
  return midPra;
}

1. You need something to call recursively. Lift the loop out of the method. Move it into a method called "loop", and replace it with a call to loop.

code:<HR>
Code:
public double bisectPra() {
  double TOL = 0.0001;
  int ii = 0;
  double min = (CO.calcIndPrAO(isFemale));
  double max = (VR.calcIndPsf(isFemale));
  double left;
  double mid;
  
  CO.calcIndCOmax(isFemale);
  CO.calcIndK(isFemale);
  VR.calcIndRvr(isFemale);
  
  loop();
  
  if (ii >= 100000) {
    midPra = -1;
  }
  
  return midPra;
}<P>private void loop() {
  while (Math.abs(max - min) > TOL) {
    left = (CO.calcCardOut(min) - VR.calcVenReturn(min));
    midPra = (min + max) / 2;
    mid = (CO.calcCardOut(midPra) - VR.calcVenReturn(midPra));
    
    if ((left * mid) < 0) {
      max = midPra;
      ii++;
    } else {
      min = midPra;
      ii++;
    }
  }
}

Of course, that won't work. If you look at the method loop, you see that it needs the values of max, min, TOL, and ii (they are not defined in the new method, and they appear in expressions in the new method).

2. Pass in the free (unbound) variables needed by the loop. Just add them as arguments to the new method, and pass them in when you call it. I've taken the liberty of removing the unneeded declaration of left and mid in bisectPra, and defined left, midPra, and mid when first used in loop:

code:<HR>
Code:
public double bisectPra() {
  double TOL = 0.0001;
  int ii = 0;
  double min = (CO.calcIndPrAO(isFemale));
  double max = (VR.calcIndPsf(isFemale));
  
  CO.calcIndCOmax(isFemale);
  CO.calcIndK(isFemale);
  VR.calcIndRvr(isFemale);
  
  loop(min, max, TOL, ii);
  
  if (ii >= 100000) {
    midPra = -1;
  }
  
  return midPra;
}<P>private void loop(double max, double min, double TOL, int ii) {
  while (Math.abs(max - min) > TOL) {
    double left = (CO.calcCardOut(min) - VR.calcVenReturn(min));
    double midPra = (min + max) / 2;
    double mid = (CO.calcCardOut(midPra) - VR.calcVenReturn(midPra));
    
    if ((left * mid) < 0) {
      max = midPra;
      ii++;
    } else {
      min = midPra;
      ii++;
    }
  }
}

3. Now, get rid of that while loop. Replace it with an if...else. If the condition is false, we can just jump out of the loop (by returning). If the condition is true, perform one iteration of the loop body, and then return the value of calling loop again (passing along the new values of the arguments):

code:<HR>
Code:
public double bisectPra() {
  double TOL = 0.0001;
  int ii = 0;
  double min = (CO.calcIndPrAO(isFemale));
  double max = (VR.calcIndPsf(isFemale));
  
  CO.calcIndCOmax(isFemale);
  CO.calcIndK(isFemale);
  VR.calcIndRvr(isFemale);
  
  loop(min, max, TOL, ii);
  
  if (ii >= 100000) {
    midPra = -1;
  }
  
  return midPra;
}<P>private void loop(double max, double min, double TOL, int ii) {
  if (Math.abs(max - min) > TOL) {
    double left = (CO.calcCardOut(min) - VR.calcVenReturn(min));
    double midPra = (min + max) / 2;
    double mid = (CO.calcCardOut(midPra) - VR.calcVenReturn(midPra));
    
    if ((left * mid) < 0) {
      max = midPra;
      ii++;
    } else {
      min = midPra;
      ii++;
    }<P>    return loop(max, min, TOL, ii);
  } else {
    return;
  }
}

4. Looking good, but not quite right. After the call to loop in bisectPra, we need the values of ii and midPra. Just as we passed information into the loop when we called it, we'll need to return this stuff. Since I'm tired of the whole exercise by now, I'll use the decidedly inelegant expedient of returning those values in an Object array, and destructuring them immediately after the call to loop:

code:<HR>
Code:
public double bisectPra() {
  double TOL = 0.0001;
  int ii = 0;
  double min = (CO.calcIndPrAO(isFemale));
  double max = (VR.calcIndPsf(isFemale));
  
  CO.calcIndCOmax(isFemale);
  CO.calcIndK(isFemale);
  VR.calcIndRvr(isFemale);
  
  Object[] result = loop(min, max, TOL, ii);
  ii = result[0].intValue();
  double midPra = result[1].doubleValue();
  
  if (ii >= 100000) {
    midPra = -1;
  }
  
  return midPra;
}<P>private Object[] loop(double max, double min, double TOL, int ii) {
  if (Math.abs(max - min) > TOL) {
    double left = (CO.calcCardOut(min) - VR.calcVenReturn(min));
    double midPra = (min + max) / 2;
    double mid = (CO.calcCardOut(midPra) - VR.calcVenReturn(midPra));
    
    if ((left * mid) < 0) {
      max = midPra;
      ii++;
    } else {
      min = midPra;
      ii++;
    }<P>    return loop(max, min, TOL, ii);
  } else {
    return new Object[] { new Integer(ii), new Double(midPra) };
  }
}

Voila. Unfortunately, your JVM isn't likely to properly handle tail calls, and mine definitely doesn't. The space behavior of the recursive version will be worse. That means you should probably stick with the special syntax of while or for to write loops in Java.
 
Status
Not open for further replies.