-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathmat.h
More file actions
394 lines (321 loc) · 8.72 KB
/
mat.h
File metadata and controls
394 lines (321 loc) · 8.72 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//
// Copyright 2018 The Simons Foundation, Inc. - All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef __ITENSOR_MAT__H_
#define __ITENSOR_MAT__H_
#include "itensor/tensor/matrange.h"
namespace itensor {
template<typename V>
using MatRefc = TenRefc<MatRange,V>;
template<typename V>
using MatRef = TenRef<MatRange,V>;
template<typename V>
using Mat = Ten<MatRange,V>;
using MatrixRef = MatRef<Real>;
using MatrixRefc = MatRefc<Real>;
using Matrix = Mat<Real>;
using CMatrixRef = MatRef<Cplx>;
using CMatrixRefc = MatRefc<Cplx>;
using CMatrix = Mat<Cplx>;
using MatrixRef1 = TenRefc<MatRange1,Real>;
using MatrixRefc1 = TenRef<MatRange1,Real>;
using Matrix1 = Ten<MatRange1,Real>;
using CMatrixRef1 = TenRefc<MatRange1,Cplx>;
using CMatrixRefc1 = TenRef<MatRange1,Cplx>;
using CMatrix1 = Ten<MatRange1,Cplx>;
template<typename M>
using hasMatRange = std::is_base_of<MatRangeType,typename stdx::decay_t<M>::range_type>;
template<typename Mat_>
auto
nrows(Mat_ const& M) -> MatRange::size_type { return M.range().rn; }
template<typename Mat_>
auto
ncols(Mat_ const& M) -> MatRange::size_type { return M.range().cn; }
template<typename Mat_>
auto
rowStride(Mat_ const& M) -> MatRange::size_type { return M.range().rs; }
template<typename Mat_>
auto
colStride(Mat_ const& M) -> MatRange::size_type { return M.range().cs; }
template<typename Mat_>
bool
isTransposed(Mat_ const& M) { return isTransposed(M.range()); }
void
operator*=(MatrixRef const& M, Real fac);
void
operator*=(CMatrixRef const& M, Real fac);
void
operator*=(CMatrixRef const& M, Cplx fac);
void
operator/=(MatrixRef const& M, Real fac);
void
operator/=(CMatrixRef const& M, Real fac);
void
operator+=(MatrixRef const& A, MatrixRefc const& B);
void
operator+=(MatrixRef const& A, Matrix && B);
void
operator+=(CMatrixRef const& A, CMatrixRefc const& B);
void
operator+=(CMatrixRef const& A, CMatrix && B);
void
operator-=(MatrixRef const& A, MatrixRefc const& B);
void
operator-=(MatrixRef const& A, Matrix && B);
void
operator-=(CMatrixRef const& A, CMatrixRefc const& B);
void
operator-=(CMatrixRef const& A, CMatrix && B);
//Copy data referenced by B to memory referenced by A
void
operator&=(MatrixRef const& A, MatrixRefc const& B);
void
operator&=(CMatrixRef const& A, MatrixRefc const& B);
//Copy data of B to memory referenced by A
void inline
operator&=(MatrixRef const& A, Matrix const& B) { A &= makeRefc(B); }
// C = beta*C + alpha*A*B
template<typename VA, typename VB>
void
gemm(MatRefc<VA> A,
MatRefc<VB> B,
MatRef<common_type<VA,VB>> C,
Real alpha,
Real beta);
template<typename VA, typename VB>
void
mult(MatRefc<VA> A,
MatRefc<VB> B,
MatRef<common_type<VA,VB>> C);
template<typename MatA,
typename MatB,
typename MatC,
class=stdx::require<hasMatRange<MatA>,
hasMatRange<MatB>,
hasMatRange<MatC>>>
void
mult(MatA const& A,
MatB const& B,
MatC & C)
{
gemm(makeRef(A),makeRef(B),makeRef(C),1.,0.);
}
// compute matrix multiply (dgemm) A*B
// add result to memory referenced by C
void
multAdd(MatrixRefc A,
MatrixRefc B,
MatrixRef C);
template<typename V>
void
mult(MatRefc<V> M,
VecRefc<V> x,
VecRef<V> y,
bool fromleft = false);
template<typename VM, typename Vx>
Vec<common_type<VM,Vx>>
mult(MatRefc<VM> M,
VecRefc<Vx> x,
bool fromleft = false);
//y = y+M*x
template<typename V>
void
multAdd(MatRefc<V> M,
VecRefc<V> x,
VecRef<V> y,
bool fromleft = false);
//y = y-M*x
template<typename V>
void
multSub(MatRefc<V> M,
VecRefc<V> x,
VecRef<V> y,
bool fromleft = false);
//void
//mult(CMatrixRefc A,
// CMatrixRefc B,
// CMatrixRef C);
//Create an NrxNc matrix
//with 1's along the diagonal
Matrix
eye(size_t Nr, size_t Nc);
//Reducing number of columns does not affect
//remaining data (column major storage)
template<typename V>
void
reduceCols(Mat<V> & M, size_t new_ncols);
template<typename V>
void
resize(Mat<V> & M, size_t nrows, size_t ncols);
template<typename T>
void
resize(MatRefc<T> const& M, size_t nr, size_t nc)
{
if((nrows(M)!=nr) || (ncols(M)!=nc))
{
auto msg = format("Matrix ref has wrong size, expected=%dx%d, actual=%dx%d",
nr,nc,nrows(M),ncols(M));
throw std::runtime_error(msg);
}
}
template<typename V>
Mat<V>
operator*(MatRefc<V> const& A, Real fac);
template<typename V>
Mat<V>
operator*(MatRefc<V> const& A, Cplx fac);
template<typename V>
Mat<V>
operator*(Real fac, MatRefc<V> const& A);
template<typename V>
Mat<V>
operator*(Cplx fac, MatRefc<V> const& A);
template<typename V>
Mat<V>
operator*(Mat<V> && A, Real fac);
template<typename V>
Mat<V>
operator*(Mat<V> && A, Cplx fac);
template<typename V>
Mat<V>
operator*(Real fac, Mat<V> && A);
template<typename V>
Mat<V>
operator*(Cplx fac, Mat<V> && A);
template<typename V>
Mat<V>
operator/(MatRefc<V> const& A, Real fac);
template<typename V>
Mat<V>
operator/(Mat<V> && A, Real fac);
template<typename MatA,typename MatB,class>
auto
operator+(MatA && A, MatB && B) -> Mat<common_type<MatA,MatB>>;
template<typename MatA,typename MatB,class>
auto
operator-(MatA && A, MatB && B) -> Mat<common_type<MatA,MatB>>;
template<typename MatA,
typename MatB,
class = stdx::require<hasMatRange<MatA>,hasMatRange<MatB>> >
Mat<common_type<MatA,MatB>>
mult(MatA const& A,
MatB const& B);
Vector
operator*(MatrixRefc const& A,
VectorRefc const& b);
Vector
operator*(VectorRefc const& a,
MatrixRefc const& B);
template<typename VA, typename VB>
auto
operator*(MatRefc<VA> const& A, MatRefc<VB> const& B) -> decltype(mult(A,B))
{ return mult(A,B); }
template<typename VA, typename VB>
auto
operator*(Mat<VA> const& A, MatRefc<VB> const& B) -> decltype(mult(makeRef(A),B))
{ return mult(makeRef(A),B); }
template<typename VA, typename VB>
auto
operator*(MatRefc<VA> const& A, Mat<VB> const& B) -> decltype(mult(A,makeRef(B)))
{ return mult(A,makeRef(B)); }
template<typename VA, typename VB>
auto
operator*(Mat<VA> const& A, Mat<VB> const& B) -> decltype(mult(makeRef(A),makeRef(B)))
{ return mult(makeRef(A),makeRef(B)); }
template<typename VA, typename VB>
Mat<VA>&
operator*=(Mat<VA> & A, MatRefc<VB> const& B) { A = mult(makeRef(A),B); return A; }
template<typename... CtrArgs>
Matrix
randomMat(CtrArgs&&... args);
template<typename... CtrArgs>
CMatrix
randomMatC(CtrArgs&&... args);
template<>
std::ostream&
operator<<(std::ostream& s, MatrixRefc const& M);
template<>
std::ostream&
operator<<(std::ostream& s, CMatrixRefc const& M);
template<typename V>
std::ostream&
operator<<(std::ostream& s, TenRef<MatRange,V> const& M) { return s << makeRefc(M); }
template<typename V>
std::ostream&
operator<<(std::ostream& s, Ten<MatRange,V> const& M) { return s << makeRefc(M); }
//
// makeMatRef functions
//
template<typename T>
auto
makeMatRef(T* p,
size_t max_offset,
size_t nrows,
size_t ncols)
-> MatRef<T>
{
return MatRef<T>({p,max_offset},MatRange{nrows,ncols});
}
template<typename T>
auto
makeMatRef(T const* p,
size_t max_offset,
size_t nrows,
size_t ncols)
-> MatRefc<T>
{
return MatRefc<T>({p,max_offset},MatRange{nrows,ncols});
}
template<typename T>
auto
makeMatRefc(T const* p,
size_t max_offset,
size_t nrows,
size_t ncols)
-> MatRefc<T>
{
return MatRefc<T>({p,max_offset},MatRange{nrows,ncols});
}
template<typename T,
class = stdx::enable_if_t<not std::is_const<T>::value> >
auto
makeMatRef(DataRange<T> const& D,
size_t nrows,
size_t ncols)
-> MatRef<T>
{
return MatRef<T>(D,MatRange{nrows,ncols});
}
template<typename T>
auto
makeMatRef(DataRange<const T> const& D,
size_t nrows,
size_t ncols)
-> MatRefc<T>
{
return MatRefc<T>(D,MatRange{nrows,ncols});
}
template<typename T>
auto
makeMatRefc(DataRange<T> const& D,
size_t nrows,
size_t ncols)
-> MatRefc<stdx::remove_const_t<T>>
{
return MatRefc<stdx::remove_const_t<T>>(DataRange<const T>(D),MatRange{nrows,ncols});
}
} //namespace itensor
#include "mat_impl.h"
#endif