LAL  7.5.0.1-08ee4f4
UnitRaise.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Jolien Creighton, John Whelan
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 #define TRUE 1
21 #define FALSE 0
22 
23 #include <lal/LALStdlib.h>
24 #include <lal/Units.h>
25 
26 /**
27  * \addtogroup UnitRaise_c
28  * \author J. T. Whelan <john.whelan@ligo.org>
29  *
30  * \brief Raises an \c LALUnit structure to a specified rational power.
31  *
32  * This function raises the \c LALUnit structure <tt>*input</tt> to
33  * the rational power <tt>*power</tt>. In this way, units such as
34  * \f$\mathrm{s}^{1/2}\f$ and \f$\mathrm{m}^{-1}\f$ can be created using existing units.
35  *
36  * ### Algorithm ###
37  *
38  * The function first multiplies the overall power of ten
39  * <tt>input->powerOfTen</tt> by the rational number <tt>*power</tt>,
40  * checking to make sure that the resulting power is still an integer.
41  * It then multiplies each of the rational powers in <tt>*input</tt> by
42  * <tt>*power</tt> by naïve multiplication of rational numbers
43  * \f[
44  * \left(\frac{N_1}{1+D_1}\right)\left( \frac{N_2}{1+D_2} \right)
45  * = \frac{N_1 N_2}{1 + (1+D_1)(1+D_2)-1}
46  * \f]
47  * and then calls <tt>XLALUnitNormalize()</tt> to bring the result into
48  * standard form.
49  *
50  */
51 /** @{ */
52 
53 /**
54  * Raises a \c LALUnit structure to a rational power given by the \c RAT4 structure \c power.
55  */
57  const RAT4 *power )
58 {
59  LALUnit unReduced;
60  UINT2 i;
61  INT4 numer;
62  UINT4 denom, denom1, denom2;
63 
64  if ( ! output || ! input || ! power )
66 
67  denom2 = power->denominatorMinusOne + 1;
68 
69  if ( input->powerOfTen % denom2 )
71 
72  numer = (input->powerOfTen / (INT4) denom2) * power->numerator;
73 
74  if ( numer >= 32767L || numer <= -32768L )
76 
77  unReduced.powerOfTen = numer;
78 
79  for (i=0; i<LALNumUnits; ++i) {
80  denom1 = 1 + input->unitDenominatorMinusOne[i];
81  denom = denom1 * denom2;
82 
83  if ( denom - 1 >= 65535L )
85 
86  unReduced.unitDenominatorMinusOne[i] = denom - 1;
87 
88  numer = input->unitNumerator[i] * power->numerator;
89 
90  if ( numer >= 32767L || numer <= -32768L )
92 
93  unReduced.unitNumerator[i] = numer;
94  } /* for i */
95 
96  *output = unReduced;
99 
100  return output;
101 }
102 
103 /**
104  * Raises a \c LALUnit structure to an integer power \c power.
105  */
107  INT2 power )
108 {
109  RAT4 pow;
110  pow.numerator = power;
111  pow.denominatorMinusOne = 0;
112  if ( ! XLALUnitRaiseRAT4( output, input, &pow ) )
114  return output;
115 }
116 
117 /**
118  * Produces the square of a \c LALUnit structure.
119  */
121 {
122  RAT4 pow;
123  pow.numerator = 2;
124  pow.denominatorMinusOne = 0;
125  if ( ! XLALUnitRaiseRAT4( output, input, &pow ) )
127  return output;
128 }
129 
130 /**
131  * Produces the square-root of a \c LALUnit structure.
132  */
134 {
135  RAT4 pow;
136  pow.numerator = 1;
137  pow.denominatorMinusOne = 1;
138  if ( ! XLALUnitRaiseRAT4( output, input, &pow ) )
140  return output;
141 }
142 
143 /** UNDOCUMENTED */
145 {
146  RAT4 pow;
147  pow.numerator = -1;
148  pow.denominatorMinusOne = 0;
149  if ( ! XLALUnitRaiseRAT4( output, input, &pow ) )
151  return output;
152 }
153 
154 /** @} */
int16_t INT2
Two-byte signed integer.
uint16_t UINT2
Two-byte unsigned integer.
uint32_t UINT4
Four-byte unsigned integer.
int32_t INT4
Four-byte signed integer.
@ LALNumUnits
The number of units.
Definition: LALDatatypes.h:480
int XLALUnitNormalize(LALUnit *unit)
Returns 0 upon success or XLAL_FAILURE if the input pointer is NULL, in which case xlalErrno is set t...
Definition: UnitNormalize.c:72
LALUnit * XLALUnitRaiseINT2(LALUnit *output, const LALUnit *input, INT2 power)
Raises a LALUnit structure to an integer power power.
Definition: UnitRaise.c:106
LALUnit * XLALUnitInvert(LALUnit *output, const LALUnit *input)
UNDOCUMENTED.
Definition: UnitRaise.c:144
LALUnit * XLALUnitRaiseRAT4(LALUnit *output, const LALUnit *input, const RAT4 *power)
Raises a LALUnit structure to a rational power given by the RAT4 structure power.
Definition: UnitRaise.c:56
LALUnit * XLALUnitSquare(LALUnit *output, const LALUnit *input)
Produces the square of a LALUnit structure.
Definition: UnitRaise.c:120
LALUnit * XLALUnitSqrt(LALUnit *output, const LALUnit *input)
Produces the square-root of a LALUnit structure.
Definition: UnitRaise.c:133
#define XLAL_ERROR_NULL(...)
Macro to invoke a failure from a XLAL routine returning a pointer.
Definition: XLALError.h:713
@ XLAL_EFAULT
Invalid pointer.
Definition: XLALError.h:408
@ XLAL_ERANGE
Output range error.
Definition: XLALError.h:411
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_EINVAL
Invalid argument.
Definition: XLALError.h:409
@ XLAL_FAILURE
Failure return value (not an error number)
Definition: XLALError.h:402
This structure stores units in the mksA system (plus Kelvin, Strain, and ADC Count).
Definition: LALDatatypes.h:498
INT2 powerOfTen
Overall power-of-ten scaling is 10^powerOfTen.
Definition: LALDatatypes.h:499
UINT2 unitDenominatorMinusOne[LALNumUnits]
Array of unit power denominators-minus-one.
Definition: LALDatatypes.h:501
INT2 unitNumerator[LALNumUnits]
Array of unit power numerators.
Definition: LALDatatypes.h:500
A four-byte rational number, used as a parameter structure for XLALUnitRaiseRAT4().
Definition: Units.h:144
INT2 numerator
The numerator.
Definition: Units.h:145
UINT2 denominatorMinusOne
One less than the denominator.
Definition: Units.h:146
void output(int gps_sec, int output_type)
Definition: tconvert.c:440