Classes | |
class | DerivativeCenteredDifference |
The numerical derivative of a functor. More... | |
class | GradientCenteredDifference |
The numerical gradient of a functor. More... | |
class | LinInterpGrid |
Functor for linear interpolation on a regular grid. More... | |
class | CoordinateDescent |
The coordinate descent method of Hooke and Jeeves. More... | |
class | OptimizationException |
Optimization exception. More... | |
class | PenaltyException |
Penalty method exception. More... | |
class | FunctionWithQuadraticPenalty |
An objective function with a quadratic penalty. More... | |
class | Opt |
Base class for optimization methods. More... | |
class | Penalty |
The penalty method for optimization with an equality constraint. More... | |
class | PenaltyQuasiNewton |
The penalty method for optimization with an equality constraint using a quasi-Newton method . More... | |
class | QuasiNewton |
The quasi-Newton BFGS method. More... | |
class | Simplex |
The downhill simplex method. More... | |
Functions | |
int | partition (const int x, const int n, const int i) |
Return the i_th fair partition of x into n parts. | |
void | partitionRange (const int x, const int n, const int i, int *a, int *b) |
Compute the i_th fair partition range of x into n ranges. | |
template<class Functor> | |
void | derivative_centered_difference (const Functor &f, const typename Functor::argument_type x, typename Functor::result_type &deriv, const typename Functor::argument_type delta=std::pow(std::numeric_limits< typename Functor::argument_type >::epsilon(), 1.0/3.0)) |
Calculate f'(x). | |
template<class Functor> | |
Functor::result_type | derivative_centered_difference (const Functor &f, const typename Functor::argument_type x, const typename Functor::argument_type delta=std::pow(std::numeric_limits< typename Functor::argument_type >::epsilon(), 1.0/3.0)) |
Return f'(x). | |
template<int N, class Functor, typename T> | |
void | gradient_centered_difference (const Functor &f, typename Functor::argument_type x, ads::FixedArray< N, typename Functor::result_type > &gradient, const T delta=std::pow(std::numeric_limits< T >::epsilon(), 1.0/3.0)) |
Calculate the gradient of f at x. | |
template<int N, class Functor, typename T> | |
ads::FixedArray< N, typename Functor::result_type > | gradient_centered_difference (const Functor &f, typename Functor::argument_type x, const T delta=std::pow(std::numeric_limits< T >::epsilon(), 1.0/3.0)) |
Return the gradient of f at x. | |
template<int N, int M, typename T> | |
void | grid_interp_extrap (const int num_points, T values[], const T positions[], const T default_values[M], const int extents[N], const T domain[2 *N], const T *distance, const T *fields[M]) |
Grid interpolation/extrapolation for a set of points. | |
template<int N, int M, typename T> | |
void | grid_interp_extrap (const int num_points, T values[], const T positions[], const T default_values[M], const int extents[N], const T domain[2 *N], const T *distance, const T *fields) |
Grid interpolation/extrapolation for a set of points. | |
template<int N, int M, typename T, typename F> | |
const F & | linear_interpolation (const ads::FixedArray< M+1, ads::FixedArray< N, T > > &positions, const ads::FixedArray< M+1, F > &values, const ads::FixedArray< N, T > &location) |
General interface for linear interpolation. | |
template<typename T, typename F> | |
const F & | linear_interpolation (const T a, T b, const F &alpha, const F &beta, T x) |
Specialization for a 1-D simplex in 1-D space. | |
template<typename T, typename F> | |
const F & | linear_interpolation (const ads::FixedArray< 2, T > &positions, const ads::FixedArray< 2, F > &values, const T location) |
Specialization for a 1-D simplex in 1-D space. | |
template<typename T, typename F> | |
const F & | linear_interpolation (const ads::FixedArray< 2, T > &a, const ads::FixedArray< 2, T > &b, const F &alpha, const F &beta, const ads::FixedArray< 2, T > &x) |
Specialization for a 1-D simplex in 2-D space. | |
template<typename T, typename F> | |
const F & | linear_interpolation (const ads::FixedArray< 2, T > &a, const ads::FixedArray< 2, T > &b, const ads::FixedArray< 2, T > &c, const F &alpha, const F &beta, const F &gamma, const ads::FixedArray< 2, T > &x) |
Specialization for a 2-D simplex in 2-D space. | |
template<typename T, typename F> | |
const F & | linear_interpolation (const ads::FixedArray< 3, T > &a, const ads::FixedArray< 3, T > &b, const ads::FixedArray< 3, T > &c, const F &alpha, const F &beta, const F &gamma, const ads::FixedArray< 3, T > &x) |
Specialization for a 2-D simplex in 3-D space. | |
template<typename T, typename F> | |
const F & | linear_interpolation (const ads::FixedArray< 3, T > &a, const ads::FixedArray< 3, T > &b, const ads::FixedArray< 3, T > &c, const ads::FixedArray< 3, T > &d, const F &alpha, const F &beta, const F &gamma, const F &delta, const ads::FixedArray< 3, T > &x) |
Specialization for a 3-D simplex in 3-D space. |
int numerical::partition | ( | const int | x, | |
const int | n, | |||
const int | i | |||
) | [inline] |
Return the i_th fair partition of x into n parts.
Partition x
into n
fair parts. Return the i_th part. The parts differ by at most one and are in non-increasing order.
x | is the number to partition. | |
n | is the number of partitions. | |
i | is the requested partition. |
x
is non-negative. n
is positive. i
is in the range [0..n). void numerical::partitionRange | ( | const int | x, | |
const int | n, | |||
const int | i, | |||
int * | a, | |||
int * | b | |||
) | [inline] |
Compute the i_th fair partition range of x into n ranges.
Partition x
into n
fair ranges. Compute the i_th range. The lengths of the ranges differ by at most one and are in non-increasing order.
x | is the number to partition. | |
n | is the number of partitions. | |
i | is the requested range. | |
a | is the begining of the range. | |
b | is the end of the open range, [a..b). |
x
is non-negative. n
is positive. i
is in the range [0..n). void numerical::grid_interp_extrap | ( | const int | num_points, | |
T | values[], | |||
const T | positions[], | |||
const T | default_values[M], | |||
const int | extents[N], | |||
const T | domain[2 *N], | |||
const T * | distance, | |||
const T * | fields[M] | |||
) |
Grid interpolation/extrapolation for a set of points.
num_points | is the number of points at which to interpolate/extrapolate the fields. | |
values | is an array of length M*num_points . It will be set to the interpolated/extrapolated fields. For M = 2 the layout is point_0_field_0 point_0_field_1 point_1_field_0 point_1_field_1 ... | |
positions | is an array of length N*num_points . It holds the positions of the points in Cartesian coordinates. In 3-D the layout is point_0_x point_0_y point_0_z point_1_x point_1_y point_1_z ... | |
default_values | are the default values for the fields. If there are no nearby grid points with known values for a given point, the field values will be set to these default values. The "nearby" grid points are the ![]() | |
extents | are the extents of the grid. | |
domain | is the Cartesian domain of the grid. In 3-D, the format is { xmin, ymin, zmin, xmax, ymax, zmax } | |
distance | is the distance array. It has size extents[0] * ... * extents[N-1] distance(0,0) distance(1,0) ... distance(X-1,0) distance(0,1) distance(1,1) ... distance(X-1,1) ... distance(0,Y-1) distance(1,Y-1) ... distance(X-1,Y-1) | |
fields | is an array of the M field arrays. Each field array has the same size and layout as the distance array. |
N
is the dimension. (1-D, 2-D and 3-D is supported.)M
is the number of fields.T
is the number type.
Explicitly specify the dimension and the number of fields in the function call as these cannot be deduced from the arguments, i.e. use grid_interp_extrap<3,2>
(...) for a 3-D problem with 2 fields.
The field values for a point are only interpolated/extrapolated if there are two grid points on every side of the point. (In 1-D, the point must be surrounded by 4 grid points. In 3-D, the point must be surrounded by grid points.) Otherwise the values are left unchanged. This enables one to use multiple grids to interpolate the values. Just call the function once for each grid. The
values
array will accumulate the results. If there are overlapping grids of different resolutions, start with the coarsest grids and end with the finest. This way interpolation/extrapolation results from the finer grids may overwrite results from the coarser grids.
Example usage: 3-D with one field. (N = 3, M = 1, T = double)
const int num_points = ...; double values[ num_points ]; double positions[ 3 * num_points ]; // Set the positions. ... const double default_values[1] = { 0 }; // A 100 x 100 x 100 grid. const int extents[3] = { 100, 100, 100 }; // The grid spans the unit cube. const double domain[6] = { 0, 0, 0, 1, 1, 1 }; double distance[1000000]; double field[1000000]; // Set the distance and field values. ... const double* fields[1] = { field }; numerical::grid_interp_extrap<3,1>(num_points, values, positions, default_values, extents, domain, distance, fields);
The figure below shows the 16 possibilities for 1-D interpolation/extrapolation. The graph at the top shows four grid points that sample a function and an interpolation/extrapolation point marked with an x. Solid/hollow points indicate that the value is/isn't known.
1-D Interpolation/Extrapolation
The cases show linear interpolation, linear extrapolation and constant extrapolation. In case 0, no grid points have known values. Thus the value is set to the user-defined default. In case 9, constant value extrapolation is perform from the closer known grid point. In this example the interpolation/extrapolation point is equidistant from the two known grid points so we show both extrapolations.
In 2-D and 3-D, the interpolation/extrapolation is performed one dimension at a time. If there are any known grid points along a dimension, then the interpolated/extrapolated value is known; otherwise it is unknown.
Below is a diagram illustrating the process in 2-D. The interpolation/extrapolation point, marked with a red x, lies on a curve. Grid points with positive/negative distance from the curve have known/unknown field values. Again, solid/hollow grid points indicate known/unkown values.
2-D Interpolation/Extrapolation
In the first step, we interpolate/extrapolate in the x direction.
For the second step we interpolate/extrapolate in the y direction. In this case we use linear interpolation between the second and third grid points to determine the field value.
void numerical::grid_interp_extrap | ( | const int | num_points, | |
T | values[], | |||
const T | positions[], | |||
const T | default_values[M], | |||
const int | extents[N], | |||
const T | domain[2 *N], | |||
const T * | distance, | |||
const T * | fields | |||
) |
Grid interpolation/extrapolation for a set of points.
num_points | is the number of points at which to interpolate/extrapolate the fields. | |
values | is an array of length M*num_points . It will be set to the interpolated/extrapolated fields. For M = 2 the layout is point_0_field_0 point_0_field_1 point_1_field_0 point_1_field_1 ... | |
positions | is an array of length N*num_points . It holds the positions of the points in Cartesian coordinates. In 3-D the layout is point_0_x point_0_y point_0_z point_1_x point_1_y point_1_z ... | |
default_values | are the default values for the fields. If there are no nearby grid points with known values for a given point, the field values will be set to these default values. The "nearby" grid points are the ![]() | |
extents | are the extents of the grid. | |
domain | is the Cartesian domain of the grid. In 3-D, the format is { xmin, ymin, zmin, xmax, ymax, zmax } | |
distance | is the distance array. It has size extents[0] * ... * extents[N-1] distance(0,0) distance(1,0) ... distance(X-1,0) distance(0,1) distance(1,1) ... distance(X-1,1) ... distance(0,Y-1) distance(1,Y-1) ... distance(X-1,Y-1) | |
fields | is the array of the vector fields. The M fields for for a grid point are contiguous in memory. |
N
is the dimension. (1-D, 2-D and 3-D is supported.)M
is the number of fields.T
is the number type.
Explicitly specify the dimension and the number of fields in the function call as these cannot be deduced from the arguments, i.e. use grid_interp_extrap<3,2>
(...) for a 3-D problem with 2 fields.
Example usage: 3-D with one field. (N = 3, M = 1, T = double)
const int num_points = ...; double values[ num_points ]; double positions[ 3 * num_points ]; // Set the positions. ... const double default_values[1] = { 0 }; // A 100 x 100 x 100 grid. const int extents[3] = { 100, 100, 100 }; // The grid spans the unit cube. const double domain[6] = { 0, 0, 0, 1, 1, 1 }; double distance[1000000]; double fields[1000000]; // Set the distance and field values. ... numerical::grid_interp_extrap<3,1>(num_points, values, positions, default_values, extents, domain, distance, fields);
const F& numerical::linear_interpolation | ( | const ads::FixedArray< M+1, ads::FixedArray< N, T > > & | positions, | |
const ads::FixedArray< M+1, F > & | values, | |||
const ads::FixedArray< N, T > & | location | |||
) |
General interface for linear interpolation.
positions | are the positions of the simplex nodes. The positions must be distinct. (This is checked with an assertion.) | |
values | are the field values at the simplex nodes. | |
location | is the point at which to interpolate the field. |
N
is the dimension of the space.M
is the dimension of the simplex.T
is the number type.F
is the field type.
N
and M
are implemented.