fors_double.c

00001 /* $Id: fors_double.c,v 1.5 2010/09/14 07:49:30 cizzo Exp $
00002  *
00003  * This file is part of the FORS Library
00004  * Copyright (C) 2002-2010 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 /*
00022  * $Author: cizzo $
00023  * $Date: 2010/09/14 07:49:30 $
00024  * $Revision: 1.5 $
00025  * $Name: fors-4_8_6 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <fors_double.h>
00033 
00034 #include <fors_utils.h>
00035 
00036 #include <cpl.h>
00037 #include <math.h>
00038 #include <assert.h>
00039 
00040 #define LIST_DEFINE
00041 #undef LIST_ELEM
00042 #define LIST_ELEM double
00043 #include <list.h>
00044 
00045 
00053 /*
00054   Copy constructor
00055  */
00056 double *
00057 double_duplicate(const double *d)
00058 {
00059     double *dp = cpl_malloc(sizeof(*dp));
00060     *dp = *d;
00061     return dp;
00062 }
00063 
00064 /*
00065   @brief  Destructor
00066   @param  d    double
00067  */
00068 void
00069 double_delete(double **d)
00070 {
00071     if (d && *d) {
00072         cpl_free(*d); *d = NULL;
00073     }
00074 }
00075 
00082 double
00083 double_eval(const double *d, void *data)
00084 {
00085     data = data;
00086     return *d;
00087 }
00088 
00089 #undef cleanup
00090 #define cleanup
00091 
00100 double
00101 double_subtract(double x, double dx,
00102                 double y, double dy,
00103                 double *error)
00104 {
00105     assure( error != NULL, return 0, NULL );
00106     
00107     assure( dx >= 0, return 0, NULL );
00108     assure( dy >= 0, return 0, NULL );
00109 
00110     *error = sqrt( dx*dx + dy*dy );
00111 
00112     return x - y;
00113 }
00114 
00115 
00116 #undef cleanup
00117 #define cleanup
00118 
00128 double
00129 double_divide(double x, double dx,
00130           double y, double dy,
00131           double *error)
00132 {
00133     assure( error != NULL, return 0, NULL );
00134 
00135     assure( y*y > 0, return 0, NULL );
00136     assure( dx >= 0, return 0, NULL );
00137     assure( dy >= 0, return 0, NULL );
00138 
00139     *error = ( dx*dx + dy*dy * x*x / (y*y) ) / (y*y);
00140     *error = sqrt(*error);
00141 
00142     return x/y;
00143 }
00144 
00145 
00146 #undef cleanup
00147 #define cleanup
00148 
00159 double
00160 double_atan2(double y, double dy,
00161              double x, double dx,
00162          double *error)
00163 {
00164     assure( error != NULL, return 0, NULL );
00165     assure( dy >= 0, return 0, NULL );
00166     assure( dx >= 0, return 0, NULL );
00167     assure( (x*x + y*y)*(x*x + y*y) > 0, return 0, NULL ); /* angle undefined */
00168     
00169     /* using error propagation formula and d(atan(u))/du = 1/(1+u^2) */
00170     *error = (dy*dy*x*x + dx*dx*y*y) / ((x*x + y*y)*(x*x + y*y));
00171     *error = sqrt(*error);
00172 
00173     assert( *error >= 0 );
00174 
00175     return atan2(y, x);
00176 }

Generated on Fri Mar 4 09:46:00 2011 for FORS Pipeline Reference Manual by  doxygen 1.4.7