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);
412 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
413 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
414 cpl_table_set_double(t, COLUMN_COEFF , row, p->
shift[0]); row++;
416 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
417 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
418 cpl_table_set_double(t, COLUMN_COEFF , row, p->
shift[1]); row++;
420 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
421 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
422 cpl_table_set_double(t, COLUMN_COEFF , row, p->
shift[2]); row++;
425 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
426 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
427 cpl_table_set_double(t, COLUMN_COEFF, row, p->
scale[0]); row++;
429 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
430 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
431 cpl_table_set_double(t, COLUMN_COEFF, row, p->
scale[1]); row++;
433 cpl_table_set_int (t, COLUMN_ORDER1, row, -1);
434 cpl_table_set_int (t, COLUMN_ORDER2, row, -1);
435 cpl_table_set_double(t, COLUMN_COEFF, row, p->
scale[2]); row++;
438 for (i = 0; i <= degree; i++){
439 for (j = 0; j+i <= degree; j++){
445 coeff = cpl_polynomial_get_coeff(p->
pol, power);
446 cpl_table_set_int (t, COLUMN_ORDER1, row, power[0]);
447 cpl_table_set_int (t, COLUMN_ORDER2, row, power[1]);
448 cpl_table_set_double(t, COLUMN_COEFF , row, coeff);
472 cpl_polynomial *pol = NULL;
477 check( pol = cpl_polynomial_new(2),
"Error initializing polynomial");
480 assure(t != NULL, CPL_ERROR_NULL_INPUT,
"Null table");
481 assure(cpl_table_has_column(t, COLUMN_ORDER1), CPL_ERROR_ILLEGAL_INPUT,
482 "No '%s' column found in table", COLUMN_ORDER1);
483 assure(cpl_table_has_column(t, COLUMN_ORDER2), CPL_ERROR_ILLEGAL_INPUT,
484 "No '%s' column found in table", COLUMN_ORDER2);
485 assure(cpl_table_has_column(t, COLUMN_COEFF ), CPL_ERROR_ILLEGAL_INPUT,
486 "No '%s' column found in table", COLUMN_COEFF );
488 type = cpl_table_get_column_type(t, COLUMN_ORDER1);
489 assure(type == CPL_TYPE_INT , CPL_ERROR_INVALID_TYPE,
490 "Column '%s' has type %s. Integer expected", COLUMN_ORDER1,
493 type = cpl_table_get_column_type(t, COLUMN_ORDER2);
494 assure(type == CPL_TYPE_INT , CPL_ERROR_INVALID_TYPE,
495 "Column '%s' has type %s. Integer expected", COLUMN_ORDER2,
498 type = cpl_table_get_column_type(t, COLUMN_COEFF);
499 assure(type == CPL_TYPE_DOUBLE, CPL_ERROR_INVALID_TYPE,
500 "Column '%s' has type %s. Double expected", COLUMN_COEFF ,
503 assure(cpl_table_get_nrow(t) > 1 + 2 + 1 + 2, CPL_ERROR_ILLEGAL_INPUT,
504 "Table must contain at least one coefficient");
507 for(i = 3 + 3; i < cpl_table_get_nrow(t); i++) {
511 check(( power[0] = cpl_table_get_int(t, COLUMN_ORDER1, i, NULL),
512 power[1] = cpl_table_get_int(t, COLUMN_ORDER2, i, NULL),
513 coeff = cpl_table_get_double(t, COLUMN_COEFF , i, NULL)),
514 "Error reading table row %d", i);
516 uves_msg_debug(
"Pol.coeff.(%" CPL_SIZE_FORMAT
", %" CPL_SIZE_FORMAT
") = %e", power[0], power[1], coeff);
518 check( cpl_polynomial_set_coeff(pol, power, coeff),
"Error creating polynomial");
531 uves_free_polynomial(&pol);
532 if (cpl_error_get_code() != CPL_ERROR_NONE)
550 assure(p != NULL, CPL_ERROR_ILLEGAL_INPUT,
"Null polynomial");
571 fprintf(stream,
"Null polynomial\n");
574 cpl_polynomial_dump(p->
pol, stream);
575 fprintf(stream,
"shift_y \t= %f \tscale_y \t= %f\n", p->
shift[0], p->
scale[0]);
578 fprintf(stream,
"shift_x%d \t= %f \tscale_x%d \t= %f\n",
603 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
605 CPL_ERROR_ILLEGAL_INPUT,
"Illegal variable number: %d", varno);
619 p->
shift[varno] *= scale;
620 p->
scale[varno] *= scale;
623 return cpl_error_get_code();
644 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
646 CPL_ERROR_ILLEGAL_INPUT,
"Illegal variable number: %d", varno);
657 p->
shift[varno] += shift;
660 return cpl_error_get_code();
678 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
680 CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 1d");
683 cpl_polynomial_eval_1d(p->
pol, (x - p->
shift[1])/p->
scale[1], NULL)
685 "Could not evaluate polynomial");
709 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
710 assure(p->dimension == 2, CPL_ERROR_ILLEGAL_INPUT,
711 "Polynomial must be 2d. It's %dd", p->dimension);
713 double scale = p->
scale[0];
714 double shift = p->
shift[0];
718 p->vec_data[0] = (x1 - p->
shift[1]) / p->
scale[1];
719 p->vec_data[1] = (x2 - p->
shift[2]) / p->
scale[2];
721 result = cpl_polynomial_eval(p->
pol, p->
vec) * scale + shift;
749 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
751 "Polynomial must be 1d");
762 check(( coeff0 = cpl_polynomial_get_coeff(p->
pol, power),
763 cpl_polynomial_set_coeff(p->
pol, power, coeff0 + (p->
shift[0] - value)/p->
scale[0])),
764 "Error setting coefficient");
767 &result, multiplicity),
"Could not find root");
769 cpl_polynomial_set_coeff(p->
pol, power, coeff0);
798 int multiplicity,
int varno,
double x_value)
803 assure( 1 <= varno && varno <= 2, CPL_ERROR_ILLEGAL_INPUT,
804 "Illegal variable number: %d", varno);
807 "Could not collapse polynomial");
810 "Could not find root");
833 assure (1 <= varno && varno <= 2, CPL_ERROR_ILLEGAL_INPUT,
834 "Illegal variable number (%d)", varno);
836 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
853 int degree = cpl_polynomial_get_degree(p->
pol);
860 yj *= (varno == 1) ? x2 : x1)
873 for (i = degree; i >= 1; i--)
877 power[0] = (varno == 1) ? i : j;
878 power[1] = (varno == 1) ? j : i;
880 c_ij = cpl_polynomial_get_coeff(p->
pol, power);
883 if (i >= 2) sum *= (varno == 1) ? x1 : x2;
891 result *= p->
scale[0];
922 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
924 CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 1d");
926 check( dummy = cpl_polynomial_eval_1d(p->
pol, (x - p->
shift[1])/p->
scale[1], &result),
927 "Error evaluating derivative");
945 cpl_polynomial *pol = NULL;
947 assure(p1 != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
948 assure(p2 != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
950 CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 2d");
952 CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 2d");
969 pol = cpl_polynomial_new(2);
970 for (i = 0; i <= degree; i++)
971 for (j = 0; j <= degree; j++) {
972 double coeff1, coeff2;
981 cpl_polynomial_set_coeff(pol, power, coeff1 + coeff2);
988 uves_free_polynomial(&pol);
1006 static cpl_error_code
1009 int dimension, degree;
1013 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1014 dimension = cpl_polynomial_get_dimension(p);
1015 degree = cpl_polynomial_get_degree(p);
1016 assure( 1 <= dimension && dimension <= 2, CPL_ERROR_ILLEGAL_INPUT,
1017 "Illegal dimension: %d", dimension);
1018 assure( 1 <= varno && varno <= dimension, CPL_ERROR_ILLEGAL_INPUT,
1019 "Illegal variable number: %d", varno);
1024 for(i = 0; i <= degree; i++)
1030 coeff = cpl_polynomial_get_coeff(p, power);
1033 cpl_polynomial_set_coeff(p, power, (i+1) * coeff);
1040 for(i = 0; i <= degree; i++)
1042 for(j = 0; i + j <= degree; j++)
1045 power[varno - 1] = i+1;
1046 power[2 - varno] = j;
1048 coeff = cpl_polynomial_get_coeff(p, power);
1050 power[varno - 1] = i;
1052 cpl_polynomial_set_coeff(p, power, (i+1) * coeff);
1058 return cpl_error_get_code();
1077 assure( p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1079 assure( 1 <= varno && varno <= dimension, CPL_ERROR_ILLEGAL_INPUT,
1080 "Illegal variable number: %d", varno);
1098 "Error calculating derivative of CPL-polynomial");
1101 return cpl_error_get_code();
1123 assure( p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1125 assure(dimension == 2, CPL_ERROR_ILLEGAL_INPUT,
"Illegal dimension: %d", dimension);
1126 assure( 0 <= degree1, CPL_ERROR_ILLEGAL_INPUT,
"Illegal degree: %d", degree1);
1127 assure( 0 <= degree2, CPL_ERROR_ILLEGAL_INPUT,
"Illegal degree: %d", degree2);
1141 factorial *= degree1;
1149 factorial *= degree2;
1154 "Error evaluating polynomial");
1179 assure( p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1181 assure(dimension == 1, CPL_ERROR_ILLEGAL_INPUT,
"Illegal dimension: %d", dimension);
1182 assure( 0 <= degree, CPL_ERROR_ILLEGAL_INPUT,
"Illegal degree: %d", degree);
1196 factorial *= degree;
1201 "Error evaluating polynomial");
1230 cpl_polynomial *pol = NULL;
1231 cpl_size *power = NULL;
1234 int degree, dimension;
1236 assure(p != NULL, CPL_ERROR_NULL_INPUT,
"Null polynomial");
1238 assure(dimension > 0, CPL_ERROR_ILLEGAL_INPUT,
1239 "Polynomial has non-positive dimension: %d", dimension);
1240 assure(dimension != 1, CPL_ERROR_ILLEGAL_OUTPUT,
1241 "Don't collapse a 1d polynomial. Evaluate it!");
1246 assure(dimension == 2, CPL_ERROR_ILLEGAL_INPUT,
"Polynomial must be 2d");
1248 assure(1 <= varno && varno <= dimension, CPL_ERROR_ILLEGAL_INPUT,
1249 "Wrong variable number");
1250 value = (value - p->
shift[varno]) / p->
scale[varno];
1253 degree = cpl_polynomial_get_degree(p->
pol);
1254 pol = cpl_polynomial_new(dimension - 1);
1255 power = cpl_malloc(
sizeof(cpl_size) * dimension);
1257 for (i = 0; i <= degree; i++)
1265 for (j = degree - i; j >= 0; j--)
1268 coeff += cpl_polynomial_get_coeff(p->
pol, power);
1269 if (j > 0) coeff *= value;
1273 cpl_polynomial_set_coeff(pol, power, coeff);
1281 for(i = 0; i <= dimension - 1; i++)
1297 assure(cpl_error_get_code() == CPL_ERROR_NONE, cpl_error_get_code(),
1298 "Error collapsing polynomial");
1301 cpl_free(power); power = NULL;
1302 uves_free_polynomial(&pol);
1303 if (cpl_error_get_code() != CPL_ERROR_NONE)
1334 const cpl_vector * x_pos,
1335 const cpl_vector * values,
1336 const cpl_vector * sigmas,
1342 cpl_matrix * ma = NULL;
1343 cpl_matrix * mb = NULL;
1344 cpl_matrix * mx = NULL;
1345 const double * x_pos_data ;
1346 const double * values_data ;
1347 const double * sigmas_data = NULL;
1348 double mean_x, mean_z;
1350 cpl_polynomial * out ;
1351 cpl_vector * x_val = NULL;
1355 assure_nomsg( x_pos != NULL && values != NULL, CPL_ERROR_NULL_INPUT);
1356 assure( poly_deg >= 0, CPL_ERROR_ILLEGAL_INPUT,
1357 "Polynomial degree is %d. Must be non-negative", poly_deg);
1358 np = cpl_vector_get_size(x_pos) ;
1361 assure( np >= nc, CPL_ERROR_ILLEGAL_INPUT,
1362 "Not enough points (%d) to fit %d-order polynomial. %d point(s) needed",
1369 ma = cpl_matrix_new(np, nc) ;
1370 mb = cpl_matrix_new(np, 1) ;
1373 mean_x = cpl_vector_get_mean(x_pos);
1374 mean_z = cpl_vector_get_mean(values);
1377 x_pos_data = cpl_vector_get_data_const(x_pos) ;
1378 values_data = cpl_vector_get_data_const(values) ;
1381 sigmas_data = cpl_vector_get_data_const(sigmas) ;
1386 for (i=0 ; i<np ; i++)
1389 if (sigmas_data[i] == 0)
1391 uves_free_matrix(&ma) ;
1392 uves_free_matrix(&mb) ;
1393 assure(
false, CPL_ERROR_DIVISION_BY_ZERO,
1394 "Sigmas must be non-zero");
1396 for (j=0 ; j<nc ; j++)
1398 cpl_matrix_set(ma, i, j,
1403 cpl_matrix_set(mb, i, 0, (values_data[i] - mean_z) / sigmas_data[i]);
1408 for (i=0 ; i<np ; i++)
1410 for (j=0 ; j<nc ; j++)
1412 cpl_matrix_set(ma, i, j,
1416 cpl_matrix_set(mb, i, 0, (values_data[i] - mean_z) / 1) ;
1421 check( mx = cpl_matrix_solve_normal(ma, mb),
1422 "Could not invert matrix");
1423 uves_free_matrix(&ma);
1424 uves_free_matrix(&mb);
1427 out = cpl_polynomial_new(1) ;
1429 for (deg=0 ; deg<nc ; deg++) {
1430 cpl_polynomial_set_coeff(out, °, cpl_matrix_get(mx, deg, 0)) ;
1432 uves_free_matrix(&mx);
1437 x_val = cpl_vector_new(1) ;
1438 for (i=0 ; i<np ; i++)
1441 cpl_vector_set(x_val, 0, x_pos_data[i] - mean_x) ;
1443 residual = (values_data[i] - mean_z) - cpl_polynomial_eval(out, x_val);
1444 *mse += residual*residual;
1446 uves_free_vector(&x_val) ;
1448 *mse /= (double)np ;
1453 uves_free_polynomial(&out);
1459 uves_free_vector(&x_val);
1460 uves_free_matrix(&ma);
1461 uves_free_matrix(&mb);
1462 uves_free_matrix(&mx);
1514 const cpl_bivector * xy_pos,
1515 const cpl_vector * values,
1516 const cpl_vector * sigmas,
1532 cpl_matrix * mat_ma;
1533 cpl_matrix * cov = NULL;
1534 const double * xy_pos_data_x ;
1535 const double * xy_pos_data_y ;
1536 const double * values_data ;
1537 const double * sigmas_data = NULL;
1538 const cpl_vector* xy_pos_x;
1539 const cpl_vector* xy_pos_y;
1540 double mean_x, mean_y, mean_z;
1541 cpl_polynomial * out ;
1542 cpl_polynomial * variance_cpl ;
1547 assure(xy_pos && values, CPL_ERROR_NULL_INPUT,
"Null input");
1548 assure(poly_deg1 >= 0, CPL_ERROR_ILLEGAL_INPUT,
"Polynomial degree1 is %d", poly_deg1);
1549 assure(poly_deg2 >= 0, CPL_ERROR_ILLEGAL_INPUT,
"Polynomial degree2 is %d", poly_deg2);
1550 np = cpl_bivector_get_size(xy_pos) ;
1553 assure( (variance == NULL && red_chisq == NULL) || sigmas != NULL,
1554 CPL_ERROR_ILLEGAL_INPUT,
1555 "Cannot calculate variance or chi_sq without knowing");
1558 nc = (1 + poly_deg1)*(1 + poly_deg2) ;
1560 assure(np >= nc, CPL_ERROR_SINGULAR_MATRIX,
"%d coefficients. Only %d points", nc, np);
1565 assure(red_chisq == NULL || np > nc, CPL_ERROR_ILLEGAL_INPUT,
1566 "%d coefficients. %d points. Cannot calculate chi square", nc, np);
1568 degx_tab = cpl_malloc(nc *
sizeof(
int)) ;
1571 degy_tab = cpl_malloc(nc *
sizeof(
int)) ;
1572 if (degy_tab == NULL) {
1579 for (degy=0 ; degy<=poly_deg2 ; degy++) {
1580 for (degx=0 ; degx<=poly_deg1 ; degx++) {
1581 degx_tab[i] = degx ;
1582 degy_tab[i] = degy ;
1592 ma = cpl_matrix_new(np, nc) ;
1593 mb = cpl_matrix_new(np, 1) ;
1596 xy_pos_x = cpl_bivector_get_x_const(xy_pos);
1597 xy_pos_y = cpl_bivector_get_y_const(xy_pos);
1599 mean_x = cpl_vector_get_mean(xy_pos_x);
1600 mean_y = cpl_vector_get_mean(xy_pos_y);
1601 mean_z = cpl_vector_get_mean(values);
1605 xy_pos_data_x = cpl_vector_get_data_const(xy_pos_x) ;
1606 xy_pos_data_y = cpl_vector_get_data_const(xy_pos_y) ;
1607 values_data = cpl_vector_get_data_const(values) ;
1610 sigmas_data = cpl_vector_get_data_const(sigmas) ;
1616 for (i=0 ; i<np ; i++) {
1617 double *ma_data = cpl_matrix_get_data(ma);
1618 double *mb_data = cpl_matrix_get_data(mb);
1624 if (sigmas_data[i] == 0)
1626 uves_free_matrix(&ma) ;
1627 uves_free_matrix(&mb) ;
1628 cpl_free(degx_tab) ;
1629 cpl_free(degy_tab) ;
1630 assure(
false, CPL_ERROR_DIVISION_BY_ZERO,
1631 "Sigmas must be non-zero. sigma[%d] is %f", i, sigmas_data[i]);
1634 for (degy=0 ; degy<=poly_deg2 ; degy++) {
1636 for (degx=0 ; degx<=poly_deg1 ; degx++) {
1637 ma_data[j + i*nc] = valx * valy / sigmas_data[i];
1638 valx *= (xy_pos_data_x[i] - mean_x);
1641 valy *= (xy_pos_data_y[i] - mean_y);
1646 mb_data[0 + i*1] = (values_data[i] - mean_z) / sigmas_data[i];
1652 for (i=0 ; i<np ; i++) {
1653 double *ma_data = cpl_matrix_get_data(ma);
1654 double *mb_data = cpl_matrix_get_data(mb);
1658 for (degy=0 ; degy<=poly_deg2 ; degy++) {
1660 for (degx=0 ; degx<=poly_deg1 ; degx++) {
1661 ma_data[j + i*nc] = valx * valy / 1;
1662 valx *= (xy_pos_data_x[i] - mean_x);
1665 valy *= (xy_pos_data_y[i] - mean_y);
1670 mb_data[0 + i*1] = (values_data[i] - mean_z) / 1;
1676 if (variance != NULL)
1678 mat = cpl_matrix_transpose_create(ma);
1681 mat_ma = cpl_matrix_product_create(mat, ma);
1684 cov = cpl_matrix_invert_create(mat_ma);
1689 variance_cpl = cpl_polynomial_new(2);
1692 uves_free_matrix(&mat);
1693 uves_free_matrix(&mat_ma);
1697 mx = cpl_matrix_solve_normal(ma, mb) ;
1699 uves_free_matrix(&ma) ;
1700 uves_free_matrix(&mb) ;
1702 cpl_free(degx_tab) ;
1703 cpl_free(degy_tab) ;
1704 uves_free_matrix(&cov) ;
1705 assure(
false, CPL_ERROR_ILLEGAL_OUTPUT,
"Matrix inversion failed") ;
1709 out = cpl_polynomial_new(2) ;
1710 powers = cpl_malloc(2 *
sizeof(cpl_size)) ;
1711 if (powers == NULL) {
1712 cpl_free(degx_tab) ;
1713 cpl_free(degy_tab) ;
1714 uves_free_matrix(&mx) ;
1715 uves_free_matrix(&cov) ;
1716 uves_free_polynomial(&out) ;
1722 for (i = 0 ; i < nc ; i++)
1724 powers[0] = degx_tab[i] ;
1725 powers[1] = degy_tab[i] ;
1726 cpl_polynomial_set_coeff(out, powers, cpl_matrix_get(mx, i, 0)) ;
1729 if (variance != NULL &&
1730 cov != NULL && variance_cpl != NULL
1734 for (j = 0; j < nc; j++)
1743 powers[0] = degx_tab[i] + degx_tab[j] ;
1744 powers[1] = degy_tab[i] + degy_tab[j] ;
1746 coeff = cpl_polynomial_get_coeff(variance_cpl, powers);
1747 cpl_polynomial_set_coeff(variance_cpl, powers,
1748 coeff + cpl_matrix_get(cov, i, j)) ;
1755 cpl_free(degx_tab) ;
1756 cpl_free(degy_tab) ;
1757 uves_free_matrix(&cov) ;
1758 uves_free_matrix(&mx) ;
1762 uves_free_polynomial(&out);
1768 if (variance != NULL)
1771 uves_free_polynomial(&variance_cpl);
1784 if (mse != NULL || red_chisq != NULL)
1788 if (mse != NULL) *mse = 0.00 ;
1789 if (red_chisq != NULL) *red_chisq = 0.00 ;
1790 for (i = 0 ; i < np ; i++)
1798 double residual = values_data[i] - regress;
1799 *mse += residual*residual;
1801 if (red_chisq != NULL)
1803 *red_chisq +=
uves_pow_int((values_data[i] - regress) /
1808 if (mse != NULL) *mse /= (double) np ;
1810 if (red_chisq != NULL)
1812 passure( np > nc,
"%d %d", np, nc);
1813 *red_chisq /= (double) (np - nc) ;