Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
Lattices_NEW/RationalMatrix.cpp
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_RationalMatrix_cpp_
8#define model_RationalMatrix_cpp_
9
10#include <iomanip>
11#include <RationalMatrix.h>
12
13namespace model
14{
15
16 /**********************************************************************/
17 template <int dim>
18 std::pair<typename RationalMatrix<dim>::MatrixDimI, typename RationalMatrix<dim>::IntScalarType> RationalMatrix<dim>::compute(const MatrixDimD &R)
19 {
20
21 // Find the BestRationalApproximation of each entry
22 MatrixDimI nums(MatrixDimI::Zero());
23 MatrixDimI dens(MatrixDimI::Ones());
24
26 for (int i = 0; i < dim; ++i)
27 {
28 for (int j = 0; j < dim; ++j)
29 {
30 BestRationalApproximation bra(R(i, j), maxDen);
31 nums(i, j) = bra.num;
32 dens(i, j) = bra.den;
33 sigma = IntegerMath<IntScalarType>::lcm(sigma, bra.den);
34 }
35 }
36
37 MatrixDimI im(MatrixDimI::Zero());
38 for (int i = 0; i < dim; ++i)
39 {
40 for (int j = 0; j < dim; ++j)
41 {
42 im(i, j) = nums(i, j) * sigma / dens(i, j);
43 }
44 }
45
46 const double error = (im.template cast<double>() / sigma - R).norm() / (dim * dim);
47 if (error > 100.0 * DBL_EPSILON)
48 {
49 std::cout << "error=" << error << std::endl;
50 std::cout << "maxDen=" << maxDen << std::endl;
51 std::cout << "im=\n"
52 << std::setprecision(15) << std::scientific << im.template cast<double>() / sigma << std::endl;
53 std::cout << "= 1/" << sigma << "*\n"
54 << std::setprecision(15) << std::scientific << im << std::endl;
55 std::cout << "R=\n"
56 << std::setprecision(15) << std::scientific << R << std::endl;
57 throw std::runtime_error("Rational Matrix failed, check maxDen");
58 }
59
60 return std::make_pair(im, sigma);
61 }
62
63 /**********************************************************************/
64 template <int dim>
66 /* init */ returnPair(compute(R)),
67 /* init */ integerMatrix(returnPair.first),
68 /* init */ mu(returnPair.second)
69 {
70 }
71
72 template <int dim>
73 RationalMatrix<dim>::RationalMatrix(const MatrixDimI& Rn,const IntScalarType& Rd) :
74 /* init */ returnPair(std::make_pair(Rn, Rd)),
75 /* init */ integerMatrix(returnPair.first),
76 /* init */ mu(returnPair.second)
77 {
78 }
79
80 template <int dim>
81 typename RationalMatrix<dim>::MatrixDimD RationalMatrix<dim>::asMatrix() const
82 {
83 return integerMatrix.template cast<double>()/mu;
84 }
85
86
87
88 template class RationalMatrix<1>;
89 template class RationalMatrix<2>;
90 template class RationalMatrix<3>;
91
92} // end namespace
93#endif
RationalMatrix(const MatrixDimD &R)
static IntScalarType lcm(const IntScalarType &a, const IntScalarType &b)
Definition IntegerMath.h:68
const double mu
int j
int i
vertex first