GIRAFFE Pipeline Reference Manual

gichebyshev.c
1 /* $Id$
2  *
3  * This file is part of the GIRAFFE Pipeline
4  * Copyright (C) 2002-2006 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 /*
21  * $Author$
22  * $Date$
23  * $Revision$
24  * $Name$
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 
31 #include <cxmemory.h>
32 #include <cxmessages.h>
33 
34 #include <cpl_matrix.h>
35 
36 #include "gichebyshev.h"
37 
38 
47 struct GiChebyshev2D {
48 
49  cxint xorder;
50  cxint yorder;
51 
52  cxdouble ax;
53  cxdouble bx;
54  cxdouble ay;
55  cxdouble by;
56 
57  cpl_matrix *coeffs;
58 
59 };
60 
61 
62 inline static cxdouble
63 _giraffe_chebyshev2d_eval(const GiChebyshev2D *self, cxdouble x, cxdouble y)
64 {
65 
66  cxint i, k;
67  cxint nx = self->xorder + 1;
68  cxint ny = self->yorder + 1;
69 
70  cxdouble xn = (2.0 * x - self->ax - self->bx) / (self->bx - self->ax);
71  cxdouble yn = (2.0 * y - self->ay - self->by) / (self->by - self->ay);
72  cxdouble cx0 = 1.0;
73  cxdouble cx1 = xn;
74  cxdouble sum = 0.;
75  cxdouble *_coeffs = cpl_matrix_get_data(self->coeffs);
76 
77 
78  cx_assert(_coeffs != NULL);
79 
80  for (i = 0, k = 0; i < nx; i++) {
81 
82  register cxint j;
83 
84  register cxdouble cy0 = 1.0;
85  register cxdouble cy1 = yn;
86  register cxdouble cx2 = 0.;
87 
88 
89  if (i < 2) {
90  cx2 = cx0;
91  }
92  else {
93  cx2 = 2.0 * cx1 * xn - cx0;
94  }
95 
96  for (j = 0; j < ny; j++) {
97 
98  cxdouble cy2 = 0.;
99 
100  if (j < 2) {
101  cy2 = cy0;
102  }
103  else {
104  cy2 = 2.0 * cy1 * yn - cy0;
105  }
106 
107  sum += cx2 * cy2 * _coeffs[k++];
108 
109  cy0 = cy1;
110  cy1 = cy2;
111 
112  }
113 
114  cx0 = cx1;
115  cx1 = cx2;
116 
117  }
118 
119  return sum;
120 
121 }
122 
123 
124 GiChebyshev2D *
125 giraffe_chebyshev2d_new(cxint xorder, cxint yorder)
126 {
127 
128  GiChebyshev2D *self = cx_calloc(1, sizeof *self);
129 
130 
131  if (self) {
132 
133  self->xorder = xorder;
134  self->yorder = yorder;
135 
136  self->coeffs = cpl_matrix_new((xorder + 1), (yorder + 1));
137 
138  if (self->coeffs == NULL) {
139  giraffe_chebyshev2d_delete(self);
140  return NULL;
141  }
142 
143  }
144 
145  return self;
146 
147 }
148 
149 
150 GiChebyshev2D *
151 giraffe_chebyshev2d_clone(const GiChebyshev2D *other)
152 {
153 
154  GiChebyshev2D *self = NULL;
155 
156 
157  if (other != NULL) {
158 
159  self = giraffe_chebyshev2d_new(other->xorder, other->yorder);
160 
161  self->ax = other->ax;
162  self->bx = other->bx;
163  self->ay = other->ay;
164  self->by = other->by;
165 
166  self->coeffs = cpl_matrix_duplicate(other->coeffs);
167 
168  }
169 
170  return self;
171 
172 }
173 
174 
175 void
176 giraffe_chebyshev2d_delete(GiChebyshev2D *self)
177 {
178 
179  if (self) {
180 
181  if (self->coeffs) {
182  cpl_matrix_delete(self->coeffs);
183  self->coeffs = NULL;
184  }
185 
186  cx_free(self);
187 
188  }
189 
190  return;
191 
192 }
193 
194 
195 void giraffe_chebyshev2d_get_order(const GiChebyshev2D *self, cxint *xorder,
196  cxint *yorder)
197 {
198 
199  cx_assert(self != NULL);
200 
201  if (xorder != NULL) {
202  *xorder = self->xorder;
203  }
204 
205  if (yorder != NULL) {
206  *yorder = self->yorder;
207  }
208 
209  return;
210 
211 }
212 
213 
214 void
215 giraffe_chebyshev2d_get_range(const GiChebyshev2D *self, cxdouble *ax,
216  cxdouble *bx, cxdouble *ay, cxdouble *by)
217 {
218 
219  cx_assert(self != NULL);
220 
221  if (ax != NULL) {
222  *ax = self->ax;
223  }
224 
225  if (bx != NULL) {
226  *bx = self->bx;
227  }
228 
229  if (ay != NULL) {
230  *ay = self->ay;
231  }
232 
233  if (by != NULL) {
234  *by = self->by;
235  }
236 
237  return;
238 
239 }
240 
241 
242 const cpl_matrix *
243 giraffe_chebyshev2d_coeffs(const GiChebyshev2D *self)
244 {
245 
246  cx_assert(self != NULL);
247 
248  return self->coeffs;
249 
250 }
251 
252 
253 cxint
254 giraffe_chebyshev2d_set(GiChebyshev2D *self, cxdouble ax, cxdouble bx,
255  cxdouble ay, cxdouble by, cpl_matrix *coeffs)
256 {
257 
258  cx_assert(self != NULL);
259 
260  self->ax = ax;
261  self->bx = bx;
262  self->ay = ay;
263  self->by = by;
264 
265  if (cpl_matrix_get_nrow(coeffs) <= self->xorder ||
266  cpl_matrix_get_ncol(coeffs) <= self->yorder) {
267  return 1;
268  }
269  else {
270 
271  cxint i;
272 
273  for (i = 0; i <= self->xorder; i++) {
274 
275  cxint j;
276 
277  for (j = 0; j <= self->yorder; j++) {
278 
279  cxdouble c = cpl_matrix_get(coeffs, i, j);
280 
281  cpl_matrix_set(self->coeffs, i, j, c);
282 
283  }
284 
285  }
286 
287  }
288 
289  return 0;
290 
291 }
292 
293 
294 cxint
295 giraffe_chebyshev2d_set_coeff(GiChebyshev2D *self, cxint i, cxint j,
296  cxdouble value)
297 {
298 
299  cx_assert(self != NULL);
300 
301  if (i > self->xorder || j > self->yorder) {
302  return 1;
303  }
304 
305  cpl_matrix_set(self->coeffs, i, j, value);
306 
307  return 0;
308 
309 }
310 
311 
312 cxdouble
313 giraffe_chebyshev2d_eval(const GiChebyshev2D *self, cxdouble x, cxdouble y)
314 {
315 
316  cx_assert(self != NULL);
317  return _giraffe_chebyshev2d_eval(self, x, y);
318 
319 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.14.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Wed Mar 11 2015 13:19:41 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2004