00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 #ifdef HAVE_CONFIG_H
00075 # include <config.h>
00076 #endif
00077
00078 #include <uves_utils_polynomial.h>
00079 #include <uves_error.h>
00080 #include <uves_utils_wrappers.h>
00081 #include <cpl_test.h>
00082
00083 #include <cpl.h>
00084
00085
00086
00087
00088
00092
00096
00100
00101 static void
00102 test_polynomial_fit_2d(void)
00103 {
00104 polynomial *pol = NULL;
00105 cpl_vector *x = NULL;
00106 cpl_vector *y = NULL;
00107 cpl_bivector *xy = NULL;
00108 cpl_vector *z = NULL;
00109
00110 int degx, degy;
00111
00112
00113 double coeff[3] = {43, -0.3, 0.0001};
00114 double valx, valy, valz;
00115 int npoints = 0;
00116 x = cpl_vector_new(1);
00117 y = cpl_vector_new(1);
00118 z = cpl_vector_new(1);
00119
00120 for (valx = -10; valx <= 50; valx += 4.7)
00121 for (valy = 0.001; valy <= 0.002; valy *= 1.1)
00122 {
00123
00124 valz = coeff[0]*valx*valx + coeff[1]*valy*valx + coeff[2]*valy*valy*valy*valy;
00125 npoints++;
00126 cpl_vector_set_size(x, npoints);
00127 cpl_vector_set_size(y, npoints);
00128 cpl_vector_set_size(z, npoints);
00129 cpl_vector_set(x, npoints-1, valx);
00130 cpl_vector_set(y, npoints-1, valy);
00131 cpl_vector_set(z, npoints-1, valz);
00132 }
00133
00134
00135 for (degx = 0; degx <= 5; degx++)
00136 for (degy = 0; degy <= 5; degy++) {
00137 uves_unwrap_bivector_vectors(&xy);
00138 xy = cpl_bivector_wrap_vectors(x, y);
00139
00140 uves_polynomial_delete(&pol);
00141 check_nomsg( pol = uves_polynomial_fit_2d(xy,
00142 z,
00143 NULL,
00144 degx, degy,
00145 NULL, NULL, NULL));
00146
00147
00148 if (degx >= 2 && degy >= 4) {
00149
00150 int i;
00151 for (i = 0; i < cpl_vector_get_size(x); i++)
00152 {
00153 cpl_test_rel(uves_polynomial_evaluate_2d(pol,
00154 cpl_vector_get(x, i),
00155 cpl_vector_get(y, i)),
00156 cpl_vector_get(z, i), 0.001);
00157 }
00158
00159
00160 #if 0
00161 for (i = 0; i <= degx; i++)
00162 for (j = 0; j <= degy; j++)
00163 if (i == 2 && j == 0)
00164 cpl_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[0], 0.0001);
00165 else if (i == 1 && j == 1)
00166 cpl_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[1], 0.0001);
00167 else if (i == 0 && j == 4)
00168 cpl_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[2], 0.0001);
00169 else
00170 {
00171 uves_msg_warning("%d, %d", i, j);
00172 cpl_test_abs(uves_polynomial_get_coeff_2d(pol, i, j), 0, 0.1);
00173 }
00174 #endif
00175 }
00176 }
00177
00178 cleanup:
00179 uves_free_vector(&x);
00180 uves_free_vector(&y);
00181 uves_free_vector(&z);
00182 uves_unwrap_bivector_vectors(&xy);
00183 uves_polynomial_delete(&pol);
00184 return;
00185 }
00186
00187
00191
00192 static void
00193 test_polynomial(void)
00194 {
00195 cpl_polynomial *cp1 = cpl_polynomial_new(2);
00196 cpl_polynomial *cp2 = cpl_polynomial_new(2);
00197 int power[2] = {0, 3};
00198 polynomial *p1;
00199 polynomial *p2;
00200 polynomial *p3;
00201
00202 cpl_polynomial_set_coeff(cp1, power, 7.0);
00203 cpl_polynomial_set_coeff(cp2, power, 9.0);
00204
00205 p1 = uves_polynomial_new(cp1);
00206 p2 = uves_polynomial_new(cp2);
00207
00208 uves_polynomial_rescale(p1, 0, 2.0);
00209 uves_polynomial_rescale(p2, 0, 2.0);
00210
00211 check_nomsg( p3 = uves_polynomial_add_2d(p1, p2) );
00212
00213 cpl_test_abs(uves_polynomial_get_coeff_2d(p3, 0, 0), 0 , 0.0001);
00214 cpl_test_abs(uves_polynomial_get_coeff_2d(p3, 0, 3), 7*2+9*2, 0.0001);
00215
00216 cleanup:
00217 uves_free_polynomial(&cp1);
00218 uves_free_polynomial(&cp2);
00219 uves_polynomial_delete(&p1);
00220 uves_polynomial_delete(&p2);
00221 uves_polynomial_delete(&p3);
00222 return;
00223
00224 }
00225
00226
00230
00231
00232 int main(void)
00233 {
00234 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00235
00236 test_polynomial_fit_2d();
00237
00238 check_nomsg( test_polynomial() );
00239
00240 cleanup:
00241 return cpl_test_end(0);
00242 }
00243
00244