32 #include "fors_polynomial.h"
34 #include "fors_utils.h"
53 const cpl_size *powers);
75 const cpl_size *powers)
77 cassure_automsg( p != NULL,
80 cassure_automsg( powers != NULL,
84 return (fabs(cpl_polynomial_get_coeff(p, powers)) > DBL_EPSILON);
104 bool overflow =
false;
105 cpl_errorstate errstat = cpl_errorstate_get();
107 cassure_automsg( p != NULL,
108 CPL_ERROR_NULL_INPUT,
110 cassure_automsg( powers != NULL,
111 CPL_ERROR_NULL_INPUT,
114 ndims = cpl_polynomial_get_dimension(p);
115 ndegs = cpl_polynomial_get_degree(p);
116 passure(cpl_errorstate_is_equal(errstat),
return true);
119 while(dim < ndims && powers[dim] > ndegs)
123 overflow = (!(dim < ndims));
154 cpl_size *pows = NULL;
155 bool overflow =
false;
156 cpl_errorstate errstat = cpl_errorstate_get();
158 cassure_automsg( p != NULL,
159 CPL_ERROR_NULL_INPUT,
162 ndims = cpl_polynomial_get_dimension(p);
163 passure(cpl_errorstate_is_equal(errstat),
return 0);
165 pows = cpl_calloc(ndims,
sizeof(*pows));
175 passure(cpl_errorstate_is_equal(errstat),
return 0);
198 cpl_errorstate errstat = cpl_errorstate_get();
200 cassure_automsg( p != NULL,
201 CPL_ERROR_NULL_INPUT,
203 cassure_automsg( powers != NULL,
204 CPL_ERROR_NULL_INPUT,
207 ndims = cpl_polynomial_get_dimension(p);
208 for (dim = 0; dim < ndims; dim++)
215 passure(cpl_errorstate_is_equal(errstat),
return true);
236 cpl_errorstate errstat = cpl_errorstate_get();
238 cassure_automsg( p != NULL,
239 CPL_ERROR_NULL_INPUT,
241 cassure_automsg( powers != NULL,
242 CPL_ERROR_NULL_INPUT,
254 passure(cpl_errorstate_is_equal(errstat),
return true);
278 const double *coeffs,
283 cpl_size *pows = NULL;
284 bool overflow =
false;
285 cpl_errorstate errstat = cpl_errorstate_get();
291 cassure_automsg( p != NULL,
292 CPL_ERROR_NULL_INPUT,
293 return cpl_error_get_code());
294 cassure_automsg( coeffs != NULL,
295 CPL_ERROR_NULL_INPUT,
296 return cpl_error_get_code());
297 cassure_automsg( n_coeffs > 0,
298 CPL_ERROR_ILLEGAL_INPUT,
299 return cpl_error_get_code());
302 ndims = cpl_polynomial_get_dimension(p);
303 pows = cpl_calloc(ndims,
sizeof(*pows));
309 cassure( n < n_coeffs,
310 CPL_ERROR_ACCESS_OUT_OF_RANGE,
311 return cpl_error_get_code(),
312 "p contains more coefficients "
314 cpl_polynomial_set_coeff(p, pows, coeffs[n]);
322 return cpl_errorstate_is_equal(errstat) ?
324 cpl_error_get_code();
331 if (powersA != NULL) { cpl_free(powersA); powersA = NULL;} \
332 if (powersB != NULL) { cpl_free(powersB); powersB = NULL;} \
333 if (powersE != NULL) { cpl_free(powersE); powersE = NULL;} \
334 cpl_polynomial_delete(ep); ep = NULL; \
357 const cpl_matrix *cov_coeff)
363 cpl_size *powersA = NULL,
366 cpl_polynomial *ep = NULL;
367 bool overflowa =
false;
368 cpl_errorstate errstat = cpl_errorstate_get();
371 cassure_automsg( p_def != NULL,
372 CPL_ERROR_NULL_INPUT,
374 cassure_automsg( cov_coeff != NULL,
375 CPL_ERROR_NULL_INPUT,
379 n_dims = cpl_polynomial_get_dimension(p_def);
380 n_col = cpl_matrix_get_ncol(cov_coeff);
381 passure(cpl_errorstate_is_equal(errstat),
return ep);
383 cassure( n_coeffs == n_col,
384 CPL_ERROR_INCOMPATIBLE_INPUT,
386 "number of p_def coefficients "
388 cassure( n_col == cpl_matrix_get_nrow(
390 CPL_ERROR_ILLEGAL_INPUT,
392 "cov_coeff is not square");
394 ep = cpl_polynomial_new(n_dims);
395 powersA = cpl_calloc(n_dims,
sizeof(*powersA));
396 powersB = cpl_calloc(n_dims,
sizeof(*powersB));
397 powersE = cpl_calloc(n_dims,
sizeof(*powersE));
398 passure(cpl_errorstate_is_equal(errstat),
return ep);
403 bool overflowb =
false;
411 passure(cpl_errorstate_is_equal(errstat),
return ep);
420 for (d = 0; d < n_dims; d++)
421 powersE[d] = powersA[d] + powersB[d];
423 coeff = cpl_polynomial_get_coeff(ep, powersE);
424 cpl_polynomial_set_coeff(ep, powersE,
435 passure(cpl_errorstate_is_equal(errstat),
return ep);
437 cpl_polynomial *ret_ep = ep; ep = NULL;
450 if (ndxstr != NULL) \
467 cpl_msg_severity level,
468 const cpl_polynomial *p_def)
473 cpl_size *pows = NULL;
475 char max_ndx_str[15];
476 cpl_errorstate errstat = cpl_errorstate_get();
479 cassure_automsg( p != NULL,
480 CPL_ERROR_NULL_INPUT,
481 return cpl_error_get_code());
485 ndims = cpl_polynomial_get_dimension(p_def);
486 cassure_automsg( ndims
487 = cpl_polynomial_get_dimension(p),
488 CPL_ERROR_INCOMPATIBLE_INPUT,
489 return cpl_error_get_code());
493 ndims = cpl_polynomial_get_dimension(p);
497 ndegs = cpl_polynomial_get_degree(p_def);
499 pows = cpl_calloc(ndims,
sizeof(*pows));
501 sprintf(max_ndx_str,
"%d", ndegs);
502 ndxstr = cpl_calloc(ndims*(strlen(max_ndx_str)+1),
sizeof(*ndxstr));
508 while (pows[0] <= ndegs)
513 coeff = cpl_polynomial_get_coeff(p, pows);
515 sprintf(ndxstr,
"%"CPL_SIZE_FORMAT, pows[0]);
516 for (dim = 1; dim < ndims; dim++)
517 sprintf(ndxstr+strlen(ndxstr),
",%"CPL_SIZE_FORMAT, pows[dim]);
520 (name != NULL) ? name :
"p",
527 while(dim > 0 && pows[dim] > ndegs)
537 return cpl_errorstate_is_equal(errstat) ?
539 cpl_error_get_code();
546 if (ndxstr != NULL) \
573 char max_ndx_str[15];
576 cassure_automsg( p != NULL,
577 CPL_ERROR_NULL_INPUT,
579 cassure_automsg( powers != NULL,
580 CPL_ERROR_NULL_INPUT,
583 ndims = cpl_polynomial_get_dimension(p);
584 ndegs = cpl_polynomial_get_degree(p);
589 for (dim = 0; dim < ndims; dim++)
590 if (powers[dim] > max_nr)
591 max_nr = powers[dim];
593 sprintf(max_ndx_str,
"%d", max_nr);
594 max_nr_strlen = strlen(max_ndx_str);
595 if (prefix != NULL && prefix[0] !=
'\0')
597 int len = strlen(prefix) + 1 + ndims*(max_nr_strlen+1);
598 ndxstr = cpl_calloc(len,
sizeof(*ndxstr));
599 sprintf(ndxstr,
"%s_", prefix);
603 ndxstr = cpl_calloc(ndims*(max_nr_strlen+1),
sizeof(*ndxstr));
606 snprintf(ndxstr+strlen(ndxstr), max_nr_strlen+1,
607 (powers[0] >= 0) ?
"%"CPL_SIZE_FORMAT :
"?",
609 for (dim = 1; dim < ndims; dim++)
610 snprintf(ndxstr+strlen(ndxstr), max_nr_strlen+2,
611 (powers[dim] >= 0) ?
",%"CPL_SIZE_FORMAT :
"?",
static bool fors_polynomial_is_coeff_set(const cpl_polynomial *p, const cpl_size *powers)
Implementation of the check whether a coefficient exists.
cpl_error_code fors_polynomial_dump(const cpl_polynomial *p, const char *name, cpl_msg_severity level, const cpl_polynomial *p_def)
Count the total number of non-zero coefficients.
int fors_polynomial_count_coeff(const cpl_polynomial *p)
Count the total number of non-zero coefficients.
char * fors_polynomial_sprint_coeff(const cpl_polynomial *p, cpl_size *powers, const char *prefix)
Print a coefficient's name into a newly allocated string.
int fors_polynomial_powers_find_next_coeff(const cpl_polynomial *p, cpl_size *powers)
Find the next non-zero coefficient.
static bool fors_polynomial_powers_next(const cpl_polynomial *p, cpl_size *powers)
Step the powers of a polynomials coefficient to the next.
int fors_polynomial_powers_find_first_coeff(const cpl_polynomial *p, cpl_size *powers)
Find the first non-zero coefficient.
cpl_error_code fors_polynomial_set_existing_coeff(cpl_polynomial *p, const double *coeffs, int n_coeffs)
Set the already existing coefficients in a polynomial to values taken from an array.
cpl_polynomial * fors_polynomial_create_variance_polynomial(const cpl_polynomial *p_def, const cpl_matrix *cov_coeff)
Create a polynomial modelling the squared influence of the error of the coefficients of another polyn...