1 /*============================================================================
2   WCSLIB 7.7 - an implementation of the FITS WCS standard.
3   Copyright (C) 1995-2021, Mark Calabretta
4 
5   This file is part of WCSLIB.
6 
7   WCSLIB is free software: you can redistribute it and/or modify it under the
8   terms of the GNU Lesser General Public License as published by the Free
9   Software Foundation, either version 3 of the License, or (at your option)
10   any later version.
11 
12   WCSLIB is distributed in the hope that it will be useful, but WITHOUT ANY
13   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15   more details.
16 
17   You should have received a copy of the GNU Lesser General Public License
18   along with WCSLIB.  If not, see http://www.gnu.org/licenses.
19 
20   Author: Mark Calabretta, Australia Telescope National Facility, CSIRO.
21   http://www.atnf.csiro.au/people/Mark.Calabretta
22   $Id: log.c,v 7.7 2021/07/12 06:36:49 mcalabre Exp $
23 *===========================================================================*/
24 
25 #include <math.h>
26 
27 #include "log.h"
28 
29 // Map status return value to message.
30 const char *log_errmsg[] = {
31   "Success",
32   "",
33   "Invalid log-coordinate reference value",
34   "One or more of the x coordinates were invalid",
35   "One or more of the world coordinates were invalid"};
36 
37 
38 //----------------------------------------------------------------------------
39 
logx2s(double crval,int nx,int sx,int slogc,const double x[],double logc[],int stat[])40 int logx2s(
41   double crval,
42   int nx,
43   int sx,
44   int slogc,
45   const double x[],
46   double logc[],
47   int stat[])
48 
49 {
50   register int ix;
51   register int *statp;
52   register const double *xp;
53   register double *logcp;
54 
55 
56   if (crval <= 0.0) {
57     return LOGERR_BAD_LOG_REF_VAL;
58   }
59 
60   xp = x;
61   logcp = logc;
62   statp = stat;
63   for (ix = 0; ix < nx; ix++, xp += sx, logcp += slogc) {
64     *logcp = crval * exp((*xp) / crval);
65     *(statp++) = 0;
66   }
67 
68   return 0;
69 }
70 
71 //----------------------------------------------------------------------------
72 
logs2x(double crval,int nlogc,int slogc,int sx,const double logc[],double x[],int stat[])73 int logs2x(
74   double crval,
75   int nlogc,
76   int slogc,
77   int sx,
78   const double logc[],
79   double x[],
80   int stat[])
81 
82 {
83   int status;
84   register int ilogc;
85   register int *statp;
86   register const double *logcp;
87   register double *xp;
88 
89 
90   if (crval <= 0.0) {
91     return LOGERR_BAD_LOG_REF_VAL;
92   }
93 
94   xp = x;
95   logcp = logc;
96   statp = stat;
97   status = 0;
98   for (ilogc = 0; ilogc < nlogc; ilogc++, logcp += slogc, xp += sx) {
99     if (*logcp > 0.0) {
100       *xp = crval * log(*logcp / crval);
101       *(statp++) = 0;
102     } else {
103       *(statp++) = 1;
104       status = LOGERR_BAD_WORLD;
105     }
106   }
107 
108   return status;
109 }
110