Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
include
Math
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
11
namespace
model
12
{
13
template
<
typename
IntScalarType>
14
struct
IntegerMath
15
{
16
17
static
IntScalarType
sgn
(
const
IntScalarType
&
a
)
18
{
19
return
a
<0? -1 : 1;
20
}
21
22
static
IntScalarType
gcd
(
const
IntScalarType
&
a
,
const
IntScalarType
&
b
)
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
{
57
IntScalarType
temp
(
a
[0]);
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
68
static
IntScalarType
lcm
(
const
IntScalarType
&
a
,
const
IntScalarType
&
b
)
69
{
70
return
a
*
b
/
gcd
(
a
,
b
);
71
}
72
73
};
74
}
75
#endif
a
a
Definition
int_R.m:14
b
b
Definition
int_R.m:16
model
Definition
DD_converter.dox:2
model::IntegerMath
Definition
IntegerMath.h:15
model::IntegerMath::gcd
static IntScalarType gcd(const ArrayType &a)
Definition
IntegerMath.h:30
model::IntegerMath::gcd
static IntScalarType gcd(const IntScalarType &a, const IntScalarType &b)
Definition
IntegerMath.h:22
model::IntegerMath::lcm
static IntScalarType lcm(const IntScalarType &a, const IntScalarType &b)
Definition
IntegerMath.h:68
model::IntegerMath::sgn
static IntScalarType sgn(const IntScalarType &a)
Definition
IntegerMath.h:17
model::IntScalarType
k
int k
Definition
triangle.cpp:3300
temp
vertex temp
Definition
triangle.cpp:9214
Generated by
1.15.0