162 #define COLUMN_ORDER1 "Order1"
163 #define COLUMN_ORDER2 "Order2"
164 #define COLUMN_COEFF "Coeff"
171 #include <uves_utils_polynomial.h>
173 #include <uves_utils.h>
174 #include <uves_utils_wrappers.h>
175 #include <uves_dump.h>
176 #include <uves_msg.h>
177 #include <uves_error.h>
226 assure(pol != NULL, CPL_ERROR_ILLEGAL_INPUT,
"Null polynomial");
232 check( p->dimension = cpl_polynomial_get_dimension(pol),
"Error reading dimension");
235 p->
vec = cpl_vector_new(p->dimension);
237 p->vec_data = cpl_vector_get_data(p->
vec);
240 p->
shift = cpl_calloc(p->dimension + 1,
sizeof(
double));
243 p->
scale = cpl_malloc((p->dimension + 1) *
sizeof(
double));
245 for (i = 0; i <= p->dimension; i++)
248 check( p->
pol = cpl_polynomial_duplicate(pol),
"Error copying polynomial");
251 if (cpl_error_get_code() != CPL_ERROR_NONE)
270 cpl_polynomial *p = NULL;
272 assure( dim >= 1, CPL_ERROR_ILLEGAL_INPUT,
"Illegal dimension: %d", dim);
274 p = cpl_polynomial_new(dim);
281 uves_free_polynomial(&p);
311 if (*p == NULL)
return;
312 cpl_polynomial_delete((*p)->pol);
313 cpl_vector_delete((*p)->vec);
314 cpl_free((*p)->shift);
315 cpl_free((*p)->scale);
331 assure( p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
333 result = cpl_polynomial_get_degree(p->
pol);
353 assure( p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
357 "Error allocating polynomial");
359 for (i = 0; i <= dimension; i++)
366 if (cpl_error_get_code() != CPL_ERROR_NONE)
396 assure( p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
398 CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 2D");
400 degree = cpl_polynomial_get_degree(p->
pol);
404 t = cpl_table_new(3 + 3 + (degree + 1)*(degree + 2)/2);
405 cpl_table_new_column(t, COLUMN_ORDER1, CPL_TYPE_INT);
406 cpl_table_new_column(t, COLUMN_ORDER2, CPL_TYPE_INT);
407 cpl_table_new_column(t, COLUMN_COEFF , CPL_TYPE_DOUBLE);
413 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
414 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
415 cpl_table_set_double(t, COLUMN_COEFF , row, p->
shift[0]); row++;
417 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
418 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
419 cpl_table_set_double(t, COLUMN_COEFF , row, p->
shift[1]); row++;
421 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
422 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
423 cpl_table_set_double(t, COLUMN_COEFF , row, p->
shift[2]); row++;
426 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
427 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
428 cpl_table_set_double(t, COLUMN_COEFF, row, p->
scale[0]); row++;
430 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
431 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
432 cpl_table_set_double(t, COLUMN_COEFF, row, p->
scale[1]); row++;
434 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
435 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
436 cpl_table_set_double(t, COLUMN_COEFF, row, p->
scale[2]); row++;
439 for (i = 0; i <= degree; i++){
440 for (j = 0; j+i <= degree; j++){
446 coeff = cpl_polynomial_get_coeff(p->
pol, power);
447 cpl_table_set_int (t, COLUMN_ORDER1, row, power[0]);
448 cpl_table_set_int (t, COLUMN_ORDER2, row, power[1]);
449 cpl_table_set_double(t, COLUMN_COEFF , row, coeff);
454 cpl_table_set_column_unit(t, COLUMN_ORDER1 ,
" ");
455 cpl_table_set_column_unit(t, COLUMN_ORDER2 ,
" ");
456 cpl_table_set_column_unit(t, COLUMN_COEFF ,
" ");
476 cpl_polynomial *pol = NULL;
481 check( pol = cpl_polynomial_new(2),
"Error initializing polynomial");
484 assure(t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
485 assure(cpl_table_has_column(t, COLUMN_ORDER1), CPL_ERROR_ILLEGAL_INPUT,
486 "No '%s' column found in table", COLUMN_ORDER1);
487 assure(cpl_table_has_column(t, COLUMN_ORDER2), CPL_ERROR_ILLEGAL_INPUT,
488 "No '%s' column found in table", COLUMN_ORDER2);
489 assure(cpl_table_has_column(t, COLUMN_COEFF ), CPL_ERROR_ILLEGAL_INPUT,
490 "No '%s' column found in table", COLUMN_COEFF );
492 type = cpl_table_get_column_type(t, COLUMN_ORDER1);
493 assure(type == CPL_TYPE_INT , CPL_ERROR_INVALID_TYPE,
494 "Column '%s' has type %s. Integer expected", COLUMN_ORDER1,
497 type = cpl_table_get_column_type(t, COLUMN_ORDER2);
498 assure(type == CPL_TYPE_INT , CPL_ERROR_INVALID_TYPE,
499 "Column '%s' has type %s. Integer expected", COLUMN_ORDER2,
502 type = cpl_table_get_column_type(t, COLUMN_COEFF);
503 assure(type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
504 "Column '%s' has type %s. Double expected", COLUMN_COEFF ,
507 assure(cpl_table_get_nrow(t) > 1 + 2 + 1 + 2, CPL_ERROR_ILLEGAL_INPUT,
508 "Table must contain at least one coefficient");
511 for(i = 3 + 3; i < cpl_table_get_nrow(t); i++) {
515 check(( power[0] = cpl_table_get_int(t, COLUMN_ORDER1, i, NULL),
516 power[1] = cpl_table_get_int(t, COLUMN_ORDER2, i, NULL),
517 coeff = cpl_table_get_double(t, COLUMN_COEFF , i, NULL)),
518 "Error reading table row %d", i);
520 uves_msg_debug(
"Pol.coeff.(%" CPL_SIZE_FORMAT
", %" CPL_SIZE_FORMAT
") = %e", power[0], power[1], coeff);
522 check( cpl_polynomial_set_coeff(pol, power, coeff),
"Error creating polynomial");
535 uves_free_polynomial(&pol);
536 if (cpl_error_get_code() != CPL_ERROR_NONE)
554 assure(p != NULL, CPL_ERROR_ILLEGAL_INPUT,
"Null polynomial");
575 fprintf(stream,
"Null polynomial\n");
578 cpl_polynomial_dump(p->
pol, stream);
579 fprintf(stream,
"shift_y \t= %f \tscale_y \t= %f\n", p->
shift[0], p->
scale[0]);
582 fprintf(stream,
"shift_x%d \t= %f \tscale_x%d \t= %f\n",
607 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
609 CPL_ERROR_ILLEGAL_INPUT,
"Illegal variable number: %d", varno);
623 p->
shift[varno] *= scale;
624 p->
scale[varno] *= scale;
627 return cpl_error_get_code();
648 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
650 CPL_ERROR_ILLEGAL_INPUT,
"Illegal variable number: %d", varno);
661 p->
shift[varno] += shift;
664 return cpl_error_get_code();
682 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
684 CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 1d");
687 cpl_polynomial_eval_1d(p->
pol, (x - p->
shift[1])/p->
scale[1], NULL)
689 "Could not evaluate polynomial");
713 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
714 assure(p->dimension == 2, CPL_ERROR_ILLEGAL_INPUT,
715 "Polynomial must be 2d. It's %dd", p->dimension);
717 double scale = p->
scale[0];
718 double shift = p->
shift[0];
722 p->vec_data[0] = (x1 - p->
shift[1]) / p->
scale[1];
723 p->vec_data[1] = (x2 - p->
shift[2]) / p->
scale[2];
725 result = cpl_polynomial_eval(p->
pol, p->
vec) * scale + shift;
753 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
755 "Polynomial must be 1d");
766 check(( coeff0 = cpl_polynomial_get_coeff(p->
pol, power),
767 cpl_polynomial_set_coeff(p->
pol, power, coeff0 + (p->
shift[0] - value)/p->
scale[0])),
768 "Error setting coefficient");
771 &result, multiplicity),
"Could not find root");
773 cpl_polynomial_set_coeff(p->
pol, power, coeff0);
802 int multiplicity,
int varno,
double x_value)
807 assure( 1 <= varno && varno <= 2, CPL_ERROR_ILLEGAL_INPUT,
808 "Illegal variable number: %d", varno);
811 "Could not collapse polynomial");
814 "Could not find root");
837 assure (1 <= varno && varno <= 2, CPL_ERROR_ILLEGAL_INPUT,
838 "Illegal variable number (%d)", varno);
840 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
857 int degree = cpl_polynomial_get_degree(p->
pol);
864 yj *= (varno == 1) ? x2 : x1)
877 for (i = degree; i >= 1; i--)
881 power[0] = (varno == 1) ? i : j;
882 power[1] = (varno == 1) ? j : i;
884 c_ij = cpl_polynomial_get_coeff(p->
pol, power);
887 if (i >= 2) sum *= (varno == 1) ? x1 : x2;
895 result *= p->
scale[0];
926 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
928 CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 1d");
930 check( dummy = cpl_polynomial_eval_1d(p->
pol, (x - p->
shift[1])/p->
scale[1], &result),
931 "Error evaluating derivative");
949 cpl_polynomial *pol = NULL;
951 assure(p1 != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
952 assure(p2 != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
954 CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 2d");
956 CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 2d");
973 pol = cpl_polynomial_new(2);
974 for (i = 0; i <= degree; i++)
975 for (j = 0; j <= degree; j++) {
976 double coeff1, coeff2;
985 cpl_polynomial_set_coeff(pol, power, coeff1 + coeff2);
992 uves_free_polynomial(&pol);
1010 static cpl_error_code
1013 int dimension, degree;
1017 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1018 dimension = cpl_polynomial_get_dimension(p);
1019 degree = cpl_polynomial_get_degree(p);
1020 assure( 1 <= dimension && dimension <= 2, CPL_ERROR_ILLEGAL_INPUT,
1021 "Illegal dimension: %d", dimension);
1022 assure( 1 <= varno && varno <= dimension, CPL_ERROR_ILLEGAL_INPUT,
1023 "Illegal variable number: %d", varno);
1028 for(i = 0; i <= degree; i++)
1034 coeff = cpl_polynomial_get_coeff(p, power);
1037 cpl_polynomial_set_coeff(p, power, (i+1) * coeff);
1044 for(i = 0; i <= degree; i++)
1046 for(j = 0; i + j <= degree; j++)
1049 power[varno - 1] = i+1;
1050 power[2 - varno] = j;
1052 coeff = cpl_polynomial_get_coeff(p, power);
1054 power[varno - 1] = i;
1056 cpl_polynomial_set_coeff(p, power, (i+1) * coeff);
1062 return cpl_error_get_code();
1081 assure( p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1083 assure( 1 <= varno && varno <= dimension, CPL_ERROR_ILLEGAL_INPUT,
1084 "Illegal variable number: %d", varno);
1102 "Error calculating derivative of CPL-polynomial");
1105 return cpl_error_get_code();
1127 assure( p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1129 assure(dimension == 2, CPL_ERROR_ILLEGAL_INPUT,
"Illegal dimension: %d", dimension);
1130 assure( 0 <= degree1, CPL_ERROR_ILLEGAL_INPUT,
"Illegal degree: %d", degree1);
1131 assure( 0 <= degree2, CPL_ERROR_ILLEGAL_INPUT,
"Illegal degree: %d", degree2);
1145 factorial *= degree1;
1153 factorial *= degree2;
1158 "Error evaluating polynomial");
1183 assure( p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1185 assure(dimension == 1, CPL_ERROR_ILLEGAL_INPUT,
"Illegal dimension: %d", dimension);
1186 assure( 0 <= degree, CPL_ERROR_ILLEGAL_INPUT,
"Illegal degree: %d", degree);
1200 factorial *= degree;
1205 "Error evaluating polynomial");
1234 cpl_polynomial *pol = NULL;
1235 cpl_size *power = NULL;
1238 int degree, dimension;
1240 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1242 assure(dimension > 0, CPL_ERROR_ILLEGAL_INPUT,
1243 "Polynomial has non-positive dimension: %d", dimension);
1244 assure(dimension != 1, CPL_ERROR_ILLEGAL_OUTPUT,
1245 "Don't collapse a 1d polynomial. Evaluate it!");
1250 assure(dimension == 2, CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 2d");
1252 assure(1 <= varno && varno <= dimension, CPL_ERROR_ILLEGAL_INPUT,
1253 "Wrong variable number");
1254 value = (value - p->
shift[varno]) / p->
scale[varno];
1257 degree = cpl_polynomial_get_degree(p->
pol);
1258 pol = cpl_polynomial_new(dimension - 1);
1259 power = cpl_malloc(
sizeof(cpl_size) * dimension);
1261 for (i = 0; i <= degree; i++)
1269 for (j = degree - i; j >= 0; j--)
1272 coeff += cpl_polynomial_get_coeff(p->
pol, power);
1273 if (j > 0) coeff *= value;
1277 cpl_polynomial_set_coeff(pol, power, coeff);
1285 for(i = 0; i <= dimension - 1; i++)
1301 assure(cpl_error_get_code() == CPL_ERROR_NONE, cpl_error_get_code(),
1302 "Error collapsing polynomial");
1305 cpl_free(power); power = NULL;
1306 uves_free_polynomial(&pol);
1307 if (cpl_error_get_code() != CPL_ERROR_NONE)
1338 const cpl_vector * x_pos,
1339 const cpl_vector * values,
1340 const cpl_vector * sigmas,
1346 cpl_matrix * ma = NULL;
1347 cpl_matrix * mb = NULL;
1348 cpl_matrix * mx = NULL;
1349 const double * x_pos_data ;
1350 const double * values_data ;
1351 const double * sigmas_data = NULL;
1352 double mean_x, mean_z;
1354 cpl_polynomial * out ;
1355 cpl_vector * x_val = NULL;
1359 assure_nomsg( x_pos != NULL && values != NULL, CPL_ERROR_NULL_INPUT);
1360 assure( poly_deg >= 0, CPL_ERROR_ILLEGAL_INPUT,
1361 "Polynomial degree is %d. Must be non-negative", poly_deg);
1362 np = cpl_vector_get_size(x_pos) ;
1365 assure( np >= nc, CPL_ERROR_ILLEGAL_INPUT,
1366 "Not enough points (%d) to fit %d-order polynomial. %d point(s) needed",
1373 ma = cpl_matrix_new(np, nc) ;
1374 mb = cpl_matrix_new(np, 1) ;
1377 mean_x = cpl_vector_get_mean(x_pos);
1378 mean_z = cpl_vector_get_mean(values);
1381 x_pos_data = cpl_vector_get_data_const(x_pos) ;
1382 values_data = cpl_vector_get_data_const(values) ;
1385 sigmas_data = cpl_vector_get_data_const(sigmas) ;
1390 for (i=0 ; i<np ; i++)
1393 if (sigmas_data[i] == 0)
1395 uves_free_matrix(&ma) ;
1396 uves_free_matrix(&mb) ;
1397 assure(
false, CPL_ERROR_DIVISION_BY_ZERO,
1398 "Sigmas must be non-zero");
1400 for (j=0 ; j<nc ; j++)
1402 cpl_matrix_set(ma, i, j,
1407 cpl_matrix_set(mb, i, 0, (values_data[i] - mean_z) / sigmas_data[i]);
1412 for (i=0 ; i<np ; i++)
1414 for (j=0 ; j<nc ; j++)
1416 cpl_matrix_set(ma, i, j,
1420 cpl_matrix_set(mb, i, 0, (values_data[i] - mean_z) / 1) ;
1425 check( mx = cpl_matrix_solve_normal(ma, mb),
1426 "Could not invert matrix");
1427 uves_free_matrix(&ma);
1428 uves_free_matrix(&mb);
1431 out = cpl_polynomial_new(1) ;
1433 for (deg=0 ; deg<nc ; deg++) {
1434 cpl_polynomial_set_coeff(out, °, cpl_matrix_get(mx, deg, 0)) ;
1436 uves_free_matrix(&mx);
1441 x_val = cpl_vector_new(1) ;
1442 for (i=0 ; i<np ; i++)
1445 cpl_vector_set(x_val, 0, x_pos_data[i] - mean_x) ;
1447 residual = (values_data[i] - mean_z) - cpl_polynomial_eval(out, x_val);
1448 *mse += residual*residual;
1450 uves_free_vector(&x_val) ;
1452 *mse /= (double)np ;
1457 uves_free_polynomial(&out);
1463 uves_free_vector(&x_val);
1464 uves_free_matrix(&ma);
1465 uves_free_matrix(&mb);
1466 uves_free_matrix(&mx);
1518 const cpl_bivector * xy_pos,
1519 const cpl_vector * values,
1520 const cpl_vector * sigmas,
1536 cpl_matrix * mat_ma;
1537 cpl_matrix * cov = NULL;
1538 const double * xy_pos_data_x ;
1539 const double * xy_pos_data_y ;
1540 const double * values_data ;
1541 const double * sigmas_data = NULL;
1542 const cpl_vector* xy_pos_x;
1543 const cpl_vector* xy_pos_y;
1544 double mean_x, mean_y, mean_z;
1545 cpl_polynomial * out ;
1546 cpl_polynomial * variance_cpl ;
1551 assure(xy_pos && values, CPL_ERROR_NULL_INPUT,
"Null input");
1552 assure(poly_deg1 >= 0, CPL_ERROR_ILLEGAL_INPUT,
"Polynomial degree1 is %d", poly_deg1);
1553 assure(poly_deg2 >= 0, CPL_ERROR_ILLEGAL_INPUT,
"Polynomial degree2 is %d", poly_deg2);
1554 np = cpl_bivector_get_size(xy_pos) ;
1557 assure( (variance == NULL && red_chisq == NULL) || sigmas != NULL,
1558 CPL_ERROR_ILLEGAL_INPUT,
1559 "Cannot calculate variance or chi_sq without knowing");
1562 nc = (1 + poly_deg1)*(1 + poly_deg2) ;
1564 assure(np >= nc, CPL_ERROR_SINGULAR_MATRIX,
"%d coefficients. Only %d points", nc, np);
1569 assure(red_chisq == NULL || np > nc, CPL_ERROR_ILLEGAL_INPUT,
1570 "%d coefficients. %d points. Cannot calculate chi square", nc, np);
1572 degx_tab = cpl_malloc(nc *
sizeof(
int)) ;
1575 degy_tab = cpl_malloc(nc *
sizeof(
int)) ;
1576 if (degy_tab == NULL) {
1583 for (degy=0 ; degy<=poly_deg2 ; degy++) {
1584 for (degx=0 ; degx<=poly_deg1 ; degx++) {
1585 degx_tab[i] = degx ;
1586 degy_tab[i] = degy ;
1596 ma = cpl_matrix_new(np, nc) ;
1597 mb = cpl_matrix_new(np, 1) ;
1600 xy_pos_x = cpl_bivector_get_x_const(xy_pos);
1601 xy_pos_y = cpl_bivector_get_y_const(xy_pos);
1603 mean_x = cpl_vector_get_mean(xy_pos_x);
1604 mean_y = cpl_vector_get_mean(xy_pos_y);
1605 mean_z = cpl_vector_get_mean(values);
1609 xy_pos_data_x = cpl_vector_get_data_const(xy_pos_x) ;
1610 xy_pos_data_y = cpl_vector_get_data_const(xy_pos_y) ;
1611 values_data = cpl_vector_get_data_const(values) ;
1614 sigmas_data = cpl_vector_get_data_const(sigmas) ;
1620 for (i=0 ; i<np ; i++) {
1621 double *ma_data = cpl_matrix_get_data(ma);
1622 double *mb_data = cpl_matrix_get_data(mb);
1628 if (sigmas_data[i] == 0)
1630 uves_free_matrix(&ma) ;
1631 uves_free_matrix(&mb) ;
1632 cpl_free(degx_tab) ;
1633 cpl_free(degy_tab) ;
1634 assure(
false, CPL_ERROR_DIVISION_BY_ZERO,
1635 "Sigmas must be non-zero. sigma[%d] is %f", i, sigmas_data[i]);
1638 for (degy=0 ; degy<=poly_deg2 ; degy++) {
1640 for (degx=0 ; degx<=poly_deg1 ; degx++) {
1641 ma_data[j + i*nc] = valx * valy / sigmas_data[i];
1642 valx *= (xy_pos_data_x[i] - mean_x);
1645 valy *= (xy_pos_data_y[i] - mean_y);
1650 mb_data[0 + i*1] = (values_data[i] - mean_z) / sigmas_data[i];
1656 for (i=0 ; i<np ; i++) {
1657 double *ma_data = cpl_matrix_get_data(ma);
1658 double *mb_data = cpl_matrix_get_data(mb);
1662 for (degy=0 ; degy<=poly_deg2 ; degy++) {
1664 for (degx=0 ; degx<=poly_deg1 ; degx++) {
1665 ma_data[j + i*nc] = valx * valy / 1;
1666 valx *= (xy_pos_data_x[i] - mean_x);
1669 valy *= (xy_pos_data_y[i] - mean_y);
1674 mb_data[0 + i*1] = (values_data[i] - mean_z) / 1;
1680 if (variance != NULL)
1682 mat = cpl_matrix_transpose_create(ma);
1685 mat_ma = cpl_matrix_product_create(mat, ma);
1688 cov = cpl_matrix_invert_create(mat_ma);
1693 variance_cpl = cpl_polynomial_new(2);
1696 uves_free_matrix(&mat);
1697 uves_free_matrix(&mat_ma);
1701 mx = cpl_matrix_solve_normal(ma, mb) ;
1703 uves_free_matrix(&ma) ;
1704 uves_free_matrix(&mb) ;
1706 cpl_free(degx_tab) ;
1707 cpl_free(degy_tab) ;
1708 uves_free_matrix(&cov) ;
1709 assure(
false, CPL_ERROR_ILLEGAL_OUTPUT,
"Matrix inversion failed") ;
1713 out = cpl_polynomial_new(2) ;
1714 powers = cpl_malloc(2 *
sizeof(cpl_size)) ;
1715 if (powers == NULL) {
1716 cpl_free(degx_tab) ;
1717 cpl_free(degy_tab) ;
1718 uves_free_matrix(&mx) ;
1719 uves_free_matrix(&cov) ;
1720 uves_free_polynomial(&out) ;
1726 for (i = 0 ; i < nc ; i++)
1728 powers[0] = degx_tab[i] ;
1729 powers[1] = degy_tab[i] ;
1730 cpl_polynomial_set_coeff(out, powers, cpl_matrix_get(mx, i, 0)) ;
1733 if (variance != NULL &&
1734 cov != NULL && variance_cpl != NULL
1738 for (j = 0; j < nc; j++)
1747 powers[0] = degx_tab[i] + degx_tab[j] ;
1748 powers[1] = degy_tab[i] + degy_tab[j] ;
1750 coeff = cpl_polynomial_get_coeff(variance_cpl, powers);
1751 cpl_polynomial_set_coeff(variance_cpl, powers,
1752 coeff + cpl_matrix_get(cov, i, j)) ;
1759 cpl_free(degx_tab) ;
1760 cpl_free(degy_tab) ;
1761 uves_free_matrix(&cov) ;
1762 uves_free_matrix(&mx) ;
1766 uves_free_polynomial(&out);
1772 if (variance != NULL)
1775 uves_free_polynomial(&variance_cpl);
1788 if (mse != NULL || red_chisq != NULL)
1792 if (mse != NULL) *mse = 0.00 ;
1793 if (red_chisq != NULL) *red_chisq = 0.00 ;
1794 for (i = 0 ; i < np ; i++)
1802 double residual = values_data[i] - regress;
1803 *mse += residual*residual;
1805 if (red_chisq != NULL)
1807 *red_chisq +=
uves_pow_int((values_data[i] - regress) /
1812 if (mse != NULL) *mse /= (double) np ;
1814 if (red_chisq != NULL)
1816 passure( np > nc,
"%d %d", np, nc);
1817 *red_chisq /= (double) (np - nc) ;
int uves_polynomial_get_dimension(const polynomial *p)
Get the dimension of a polynomial.
polynomial * uves_polynomial_collapse(const polynomial *p, int varno, double value)
Collapse a polynomial by fixing one variable to a constant.
polynomial * uves_polynomial_fit_1d(const cpl_vector *x_pos, const cpl_vector *values, const cpl_vector *sigmas, int poly_deg, double *mse)
Fit a 1d function with a polynomial.
double uves_polynomial_solve_2d(const polynomial *p, double value, double guess, int multiplicity, int varno, double x_value)
Solve p(x1, x2) = value.
double uves_polynomial_derivative_1d(const polynomial *p, double x)
Evaluate the derivative of a 1d polynomial.
void uves_polynomial_delete(polynomial **p)
Delete a polynomial.
cpl_error_code uves_polynomial_derivative(polynomial *p, int varno)
Calculate the partial derivative of a polynomial.
polynomial * uves_polynomial_add_2d(const polynomial *p1, const polynomial *p2)
Add two polynomials.
polynomial * uves_polynomial_convert_from_table(cpl_table *t)
Convert a table to a polynomial.
static cpl_error_code derivative_cpl_polynomial(cpl_polynomial *p, int varno)
Calculate the partial derivative of a CPL-polynomial.
double uves_pow_int(double x, int y)
Calculate x to the y'th.
#define passure(BOOL,...)
cpl_table * uves_polynomial_convert_to_table(const polynomial *p)
Convert a polynomial to a table.
double uves_polynomial_get_coeff_2d(const polynomial *p, int degree1, int degree2)
Get a coefficient of a 2D polynomial.
double uves_polynomial_derivative_2d(const polynomial *p, double x1, double x2, int varno)
Evaluate the partial derivative of a 2d polynomial.
int uves_polynomial_get_degree(const polynomial *p)
Get degree.
polynomial * uves_polynomial_duplicate(const polynomial *p)
Copy a polynomial.
void uves_polynomial_delete_const(const polynomial **p)
Delete a const polynomial.
cpl_error_code uves_polynomial_rescale(polynomial *p, int varno, double scale)
Rescale a polynomial.
polynomial * uves_polynomial_new(const cpl_polynomial *pol)
Create a polynomial.
double uves_polynomial_evaluate_2d(const polynomial *p, double x1, double x2)
Evaluate a 2d polynomial.
double uves_polynomial_evaluate_1d(const polynomial *p, double x)
Evaluate a 1d polynomial.
double uves_polynomial_get_coeff_1d(const polynomial *p, int degree)
Get a coefficient of a 1D polynomial.
void uves_polynomial_dump(const polynomial *p, FILE *stream)
Print a polynomial.
const char * uves_tostring_cpl_type(cpl_type t)
Convert a CPL type to a string.
cpl_error_code uves_polynomial_shift(polynomial *p, int varno, double shift)
Shift a polynomial.
#define uves_msg_debug(...)
Print a debug message.
#define assure_nomsg(BOOL, CODE)
polynomial * uves_polynomial_new_zero(int dim)
Create a zero polynomial.
double uves_polynomial_solve_1d(const polynomial *p, double value, double guess, int multiplicity)
Solve p(x) = value.
polynomial * uves_polynomial_fit_2d(const cpl_bivector *xy_pos, const cpl_vector *values, const cpl_vector *sigmas, int poly_deg1, int poly_deg2, double *mse, double *red_chisq, polynomial **variance)
Fit a 2d surface with a polynomial in x and y.