LAL  7.5.0.1-ec27e42
UnitMultiply.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 UnitMultiply_c
28  * \author J. T. Whelan <john.whelan@ligo.org>
29  *
30  * \brief Multiplies two \c LALUnit structures.
31  *
32  * This function multiplies together the \c LALUnit structures
33  * <tt>*(input->unitOne)</tt> and <tt>*(input->unitTwo)</tt>, thus allowing a
34  * module to <em>e.g.</em>, multiply two \c REAL8TimeSeries and
35  * give the resulting \c REAL8TimeSeries the correct units.
36  *
37  * ### Algorithm ###
38  *
39  * The function first adds together the overall powers of ten in the two
40  * input unit structures, then adds each of the corresponding rational
41  * powers in <tt>*(input->unitOne)</tt> and <tt>*(input->unitTwo)</tt> by naïve
42  * addition of rational numbers
43  * \f[
44  * \frac{N_1}{1+D_1} + \frac{N_2}{1+D_2} =
45  * \frac{N_1 (1+D_2) + N_2(1+D_1)}{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  * This function multiplies together the \c LALUnit structures
55  * <tt>*(input->unitOne)</tt> and <tt>*(input->unitTwo)</tt>, thus allowing a
56  * module to eg, multiply two \c REAL8TimeSeries and
57  * give the resulting \c REAL8TimeSeries the correct units.
58  *
59  * ### Uses ###
60  *
61  * <tt>XLALUnitNormalize()</tt>
62  *
63  */
64 LALUnit * XLALUnitMultiply( LALUnit *output, const LALUnit *unit1, const LALUnit *unit2 )
65 {
66  LALUnit unReduced;
67  UINT2 i;
68  INT4 numer;
69  UINT4 denom, denom1, denom2;
70 
71  if ( ! output || ! unit1 || ! unit2 )
73 
74  numer = unit1->powerOfTen + unit2->powerOfTen;
75  if ( numer >= 32767L || numer <= -32768L )
77 
78  unReduced.powerOfTen = numer;
79  for (i=0; i<LALNumUnits; ++i) {
80  denom1 = 1 + unit1->unitDenominatorMinusOne[i];
81  denom2 = 1 + unit2->unitDenominatorMinusOne[i];
82  denom = denom1 * denom2;
83 
84  if ( denom >= 65535L )
86 
87  /* One could use the gcd function to find the common factors of
88  denom1 and denom2, but we have to reduce the fractions after
89  addition anyway; consider e.g., 1/6 + 1/10 = (5+3)/30 = 4/15 */
90  unReduced.unitDenominatorMinusOne[i] = denom - 1;
91  numer = ((INT4) denom2) * unit1->unitNumerator[i]
92  + ((INT4) denom1) * unit2->unitNumerator[i];
93 
94  if ( numer >= 32767L || numer <= -32768L )
96 
97  unReduced.unitNumerator[i] = numer;
98  } /* for i */
99 
100  *output = unReduced;
103 
104  return output;
105 }
106 
107 /** UNDOCUMENTED */
108 LALUnit * XLALUnitDivide( LALUnit *output, const LALUnit *unit1, const LALUnit *unit2 )
109 {
110  LALUnit scratch;
111  /* invert unit2 and then multiply by unit1 */
112  if ( ! XLALUnitInvert( &scratch, unit2 ) )
114  if ( ! XLALUnitMultiply( output, unit1, &scratch ) )
116  return output;
117 }
118 
119 /** @} */
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
LALUnit * XLALUnitDivide(LALUnit *output, const LALUnit *unit1, const LALUnit *unit2)
UNDOCUMENTED.
Definition: UnitMultiply.c:108
LALUnit * XLALUnitMultiply(LALUnit *output, const LALUnit *unit1, const LALUnit *unit2)
This function multiplies together the LALUnit structures *(input->unitOne) and *(input->unitTwo),...
Definition: UnitMultiply.c:64
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 * XLALUnitInvert(LALUnit *output, const LALUnit *input)
UNDOCUMENTED.
Definition: UnitRaise.c:144
#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_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
void output(int gps_sec, int output_type)
Definition: tconvert.c:440