1#ifndef STRAWBERRY_DMRGEXTERNALQ
2#define STRAWBERRY_DMRGEXTERNALQ
7#include <boost/functional/hash.hpp>
8#include <boost/rational.hpp>
11typedef boost::rational<int>
frac;
17#ifndef POSMOD_FUNCTION
18#define POSMOD_FUNCTION
35 if (r.denominator()==1) {ss << r.numerator();}
44 struct hash<
std::array<int,N> >
46 inline size_t operator()(
const std::array<int,N> &a)
const
49 for (
size_t i=0; i<N; ++i)
51 boost::hash_combine(seed, a[i]);
61template<
size_t Nq,
size_t Nlegs>
62struct hash<
std::array<qarray<Nq>,Nlegs> >
67 for (
size_t leg=0; leg<Nlegs; ++leg)
68 for (
size_t q=0; q<Nq; ++q)
70 boost::hash_combine(seed, ix[leg][q]);
78struct hash<
std::tuple<size_t,size_t,size_t,qarray<Nq>,qarray<Nq> > >
83 boost::hash_combine(seed, get<0>(ix));
84 boost::hash_combine(seed, get<1>(ix));
85 boost::hash_combine(seed, get<2>(ix));
86 for (
size_t q=0; q<Nq; ++q) { boost::hash_combine(seed, get<3>(ix)[q]); }
87 for (
size_t q=0; q<Nq; ++q) { boost::hash_combine(seed, get<4>(ix)[q]); }
94struct hash<
std::pair<qarray<Nq>,size_t> >
99 for (
size_t q=0; q<Nq; ++q) { boost::hash_combine(seed, get<0>(ix)[q]); }
100 boost::hash_combine(seed, get<1>(ix));
110struct hash<
std::array<size_t,2> >
112 inline size_t operator()(
const std::array<size_t,2> &a)
const
115 boost::hash_combine(seed, a[0]);
116 boost::hash_combine(seed, a[1]);
131 for (
size_t q=0; q<Nq; ++q)
133 boost::hash_combine(seed, ix[q]);
141template<
typename MatrixTypeA,
typename MatrixTypeB>
144 return A.rows()*
A.cols()*
B.cols();
148template<
typename MatrixTypeA,
typename MatrixTypeB,
typename MatrixTypeC>
149std::vector<size_t>
mult_cost (
const MatrixTypeA &
A,
const MatrixTypeB &
B,
const MatrixTypeC &C)
151 std::vector<size_t> out(2);
162template<
typename MatrixTypeA,
typename MatrixTypeB,
typename MatrixTypeC,
typename MatrixTypeD>
163std::vector<size_t>
mult_cost (
const MatrixTypeA &
A,
const MatrixTypeB &
B,
const MatrixTypeC &C,
const MatrixTypeD &D)
165 std::vector<size_t> out(5);
170 out[1] =
mult_cost(
A,
B) +
A.rows()*C.rows()*C.cols() +
A.rows()*D.rows()*D.cols();
173 out[2] =
mult_cost(
B,C) +
A.rows()*
A.cols()*C.cols() +
A.rows()*D.rows()*D.cols();
176 out[3] =
mult_cost(
B,C) +
B.rows()*D.rows()*D.cols() +
A.rows()*
A.cols()*D.cols();
179 out[4] =
mult_cost(C,D) +
B.rows()*
B.cols()*D.cols() +
A.rows()*
A.cols()*D.cols();
183template<
typename MatrixType>
186 std::cout << label <<
": " << M.rows() <<
"x" << M.cols() << std::endl;
190template<
typename MatrixTypeA,
typename MatrixTypeB,
typename MatrixTypeC,
typename MatrixTypeR,
typename Scalar>
191void optimal_multiply (Scalar alpha,
const MatrixTypeA &
A,
const MatrixTypeB &
B,
const MatrixTypeC &C, MatrixTypeR &result,
bool DEBUG=
false)
201 std::vector<size_t> cost(2);
203 size_t opt_mult = min_element(cost.begin(),cost.end())- cost.begin();
207 MatrixTypeR Mtmp =
A *
B;
208 result.noalias() = alpha * Mtmp * C;
210 else if (opt_mult == 1)
212 MatrixTypeR Mtmp =
B * C;
213 result.noalias() = alpha *
A * Mtmp;
218template<
typename MatrixTypeA,
typename MatrixTypeB,
typename MatrixTypeC,
typename MatrixTypeD,
typename MatrixTypeR,
typename Scalar>
219void optimal_multiply (Scalar alpha,
const MatrixTypeA &
A,
const MatrixTypeB &
B,
const MatrixTypeC &C,
const MatrixTypeD &D, MatrixTypeR &result,
231 std::vector<size_t> cost(5);
233 size_t opt_mult = min_element(cost.begin(),cost.end())- cost.begin();
237 MatrixTypeR Mtmp1 =
A *
B;
238 MatrixTypeR Mtmp2 = C * D;
239 result = alpha * Mtmp1 * Mtmp2;
241 else if (opt_mult == 1)
243 MatrixTypeR Mtmp =
A *
B;
245 result = alpha * Mtmp * D;
247 else if (opt_mult == 2)
249 MatrixTypeR Mtmp =
B * C;
251 result = alpha * Mtmp * D;
253 else if (opt_mult == 3)
255 MatrixTypeR Mtmp =
B * C;
257 result = alpha *
A * Mtmp;
259 else if (opt_mult == 4)
261 MatrixTypeR Mtmp = C * D;
263 result = alpha *
A * Mtmp;
274template<
typename Scalar>
285 return -0.5+sqrt(0.25+x);
290 ArrayXd v(abs(max-min)+1);
292 for (
int i=0; i<v.rows(); ++i)
294 v(i) = double(min+i);
boost::rational< int > frac
double calc_S_from_SSp1(double x)
size_t mult_cost(const MatrixTypeA &A, const MatrixTypeB &B)
std::string print_frac_nice(frac r)
Scalar localSumTrivial(int i)
void print_size(const MatrixType &M, string label)
void optimal_multiply(Scalar alpha, const MatrixTypeA &A, const MatrixTypeB &B, const MatrixTypeC &C, MatrixTypeR &result, bool DEBUG=false)
int closest_int(double x, int min, int max)
size_t operator()(const qarray< Nq > &ix) const
size_t operator()(const std::array< int, N > &a) const
size_t operator()(const std::array< qarray< Nq >, Nlegs > &ix) const
size_t operator()(const std::array< size_t, 2 > &a) const
size_t operator()(const std::pair< qarray< Nq >, size_t > &ix) const
size_t operator()(const std::tuple< size_t, size_t, size_t, qarray< Nq >, qarray< Nq > > &ix) const