UVES Pipeline Reference Manual  5.4.6
uves_polynomial-test.c
1 /* *
2  * This file is part of the ESO UVES Pipeline *
3  * Copyright (C) 2004,2005 European Southern Observatory *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18  * */
19 
20 /*
21  * $Author: amodigli $
22  * $Date: 2012-01-17 07:53:20 $
23  * $Revision: 1.9 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.8 2009/06/05 05:49:02 amodigli
27  * updated init/end to cpl5
28  *
29  * Revision 1.7 2007/09/11 17:10:20 amodigli
30  * moved test_midas_poly to test_dfs
31  *
32  * Revision 1.6 2007/06/20 13:04:26 amodigli
33  * fix interface to
34  *
35  * Revision 1.5 2007/05/23 06:43:23 jmlarsen
36  * Removed unused variables
37  *
38  * Revision 1.4 2007/05/03 15:18:22 jmlarsen
39  * Added function to add polynomials
40  *
41  * Revision 1.3 2007/04/24 12:50:29 jmlarsen
42  * Replaced cpl_propertylist -> uves_propertylist which is much faster
43  *
44  * Revision 1.2 2007/03/19 13:51:41 jmlarsen
45  * Added test of 2d fitting
46  *
47  * Revision 1.1 2007/03/15 12:27:18 jmlarsen
48  * Moved unit tests to ./uves/tests and ./flames/tests
49  *
50  * Revision 1.3 2007/02/27 14:04:14 jmlarsen
51  * Move unit test infrastructure to IRPLIB
52  *
53  * Revision 1.2 2007/01/29 12:17:54 jmlarsen
54  * Support setting verbosity from command line
55  *
56  * Revision 1.1 2006/11/24 09:40:17 jmlarsen
57  * Added polynomial tests
58  *
59  * Revision 1.1 2006/11/22 08:04:59 jmlarsen
60  * Added uves_dfs unit test module
61  *
62  * Revision 1.20 2006/11/16 09:49:25 jmlarsen
63  * Fixed doxygen bug
64  *
65  * Revision 1.19 2006/11/08 14:04:03 jmlarsen
66  * Doxybugfix
67  *
68  */
69 
70 /*-----------------------------------------------------------------------------
71  Includes
72  -----------------------------------------------------------------------------*/
73 
74 #ifdef HAVE_CONFIG_H
75 # include <config.h>
76 #endif
77 
78 #include <uves_utils_polynomial.h>
79 #include <uves_error.h>
80 #include <uves_utils_wrappers.h>
81 #include <cpl_test.h>
82 
83 #include <cpl.h>
84 /*-----------------------------------------------------------------------------
85  Defines
86  -----------------------------------------------------------------------------*/
87 
88 /*----------------------------------------------------------------------------*/
92 /*----------------------------------------------------------------------------*/
96 /*----------------------------------------------------------------------------*/
100 /*----------------------------------------------------------------------------*/
101 static void
103 {
104  polynomial *pol = NULL;
105  cpl_vector *x = NULL;
106  cpl_vector *y = NULL;
107  cpl_bivector *xy = NULL;
108  cpl_vector *z = NULL;
109 /* cpl_vector *sigma = NULL;*/
110  int degx, degy;
111 
112  /* build data */
113  double coeff[3] = {43, -0.3, 0.0001};
114  double valx, valy, valz;
115  int npoints = 0;
116  x = cpl_vector_new(1);
117  y = cpl_vector_new(1);
118  z = cpl_vector_new(1);
119 
120  for (valx = -10; valx <= 50; valx += 4.7)
121  for (valy = 0.001; valy <= 0.002; valy *= 1.1)
122  {
123  /* z = (2, 4) degree polynomial in x, y */
124  valz = coeff[0]*valx*valx + coeff[1]*valy*valx + coeff[2]*valy*valy*valy*valy;
125  npoints++;
126  cpl_vector_set_size(x, npoints);
127  cpl_vector_set_size(y, npoints);
128  cpl_vector_set_size(z, npoints);
129  cpl_vector_set(x, npoints-1, valx);
130  cpl_vector_set(y, npoints-1, valy);
131  cpl_vector_set(z, npoints-1, valz);
132  }
133 
134  /* call function */
135  for (degx = 0; degx <= 5; degx++)
136  for (degy = 0; degy <= 5; degy++) {
137  uves_unwrap_bivector_vectors(&xy);
138  xy = cpl_bivector_wrap_vectors(x, y);
139 
140  uves_polynomial_delete(&pol);
142  z,
143  NULL, /* sigma */
144  degx, degy,
145  NULL, NULL, NULL));
146 
147  /* test results */
148  if (degx >= 2 && degy >= 4) {
149  /* Then we should have reproduced the input polynomial */
150  int i;
151  for (i = 0; i < cpl_vector_get_size(x); i++)
152  {
153  cpl_test_rel(uves_polynomial_evaluate_2d(pol,
154  cpl_vector_get(x, i),
155  cpl_vector_get(y, i)),
156  cpl_vector_get(z, i), 0.001);
157  }
158 
159  /* comparing the actual coefficients is less reliable */
160 #if 0
161  for (i = 0; i <= degx; i++)
162  for (j = 0; j <= degy; j++)
163  if (i == 2 && j == 0)
164  cpl_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[0], 0.0001);
165  else if (i == 1 && j == 1)
166  cpl_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[1], 0.0001);
167  else if (i == 0 && j == 4)
168  cpl_test_rel(uves_polynomial_get_coeff_2d(pol, i, j), coeff[2], 0.0001);
169  else
170  {
171  uves_msg_warning("%d, %d", i, j);
172  cpl_test_abs(uves_polynomial_get_coeff_2d(pol, i, j), 0, 0.1);
173  }
174 #endif
175  }
176  }
177 
178  cleanup:
179  uves_free_vector(&x);
180  uves_free_vector(&y);
181  uves_free_vector(&z);
182  uves_unwrap_bivector_vectors(&xy);
183  uves_polynomial_delete(&pol);
184  return;
185 }
186 
187 /*----------------------------------------------------------------------------*/
191 /*----------------------------------------------------------------------------*/
192 static void
194 {
195  cpl_polynomial *cp1 = cpl_polynomial_new(2);
196  cpl_polynomial *cp2 = cpl_polynomial_new(2);
197  cpl_size power[2] = {0, 3};
198  polynomial *p1;
199  polynomial *p2;
200  polynomial *p3;
201 
202  cpl_polynomial_set_coeff(cp1, power, 7.0);
203  cpl_polynomial_set_coeff(cp2, power, 9.0);
204 
205  p1 = uves_polynomial_new(cp1);
206  p2 = uves_polynomial_new(cp2);
207 
208  uves_polynomial_rescale(p1, 0, 2.0);
209  uves_polynomial_rescale(p2, 0, 2.0);
210 
211  check_nomsg( p3 = uves_polynomial_add_2d(p1, p2) );
212 
213  cpl_test_abs(uves_polynomial_get_coeff_2d(p3, 0, 0), 0 , 0.0001);
214  cpl_test_abs(uves_polynomial_get_coeff_2d(p3, 0, 3), 7*2+9*2, 0.0001);
215 
216  cleanup:
217  uves_free_polynomial(&cp1);
218  uves_free_polynomial(&cp2);
222  return;
223 
224 }
225 
226 /*----------------------------------------------------------------------------*/
230 /*----------------------------------------------------------------------------*/
231 
232 int main(void)
233 {
234  cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
235 
237 
239 
240  cleanup:
241  return cpl_test_end(0);
242 }
243 
244 
void uves_polynomial_delete(polynomial **p)
Delete a polynomial.
static void test_polynomial_fit_2d(void)
Test polynomial fitting.
#define uves_msg_warning(...)
Print an warning message.
Definition: uves_msg.h:87
polynomial * uves_polynomial_add_2d(const polynomial *p1, const polynomial *p2)
Add two polynomials.
#define check_nomsg(CMD)
Definition: uves_error.h:204
double uves_polynomial_get_coeff_2d(const polynomial *p, int degree1, int degree2)
Get a coefficient of a 2D polynomial.
int main(void)
Test of uves_polynomial.
static void test_polynomial(void)
Various tests.
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.
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.