File: tensor_lapack_test.cc

package info (click to toggle)
btas 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,132 kB
  • sloc: cpp: 26,486; ansic: 1,545; makefile: 5
file content (136 lines) | stat: -rw-r--r-- 3,591 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "test.h"

#include <ctime>
#include <iostream>

#include "btas/tensor.h"
#include "btas/tensor_func.h"
#include "btas/generic/gesvd_impl.h"
#include "btas/generic/contract.h"

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real_distribution.hpp>
#include "btas/generic/default_random_seed.h"

using std::cout;
using std::endl;
using namespace btas;

template <typename T>
std::ostream& 
operator<<(std::ostream& s, const Tensor<T>& X)
    {
    for(auto i : X.range()) s << i << " " << X(i) << "\n";
    return s;
    }

template <typename T>
T
randomReal()
    {
    static boost::random::mt19937 rng(btas::random_seed_accessor());
    static auto dist = boost::random::uniform_real_distribution<T>{0., 1.};
    return dist(rng);
    }

template <typename T>
std::complex<T>
randomCplx()
    {
    return std::complex<T>(randomReal<T>(),randomReal<T>());
    }

const auto eps_double = 1.e4 * std::numeric_limits<double>::epsilon();
const auto eps_float = 1.e4 * std::numeric_limits<float>::epsilon();

TEST_CASE("Tensor SVD") {

  SECTION("Double-precision SVD") {

    auto do_test = [](auto v) {
      using ztype = decltype(v);
      using zT = Tensor<ztype>;
      using dT = Tensor<double>;
      const size_t M = 5, N = 6;
      const auto R = std::min(M, N);
      zT A(M, N);
      A.generate([]() {
        if constexpr (std::is_same_v<ztype,double>)
          return randomReal<double>();
        else
          return randomCplx<double>();
      });
      zT U(M, M);
      zT Vt(N, N);
      dT Sigma(R);
      auto Acopy = A;
      gesvd(lapack::Job::AllVec, lapack::Job::AllVec, A, Sigma, U, Vt);

      ztype one(1.0);
      ztype zero(0.0);
      zT A_(M, N);
      zT S(M, N);
      S.fill(zero);
      for (auto i = 0; i < R; i++) {
        S(i, i) = Sigma(i);
      }
      zT temp1(M, N);
      contract(one, U, {'i', 'j'}, S, {'j', 'k'}, zero, temp1, {'i', 'k'});
      contract(one, temp1, {'i', 'j'}, Vt, {'j', 'k'}, zero, A_, {'i', 'k'});
      double res = 0;
      for (auto i = 0; i < A.extent(0); i++) {
        for (auto j = 0; j < A.extent(1); j++) {
          res += pow(abs(Acopy(i, j) - A_(i, j)), 2);
        }
      }
      CHECK(res < eps_double);
    };

    do_test(double{});
    do_test(std::complex<double>{});

  }
  SECTION("Double precision 3D SVD"){

    auto do_test = [](auto v) {
      using ztype = decltype(v);
      using zT = Tensor<ztype>;
      using dT = Tensor<double>;
      const size_t M = 2, N = 3, O = 5;
      zT A(M, N, O);
      A.generate([]() {
        if constexpr (std::is_same_v<ztype,double>)
          return randomReal<double>();
        else
          return randomCplx<double>();
      });
      zT U(M, N, M*N);
      zT Vt(O, O);
      dT Sigma(O);
      auto Acopy = A;
      gesvd(lapack::Job::AllVec, lapack::Job::AllVec, A, Sigma, U, Vt);
      zT A_(M, N, O);
      zT S(M*N, O);
      S.fill(0.0);
      for (auto i = 0; i < O; i++) {
        S(i, i) = Sigma(i);
      }
      ztype one(1.0);
      ztype zero(0.0);
      zT temp1(M, N, O);
      contract(one, U, {'i', 'j', 'k'}, S, {'k', 'l'}, zero, temp1, {'i', 'j', 'l'});
      contract(one, temp1, {'i', 'j', 'l'}, Vt, {'l', 'm'},zero, A_, {'i', 'j', 'm'});
      double res = 0;
      for (auto i = 0; i < A.extent(0); i++) {
        for (auto j = 0; j < A.extent(1); j++) {
          for (auto k = 0; k < A.extent(2); k++) {
            res += abs(Acopy(i, j, k) - A_(i, j, k));
          }
        }
      }
      CHECK(res<eps_double);
    };
    do_test(double{});
    do_test(std::complex<double>{});
  }
}