Vaucanson 1.4
algebra.hxx
00001 // q_number.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2011 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 
00018 #ifndef INCLUDE_VAUCANSON_MISC_ALGEBRA_HXX
00019 # define INCLUDE_VAUCANSON_MISC_ALGEBRA_HXX
00020 
00021 # include <vaucanson/misc/algebra.hh>
00022 
00023 namespace vcsn {
00024 
00025   namespace misc {
00026 
00027 
00028     template<typename T>
00029     inline
00030     T
00031     gcd (T a, T b)
00032     {
00033       if (0 == a)
00034                 return b;
00035         T s=1;
00036         if(b < 0){
00037                 s=-1;
00038                 b=-b;
00039         }
00040         if(a<0)
00041           a=-a;
00042       T r;
00043       while (0 != b)
00044         {
00045           r = a % b;
00046           a = b;
00047           b = r;
00048         }
00049       return a*s;
00050     }
00051 
00052     template<typename T>
00053     inline
00054         T
00055     lcm (T a, T b)
00056     {
00057       T res = gcd (a, b);
00058       if (res)
00059             return a /res * b;
00060       return T(0);
00061     }
00062 
00063     template<typename T>
00064     inline
00065     bool
00066     is_coprime (T a, T b)
00067     {
00068       T g=gcd (a, b);
00069       return 1 == g || -1 == g;
00070     }
00071 
00072     template<typename T>
00073     inline
00074     T abs (T a)
00075     {
00076       return a > 0 ? a : -a;
00077     }
00078   } // !misc
00079 
00080 } // !vcsn
00081 #endif // !INCLUDE_VAUCANSON_MISC_ALGEBRA_HXX