Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
Lattices/RationalMatrix.cpp
Go to the documentation of this file.
1/* This file is part of PIL, the Particle Interaction Library.
2 *
3 *
4 * MODEL is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_RationalMatrix_cpp_
9#define model_RationalMatrix_cpp_
10
11#include <RationalMatrix.h>
12
13namespace model
14{
15 /**********************************************************************/
16 template <int dim>
17 typename RationalMatrix<dim>::IntValueType RationalMatrix<dim>::gcd(const IntValueType &a, const IntValueType &b)
18 {
19 return b > 0 ? gcd(b, a % b) : a;
20 }
21
22 /**********************************************************************/
23 template <int dim>
24 typename RationalMatrix<dim>::IntValueType RationalMatrix<dim>::lcm(const IntValueType &a, const IntValueType &b)
25 {
26 return a * b / gcd(a, b);
27 }
28
29 /**********************************************************************/
30 template <int dim>
31 std::pair<typename RationalMatrix<dim>::MatrixInt, typename RationalMatrix<dim>::IntValueType> RationalMatrix<dim>::compute(const MatrixDimD &R)
32 {
33
34 // Find the BestRationalApproximation of each entry
35 MatrixDimI nums(MatrixDimI::Zero());
36 MatrixDimI dens(MatrixDimI::Ones());
37
38 IntValueType sigma = 1;
39 for (int i = 0; i < dim; ++i)
40 {
41 for (int j = 0; j < dim; ++j)
42 {
43 BestRationalApproximation bra(R(i, j), maxDen);
44 nums(i, j) = bra.num;
45 dens(i, j) = bra.den;
46 sigma = lcm(sigma, bra.den);
47 }
48 }
49
50 MatrixInt im(MatrixInt::Zero());
51 for (int i = 0; i < dim; ++i)
52 {
53 for (int j = 0; j < dim; ++j)
54 {
55 im(i, j) = nums(i, j) * sigma / dens(i, j);
56 }
57 }
58
59 const double error = (im.template cast<double>() / sigma - R).norm() / (dim * dim);
60 if (error > 100.0 * DBL_EPSILON)
61 {
62 std::cout << "error=" << error << std::endl;
63 std::cout << "maxDen=" << maxDen << std::endl;
64 std::cout << "im=\n"
65 << std::setprecision(15) << std::scientific << im.template cast<double>() / sigma << std::endl;
66 std::cout << "= 1/" << sigma << "*\n"
67 << std::setprecision(15) << std::scientific << im << std::endl;
68 std::cout << "R=\n"
69 << std::setprecision(15) << std::scientific << R << std::endl;
70 assert(false && "Rational Matrix failed, check maxDen.");
71 }
72
73 return std::make_pair(im, sigma);
74 }
75
76 /**********************************************************************/
77 template <int dim>
78 RationalMatrix<dim>::RationalMatrix(const MatrixDimD &R) : /* init */ returnPair(compute(R)),
79 /* init */ integerMatrix(returnPair.first),
80 /* init */ sigma(returnPair.second)
81 {
82 }
83
84 template class RationalMatrix<3>;
85
86} // end namespace
87#endif
RationalMatrix(const MatrixDimD &R)
a
Definition int_R.m:14
b
Definition int_R.m:16
int j
int i
vertex first