File: VectorGeneral.h

package info (click to toggle)
ergo 3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 17,396 kB
  • sloc: cpp: 94,740; ansic: 17,015; sh: 7,559; makefile: 1,402; yacc: 127; lex: 110; awk: 23
file content (407 lines) | stat: -rw-r--r-- 13,136 bytes parent folder | download
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
395
396
397
398
399
400
401
402
403
404
405
406
407
/* Ergo, version 3.8, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2019 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
 * and Anastasia Kruchinina.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Primary academic reference:
 * Ergo: An open-source program for linear-scaling electronic structure
 * calculations,
 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
 * Kruchinina,
 * SoftwareX 7, 107 (2018),
 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */

/** @file VectorGeneral.h General vector class
 *
 * Copyright(c) Emanuel Rubensson 2006
 *
 * @author Emanuel Rubensson  @a responsible @a author
 * @date January 2006
 *
 */
#ifndef MAT_VECTORGENERAL
#define MAT_VECTORGENERAL
#include <iostream>
#include <fstream>
#include <ios>
#include "FileWritable.h"
#include "matrix_proxy.h"
#include "ValidPtr.h"
namespace mat {
  template<typename Treal, typename Tvector>
    class VectorGeneral : public FileWritable {
  public:

    inline void resetSizesAndBlocks(SizesAndBlocks const & newRows) {
      vectorPtr.haveDataStructureSet(true);
      vectorPtr->resetRows(newRows);
    }

    inline bool is_empty() const {
      return !vectorPtr.haveDataStructureGet();
    }

    inline void clear_structure(){
      vectorPtr.haveDataStructureSet(false);
    }

    VectorGeneral(SizesAndBlocks const & newRows):vectorPtr(new Tvector) {
      resetSizesAndBlocks(newRows);      
    }
    VectorGeneral():vectorPtr(new Tvector) {}
    
    /* In the code we are using std::vector<VectorGeneral> which in the c++ standard before c++11 requires move operation like T x_new = x which calls implicitly the copy constructor. To make it work with g++ versions without c++11 support we remove the keyword explicit. */
#if __cplusplus >= 201103L      
    explicit VectorGeneral(const VectorGeneral<Treal, Tvector>& other)
#else 
    VectorGeneral(const VectorGeneral<Treal, Tvector>& other)
#endif    
      :FileWritable(other), vectorPtr(new Tvector) {
      if (other.vectorPtr.haveDataStructureGet()) {
	vectorPtr.haveDataStructureSet(true);
      }
      *vectorPtr = *other.vectorPtr;
    }
      
    inline void assign_from_full
      (std::vector<Treal> const & fullVector, 
       SizesAndBlocks const & newRows) {
      resetSizesAndBlocks(newRows);
      this->vectorPtr->assignFromFull(fullVector);
    }
    inline void fullvector(std::vector<Treal> & fullVector) const {
      this->vectorPtr->fullVector(fullVector);
    }
    VectorGeneral<Treal, Tvector>& 
      operator=(const VectorGeneral<Treal, Tvector>& other) {
      if (other.vectorPtr.haveDataStructureGet()) {
	vectorPtr.haveDataStructureSet(true);
      }
      *this->vectorPtr = *other.vectorPtr; 
      return *this;
    } 

    inline void clear() {
      if (is_empty())
	// This means that the object's data structure has not been set
	// There is nothing to clear and the vectorPtr is not valid either
	return;
      vectorPtr->clear();
    }

    inline void rand() {
      vectorPtr->randomNormalized();
    }
    
    /* LEVEL 2 operations */
    /* OPERATIONS INVOLVING ORDINARY MATRICES */
    /** y = alpha * op(A) * x */
    template<typename Tmatrix>
      VectorGeneral<Treal, Tvector>& operator=
      (const XYZ<Treal,
       MatrixGeneral<Treal, Tmatrix>,
       VectorGeneral<Treal, Tvector> >& smv);
    
    /** y += alpha * op(A) * x */
    template<typename Tmatrix>
      VectorGeneral<Treal, Tvector>& operator+=
      (const XYZ<Treal,
       MatrixGeneral<Treal, Tmatrix>,
       VectorGeneral<Treal, Tvector> >& smv);
    /** y = alpha * op(A) * x + beta * y */
    template<typename Tmatrix>
      VectorGeneral<Treal, Tvector>& operator=
      (const XYZpUV<Treal,
       MatrixGeneral<Treal, Tmatrix>,
       VectorGeneral<Treal, Tvector>,
       Treal,
       VectorGeneral<Treal, Tvector> >& smvpsv);

    /** y = op(A) * x                      : A is general */
    template<typename Tmatrix>
      VectorGeneral<Treal, Tvector>& operator=
      (const XY<MatrixGeneral<Treal, Tmatrix>,
       VectorGeneral<Treal, Tvector> >& mv) {
      Treal ONE = 1.0;
      return this->operator=(XYZ<Treal, MatrixGeneral<Treal, Tmatrix>,
			     VectorGeneral<Treal, Tvector> >(ONE, mv.A, mv.B,
							     false, mv.tA, mv.tB));      
    }
    
    /* OPERATIONS INVOLVING SYMMETRIC MATRICES */
    /** y = alpha * A * x                      : A is symmetric */
    template<typename Tmatrix>
      VectorGeneral<Treal, Tvector>& operator=
      (const XYZ<Treal,
       MatrixSymmetric<Treal, Tmatrix>,
       VectorGeneral<Treal, Tvector> >& smv);
    /** y += alpha * A * x                     : A is symmetric */
    template<typename Tmatrix>
      VectorGeneral<Treal, Tvector>& operator+=
      (const XYZ<Treal,
       MatrixSymmetric<Treal, Tmatrix>,
       VectorGeneral<Treal, Tvector> >& smv);
    /** y = alpha * A * x + beta * y           : A is symmetric */
    template<typename Tmatrix>
      VectorGeneral<Treal, Tvector>& operator=
      (const XYZpUV<Treal,
       MatrixSymmetric<Treal, Tmatrix>,
       VectorGeneral<Treal, Tvector>,
       Treal,
       VectorGeneral<Treal, Tvector> >& smvpsv);

    /* OPERATIONS INVOLVING TRIANGULAR MATRICES */
    /** y = op(A) * x                      : A is triangular */
    template<typename Tmatrix>
      VectorGeneral<Treal, Tvector>& operator=
      (const XY<MatrixTriangular<Treal, Tmatrix>,
       VectorGeneral<Treal, Tvector> >& mv);


    /* LEVEL 1 operations */
    inline Treal eucl() const {
      return vectorPtr->eucl();
    }

    inline VectorGeneral<Treal, Tvector>& 
      operator*=(Treal const alpha) {
      *vectorPtr *= alpha;
      return *this;
    }

    inline VectorGeneral<Treal, Tvector>& 
      operator=(int const k) {
      *vectorPtr = k;
      return *this;
    }

    /** y += alpha * x */
    VectorGeneral<Treal, Tvector>& operator+=
      (const XY<Treal, VectorGeneral<Treal, Tvector> >& sv);
    

    inline Tvector const & getVector() const {return *vectorPtr;}
    
    std::string obj_type_id() const {return "VectorGeneral";}
  protected:
    ValidPtr<Tvector> vectorPtr;

    inline void writeToFileProt(std::ofstream & file) const {
      if (is_empty())
	// This means that the object's data structure has not been set
	return;
      vectorPtr->writeToFile(file);
    }
    inline void readFromFileProt(std::ifstream & file) {
      if (is_empty())
	// This means that the object's data structure has not been set
	return;
      vectorPtr->readFromFile(file);
    }

    inline void inMemorySet(bool inMem) {
      vectorPtr.inMemorySet(inMem);
    }

  private:
    
  }; /* end class VectorGeneral */


    /* LEVEL 2 operations */
    /* OPERATIONS INVOLVING ORDINARY MATRICES */
    /** y = alpha * op(A) * x */
  template<typename Treal, typename Tvector>
    template<typename Tmatrix>
    VectorGeneral<Treal, Tvector>& 
    VectorGeneral<Treal, Tvector>::operator=
    (const XYZ<Treal,
     MatrixGeneral<Treal, Tmatrix>,
     VectorGeneral<Treal, Tvector> >& smv) {
    assert(!smv.tC);
    vectorPtr.haveDataStructureSet(true);
    if ( this == &smv.C ) {
      // We need a copy of the smv.C vector since it is the same as *this
      VectorGeneral<Treal, Tvector> tmp(smv.C);
      Tvector::gemv(smv.tB, smv.A, smv.B.getMatrix(),
		    *tmp.vectorPtr, 0, *this->vectorPtr);
    }
    else       
      Tvector::gemv(smv.tB, smv.A, smv.B.getMatrix(),
		    *smv.C.vectorPtr, 0, *this->vectorPtr);
    return *this;
  }
  
  /** y += alpha * op(A) * x */
  template<typename Treal, typename Tvector>
    template<typename Tmatrix>
    VectorGeneral<Treal, Tvector>& 
    VectorGeneral<Treal, Tvector>::operator+=
    (const XYZ<Treal,
     MatrixGeneral<Treal, Tmatrix>,
     VectorGeneral<Treal, Tvector> >& smv) {
    assert(!smv.tC);
    assert(this != &smv.C);
    Tvector::gemv(smv.tB, smv.A, smv.B.getMatrix(),
		  *smv.C.vectorPtr, 1, *this->vectorPtr);
    return *this;
  }
  
  
  /** y = alpha * op(A) * x + beta * y */
  template<typename Treal, typename Tvector>
    template<typename Tmatrix>
    VectorGeneral<Treal, Tvector>& 
    VectorGeneral<Treal, Tvector>::operator=
    (const XYZpUV<Treal,
     MatrixGeneral<Treal, Tmatrix>,
     VectorGeneral<Treal, Tvector>,
     Treal,
     VectorGeneral<Treal, Tvector> >& smvpsv) {
    assert(!smvpsv.tC && !smvpsv.tE);
    assert(this != &smvpsv.C);
    if (this == &smvpsv.E)
      Tvector::gemv(smvpsv.tB, smvpsv.A, smvpsv.B.getMatrix(),
		    *smvpsv.C.vectorPtr, smvpsv.D, *this->vectorPtr);
    else
      throw Failure("VectorGeneral<Treal, Tvector>::operator="
		    "(const XYZpUV<Treal, "
		    "MatrixGeneral<Treal, Tmatrix>, "
		    "VectorGeneral<Treal, Tvector>, "
		    "Treal, "
		    "VectorGeneral<Treal, Tvector> >&) : "
		    "y = alpha * op(A) * x + beta * z "
		    "not supported for z != y");
    return *this;
  }

  
  /* OPERATIONS INVOLVING SYMMETRIC MATRICES */
  /** y = alpha * A * x                      : A is symmetric */
  template<typename Treal, typename Tvector>
    template<typename Tmatrix>
    VectorGeneral<Treal, Tvector>& 
    VectorGeneral<Treal, Tvector>::operator=
    (const XYZ<Treal,
     MatrixSymmetric<Treal, Tmatrix>,
     VectorGeneral<Treal, Tvector> >& smv) {
    assert(!smv.tC);
    assert(this != &smv.C);
    vectorPtr.haveDataStructureSet(true);
    Tvector::symv('U', smv.A, smv.B.getMatrix(),
    		  *smv.C.vectorPtr, 0, *this->vectorPtr);
    return *this;    
  }
  

  /** y += alpha * A * x                     : A is symmetric */
  template<typename Treal, typename Tvector>
    template<typename Tmatrix>
    VectorGeneral<Treal, Tvector>& 
    VectorGeneral<Treal, Tvector>::operator+=
    (const XYZ<Treal,
     MatrixSymmetric<Treal, Tmatrix>,
     VectorGeneral<Treal, Tvector> >& smv) {
    assert(!smv.tC);
    assert(this != &smv.C);
    Tvector::symv('U', smv.A, smv.B.getMatrix(),
    		  *smv.C.vectorPtr, 1, *this->vectorPtr);
    return *this;    
  }

  /** y = alpha * A * x + beta * y           : A is symmetric */
  template<typename Treal, typename Tvector>
    template<typename Tmatrix>
    VectorGeneral<Treal, Tvector>& 
    VectorGeneral<Treal, Tvector>::operator=
    (const XYZpUV<Treal,
     MatrixSymmetric<Treal, Tmatrix>,
     VectorGeneral<Treal, Tvector>,
     Treal,
     VectorGeneral<Treal, Tvector> >& smvpsv) {
    assert(!smvpsv.tC && !smvpsv.tE);
    assert(this != &smvpsv.C);
    if (this == &smvpsv.E)
      Tvector::symv('U', smvpsv.A, smvpsv.B.getMatrix(),
		    *smvpsv.C.vectorPtr, smvpsv.D, *this->vectorPtr);
    else
      throw Failure("VectorGeneral<Treal, Tvector>::operator="
		    "(const XYZpUV<Treal, "
		    "MatrixSymmetric<Treal, Tmatrix>, "
		    "VectorGeneral<Treal, Tvector>, "
		    "Treal, "
		    "VectorGeneral<Treal, Tvector> >&) : "
		    "y = alpha * A * x + beta * z "
		    "not supported for z != y");
    return *this;    
  }

  /* OPERATIONS INVOLVING TRIANGULAR MATRICES */
  /** x = op(A) * x                      : A is triangular */

  template<typename Treal, typename Tvector>
    template<typename Tmatrix>
    VectorGeneral<Treal, Tvector>& 
    VectorGeneral<Treal, Tvector>::operator=
    (const XY<MatrixTriangular<Treal, Tmatrix>,
     VectorGeneral<Treal, Tvector> >& mv) {
    assert(!mv.tB);
    if (this != &mv.B)
      throw Failure("y = A * x not supported for y != x ");
    Tvector::trmv('U', mv.tA, 
		  mv.A.getMatrix(), 
		  *this->vectorPtr);
    return *this;
  }
  
  /* LEVEL 1 operations */

  /** y += alpha * x */
  template<typename Treal, typename Tvector>
    VectorGeneral<Treal, Tvector>& 
    VectorGeneral<Treal, Tvector>::operator+=
    (const XY<Treal, VectorGeneral<Treal, Tvector> >& sv) {
    assert(!sv.tB);
    assert(this != &sv.B);
    Tvector::axpy(sv.A, *sv.B.vectorPtr, *this->vectorPtr);
    return *this;
  }



  /* Defined outside class */
  /** transpose(x) * y 
   * Scalar (dot) product of two vectors 
   */
  template<typename Treal, typename Tvector>
    Treal operator*(Xtrans<VectorGeneral<Treal, Tvector> > const & xT,
		    VectorGeneral<Treal, Tvector> const & y) {
    if (xT.tA == false)
      throw Failure("operator*("
		    "Xtrans<VectorGeneral<Treal, Tvector> > const &,"
		    " VectorGeneral<Treal, Tvector> const &): "
		    "Dimension mismatch in vector operation");
    return Tvector::dot(xT.A.getVector(), y.getVector());
  }
  
  

}  /* end namespace mat */
#endif