Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
IntegerMath.h
Go to the documentation of this file.
1/* This file is part of model.
2 *
3 * MoDELib is distributed without any warranty under the MIT License.
4 */
5
6
7#ifndef model_IntegerMath_h_
8#define model_IntegerMath_h_
9
10
11namespace model
12{
13 template <typename IntScalarType>
15 {
16
18 {
19 return a<0? -1 : 1;
20 }
21
23 {
24 const IntScalarType absA(abs(a));
25 const IntScalarType absB(abs(b));
26 return absB > 0 ? gcd(absB, absA % absB) : (absA > 0 ? absA : 1);
27 }
28
29 template<typename ArrayType>
30 static IntScalarType gcd(const ArrayType& a)
31 {
32
33 const size_t aSize(a.size());
34 switch (aSize)
35 {
36 case 0:
37 {
38 throw std::runtime_error("gcd: array size is zero\n");
39 return 0;
40 break;
41 }
42
43 case 1:
44 {
45 return a[0];
46 break;
47 }
48
49 case 2:
50 {
51 return gcd(a[0],a[1]);
52 break;
53 }
54
55 default:
56 {
58 for(size_t k=1;k<aSize;++k)
59 {
60 temp=gcd(temp,a[k]);
61 }
62 return temp;
63 break;
64 }
65 }
66 }
67
69 {
70 return a * b / gcd(a, b);
71 }
72
73 };
74}
75#endif
a
Definition int_R.m:14
b
Definition int_R.m:16
static IntScalarType gcd(const ArrayType &a)
Definition IntegerMath.h:30
static IntScalarType gcd(const IntScalarType &a, const IntScalarType &b)
Definition IntegerMath.h:22
static IntScalarType lcm(const IntScalarType &a, const IntScalarType &b)
Definition IntegerMath.h:68
static IntScalarType sgn(const IntScalarType &a)
Definition IntegerMath.h:17
int k
vertex temp