add numerical recipes library
This commit is contained in:
51
lib/nr/ansi/recipes/hunt.c
Normal file
51
lib/nr/ansi/recipes/hunt.c
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
void hunt(float xx[], unsigned long n, float x, unsigned long *jlo)
|
||||
{
|
||||
unsigned long jm,jhi,inc;
|
||||
int ascnd;
|
||||
|
||||
ascnd=(xx[n] >= xx[1]);
|
||||
if (*jlo <= 0 || *jlo > n) {
|
||||
*jlo=0;
|
||||
jhi=n+1;
|
||||
} else {
|
||||
inc=1;
|
||||
if (x >= xx[*jlo] == ascnd) {
|
||||
if (*jlo == n) return;
|
||||
jhi=(*jlo)+1;
|
||||
while (x >= xx[jhi] == ascnd) {
|
||||
*jlo=jhi;
|
||||
inc += inc;
|
||||
jhi=(*jlo)+inc;
|
||||
if (jhi > n) {
|
||||
jhi=n+1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (*jlo == 1) {
|
||||
*jlo=0;
|
||||
return;
|
||||
}
|
||||
jhi=(*jlo)--;
|
||||
while (x < xx[*jlo] == ascnd) {
|
||||
jhi=(*jlo);
|
||||
inc <<= 1;
|
||||
if (inc >= jhi) {
|
||||
*jlo=0;
|
||||
break;
|
||||
}
|
||||
else *jlo=jhi-inc;
|
||||
}
|
||||
}
|
||||
}
|
||||
while (jhi-(*jlo) != 1) {
|
||||
jm=(jhi+(*jlo)) >> 1;
|
||||
if (x >= xx[jm] == ascnd)
|
||||
*jlo=jm;
|
||||
else
|
||||
jhi=jm;
|
||||
}
|
||||
if (x == xx[n]) *jlo=n-1;
|
||||
if (x == xx[1]) *jlo=1;
|
||||
}
|
||||
Reference in New Issue
Block a user