Vaucanson  1.4.1
algebra.hxx
1 // q_number.hh: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2011 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 
18 #ifndef INCLUDE_VAUCANSON_MISC_ALGEBRA_HXX
19 # define INCLUDE_VAUCANSON_MISC_ALGEBRA_HXX
20 
21 # include <vaucanson/misc/algebra.hh>
22 
23 namespace vcsn {
24 
25  namespace misc {
26 
27 
28  template<typename T>
29  inline
30  T
31  gcd (T a, T b)
32  {
33  if (0 == a)
34  return b;
35  T s=1;
36  if(b < 0){
37  s=-1;
38  b=-b;
39  }
40  if(a<0)
41  a=-a;
42  T r;
43  while (0 != b)
44  {
45  r = a % b;
46  a = b;
47  b = r;
48  }
49  return a*s;
50  }
51 
52  template<typename T>
53  inline
54  T
55  lcm (T a, T b)
56  {
57  T res = gcd (a, b);
58  if (res)
59  return a /res * b;
60  return T(0);
61  }
62 
63  template<typename T>
64  inline
65  bool
66  is_coprime (T a, T b)
67  {
68  T g=gcd (a, b);
69  return 1 == g || -1 == g;
70  }
71 
72  template<typename T>
73  inline
74  T abs (T a)
75  {
76  return a > 0 ? a : -a;
77  }
78  } // !misc
79 
80 } // !vcsn
81 #endif // !INCLUDE_VAUCANSON_MISC_ALGEBRA_HXX