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: amodigli $
00022  * $Date: 2012/01/17 07:53:20 $
00023  * $Revision: 1.9 $
00024  * $Name: uves-5_0_0 $
00025  * $Log: uves_polynomial-test.c,v $
00026  * Revision 1.9  2012/01/17 07:53:20  amodigli
00027  * update to CPL6
00028  *
00029  * Revision 1.8  2009/06/05 05:49:02  amodigli
00030  * updated init/end to cpl5
00031  *
00032  * Revision 1.7  2007/09/11 17:10:20  amodigli
00033  * moved test_midas_poly to test_dfs
00034  *
00035  * Revision 1.6  2007/06/20 13:04:26  amodigli
00036  * fix interface to
00037  *
00038  * Revision 1.5  2007/05/23 06:43:23  jmlarsen
00039  * Removed unused variables
00040  *
00041  * Revision 1.4  2007/05/03 15:18:22  jmlarsen
00042  * Added function to add polynomials
00043  *
00044  * Revision 1.3  2007/04/24 12:50:29  jmlarsen
00045  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00046  *
00047  * Revision 1.2  2007/03/19 13:51:41  jmlarsen
00048  * Added test of 2d fitting
00049  *
00050  * Revision 1.1  2007/03/15 12:27:18  jmlarsen
00051  * Moved unit tests to ./uves/tests and ./flames/tests
00052  *
00053  * Revision 1.3  2007/02/27 14:04:14  jmlarsen
00054  * Move unit test infrastructure to IRPLIB
00055  *
00056  * Revision 1.2  2007/01/29 12:17:54  jmlarsen
00057  * Support setting verbosity from command line
00058  *
00059  * Revision 1.1  2006/11/24 09:40:17  jmlarsen
00060  * Added polynomial tests
00061  *
00062  * Revision 1.1  2006/11/22 08:04:59  jmlarsen
00063  * Added uves_dfs unit test module
00064  *
00065  * Revision 1.20  2006/11/16 09:49:25  jmlarsen
00066  * Fixed doxygen bug
00067  *
00068  * Revision 1.19  2006/11/08 14:04:03  jmlarsen
00069  * Doxybugfix
00070  *
00071  */
00072 
00073 /*-----------------------------------------------------------------------------
00074                                 Includes
00075  -----------------------------------------------------------------------------*/
00076 
00077 #ifdef HAVE_CONFIG_H
00078 #  include <config.h>
00079 #endif
00080 
00081 #include <uves_utils_polynomial.h>
00082 #include <uves_error.h>
00083 #include <uves_utils_wrappers.h>
00084 #include <cpl_test.h>
00085 
00086 #include <cpl.h>
00087 /*-----------------------------------------------------------------------------
00088                                 Defines
00089  -----------------------------------------------------------------------------*/
00090 
00091 /*----------------------------------------------------------------------------*/
00095 /*----------------------------------------------------------------------------*/
00099 /*----------------------------------------------------------------------------*/
00103 /*----------------------------------------------------------------------------*/
00104 static void
00105 test_polynomial_fit_2d(void)
00106 {
00107     polynomial *pol = NULL;
00108     cpl_vector *x = NULL;
00109     cpl_vector *y = NULL;
00110     cpl_bivector *xy = NULL;
00111     cpl_vector *z = NULL;
00112 /*    cpl_vector *sigma = NULL;*/
00113     int degx, degy;
00114 
00115     /* build data */
00116     double coeff[3] = {43, -0.3, 0.0001};
00117     double valx, valy, valz;
00118     int npoints = 0;
00119     x = cpl_vector_new(1);
00120     y = cpl_vector_new(1);
00121     z = cpl_vector_new(1);
00122     
00123     for (valx = -10; valx <= 50; valx += 4.7)
00124         for (valy = 0.001; valy <= 0.002; valy *= 1.1)
00125             {
00126                 /* z = (2, 4) degree polynomial in x, y */
00127                 valz = coeff[0]*valx*valx + coeff[1]*valy*valx + coeff[2]*valy*valy*valy*valy;
00128                 npoints++;
00129                 cpl_vector_set_size(x, npoints);
00130                 cpl_vector_set_size(y, npoints);
00131                 cpl_vector_set_size(z, npoints);
00132                 cpl_vector_set(x, npoints-1, valx);
00133                 cpl_vector_set(y, npoints-1, valy);
00134                 cpl_vector_set(z, npoints-1, valz);
00135             }
00136     
00137     /* call function */
00138     for (degx = 0; degx <= 5; degx++)
00139         for (degy = 0; degy <= 5; degy++) {
00140             uves_unwrap_bivector_vectors(&xy);
00141             xy = cpl_bivector_wrap_vectors(x, y);
00142             
00143             uves_polynomial_delete(&pol);                    
00144             check_nomsg( pol = uves_polynomial_fit_2d(xy,
00145                                                       z,
00146                                                       NULL, /* sigma */
00147                                                       degx, degy,
00148                                                       NULL, NULL, NULL));
00149             
00150             /* test results */
00151             if (degx >= 2 && degy >= 4) {
00152                 /* Then we should have reproduced the input polynomial */
00153                 int i;
00154                 for (i = 0; i < cpl_vector_get_size(x); i++)
00155                     {
00156                         cpl_test_rel(uves_polynomial_evaluate_2d(pol, 
00157                                                                     cpl_vector_get(x, i),
00158                                                                     cpl_vector_get(y, i)),
00159                                         cpl_vector_get(z, i), 0.001);
00160                     }
00161                 
00162                 /* comparing the actual coefficients is less reliable */
00163 #if 0
00164                 for (i = 0; i <= degx; i++)
00165                     for (j = 0; j <= degy; j++)
00166                         if (i == 2 && j == 0)
00167                             cpl_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[0], 0.0001);
00168                         else if (i == 1 && j == 1)
00169                             cpl_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[1], 0.0001);
00170                         else if (i == 0 && j == 4)
00171                             cpl_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[2], 0.0001);
00172                         else
00173                             {
00174                                 uves_msg_warning("%d, %d", i, j);
00175                                 cpl_test_abs(uves_polynomial_get_coeff_2d(pol, i, j), 0, 0.1);
00176                             }
00177 #endif
00178             }
00179         }
00180     
00181   cleanup:
00182     uves_free_vector(&x);
00183     uves_free_vector(&y);
00184     uves_free_vector(&z);
00185     uves_unwrap_bivector_vectors(&xy);
00186     uves_polynomial_delete(&pol);                    
00187     return;
00188 }
00189 
00190 /*----------------------------------------------------------------------------*/
00194 /*----------------------------------------------------------------------------*/
00195 static void
00196 test_polynomial(void)
00197 {
00198     cpl_polynomial *cp1 = cpl_polynomial_new(2);
00199     cpl_polynomial *cp2 = cpl_polynomial_new(2);
00200     cpl_size power[2] = {0, 3};
00201     polynomial *p1;
00202     polynomial *p2;
00203     polynomial *p3;
00204 
00205     cpl_polynomial_set_coeff(cp1, power, 7.0);
00206     cpl_polynomial_set_coeff(cp2, power, 9.0);
00207 
00208     p1 = uves_polynomial_new(cp1);
00209     p2 = uves_polynomial_new(cp2);
00210 
00211     uves_polynomial_rescale(p1, 0, 2.0);
00212     uves_polynomial_rescale(p2, 0, 2.0);
00213 
00214     check_nomsg( p3 = uves_polynomial_add_2d(p1, p2) );
00215 
00216     cpl_test_abs(uves_polynomial_get_coeff_2d(p3, 0, 0), 0 , 0.0001);
00217     cpl_test_abs(uves_polynomial_get_coeff_2d(p3, 0, 3), 7*2+9*2, 0.0001);
00218     
00219   cleanup:
00220     uves_free_polynomial(&cp1);
00221     uves_free_polynomial(&cp2);
00222     uves_polynomial_delete(&p1);
00223     uves_polynomial_delete(&p2);
00224     uves_polynomial_delete(&p3);
00225     return;
00226     
00227 }
00228    
00229 /*----------------------------------------------------------------------------*/
00233 /*----------------------------------------------------------------------------*/
00234 
00235 int main(void)
00236 {
00237     cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00238 
00239     test_polynomial_fit_2d();
00240 
00241     check_nomsg( test_polynomial() );
00242 
00243   cleanup:
00244     return cpl_test_end(0);
00245 }
00246 
00247 

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1