MADNESS 0.10.1
PNOTensors.h
Go to the documentation of this file.
1/*
2 * PNOmadness::Tensors.h
3 *
4 * Created on: Oct 22, 2018
5 * Author: kottmanj
6 */
7
8#ifndef PAPER_CODE_PNOTENSORS_H_
9#define PAPER_CODE_PNOTENSORS_H_
10
11#include <stdio.h>
12#include <valarray>
13#include <madness.h>
14
15
16namespace PNOTensors {
17 /// index in packed lower triangle matrix, row >= col
18 template <typename Int>
19 size_t tridx(Int row, Int col) {
20 return (row < col) ? (row + col * (col + 1) / 2)
21 : (col + row * (row + 1) / 2);
22 }
23
24 /// # of elements in lower triangle matrix
25 template <typename Int>
26 size_t ntri(Int n) {
27 return n * (n + 1) / 2;
28 }
29
30 /// stores pairs of pairs that share first index (i.e. {ij} and {ik}, where i >= j) as a 3-index tensor:
31 /// case 1 -- i >= j, i >= k: only need j >= k, or ij >= ik; store as {{ij},k}
32 /// case 2 -- i >= j, k > i: can also be found at Tensor_IJ_JK(k,i,j)
33 template <typename T>
35 public:
36 Tensor_IJ_IK(size_t n) : n_(n), data_(ntri(n) * n) {
37 reset();
38 }
39 ~Tensor_IJ_IK() = default;
40
41 std::tuple<size_t,bool> ijk(size_t i, size_t j, size_t k) const {
42 assert(i < size_t(n_));
43 assert(j < size_t(n_));
44 assert(k < size_t(n_));
45 assert(i >= j);
46 if (i >= k) {
47 return j >= k ? std::make_tuple(tridx(i,j) * n_ + k,false) : std::make_tuple(tridx(i,k) * n_ + j, true);
48 }
49 else // i < k
50 return std::make_tuple(tridx(i,j) * n_ + k,false);
51 }
52
53 bool is_unique(size_t i, size_t j, size_t k) const {
54 assert(i < size_t(n_));
55 assert(j < size_t(n_));
56 assert(k < size_t(n_));
57 assert(i >= j);
58 if (i < k)
59 return true;
60 else
61 return j >= k;
62 }
63
64 bool is_initialized(size_t i, size_t j, size_t k) const {
65 assert(i < size_t(n_));
66 assert(j < size_t(n_));
67 assert(k < size_t(n_));
68 assert(i >= j);
69 size_t ijk;
70 std::tie(ijk,std::ignore) = this->ijk(i,j,k);
71 return data_[ijk].size() != 0;
72 }
73 madness::Tensor<T> get(size_t i, size_t j, size_t k) const {
74 assert(i < size_t(n_));
75 assert(j < size_t(n_));
76 assert(k < size_t(n_));
77 assert(i >= j);
78 size_t ijk; bool swap;
79 std::tie(ijk, swap) = this->ijk(i,j,k);
80 return swap ? data_[ijk].swapdim(0,1) : data_[ijk];
81 }
82 void set(size_t i, size_t j, size_t k, const madness::Tensor<T>& t) {
83 assert(i < size_t(n_));
84 assert(j < size_t(n_));
85 assert(k < size_t(n_));
86 assert(i >= j);
87 size_t ijk; bool swap;
88 std::tie(ijk, swap) = this->ijk(i,j,k);
89 data_[ijk] = swap ? t.swapdim(0,1) : t;
90 }
91
92 void reset() {
93 std::fill(std::begin(data_), std::end(data_), madness::Tensor<T>());
94 }
95
96 private:
97 int n_;
98 std::valarray<madness::Tensor<T>> data_;
99 };
100
101 /// stores pairs of pairs that share second index (i.e. {ij} and {kj}, where i >= j) as a 3-index tensor:
102 /// case 1 -- i >= j, j >= k: store as {{ij},k}
103 /// case 2 -- i >= j, k > j: only need i >= k, or ij >= kj; store as {{ij},k}
104 template <typename T>
106 public:
107 Tensor_IJ_KJ(size_t n) : n_(n), data_(ntri(n) * n) {
108 reset();
109 }
110 ~Tensor_IJ_KJ() = default;
111
112 std::tuple<size_t,bool> ijk(size_t i, size_t j, size_t k) const {
113 assert(i < size_t(n_));
114 assert(j < size_t(n_));
115 assert(k < size_t(n_));
116 assert(i >= j);
117 if (k > j)
118 return i >= k ? std::make_tuple(tridx(i,j) * n_ + k,false) : std::make_tuple(tridx(k,j) * n_ + i, true);
119 else
120 return std::make_tuple(tridx(i,j) * n_ + k,false);
121 }
122 bool is_unique(size_t i, size_t j, size_t k) const {
123 assert(i < size_t(n_));
124 assert(j < size_t(n_));
125 assert(k < size_t(n_));
126 assert(i >= j);
127 if (k <= j)
128 return true;
129 else
130 return (i >= k);
131 }
132 bool is_initialized(size_t i, size_t j, size_t k) const {
133 assert(i < size_t(n_));
134 assert(j < size_t(n_));
135 assert(k < size_t(n_));
136 assert(i >= j);
137
138 size_t ijk;
139 std::tie(ijk, std::ignore) = this->ijk(i, j, k);
140 return data_[ijk].size() != 0;
141 }
142 madness::Tensor<T> get(size_t i, size_t j, size_t k) const {
143 assert(i < size_t(n_));
144 assert(j < size_t(n_));
145 assert(k < size_t(n_));
146 assert(i >= j);
147 size_t ijk;
148 bool swap;
149 std::tie(ijk, swap) = this->ijk(i,j,k);
150 return swap ? data_[ijk].swapdim(0,1) : data_[ijk];
151 }
152 void set(size_t i, size_t j, size_t k, const madness::Tensor<T>& t) {
153 assert(i < size_t(n_));
154 assert(j < size_t(n_));
155 assert(k < size_t(n_));
156 assert(i >= j);
157 size_t ijk;
158 bool swap;
159 std::tie(ijk, swap) = this->ijk(i,j,k);
160 data_[ijk] = swap ? t.swapdim(0,1) : t;
161 }
162
163 void reset() {
164 std::fill(std::begin(data_), std::end(data_), madness::Tensor<T>());
165 }
166
167 private:
168 int n_;
169 std::valarray<madness::Tensor<T>> data_;
170 };
171
172} // was anonymous namespace --- now PNOTensors
173
174#endif /* PAPER_CODE_PNOTENSORS_H_ */
Definition PNOTensors.h:34
bool is_initialized(size_t i, size_t j, size_t k) const
Definition PNOTensors.h:64
Tensor_IJ_IK(size_t n)
Definition PNOTensors.h:36
madness::Tensor< T > get(size_t i, size_t j, size_t k) const
Definition PNOTensors.h:73
void reset()
Definition PNOTensors.h:92
int n_
Definition PNOTensors.h:97
std::valarray< madness::Tensor< T > > data_
Definition PNOTensors.h:98
bool is_unique(size_t i, size_t j, size_t k) const
Definition PNOTensors.h:53
void set(size_t i, size_t j, size_t k, const madness::Tensor< T > &t)
Definition PNOTensors.h:82
std::tuple< size_t, bool > ijk(size_t i, size_t j, size_t k) const
Definition PNOTensors.h:41
Definition PNOTensors.h:105
int n_
Definition PNOTensors.h:168
std::valarray< madness::Tensor< T > > data_
Definition PNOTensors.h:169
bool is_unique(size_t i, size_t j, size_t k) const
Definition PNOTensors.h:122
Tensor_IJ_KJ(size_t n)
Definition PNOTensors.h:107
std::tuple< size_t, bool > ijk(size_t i, size_t j, size_t k) const
Definition PNOTensors.h:112
madness::Tensor< T > get(size_t i, size_t j, size_t k) const
Definition PNOTensors.h:142
void reset()
Definition PNOTensors.h:163
void set(size_t i, size_t j, size_t k, const madness::Tensor< T > &t)
Definition PNOTensors.h:152
bool is_initialized(size_t i, size_t j, size_t k) const
Definition PNOTensors.h:132
A tensor is a multidimension array.
Definition tensor.h:317
Tensor< T > swapdim(long idim, long jdim)
Returns new view/tensor swaping dimensions i and j.
Definition tensor.h:1605
General header file for using MADNESS.
Definition PNOTensors.h:16
size_t tridx(Int row, Int col)
index in packed lower triangle matrix, row >= col
Definition PNOTensors.h:19
size_t ntri(Int n)
Definition PNOTensors.h:26
static const long k
Definition rk.cc:44