oILAB
Loading...
Searching...
No Matches
LatticeCore.cpp
Go to the documentation of this file.
1/* This file is part of gbLAB.
2 *
3 * gbLAB is distributed without any warranty under the MIT License.
4 */
5
6
7#ifndef gbLAB_LatticeCore_cpp_
8#define gbLAB_LatticeCore_cpp_
9
10#include <LatticeCore.h>
12#include <Eigen/Dense>
13
14namespace gbLAB
15{
16
17
18
19template <int dim>
21{
22 Eigen::Array<IntScalarType, dim, 1> nums = VectorDimI::Zero();
23
24 if (nd.squaredNorm() > 0.0)
25 {
26 const Eigen::Array<double, dim, 1> nda(nd.array().abs()); // vector of close-to-integer numbers corresponding to lattice coordinates
27 size_t maxID = 0;
28 const double maxVal(nda.maxCoeff(&maxID));
29 nd /= maxVal; // make each value of nd in [-1:1]
30
31 nums = VectorDimI::Ones();
32 Eigen::Array<IntScalarType, dim, 1> dens = VectorDimI::Ones();
33 IntScalarType denProd = 1;
34
35 for (int k = 0; k < dim; ++k)
36 {
37 //BestRationalApproximation bra(nd(k), 10000);
38 BestRationalApproximation bra(nd(k), 1000);
39
40 nums(k) = bra.num;
41 dens(k) = bra.den;
42 denProd *= bra.den;
43 }
44
45 for (int k = 0; k < dim; ++k)
46 {
47 nums(k) *= (denProd / dens(k));
48 }
49 }
50
51 return nums.matrix();
52}
53
54template <int dim>
56{
57 const VectorDimD nd(invA*d);
58 const VectorDimD rd(nd.array().round());
59 if ((nd - rd).norm() > roundTol)
60 {
61 std::cout << "nd=" << nd.transpose() << std::endl;
62 std::cout << "rd=" << rd.transpose() << std::endl;
63 std::cout << "rounding error = |nd-rd| = " << (nd-rd).norm() << std::endl;
64 throw(std::runtime_error("Input vector is not a lattice vector"));
65 }
66 return rd.template cast<IntScalarType>();
67}
68
69
70
71 template struct LatticeCore<1>;
72 template struct LatticeCore<2>;
73 template struct LatticeCore<3>;
74 template struct LatticeCore<4>;
75 template struct LatticeCore<5>;
76} // end namespace
77#endif
static VectorDimI rationalApproximation(VectorDimD v)
Approximates a direction in terms of integer coordinates.
static VectorDimI integerCoordinates(const VectorDimD &d, const MatrixDimD &invA)
Returns the integer coordinates of a vector with respect to a lattices with structure matrix .
Eigen::Matrix< double, dim, dim > MatrixDimD
Definition LatticeCore.h:25
Eigen::Matrix< double, dim, 1 > VectorDimD
Definition LatticeCore.h:24
long long int IntScalarType
Definition LatticeCore.h:26
Eigen::Matrix< IntScalarType, dim, 1 > VectorDimI
Definition LatticeCore.h:27