[alt.sources.wanted] correction to spline code

sandell@ferret.ils.nwu.edu (08/20/90)

From: Greg Sandell <sandell@ferret.ils.nwu.edu>


Several people asked me to send them a copy of the spline code,
and I've responded to at least ten of them by now.  One person
pointed out an error in my comment code, and I'd like to 
correct that:

> Okay, here's the spline code from Numerical Recipes.  In the original
> code, all arrays began at subscript zero, not one.  (I kid you not.)
                            ^^^^^^^^^^^^^^^^^^^^^^^

That's wrong.  I meant the exact opposite.  The code in "Numerical
Recipes in C" treats all arrays as though data begins in array
subscript one...obviously an artifact of the translation from
FORTRAN.  

> I've fixed it so it works right with normal C conventions.  

This meant that I have fixed the code so that all arrays begin
at subscript zero.

****************************************************************
* Greg Sandell (sandell@ils.nwu.edu)              Evanston, IL *
* Institute for the Learning Sciences, Northwestern University *
****************************************************************

walker@hpl-opus.HP.COM (Rick Walker) (08/21/90)

/ hpl-opus:alt.sources.wanted / sandell@ferret.ils.nwu.edu / 10:10 am  Aug 19, 1990 /
From: Greg Sandell <sandell@ferret.ils.nwu.edu>


> > Okay, here's the spline code from Numerical Recipes.  In the original
> > code, all arrays began at subscript zero, not one.  (I kid you not.)
>
> That's wrong.  I meant the exact opposite.  The code in "Numerical
> Recipes in C" treats all arrays as though data begins in array
> subscript one...obviously an artifact of the translation from
> FORTRAN.  
> 
> > I've fixed it so it works right with normal C conventions.  
> 
> This meant that I have fixed the code so that all arrays begin
> at subscript zero.
> 

--------------------------
The authors of "Numerical Recipes" actually made some effort to make
their vector allocator work with arbitrary ranges on the subscripts:

float *vector(nl,nh)
int nl,nh; 	/* allocates a float vector with range [nl..nh] */
{
	float *v;

	v=(float *)malloc((unsigned) (nh-nl+1)*sizeof(float));
	if(!v) nrerror("allocation failure in vector()");
	return v-nl;
}

The allocator returns a pointer offset by the value of nl, the lower
bound on the index.  This means that when the index nl is used, the
0th element will actally be accessed.

This approach allows the code writer to use any convention that is
best suited to the problem at hand.  Sometimes that requires an index
[0..n-1], while at other times [1.. n] is more convenient and 
easier to understand.  In some instances it might be convenient to 
have a vector that is addressed from [100..109].

All in all, I think it is a clever and useful trick.  The code is
definitely not in need of "fixing".  (IMHO).

Rick Walker