uves_polynomial-test.c

00001 /*                                                                              *
00002  *   This file is part of the ESO UVES Pipeline                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library is free software; you can redistribute it and/or modify       *
00006  *   it under the terms of the GNU General Public License as published by       *
00007  *   the Free Software Foundation; either version 2 of the License, or          *
00008  *   (at your option) any later version.                                        *
00009  *                                                                              *
00010  *   This program is distributed in the hope that it will be useful,            *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00013  *   GNU General Public License for more details.                               *
00014  *                                                                              *
00015  *   You should have received a copy of the GNU General Public License          *
00016  *   along with this program; if not, write to the Free Software                *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
00018  *                                                                              */
00019  
00020 /*
00021  * $Author: jmlarsen $
00022  * $Date: 2007/05/23 06:43:23 $
00023  * $Revision: 1.5 $
00024  * $Name: uves-3_3_1 $
00025  * $Log: uves_polynomial-test.c,v $
00026  * Revision 1.5  2007/05/23 06:43:23  jmlarsen
00027  * Removed unused variables
00028  *
00029  * Revision 1.4  2007/05/03 15:18:22  jmlarsen
00030  * Added function to add polynomials
00031  *
00032  * Revision 1.3  2007/04/24 12:50:29  jmlarsen
00033  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00034  *
00035  * Revision 1.2  2007/03/19 13:51:41  jmlarsen
00036  * Added test of 2d fitting
00037  *
00038  * Revision 1.1  2007/03/15 12:27:18  jmlarsen
00039  * Moved unit tests to ./uves/tests and ./flames/tests
00040  *
00041  * Revision 1.3  2007/02/27 14:04:14  jmlarsen
00042  * Move unit test infrastructure to IRPLIB
00043  *
00044  * Revision 1.2  2007/01/29 12:17:54  jmlarsen
00045  * Support setting verbosity from command line
00046  *
00047  * Revision 1.1  2006/11/24 09:40:17  jmlarsen
00048  * Added polynomial tests
00049  *
00050  * Revision 1.1  2006/11/22 08:04:59  jmlarsen
00051  * Added uves_dfs unit test module
00052  *
00053  * Revision 1.20  2006/11/16 09:49:25  jmlarsen
00054  * Fixed doxygen bug
00055  *
00056  * Revision 1.19  2006/11/08 14:04:03  jmlarsen
00057  * Doxybugfix
00058  *
00059  */
00060 
00061 /*-----------------------------------------------------------------------------
00062                                 Includes
00063  -----------------------------------------------------------------------------*/
00064 
00065 #ifdef HAVE_CONFIG_H
00066 #  include <config.h>
00067 #endif
00068 
00069 #include <uves_utils_polynomial.h>
00070 #include <uves_error.h>
00071 #include <uves_utils_wrappers.h>
00072 #include <irplib_test.h>
00073 
00074 #include <cpl.h>
00075 /*-----------------------------------------------------------------------------
00076                                 Defines
00077  -----------------------------------------------------------------------------*/
00078 
00079 /*----------------------------------------------------------------------------*/
00083 /*----------------------------------------------------------------------------*/
00087 /*----------------------------------------------------------------------------*/
00091 /*----------------------------------------------------------------------------*/
00092 static void
00093 test_polynomial_fit_2d(void)
00094 {
00095     polynomial *pol = NULL;
00096     cpl_vector *x = NULL;
00097     cpl_vector *y = NULL;
00098     cpl_bivector *xy = NULL;
00099     cpl_vector *z = NULL;
00100 /*    cpl_vector *sigma = NULL;*/
00101     int degx, degy;
00102 
00103     /* build data */
00104     double coeff[3] = {43, -0.3, 0.0001};
00105     double valx, valy, valz;
00106     int npoints = 0;
00107     x = cpl_vector_new(1);
00108     y = cpl_vector_new(1);
00109     z = cpl_vector_new(1);
00110     
00111     for (valx = -10; valx <= 50; valx += 4.7)
00112         for (valy = 0.001; valy <= 0.002; valy *= 1.1)
00113             {
00114                 /* z = (2, 4) degree polynomial in x, y */
00115                 valz = coeff[0]*valx*valx + coeff[1]*valy*valx + coeff[2]*valy*valy*valy*valy;
00116                 npoints++;
00117                 cpl_vector_set_size(x, npoints);
00118                 cpl_vector_set_size(y, npoints);
00119                 cpl_vector_set_size(z, npoints);
00120                 cpl_vector_set(x, npoints-1, valx);
00121                 cpl_vector_set(y, npoints-1, valy);
00122                 cpl_vector_set(z, npoints-1, valz);
00123             }
00124     
00125     /* call function */
00126     for (degx = 0; degx <= 5; degx++)
00127         for (degy = 0; degy <= 5; degy++) {
00128             uves_unwrap_bivector_vectors(&xy);
00129             xy = cpl_bivector_wrap_vectors(x, y);
00130             
00131             uves_polynomial_delete(&pol);                    
00132             check_nomsg( pol = uves_polynomial_fit_2d(xy,
00133                                                       z,
00134                                                       NULL, /* sigma */
00135                                                       degx, degy,
00136                                                       NULL, NULL, NULL));
00137             
00138             /* test results */
00139             if (degx >= 2 && degy >= 4) {
00140                 /* Then we should have reproduced the input polynomial */
00141                 int i;
00142                 for (i = 0; i < cpl_vector_get_size(x); i++)
00143                     {
00144                         irplib_test_rel(uves_polynomial_evaluate_2d(pol, 
00145                                                                     cpl_vector_get(x, i),
00146                                                                     cpl_vector_get(y, i)),
00147                                         cpl_vector_get(z, i), 0.001);
00148                     }
00149                 
00150                 /* comparing the actual coefficients is less reliable */
00151 #if 0
00152                 for (i = 0; i <= degx; i++)
00153                     for (j = 0; j <= degy; j++)
00154                         if (i == 2 && j == 0)
00155                             irplib_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[0], 0.0001);
00156                         else if (i == 1 && j == 1)
00157                             irplib_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[1], 0.0001);
00158                         else if (i == 0 && j == 4)
00159                             irplib_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[2], 0.0001);
00160                         else
00161                             {
00162                                 uves_msg_warning("%d, %d", i, j);
00163                                 irplib_test_abs(uves_polynomial_get_coeff_2d(pol, i, j), 0, 0.1);
00164                             }
00165 #endif
00166             }
00167         }
00168     
00169   cleanup:
00170     uves_free_vector(&x);
00171     uves_free_vector(&y);
00172     uves_free_vector(&z);
00173     uves_unwrap_bivector_vectors(&xy);
00174     uves_polynomial_delete(&pol);                    
00175     return;
00176 }
00177 
00178 /*----------------------------------------------------------------------------*/
00182 /*----------------------------------------------------------------------------*/
00183 static void
00184 parse_midas_poly(void)
00185 {
00186     uves_propertylist *header = uves_propertylist_new();
00187     polynomial *p = NULL;
00188 
00189     const char *data[] = {"",
00190             "'COEFFI','I*4',1,7,'7I10'",
00191             "     53889         2         3         2         1         4         5",
00192             "",
00193             "'COEFFR','R*4',1,5,'5E14.7'",
00194             " 4.3300000E+02 4.0880000E+03 1.0000000E+00 2.1000000E+01 0.0000000E+00",
00195             "",
00196             "'COEFFD','R*8',1,30,'3E23.15'",
00197             " -7.097005629698889E+01  4.050908371864904E-02 -2.886756545398909E-06",
00198             "  5.504345508879626E-10 -5.583004967206025E-14  7.624532125635992E+01",
00199             " -2.428213567964009E-03  1.819158447566360E-06 -5.090366383338846E-10",
00200             "  5.198098506055602E-14  3.513177145982783E-01  5.570332137951829E-04",
00201             " -3.876157463910250E-07  1.113253735718822E-10 -1.132455173423791E-14",
00202             "  2.977232589499959E-02 -5.389240622889887E-05  3.777456726044612E-08",
00203             " -1.083863050648735E-11  1.098450510939580E-15 -1.093309039442914E-03",            
00204             "  2.402609262989674E-06 -1.688416547941747E-09  4.839101712729582E-13",
00205             " -4.884504488944702E-17  1.919853952642526E-05 -4.004133160220927E-08",
00206             "  2.816206503824200E-11 -8.051313882805877E-15  8.090579180112579E-19",
00207             " ",
00208             "'TAB_IN_OUT_YSHIFT','R*8',1,1,'3E23.15'",
00209             "  4.180818583555659E+01            ",
00210             " "};
00211     unsigned int i;
00212     
00213     /* Create typical FLAMES ordef table header */
00214     for (i = 0; i < 8000; i++)
00215     {
00216         uves_propertylist_append_string(
00217         header, "HISTORY",
00218         " 35834     35835     35836     35837     35838     35839     35840");
00219     }
00220 
00221     for (i = 0; i < sizeof(data)/sizeof(char *); i++)
00222     {
00223         uves_propertylist_append_string(
00224         header, "HISTORY",
00225         data[i]);
00226     }
00227 
00228     check_nomsg( p = uves_polynomial_convert_from_plist_midas(header, 
00229                                   "COEFF"));
00230     
00231     assure( uves_polynomial_get_dimension(p) == 2, CPL_ERROR_ILLEGAL_OUTPUT,
00232         "Dimension is %d, 2 expected", uves_polynomial_get_dimension(p));
00233 
00234   cleanup:
00235     uves_free_propertylist(&header);
00236     uves_polynomial_delete(&p);
00237 
00238     return;
00239 }
00240 
00241 /*----------------------------------------------------------------------------*/
00245 /*----------------------------------------------------------------------------*/
00246 static void
00247 test_polynomial(void)
00248 {
00249     cpl_polynomial *cp1 = cpl_polynomial_new(2);
00250     cpl_polynomial *cp2 = cpl_polynomial_new(2);
00251     int power[2] = {0, 3};
00252     polynomial *p1;
00253     polynomial *p2;
00254     polynomial *p3;
00255 
00256     cpl_polynomial_set_coeff(cp1, power, 7.0);
00257     cpl_polynomial_set_coeff(cp2, power, 9.0);
00258 
00259     p1 = uves_polynomial_new(cp1);
00260     p2 = uves_polynomial_new(cp2);
00261 
00262     uves_polynomial_rescale(p1, 0, 2.0);
00263     uves_polynomial_rescale(p2, 0, 2.0);
00264 
00265     check_nomsg( p3 = uves_polynomial_add_2d(p1, p2) );
00266 
00267     irplib_test_abs(uves_polynomial_get_coeff_2d(p3, 0, 0), 0 , 0.0001);
00268     irplib_test_abs(uves_polynomial_get_coeff_2d(p3, 0, 3), 7*2+9*2, 0.0001);
00269     
00270   cleanup:
00271     uves_free_polynomial(&cp1);
00272     uves_free_polynomial(&cp2);
00273     uves_polynomial_delete(&p1);
00274     uves_polynomial_delete(&p2);
00275     uves_polynomial_delete(&p3);
00276     return;
00277     
00278 }
00279    
00280 /*----------------------------------------------------------------------------*/
00284 /*----------------------------------------------------------------------------*/
00285 
00286 int main(void)
00287 {
00288     IRPLIB_TEST_INIT;
00289 
00290     check( parse_midas_poly(),
00291        "Test of MIDAS array conversion");
00292 
00293     test_polynomial_fit_2d();
00294 
00295     check_nomsg( test_polynomial() );
00296 
00297   cleanup:
00298     IRPLIB_TEST_END;
00299 }
00300 
00301 

Generated on Tue Jun 19 14:39:17 2007 for UVES Pipeline Reference Manual by  doxygen 1.4.6