Miscellaneous functions for handling 2D tables.
More...
|
void | InitializeTable2D (Table2D *tab, double xmin, double xmax, int nx, double ymin, double ymax, int ny) |
|
int | LocateIndex (double *yarr, int beg, int end, double y) |
|
int | InverseLookupTable2D (Table2D *tab, double y, double f, double *x) |
|
int | Table2DInterpolate (Table2D *tab, double x, double y, double *f) |
|
void | WriteBinaryTable2D (char *fname, Table2D *tab) |
|
◆ InitializeTable2D()
void InitializeTable2D |
( |
Table2D * |
tab, |
|
|
double |
xmin, |
|
|
double |
xmax, |
|
|
int |
nx, |
|
|
double |
ymin, |
|
|
double |
ymax, |
|
|
int |
ny |
|
) |
| |
Allocate memory for the arrays contained in the *tab
structure and generate a uniformly spaced grid in log10(x), log10(y) within the range provided by xmin
, xmax
, ymin
, ymax
. On output, the function initializes the following structure members:
nx, ny
lnxmin, lnxmax, lnymin, lnymax
lnx[], lny[]
dlnx, dlny
dlnx_1, dlny_1
x[], y[]
dx[], dy[]
- Parameters
-
[in,out] | tab | pointer to a Table2D structure |
[in] | xmin | lower column limit. |
[in] | xmax | upper column limit. |
[in] | nx | number of equally-spaced column bins (in log space) |
[in] | ymin | lower row limit. |
[in] | ymax | upper row limit. |
[in] | ny | number of equally-spaced row bins (in log space) |
◆ InverseLookupTable2D()
int InverseLookupTable2D |
( |
Table2D * |
tab, |
|
|
double |
y, |
|
|
double |
f, |
|
|
double * |
x |
|
) |
| |
Perform inverse lookup table interpolation: given a 2D table f(i,j)=f(x(i), y(j))
and the value of y
, find the value of x
. The algorithm proceeds by a combination of 1D lookup table algorithms:
- Find the value of
j
such that y[j]<y<y[j+1]
;
- The solution must then lie on the intermediate line between
j
and j+1
where the value of y
is given.
- To find the horizontal coordinate, we first locate initial and final indices
ib
and ie
at j
and j+1
using 1D lookup table.
- Then construct a 1D array between these two indices values, perform again 1D lookup table to find the actual index
i
and invert the bilinear interpolant by solving
for
(normlized coordinate between 0 and 1).
- Note
- The table must be monotonic in both
and
y
.
- Parameters
-
[in] | tab | a pointer to a Table2D structure |
[in] | y | the ordinata |
[in] | f | the value of the function |
[out] | *x | the value of the abscissa. |
- Returns
- Returns 0 on success, otherwise:
- -2 if
y
is below range;
- 2 if
y
is above range.
◆ LocateIndex()
int LocateIndex |
( |
double * |
yarr, |
|
|
int |
beg, |
|
|
int |
end, |
|
|
double |
y |
|
) |
| |
Given an array yarr
[beg..end] and a given value y
, returns the array index i
such that yarr[i] < y < yarr[i+1]
(for increasing arrays) or yarr[i+1] < y < yarr[i]
(for decreasing arrays). yarr
must be monotonically increasing or monotonically decreasing.
- Parameters
-
[in] | yarr | the 1D array |
[in] | beg | initial index of the array |
[in] | end | final index of the array |
[in] | y | the value to be searched for |
- Returns
- On success return the index of the array. Otherwise returns -1.
Reference
- "Numerical Recipes in C", Sect. 3.4 "How to Search an Ordered Table"
◆ Table2DInterpolate()
int Table2DInterpolate |
( |
Table2D * |
tab, |
|
|
double |
x, |
|
|
double |
y, |
|
|
double * |
f |
|
) |
| |
Use bilinear interpolation to find the function f(x,y) from the 2D table *tab
. Since the grid is equally spaced in log(x)
and log(y)
, the (i,j) indices such that x[i] <= x < x[i+1]
and y[j] <= y < y[j+1]
are found by a simple division. Then bilinear interpolation can be done in either log or linear coordinates. The latter is expected to be slightly faster since it avoids one pow()
operation.
- Parameters
-
[in] | *tab | a pointer to a Table2D structure |
[in] | x | the abscissa where interpolation is needed. |
[in] | y | the ordinata where interpolation is needed. |
[out] | *f | the interpolated value |
- Returns
- - Return 0 on success, otherwise:
- -1 if
x
is below column range;
- 1 if
x
is above column range;
- -2 if
y
is below row range;
- 2 if
y
is above row range.
◆ WriteBinaryTable2D()
void WriteBinaryTable2D |
( |
char * |
fname, |
|
|
Table2D * |
tab |
|
) |
| |
The binary table is a compact format used to write a 2D array together with simple structured coordinates. The file consists of the following information:
nx
ny
<x[0]..x[nx-1]>
<y[0]..y[ny-1]>
<q[0][0]..q[0][nx-1]
q[1][0]..q[1][nx-1]
.......
q[ny-1][0]..q[ny-1][nx-1]>
All fields are written in binary format using double precision arithmetic.