Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/math/doc/common_factor.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 9.9 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
2<html>
3<head>
4<title>Boost GCD &amp; LCM Library</title>
5</head>
6
7<body bgcolor="white" text="black" link="blue" alink="red" vlink="purple">
8<h1>
9<img src="../../../boost.png" alt="boost.png (6897 bytes)"
10align="left" width="277" height="86">Greatest Common Divisor<br>
11&nbsp;Least
12Common Multiple<br>
13&nbsp;</h1>
14
15<p>The class and function templates in <cite><a
16href="../../../boost/math/common_factor.hpp">&lt;boost/math/common_factor.hpp&gt;</a>
17</cite> provide run-time and compile-time evaluation of the greatest
18common divisor (GCD) or least common multiple (LCM) of two integers.
19These facilities are useful for many numeric-oriented generic
20programming problems.</p>
21
22<h2><a name="contents">Contents</a></h2>
23
24<ul>
25        <li><a href="#contents">Contents</a></li>
26        <li>Header <cite><a href="#cf_hpp">&lt;boost/math/common_factor.hpp&gt;</a></cite>
27        <ul>
28        <li><a href="#synopsis">Synopsis</a></li>
29        </ul></li>
30        <li>Header <cite><a href="#cfrt_hpp">&lt;boost/math/common_factor_rt.hpp&gt;</a></cite>
31        <ul>
32        <li><a href="#gcd_obj">GCD Function Object</a></li>
33        <li><a href="#lcm_obj">LCM Function Object</a></li>
34                <li><a href="#run_gcd_lcm">Run-time GCD &amp; LCM Determination</a></li>
35        </ul></li>
36        <li>Header <cite><a href="#cfct_hpp">&lt;boost/math/common_factor_ct.hpp&gt;</a></cite></li>
37        <li><a href="#example">Example</a></li>
38        <li><a href="#demo">Demonstration Program</a></li>
39        <li><a href="#rationale">Rationale</a></li>
40        <li><a href="#history">History</a></li>
41        <li><a href="#credits">Credits</a></li>
42</ul>
43
44<h2>Header <cite><a name="cf_hpp" href="../../../boost/math/common_factor.hpp">&lt;boost/math/common_factor.hpp&gt;</a></cite></h2>
45
46<p>This header simply includes the headers <cite><a
47href="../../../boost/math/common_factor_ct.hpp">&lt;boost/math/common_factor_ct.hpp&gt;</a></cite>
48and <cite><a
49href="../../../boost/math/common_factor_rt.hpp">&lt;boost/math/common_factor_rt.hpp&gt;</a></cite>.
50It used to contain the code, but the compile-time and run-time
51facilities were moved to separate headers (since they were independent),
52and this header maintains compatibility.</p>
53
54<h3><a name="synopsis">Synopsis</a></h3>
55
56<blockquote><pre>namespace boost
57{
58namespace math
59{
60
61template &lt; typename IntegerType &gt;
62    class gcd_evaluator;
63template &lt; typename IntegerType &gt;
64    class lcm_evaluator;
65
66template &lt; typename IntegerType &gt;
67    IntegerType  gcd( IntegerType const &amp;a, IntegerType const &amp;b );
68template &lt; typename IntegerType &gt;
69    IntegerType  lcm( IntegerType const &amp;a, IntegerType const &amp;b );
70
71template &lt; unsigned long Value1, unsigned long Value2 &gt;
72    struct static_gcd;
73template &lt; unsigned long Value1, unsigned long Value2 &gt;
74    struct static_lcm;
75
76}
77}
78</pre></blockquote>
79
80<h2>Header <cite><a name="cfrt_hpp" href="../../../boost/math/common_factor_rt.hpp">&lt;boost/math/common_factor_rt.hpp&gt;</a></cite></h2>
81
82<h3><a name="gcd_obj">GCD Function Object</a></h3>
83
84<blockquote><pre>template &lt; typename IntegerType &gt;
85class boost::math::gcd_evaluator
86{
87public:
88    // Types
89    typedef IntegerType  result_type;
90    typedef IntegerType  first_argument_type;
91    typedef IntegerType  second_argument_type;
92
93    // Function object interface
94    result_type  operator ()( first_argument_type const &amp;a,
95     second_argument_type const &amp;b ) const;
96};
97</pre></blockquote>
98
99<p>The <code>boost::math::gcd_evaluator</code> class template defines a
100function object class to return the greatest common divisor of two
101integers.  The template is parameterized by a single type, called
102<code>IntegerType</code> here.  This type should be a numeric type that
103represents integers.  The result of the function object is always
104nonnegative, even if either of the operator arguments is negative.</p>
105
106<p>This function object class template is used in the corresponding
107version of the <a href="#run_gcd_lcm">GCD function template</a>.  If a
108numeric type wants to customize evaluations of its greatest common
109divisors, then the type should specialize on the
110<code>gcd_evaluator</code> class template.</p>
111
112<h3><a name="lcm_obj">LCM Function Object</a></h3>
113
114<blockquote><pre>template &lt; typename IntegerType &gt;
115class boost::math::lcm_evaluator
116{
117public:
118    // Types
119    typedef IntegerType  result_type;
120    typedef IntegerType  first_argument_type;
121    typedef IntegerType  second_argument_type;
122
123    // Function object interface
124    result_type  operator ()( first_argument_type const &amp;a,
125     second_argument_type const &amp;b ) const;
126};
127</pre></blockquote>
128
129<p>The <code>boost::math::lcm_evaluator</code> class template defines a
130function object class to return the least common multiple of two
131integers.  The template is parameterized by a single type, called
132<code>IntegerType</code> here.  This type should be a numeric type that
133represents integers.  The result of the function object is always
134nonnegative, even if either of the operator arguments is negative.  If
135the least common multiple is beyond the range of the integer type, the
136results are undefined.</p>
137
138<p>This function object class template is used in the corresponding
139version of the <a href="#run_gcd_lcm">LCM function template</a>.  If a
140numeric type wants to customize evaluations of its least common
141multiples, then the type should specialize on the
142<code>lcm_evaluator</code> class template.</p>
143
144<h3><a name="run_gcd_lcm">Run-time GCD &amp; LCM Determination</a></h3>
145
146<blockquote><pre>template &lt; typename IntegerType &gt;
147IntegerType  boost::math::gcd( IntegerType const &amp;a, IntegerType const &amp;b );
148
149template &lt; typename IntegerType &gt;
150IntegerType  boost::math::lcm( IntegerType const &amp;a, IntegerType const &amp;b );
151</pre></blockquote>
152
153<p>The <code>boost::math::gcd</code> function template returns the
154greatest common (nonnegative) divisor of the two integers passed to it.
155The <code>boost::math::lcm</code> function template returns the least
156common (nonnegative) multiple of the two integers passed to it.  The
157function templates are parameterized on the function arguments'
158<var>IntegerType</var>, which is also the return type.  Internally,
159these function templates use an object of the corresponding version of
160the <code><a href="#gcd_obj">gcd_evaluator</a></code> and <code><a
161href="#lcm_obj">lcm_evaluator</a></code> class templates,
162respectively.</p>
163
164<h2>Header <cite><a name="cfct_hpp" href="../../../boost/math/common_factor_ct.hpp">&lt;boost/math/common_factor_ct.hpp&gt;</a></cite></h2>
165
166<blockquote><pre>template &lt; unsigned long Value1, unsigned long Value2 &gt;
167struct boost::math::static_gcd
168{
169    static unsigned long const  value = <em>implementation_defined</em>;
170};
171
172template &lt; unsigned long Value1, unsigned long Value2 &gt;
173struct boost::math::static_lcm
174{
175    static unsigned long const  value = <em>implementation_defined</em>;
176};
177</pre></blockquote>
178
179<p>The <code>boost::math::static_gcd</code> and
180<code>boost::math::static_lcm</code> class templates take two
181value-based template parameters of the <code>unsigned long</code> type
182and have a single static constant data member, <code>value</code>, of
183that same type.  The value of that member is the greatest common factor
184or least common multiple, respectively, of the template arguments.  A
185compile-time error will occur if the least common multiple is beyond the
186range of an <code>unsigned long</code>.</p>
187
188<h2><a name="example">Example</a></h2>
189
190<blockquote><pre>#include &lt;boost/math/common_factor.hpp&gt;
191#include &lt;algorithm&gt;
192#include &lt;iterator&gt;
193
194
195int main()
196{
197    using std::cout;
198    using std::endl;
199
200    cout &lt;&lt; &quot;The GCD and LCM of 6 and 15 are &quot;
201     &lt;&lt; boost::math::gcd(6, 15) &lt;&lt; &quot; and &quot;
202     &lt;&lt; boost::math::lcm(6, 15) &lt;&lt; &quot;, respectively.&quot;
203     &lt;&lt; endl;
204
205    cout &lt;&lt; &quot;The GCD and LCM of 8 and 9 are &quot;
206     &lt;&lt; boost::math::static_gcd&lt;8, 9&gt;::value
207     &lt;&lt; &quot; and &quot;
208     &lt;&lt; boost::math::static_lcm&lt;8, 9&gt;::value
209     &lt;&lt; &quot;, respectively.&quot; &lt;&lt; endl;
210
211    int  a[] = { 4, 5, 6 }, b[] = { 7, 8, 9 }, c[3];
212    std::transform( a, a + 3, b, c, boost::math::gcd_evaluator&lt;int&gt;() );
213    std::copy( c, c + 3, std::ostream_iterator&lt;int&gt;(cout, &quot; &quot;) );
214}
215</pre></blockquote>
216
217<h2><a name="demo">Demonstration Program</a></h2>
218
219<p>The program <a
220href="../test/common_factor_test.cpp">common_factor_test.cpp</a> is a
221demonstration of the results from instantiating various examples of the
222run-time GCD and LCM function templates and the compile-time GCD and
223LCM class templates.  (The run-time GCD and LCM class templates are
224tested indirectly through the run-time function templates.)</p>
225
226<h2><a name="rationale">Rationale</a></h2>
227
228<p>The greatest common divisor and least common multiple functions are
229greatly used in some numeric contexts, including some of the other Boost
230libraries.  Centralizing these functions to one header improves code
231factoring and eases maintainence.</p>
232
233<h2><a name="history">History</a></h2>
234
235<dl>
236        <dt>2 Jul 2002
237        <dd>Compile-time and run-time items separated to new headers.
238
239        <dt>7 Nov 2001
240        <dd>Initial version
241</dl>
242
243<h2><a name="credits">Credits</a></h2>
244
245<p>The author of the Boost compilation of GCD and LCM computations is <a
246href="../../../people/daryle_walker.html">Daryle Walker</a>.  The code was
247prompted by existing code hiding in the implementations of <a
248href="../../../people/paul_moore.htm">Paul Moore</a>'s <a
249href="../../rational/index.html">rational</a> library and Steve Cleary's <a
250href="../../pool/doc/index.html">pool</a> library.  The code had updates by
251Helmut Zeisel.</p>
252
253<hr>
254
255<p>Revised July 2, 2002</p>
256
257<p>&copy; Copyright Daryle Walker 2001-2002.  Permission to copy, use,
258modify, sell and distribute this document is granted provided this
259copyright notice appears in all copies.  This document is provided
260&quot;as is&quot; without express or implied warranty, and with no claim
261as to its suitability for any purpose.</p>
262</body>
263</html>
Note: See TracBrowser for help on using the repository browser.