1 | |
---|
2 | [section:gcd_lcm Greatest Common Divisor and Least Common Multiple] |
---|
3 | |
---|
4 | [section Introduction] |
---|
5 | |
---|
6 | The class and function templates in <boost/math/common_factor.hpp> |
---|
7 | provide run-time and compile-time evaluation of the greatest common divisor |
---|
8 | (GCD) or least common multiple (LCM) of two integers. |
---|
9 | These facilities are useful for many numeric-oriented generic |
---|
10 | programming 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 | |
---|
59 | The boost::math::gcd_evaluator class template defines a function object |
---|
60 | class to return the greatest common divisor of two integers. |
---|
61 | The template is parameterized by a single type, called IntegerType here. |
---|
62 | This type should be a numeric type that represents integers. |
---|
63 | The result of the function object is always nonnegative, even if either of |
---|
64 | the operator arguments is negative. |
---|
65 | |
---|
66 | This function object class template is used in the corresponding version of |
---|
67 | the GCD function template. If a numeric type wants to customize evaluations |
---|
68 | of its greatest common divisors, then the type should specialize on the |
---|
69 | gcd_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 | |
---|
91 | The boost::math::lcm_evaluator class template defines a function object |
---|
92 | class to return the least common multiple of two integers. The template |
---|
93 | is parameterized by a single type, called IntegerType here. This type |
---|
94 | should be a numeric type that represents integers. The result of the |
---|
95 | function object is always nonnegative, even if either of the operator |
---|
96 | arguments is negative. If the least common multiple is beyond the range |
---|
97 | of the integer type, the results are undefined. |
---|
98 | |
---|
99 | This function object class template is used in the corresponding version |
---|
100 | of the LCM function template. If a numeric type wants to customize |
---|
101 | evaluations of its least common multiples, then the type should |
---|
102 | specialize 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 | |
---|
116 | The boost::math::gcd function template returns the greatest common |
---|
117 | (nonnegative) divisor of the two integers passed to it. |
---|
118 | The boost::math::lcm function template returns the least common |
---|
119 | (nonnegative) multiple of the two integers passed to it. |
---|
120 | The function templates are parameterized on the function arguments' |
---|
121 | IntegerType, which is also the return type. Internally, these function |
---|
122 | templates use an object of the corresponding version of the |
---|
123 | gcd_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 | |
---|
143 | The boost::math::static_gcd and boost::math::static_lcm class templates |
---|
144 | take two value-based template parameters of the unsigned long type |
---|
145 | and have a single static constant data member, value, of that same type. |
---|
146 | The value of that member is the greatest common factor or least |
---|
147 | common multiple, respectively, of the template arguments. |
---|
148 | A compile-time error will occur if the least common multiple |
---|
149 | is 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 | |
---|
183 | This header simply includes the headers |
---|
184 | [@../../boost/math/common_factor_ct.hpp <boost/math/common_factor_ct.hpp>] |
---|
185 | and [@../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]. |
---|
186 | |
---|
187 | Note this is a legacy header: it used to contain the actual implementation, |
---|
188 | but the compile-time and run-time facilities |
---|
189 | were moved to separate headers (since they were independent of each other). |
---|
190 | |
---|
191 | [endsect] |
---|
192 | |
---|
193 | [section Demonstration Program] |
---|
194 | |
---|
195 | The program [@../../libs/math/test/common_factor_test.cpp common_factor_test.cpp] is a demonstration of the results from |
---|
196 | instantiating various examples of the run-time GCD and LCM function |
---|
197 | templates and the compile-time GCD and LCM class templates. |
---|
198 | (The run-time GCD and LCM class templates are tested indirectly through |
---|
199 | the run-time function templates.) |
---|
200 | |
---|
201 | [endsect] |
---|
202 | |
---|
203 | [section Rationale] |
---|
204 | |
---|
205 | The greatest common divisor and least common multiple functions are |
---|
206 | greatly used in some numeric contexts, including some of the other |
---|
207 | Boost libraries. Centralizing these functions to one header improves |
---|
208 | code 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 | |
---|
222 | The author of the Boost compilation of GCD and LCM computations is |
---|
223 | Daryle Walker. The code was prompted by existing code hiding in the |
---|
224 | implementations of Paul Moore's rational library and Steve Cleary's |
---|
225 | pool library. The code had updates by Helmut Zeisel. |
---|
226 | |
---|
227 | [endsect] |
---|
228 | |
---|
229 | [endsect] |
---|
230 | |
---|