Skip to content

Commit

Permalink
Merge pull request #11 from NAThompson/develop
Browse files Browse the repository at this point in the history
Modular exponentiation, modular multiplicative inverse
  • Loading branch information
Lastique committed Nov 3, 2018
2 parents 099cf26 + cad4623 commit aa68e17
Show file tree
Hide file tree
Showing 20 changed files with 865 additions and 232 deletions.
74 changes: 36 additions & 38 deletions doc/gcd/math-gcd.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ programming problems.

namespace boost
{
namespace math
namespace integer
{

template < typename IntegerType >
Expand Down Expand Up @@ -53,10 +53,10 @@ programming problems.

[section GCD Function Object]

[*Header: ] [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
[*Header: ] [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]

template < typename IntegerType >
class boost::math::gcd_evaluator
class boost::integer::gcd_evaluator
{
public:
// Types
Expand All @@ -65,12 +65,12 @@ programming problems.
typedef IntegerType second_argument_type;

// Function object interface
constexpr result_type operator ()(
constexpr result_type operator ()(
first_argument_type const &a,
second_argument_type const &b ) const;
};

The boost::math::gcd_evaluator class template defines a function object
The boost::integer::gcd_evaluator class template defines a function object
class to return the greatest common divisor of two integers.
The template is parameterized by a single type, called IntegerType here.
This type should be a numeric type that represents integers.
Expand All @@ -82,17 +82,17 @@ the GCD function template. If a numeric type wants to customize evaluations
of its greatest common divisors, then the type should specialize on the
gcd_evaluator class template.

Note that these function objects are `constexpr` in C++14 and later only.
Note that these function objects are `constexpr` in C++14 and later only.
They are also declared `noexcept` when appropriate.

[endsect]

[section LCM Function Object]

[*Header: ] [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
[*Header: ] [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]

template < typename IntegerType >
class boost::math::lcm_evaluator
class boost::integer::lcm_evaluator
{
public:
// Types
Expand All @@ -101,12 +101,12 @@ They are also declared `noexcept` when appropriate.
typedef IntegerType second_argument_type;

// Function object interface
constexpr result_type operator ()(
constexpr result_type operator ()(
first_argument_type const &a,
second_argument_type const &b ) const;
};

The boost::math::lcm_evaluator class template defines a function object
The boost::integer::lcm_evaluator class template defines a function object
class to return the least common multiple of two integers. The template
is parameterized by a single type, called IntegerType here. This type
should be a numeric type that represents integers. The result of the
Expand All @@ -126,13 +126,13 @@ They are also declared `noexcept` when appropriate.

[section:run_time Run-time GCD & LCM Determination]

[*Header: ] [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
[*Header: ] [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]

template < typename IntegerType >
constexpr IntegerType boost::math::gcd( IntegerType const &a, IntegerType const &b );
constexpr IntegerType boost::integer::gcd( IntegerType const &a, IntegerType const &b );

template < typename IntegerType >
constexpr IntegerType boost::math::lcm( IntegerType const &a, IntegerType const &b );
constexpr IntegerType boost::integer::lcm( IntegerType const &a, IntegerType const &b );

template < typename IntegerType, typename... Args >
constexpr IntegerType gcd( IntegerType const &a, IntegerType const &b, Args const&... );
Expand All @@ -148,18 +148,18 @@ They are also declared `noexcept` when appropriate.
std::pair<typename std::iterator_traits<I>::value_type, I>
lcm_range(I first, I last);

The boost::math::gcd function template returns the greatest common
The boost::integer::gcd function template returns the greatest common
(nonnegative) divisor of the two integers passed to it.
`boost::math::gcd_range` is the iteration of the above gcd algorithm over a
range, returning the greatest common divisor of all the elements. The algorithm
terminates when the gcd reaches unity or the end of the range. Thus it also
returns the iterator after the last element inspected because this may not be
`boost::integer::gcd_range` is the iteration of the above gcd algorithm over a
range, returning the greatest common divisor of all the elements. The algorithm
terminates when the gcd reaches unity or the end of the range. Thus it also
returns the iterator after the last element inspected because this may not be
equal to the end of the range. The variadic version of `gcd` behaves similarly
but does not indicate which input value caused the gcd to reach unity.

The boost::math::lcm function template returns the least common
The boost::integer::lcm function template returns the least common
(nonnegative) multiple of the two integers passed to it.
As with gcd, there are range and variadic versions of the function for
As with gcd, there are range and variadic versions of the function for
more than 2 arguments.

Note that these functions are constexpr in C++14 and later only.
Expand All @@ -171,17 +171,17 @@ They are also declared `noexcept` when appropriate.

[note These functions are deprecated in favor of constexpr `gcd` and `lcm` on C++14 capable compilers.]

[*Header: ] [@../../../../boost/math/common_factor_ct.hpp <boost/math/common_factor_ct.hpp>]
[*Header: ] [@../../../../boost/integer/common_factor_ct.hpp <boost/integer/common_factor_ct.hpp>]

typedef ``['unspecified]`` static_gcd_type;

template < static_gcd_type Value1, static_gcd_type Value2 >
struct boost::math::static_gcd : public mpl::integral_c<static_gcd_type, implementation_defined>
struct boost::integer::static_gcd : public mpl::integral_c<static_gcd_type, implementation_defined>
{
};

template < static_gcd_type Value1, static_gcd_type Value2 >
struct boost::math::static_lcm : public mpl::integral_c<static_gcd_type, implementation_defined>
struct boost::integer::static_lcm : public mpl::integral_c<static_gcd_type, implementation_defined>
{
};

Expand All @@ -190,7 +190,7 @@ for use in integral-constant-expressions by the compiler. Usually this
the same type as `boost::uintmax_t`, but may fall back to being `unsigned long`
for some older compilers.

The boost::math::static_gcd and boost::math::static_lcm class templates
The boost::integer::static_gcd and boost::integer::static_lcm class templates
take two value-based template parameters of the ['static_gcd_type] type
and inherit from the type `boost::mpl::integral_c`.
Inherited from the base class, they have a member /value/
Expand All @@ -201,7 +201,7 @@ is beyond the range of `static_gcd_type`.

[h3 Example]

#include <boost/math/common_factor.hpp>
#include <boost/integer/common_factor.hpp>
#include <algorithm>
#include <iterator>
#include <iostream>
Expand All @@ -212,28 +212,28 @@ is beyond the range of `static_gcd_type`.
using std::endl;

cout << "The GCD and LCM of 6 and 15 are "
<< boost::math::gcd(6, 15) << " and "
<< boost::math::lcm(6, 15) << ", respectively."
<< boost::integer::gcd(6, 15) << " and "
<< boost::integer::lcm(6, 15) << ", respectively."
<< endl;

cout << "The GCD and LCM of 8 and 9 are "
<< boost::math::static_gcd<8, 9>::value
<< boost::integer::static_gcd<8, 9>::value
<< " and "
<< boost::math::static_lcm<8, 9>::value
<< boost::integer::static_lcm<8, 9>::value
<< ", respectively." << endl;

int a[] = { 4, 5, 6 }, b[] = { 7, 8, 9 }, c[3];
std::transform( a, a + 3, b, c, boost::math::gcd_evaluator<int>() );
std::transform( a, a + 3, b, c, boost::integer::gcd_evaluator<int>() );
std::copy( c, c + 3, std::ostream_iterator<int>(cout, " ") );
}

[endsect]

[section:gcd_header Header <boost/math/common_factor.hpp>]
[section:gcd_header Header <boost/integer/common_factor.hpp>]

This header simply includes the headers
[@../../../../boost/math/common_factor_ct.hpp <boost/math/common_factor_ct.hpp>]
and [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>].
[@../../../../boost/integer/common_factor_ct.hpp <boost/integer/common_factor_ct.hpp>]
and [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>].

Note this is a legacy header: it used to contain the actual implementation,
but the compile-time and run-time facilities
Expand All @@ -243,7 +243,7 @@ were moved to separate headers (since they were independent of each other).

[section:demo Demonstration Program]

The program [@../../../../libs/math/test/common_factor_test.cpp common_factor_test.cpp] is a demonstration of the results from
The program [@../../../../libs/integer/test/common_factor_test.cpp common_factor_test.cpp] is a demonstration of the results from
instantiating various examples of the run-time GCD and LCM function
templates and the compile-time GCD and LCM class templates.
(The run-time GCD and LCM class templates are tested indirectly through
Expand All @@ -256,13 +256,13 @@ the run-time function templates.)
The greatest common divisor and least common multiple functions are
greatly used in some numeric contexts, including some of the other
Boost libraries. Centralizing these functions to one header improves
code factoring and eases maintainence.
code factoring and eases maintenance.

[endsect]

[section:gcd_history History]

* 24th April 2017 Moved to Jeremy Murphy's improved algorithms, added constexpr and noexcept support,
* 24th April 2017 Moved to Jeremy Murphy's improved algorithms, added constexpr and noexcept support,
added compiler intrinsic support, added variadic and range based versions of the algorithms.
* 13 May 2013 Moved into main Boost.Math Quickbook documentation.
* 17 Dec 2005: Converted documentation to Quickbook Format.
Expand All @@ -288,5 +288,3 @@ Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt).
]


2 changes: 1 addition & 1 deletion doc/html/boost_integer/cstdint.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Removed from library: Standard Integer Types</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.77.1">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Integer">
<link rel="up" href="../index.html" title="Boost.Integer">
<link rel="prev" href="history.html" title="History">
Expand Down
109 changes: 109 additions & 0 deletions doc/html/boost_integer/extended_euclidean.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Extended Euclidean Algorithm</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Integer">
<link rel="up" href="../index.html" title="Boost.Integer">
<link rel="prev" href="gcd_lcm.html" title="Greatest Common Divisor and Least Common Multiple">
<link rel="next" href="mod_inverse.html" title="Modular Multiplicative Inverse">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="gcd_lcm.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="mod_inverse.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_integer.extended_euclidean"></a><a class="link" href="extended_euclidean.html" title="Extended Euclidean Algorithm">Extended Euclidean Algorithm</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="extended_euclidean.html#boost_integer.extended_euclidean.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="extended_euclidean.html#boost_integer.extended_euclidean.synopsis">Synopsis</a></span></dt>
<dt><span class="section"><a href="extended_euclidean.html#boost_integer.extended_euclidean.usage">Usage</a></span></dt>
<dt><span class="section"><a href="extended_euclidean.html#boost_integer.extended_euclidean.references">References</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_integer.extended_euclidean.introduction"></a><a class="link" href="extended_euclidean.html#boost_integer.extended_euclidean.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
The extended Euclidean algorithm solves the integer relation <span class="emphasis"><em>mx
+ ny</em></span> = gcd(<span class="emphasis"><em>m</em></span>, <span class="emphasis"><em>n</em></span>) for
<span class="emphasis"><em>x</em></span> and <span class="emphasis"><em>y</em></span>.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_integer.extended_euclidean.synopsis"></a><a class="link" href="extended_euclidean.html#boost_integer.extended_euclidean.synopsis" title="Synopsis">Synopsis</a>
</h3></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">integer</span><span class="special">/</span><span class="identifier">extended_euclidean</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">integer</span> <span class="special">{</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Z</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">euclidean_result_t</span> <span class="special">{</span>
<span class="identifier">Z</span> <span class="identifier">gcd</span><span class="special">;</span>
<span class="identifier">Z</span> <span class="identifier">x</span><span class="special">;</span>
<span class="identifier">Z</span> <span class="identifier">y</span><span class="special">;</span>
<span class="special">};</span>


<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Z</span><span class="special">&gt;</span>
<span class="identifier">euclidean_result_t</span><span class="special">&lt;</span><span class="identifier">Z</span><span class="special">&gt;</span> <span class="identifier">extended_euclidean</span><span class="special">(</span><span class="identifier">Z</span> <span class="identifier">m</span><span class="special">,</span> <span class="identifier">Z</span> <span class="identifier">n</span><span class="special">);</span>

<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_integer.extended_euclidean.usage"></a><a class="link" href="extended_euclidean.html#boost_integer.extended_euclidean.usage" title="Usage">Usage</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">m</span> <span class="special">=</span> <span class="number">12</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">n</span> <span class="special">=</span> <span class="number">15</span><span class="special">;</span>
<span class="keyword">auto</span> <span class="identifier">res</span> <span class="special">=</span> <span class="identifier">extended_euclidean</span><span class="special">(</span><span class="identifier">m</span><span class="special">,</span> <span class="identifier">n</span><span class="special">);</span>

<span class="keyword">int</span> <span class="identifier">gcd</span> <span class="special">=</span> <span class="identifier">res</span><span class="special">.</span><span class="identifier">gcd</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">res</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">res</span><span class="special">.</span><span class="identifier">y</span><span class="special">;</span>
<span class="comment">// mx + ny = gcd(m,n) should now hold</span>
</pre>
<p>
Unlike most of the library, the extended Euclidean algorithm requires C++11
features.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_integer.extended_euclidean.references"></a><a class="link" href="extended_euclidean.html#boost_integer.extended_euclidean.references" title="References">References</a>
</h3></div></div></div>
<p>
Wagstaff, Samuel S., <span class="emphasis"><em>The Joy of Factoring</em></span>, Vol. 68.
American Mathematical Soc., 2013.
</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2001-2009 Beman
Dawes, Daryle Walker, Gennaro Prota, John Maddock<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="gcd_lcm.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="mod_inverse.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
Loading

0 comments on commit aa68e17

Please sign in to comment.