Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-3a66518
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
UnitNormalize.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 UnitNormalize_c
28 * \author J. T. Whelan <john.whelan@ligo.org>
29 *
30 * \brief Brings an \c LALUnit structure into standard form by reducing all of the rational exponents into LCD form.
31 *
32 * Since the \c LALUnit structure stores the rational powers of the
33 * fundamental units as numerator and denominator, it is possible to
34 * represent the same units in different ways, <em>e.g.</em>, \f$\mathrm{m}^2\f$
35 * versus \f$\mathrm{m}^{4/2}\f$. This function reduces all of those fractions to
36 * convert the structure to its simplest form.
37 *
38 * ### Algorithm ###
39 *
40 * The rational powers are reduced using Euclid's algorithm \cite Geddes_1992 .
41 *
42 * ### Notes ###
43 *
44 * Note that the functions <tt>XLALUnitRaiseRAT4()</tt>,
45 * <tt>XLALUnitMultiply()</tt>, and <tt>XLALUnitCompare()</tt> all call
46 * <tt>XLALUnitNormalize()</tt> themselves, so there is usually no need to
47 * call it explicitly.
48 *
49 */
50/** @{ */
51
52/* this is an implementation of the Euclidean Algorithm */
53static UINT2
54gcd(INT2 numer, UINT2 denom)
55{
56 UINT2 next_numer,next_denom,remainder;
57 next_numer=abs(numer);
58 next_denom=denom;
59 while(next_denom != 0){
60 remainder=next_numer%next_denom;
61 next_numer=next_denom;
62 next_denom=remainder;
63 }
64 return next_numer;
65}
66
67/**
68 * Returns 0 upon success or #XLAL_FAILURE
69 * if the input pointer is \c NULL, in which case ::xlalErrno
70 * is set to #XLAL_EFAULT.
71 */
73{
74 UINT2 commonFactor;
75 UINT2 i;
76
77 if ( ! unit )
79
80 for (i=0; i<LALNumUnits; ++i)
81 {
82 commonFactor = gcd ( unit->unitNumerator[i], unit->unitDenominatorMinusOne[i] + 1 );
83 unit->unitNumerator[i] /= commonFactor;
84 unit->unitDenominatorMinusOne[i] = ( unit->unitDenominatorMinusOne[i] + 1 ) / commonFactor - 1;
85 } /* for i */
86
87 return 0;
88}
89
90
91/** @} */
int16_t INT2
Two-byte signed integer.
uint16_t UINT2
Two-byte unsigned integer.
@ LALNumUnits
The number of units.
Definition: LALDatatypes.h:480
static UINT2 gcd(INT2 numer, UINT2 denom)
Definition: UnitNormalize.c:54
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
#define XLAL_ERROR(...)
Macro to invoke a failure from a XLAL routine returning an integer.
Definition: XLALError.h:700
@ XLAL_EFAULT
Invalid pointer.
Definition: XLALError.h:408
This structure stores units in the mksA system (plus Kelvin, Strain, and ADC Count).
Definition: LALDatatypes.h:498
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