Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
DiophantineSolver.cpp
Go to the documentation of this file.
1/* This file is part of MoDELib, distributed under GNU GPL v2. */
2#include <DiophantineSolver.h>
3#include <IntegerMath.h>
4#include <climits>
5
6namespace model
7{
8
9 auto DiophantineSolver::extended_gcd(long int a, long int b, long int &x, long int &y) -> long int
10 {
11 if (b == 0)
12 {
13 x = 1;
14 y = 0;
15 return a;
16 }
17 long int x1, y1;
18 long int g = extended_gcd(b, a % b, x1, y1);
19 x = y1;
20 y = x1 - (a / b) * y1;
21 return g;
22 }
23
24 auto DiophantineSolver::solveDiophantine2vars(long int a, long int b, long int c, long int &x, long int &y) -> void
25 {
26
27 long int g = extended_gcd(a, b, x, y);
28
29 if (c % g != 0)
30 {
31 puts("Impossible");
32 exit(0);
33 }
34
35 c /= g;
36 // long int temp;
37 // if (a<b)
38 // {
39 // temp=0;
40 // }
41 // else
42 // {
43 // temp=LLONG_MAX;
44 // }
45 // x = x * c + temp*b/g;
46 // y = y * c - temp*a/g;
47
48 x = x * c;
49 y = y * c;
50 }
51
52 auto DiophantineSolver::solveDiophantine3vars(const Eigen::Matrix<long int, 3, 1> &alphas, const long int t1,
53 Eigen::Matrix<long int, 3, 1> &sols) -> void
54 {
55 // const auto gcd(LatticeGCD<dim>::gcd(key.reciprocalDirectionComponents().transpose()*N));
56 // const long int gcd(LatticeGCD<3>::gcd(alphas));
57 const long int gcd(IntegerMath<long int>::gcd(alphas));
58
59 if (t1 % gcd != 0)
60 {
61 assert(0 && "Integral solution for shifts does not exist");
62 }
63
64 const long int a(alphas(0) / gcd);
65 const long int b(alphas(1) / gcd);
66 const long int c(alphas(2) / gcd);
67 const long int t(t1 / gcd);
68
69 // const long int gcdab(LatticeGCD<3>::gcd(a, b));
70 const long int gcdab(IntegerMath<long int>::gcd(a, b));
71
72 const long int ap(a / gcdab);
73 const long int bp(b / gcdab);
74 long int u0, v0;
75 solveDiophantine2vars(ap, bp, c, u0, v0);
76 long int z0, t0;
77 solveDiophantine2vars(c, gcdab, t, z0, t0);
78 long int x0, y0;
79 solveDiophantine2vars(ap, bp, t0, x0, y0);
80
81 // k and m are arbitrary variables
82 const long int k = 0;
83 const long int m = 0;
84
85 const long int x(x0 + bp * k - u0 * m);
86 const long int y(y0 - ap * k + v0 * m);
87 const long int z(z0 + gcdab * m);
88
89 sols << x, y, z;
90 }
91} // namespace model
static void solveDiophantine2vars(long int a, long int b, long int c, long int &x, long int &y)
static void solveDiophantine3vars(const Eigen::Matrix< long int, 3, 1 > &alphas, const long int t1, Eigen::Matrix< long int, 3, 1 > &sols)
static long int extended_gcd(long int a, long int b, long int &x, long int &y)
id z()
final point of the source segment t
c
Definition int_R.m:15
a
Definition int_R.m:14
b
Definition int_R.m:16
x
Definition int_R.m:12
static IntScalarType gcd(const IntScalarType &a, const IntScalarType &b)
Definition IntegerMath.h:22
REAL t0
INEXACT REAL t1
int k
void t struct mesh * m