![]() | ![]() | Gwyddion Library Reference Manual | ![]() |
---|
gwymath — Mathematical utility functions
#define ROUND (x) gchar* gwy_math_SI_prefix (gdouble magnitude); gdouble gwy_math_humanize_numbers (gdouble unit, gdouble maximum, gint *precision); gint gwy_math_find_nearest_line (gdouble x, gdouble y, gdouble *d2min, gint n, gdouble *coords); gint gwy_math_find_nearest_point (gdouble x, gdouble y, gdouble *d2min, gint n, gdouble *coords); gdouble* gwy_math_lin_solve (gint n, const gdouble *matrix, const gdouble *rhs, gdouble *result); gdouble* gwy_math_lin_solve_rewrite (gint n, gdouble *matrix, gdouble *rhs, gdouble *result); gdouble* gwy_math_fit_polynom (gint ndata, gdouble *xdata, gdouble *ydata, gint n, gdouble *coeffs); gboolean gwy_math_choleski_decompose (gint n, gdouble *matrix); void gwy_math_choleski_solve (gint n, gdouble *decomp, gdouble *rhs);
Functions gwy_math_SI_prefix() and gwy_math_humanize_numbers() deal with number representation.
Nearest object finding functions gwy_math_find_nearest_line() and gwy_math_find_nearest_point() can be useful in widget and vector layer implementation.
And gwy_math_lin_solve(), gwy_math_lin_solve_rewrite(), and gwy_math_fit_polynom() are general purpose numeric methods.
#define ROUND(x) ((gint)floor((x) + 0.5))
Rounds a number to nearest integer.
x : | A double value. |
gchar* gwy_math_SI_prefix (gdouble magnitude);
Finds SI prefix corresponding to a given power of 1000.
In fact, magnitude doesn't have to be power of 1000, but then the result is mostly meaningless.
magnitude : | A power of 1000. |
Returns : | The SI unit prefix corresponding to magnitude, "?" if magnitude is outside of the SI prefix range. The returned value must be considered constant and never modified or freed. |
gdouble gwy_math_humanize_numbers (gdouble unit, gdouble maximum, gint *precision);
Find a human readable representation for a range of numbers.
unit : | The smallest possible step. |
maximum : | The maximum possible value. |
precision : | A location to store printf() precession, if not NULL. |
Returns : | The magnitude i.e., a power of 1000. |
gint gwy_math_find_nearest_line (gdouble x, gdouble y, gdouble *d2min, gint n, gdouble *coords);
Find the line from coords nearest to the point (x, y).
x : | X-coordinate of the point to search. |
y : | Y-coordinate of the point to search. |
d2min : | Where to store the squared minimal distance, or NULL. |
n : | The number of lines (i.e. coords has 4n items). |
coords : | Line coordinates stored as x00, y00, x01, y01, x10, y10, etc. |
Returns : | The line number. It may return -1 if (x, y) doesn't lie in the orthogonal stripe of any of the lines. |
gint gwy_math_find_nearest_point (gdouble x, gdouble y, gdouble *d2min, gint n, gdouble *coords);
Find the point from coords nearest to the point (x, y).
x : | X-coordinate of the point to search. |
y : | Y-coordinate of the point to search. |
d2min : | Where to store the squared minimal distance, or NULL. |
n : | The number of points (i.e. coords has 2n items). |
coords : | Point coordinates stored as x0, y0, x1, y1, x2, y2, etc. |
Returns : | The point number. |
gdouble* gwy_math_lin_solve (gint n, const gdouble *matrix, const gdouble *rhs, gdouble *result);
Solve a regular system of linear equations.
n : | The size of the system. |
matrix : | The matrix of the system (n times n), ordered by row, then column. |
rhs : | The right hand side of the sytem. |
result : | Where the result should be stored. May be NULL to allocate a fresh array for the result. |
Returns : | The solution (result if it wasn't NULL), may be NULL if the matrix is singular. |
gdouble* gwy_math_lin_solve_rewrite (gint n, gdouble *matrix, gdouble *rhs, gdouble *result);
Solve a regular system of linear equations.
This is a memory-conservative version of gwy_math_lin_solve() overwriting matrix and rhs with intermediate results.
n : | The size of the system. |
matrix : | The matrix of the system (n times n), ordered by row, then column. |
rhs : | The right hand side of the sytem. |
result : | Where the result should be stored. May be NULL to allocate a fresh array for the result. |
Returns : | The solution (result if it wasn't NULL), may be NULL if the matrix is singular. |
gdouble* gwy_math_fit_polynom (gint ndata, gdouble *xdata, gdouble *ydata, gint n, gdouble *coeffs);
Fits a polynom through a general (x, y) data set.
ndata : | The number of items in xdata, ydata. |
xdata : | Independent variable data (of size ndata). |
ydata : | Dependent variable data (of size ndata). |
n : | The degree of polynom to fit. |
coeffs : | An array of size n+1 to store the coefficients to, or NULL (a fresh array is allocated then). |
Returns : | The coefficients of the polynom (coeffs when it was not NULL, otherwise a newly allocated array). |
gboolean gwy_math_choleski_decompose (gint n, gdouble *matrix);
Decomposes a symmetric positive definite matrix in place.
n : | The dimension of a. |
matrix : | Lower triangular part of a symmetric matrix, stored by rows, i.e., matrix = [a_00 a_10 a_11 a_20 a_21 a_22 a_30 ...]. |
Returns : | Whether the matrix was really positive definite. If FALSE, the decomposition failed and a does not contain any meaningful values. |
Since 1.6
void gwy_math_choleski_solve (gint n, gdouble *decomp, gdouble *rhs);
Solves a system of linear equations with predecomposed symmetric positive definite matrix a and right hand side b.
n : | The dimension of a. |
decomp : | Lower triangular part of Choleski decomposition as computed by gwy_math_choleski_decompose(). |
rhs : | Right hand side vector. Is is modified in place, on return it contains the solution. |
Since 1.6
<< gwyentities | gwynlfit >> |