Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/math/doc/math-gcd.qbk @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 7.4 KB
Line 
1
2[section:gcd_lcm Greatest Common Divisor and Least Common Multiple]
3
4[section Introduction]
5
6The class and function templates in <boost/math/common_factor.hpp>
7provide run-time and compile-time evaluation of the greatest common divisor
8(GCD) or least common multiple (LCM) of two integers.
9These facilities are useful for many numeric-oriented generic
10programming problems.
11
12[endsect]
13
14[section Synopsis]
15
16   namespace boost
17   {
18   namespace math
19   {
20
21   template < typename IntegerType >
22      class gcd_evaluator;
23   template < typename IntegerType >
24      class lcm_evaluator;
25
26   template < typename IntegerType >
27      IntegerType  gcd( IntegerType const &a, IntegerType const &b );
28   template < typename IntegerType >
29      IntegerType  lcm( IntegerType const &a, IntegerType const &b );
30
31   template < unsigned long Value1, unsigned long Value2 >
32      struct static_gcd;
33   template < unsigned long Value1, unsigned long Value2 >
34      struct static_lcm;
35
36   }
37   }
38   
39[endsect]
40
41[section GCD Function Object]
42
43[*Header: ] [@../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
44
45   template < typename IntegerType >
46   class boost::math::gcd_evaluator
47   {
48   public:
49      // Types
50      typedef IntegerType  result_type;
51      typedef IntegerType  first_argument_type;
52      typedef IntegerType  second_argument_type;
53
54      // Function object interface
55      result_type  operator ()( first_argument_type const &a,
56      second_argument_type const &b ) const;
57   };
58
59The boost::math::gcd_evaluator class template defines a function object
60class to return the greatest common divisor of two integers.
61The template is parameterized by a single type, called IntegerType here.
62This type should be a numeric type that represents integers.
63The result of the function object is always nonnegative, even if either of
64the operator arguments is negative.
65
66This function object class template is used in the corresponding version of
67the GCD function template. If a numeric type wants to customize evaluations
68of its greatest common divisors, then the type should specialize on the
69gcd_evaluator class template.
70
71[endsect]
72
73[section LCM Function Object]
74
75[*Header: ] [@../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
76
77   template < typename IntegerType >
78   class boost::math::lcm_evaluator
79   {
80   public:
81      // Types
82      typedef IntegerType  result_type;
83      typedef IntegerType  first_argument_type;
84      typedef IntegerType  second_argument_type;
85
86      // Function object interface
87      result_type  operator ()( first_argument_type const &a,
88      second_argument_type const &b ) const;
89   };
90
91The boost::math::lcm_evaluator class template defines a function object
92class to return the least common multiple of two integers. The template
93is parameterized by a single type, called IntegerType here. This type
94should be a numeric type that represents integers. The result of the
95function object is always nonnegative, even if either of the operator
96arguments is negative. If the least common multiple is beyond the range
97of the integer type, the results are undefined.
98
99This function object class template is used in the corresponding version
100of the LCM function template. If a numeric type wants to customize
101evaluations of its least common multiples, then the type should
102specialize on the lcm_evaluator class template.
103
104[endsect]
105
106[section Run-time GCD & LCM Determination]
107
108[*Header: ] [@../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
109
110   template < typename IntegerType >
111   IntegerType  boost::math::gcd( IntegerType const &a, IntegerType const &b );
112
113   template < typename IntegerType >
114   IntegerType  boost::math::lcm( IntegerType const &a, IntegerType const &b );
115
116The boost::math::gcd function template returns the greatest common
117(nonnegative) divisor of the two integers passed to it.
118The boost::math::lcm function template returns the least common
119(nonnegative) multiple of the two integers passed to it.
120The function templates are parameterized on the function arguments'
121IntegerType, which is also the return type. Internally, these function
122templates use an object of the corresponding version of the
123gcd_evaluator and lcm_evaluator class templates, respectively.
124
125[endsect]
126
127[section Compile time GCD and LCM determination]
128
129[*Header: ] [@../../boost/math/common_factor_ct.hpp <boost/math/common_factor_ct.hpp>]
130
131   template < unsigned long Value1, unsigned long Value2 >
132   struct boost::math::static_gcd
133   {
134      static unsigned long const  value = implementation_defined;
135   };
136
137   template < unsigned long Value1, unsigned long Value2 >
138   struct boost::math::static_lcm
139   {
140      static unsigned long const  value = implementation_defined;
141   };
142
143The boost::math::static_gcd and boost::math::static_lcm class templates
144take two value-based template parameters of the unsigned long type
145and have a single static constant data member, value, of that same type.
146The value of that member is the greatest common factor or least
147common multiple, respectively, of the template arguments.
148A compile-time error will occur if the least common multiple
149is beyond the range of an unsigned long.
150
151[h3 Example]
152
153   #include <boost/math/common_factor.hpp>
154   #include <algorithm>
155   #include <iterator>
156
157
158   int main()
159   {
160      using std::cout;
161      using std::endl;
162
163      cout << "The GCD and LCM of 6 and 15 are "
164      << boost::math::gcd(6, 15) << " and "
165      << boost::math::lcm(6, 15) << ", respectively."
166      << endl;
167
168      cout << "The GCD and LCM of 8 and 9 are "
169      << boost::math::static_gcd<8, 9>::value
170      << " and "
171      << boost::math::static_lcm<8, 9>::value
172      << ", respectively." << endl;
173
174      int  a[] = { 4, 5, 6 }, b[] = { 7, 8, 9 }, c[3];
175      std::transform( a, a + 3, b, c, boost::math::gcd_evaluator<int>() );
176      std::copy( c, c + 3, std::ostream_iterator<int>(cout, " ") );
177   }
178
179[endsect]
180
181[section Header <boost/math/common_factor.hpp>]
182
183This header simply includes the headers
184[@../../boost/math/common_factor_ct.hpp <boost/math/common_factor_ct.hpp>]
185and [@../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>].
186 
187Note this is a legacy header: it used to contain the actual implementation,
188but the compile-time and run-time facilities
189were moved to separate headers (since they were independent of each other).
190
191[endsect]
192
193[section Demonstration Program]
194
195The program [@../../libs/math/test/common_factor_test.cpp common_factor_test.cpp] is a demonstration of the results from
196instantiating various examples of the run-time GCD and LCM function
197templates and the compile-time GCD and LCM class templates.
198(The run-time GCD and LCM class templates are tested indirectly through
199the run-time function templates.)
200
201[endsect]
202
203[section Rationale]
204
205The greatest common divisor and least common multiple functions are
206greatly used in some numeric contexts, including some of the other
207Boost libraries. Centralizing these functions to one header improves
208code factoring and eases maintainence.
209
210[endsect]
211
212[section History]
213
214* 17 Dec 2005: Converted documentation to Quickbook Format.
215* 2 Jul 2002: Compile-time and run-time items separated to new headers.
216* 7 Nov 2001: Initial version
217
218[endsect]
219
220[section Credits]
221
222The author of the Boost compilation of GCD and LCM computations is
223Daryle Walker. The code was prompted by existing code hiding in the
224implementations of Paul Moore's rational library and Steve Cleary's
225pool library. The code had updates by Helmut Zeisel.
226
227[endsect]
228
229[endsect]
230
Note: See TracBrowser for help on using the repository browser.