carl  24.04
Computer ARithmetic Library
EEA.h
Go to the documentation of this file.
1 /**
2  * @file: EEA.h
3  * @author: Sebastian Junges
4  *
5  * @since October 21, 2013
6  */
7 
8 #pragma once
9 
10 #include <cassert>
11 #include <utility>
12 
13 namespace carl {
14 /**
15  * Extended euclidean algorithm for numbers.
16  */
17 template<typename IntegerType>
18 struct EEA {
19 
20  static std::pair<IntegerType, IntegerType> calculate(const IntegerType& a, const IntegerType& b) {
21  IntegerType s;
22  IntegerType t;
23  if (a >= 0 && b >= 0) {
24  calculate_recursive(a, b, s, t);
25  } else if (a < 0 && b >= 0) {
26  calculate_recursive(-a, b, s, t);
27  } else if (a >= 0 && b < 0) {
28  calculate_recursive(a, -b, s, t);
29  } else {
30  calculate_recursive(-a, -b, s, t);
31  }
32 
33  if (a < 0) {
34  s = -s;
35  }
36  if (b < 0) {
37  t = -t;
38  }
39  return std::make_pair(s, t);
40  }
41  /// \todo a iterative implementation might be faster
42  static void calculate_recursive(const IntegerType& a, const IntegerType& b, IntegerType& s, IntegerType& t) {
43  assert(a >= 0 && b >= 0);
44  if (b == 0) {
45  s = 1;
46  t = 0;
47  } else {
48  IntegerType quotient(0);
49  IntegerType remainder(0);
50  IntegerType tmp(0);
51  // quotient, remainder = divide(a,b)
52  divide(a, b, quotient, remainder);
53  calculate_recursive(b, remainder, s, tmp);
54  t = s - quotient * tmp;
55  s = tmp;
56  }
57  }
58 };
59 } // namespace carl
carl is the main namespace for the library.
Interval< Number > quotient(const Interval< Number > &_lhs, const Interval< Number > &_rhs)
Implements the division with remainder.
Definition: Interval.h:1488
void divide(const cln::cl_I &dividend, const cln::cl_I &divisor, cln::cl_I &quotient, cln::cl_I &remainder)
Definition: operations.h:326
cln::cl_I remainder(const cln::cl_I &a, const cln::cl_I &b)
Calculate the remainder of the integer division.
Definition: operations.h:526
Extended euclidean algorithm for numbers.
Definition: EEA.h:18
static std::pair< IntegerType, IntegerType > calculate(const IntegerType &a, const IntegerType &b)
Definition: EEA.h:20
static void calculate_recursive(const IntegerType &a, const IntegerType &b, IntegerType &s, IntegerType &t)
Definition: EEA.h:42