Psi4
vector.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2018 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 #ifndef _psi_src_lib_libmints_vector_h
30 #define _psi_src_lib_libmints_vector_h
31 
33 #include "psi4/libmints/typedefs.h"
34 
35 #include <cstdlib>
36 #include <cstdio>
37 #include <vector>
38 #include <iterator>
39 #include <memory>
40 
41 namespace psi {
42 
43 class Matrix;
44 
45 class VectorIterator;
46 
48 class PSI_API Vector {
49  protected:
51  std::vector<double> v_;
53  std::vector<double *> vector_;
55  int nirrep_;
59  std::string name_;
60 
62  void alloc();
63 
65  void release();
66 
68  void copy_from(const Vector &other);
69 
71  void assign_pointer_offsets();
72 
74  std::vector<int> numpy_shape_;
75 
76  public:
78  Vector();
79 
81  Vector(const Vector &copy);
82 
85  Vector(int nirrep, int *dimpi);
86 
88  Vector(int dim);
89 
92  Vector(const std::string &name, int nirrep, int *dimpi);
93 
95  Vector(const std::string &name, int dim);
96 
98  Vector(const Dimension &dimpi);
99 
101  Vector(const std::string &name, const Dimension &dimpi);
102 
104  virtual ~Vector();
105 
109  static SharedVector create(const std::string &name, const Dimension &dim);
110 
111  void init(int nirrep, int *dimpi);
112 
113  void init(int nirrep, const int *dimpi, const std::string &name = "");
114 
115  void init(const Dimension &v);
116 
117  Vector *clone();
118 
120  void set(double *vec);
121 
123  double *pointer(int h = 0) { return vector_[h]; }
124 
125  const double *pointer(int h = 0) const { return vector_[h]; }
126 
128  double get(int h, int m) { return vector_[h][m]; }
129 
131  void set(int h, int m, double val) { vector_[h][m] = val; }
132 
134  double get(int m) { return vector_[0][m]; }
135 
137  void set(int m, double val) { vector_[0][m] = val; }
138 
139  void add(int m, double val) { vector_[0][m] += val; }
140 
141  void add(int h, int m, double val) { vector_[h][m] += val; }
142 
143  void add(const std::vector<double> &rhs);
144 
146  void add(const SharedVector &other);
147  void add(const Vector &other);
148 
150  void subtract(const SharedVector &other);
151  void subtract(const Vector &other);
152 
153  void axpy(double scale, const SharedVector &other);
154  void axpy(double scale, const Vector &other);
155 
157  void zero();
158 
165  SharedVector get_block(const Slice &slice);
166 
173  void set_block(const Slice &slice, SharedVector block);
174 
175  double &operator()(int i) { return vector_[0][i]; }
176 
177  const double &operator()(int i) const { return vector_[0][i]; }
178 
179  double &operator[](int i) { return vector_[0][i]; }
180 
181  const double &operator[](int i) const { return vector_[0][i]; }
182 
184  double *to_block_vector();
185 
187  int dim(int h = 0) const { return dimpi_[h]; }
188 
190  const Dimension &dimpi() const { return dimpi_; }
191 
193  int nirrep() const { return nirrep_; }
194 
200  void set_name(const std::string &name) { name_ = name; }
201 
205  std::string name() const { return name_; }
206 
208  void print_out() { print("outfile"); }
209 
216  void print(std::string outfile = "outfile", const char *extra = nullptr) const;
217 
219  void copy(const Vector *rhs);
220 
222  void copy(const Vector &rhs);
223 
225  void gemv(bool transa, double alpha, Matrix *A, Vector *X, double beta);
226 
228  double vector_dot(const SharedVector &other);
229  double vector_dot(const Vector &other);
230  double dot(Vector *X);
231 
233  double norm();
234  double sum_of_squares();
235  double rms();
236 
238  void scale(const double &sc);
239 
240  typedef std::vector<double>::iterator iterator;
241  typedef std::vector<double>::const_iterator const_iterator;
242 
244 
245  iterator begin() { return v_.begin(); }
246 
247  const_iterator begin() const { return v_.begin(); }
249 
251 
252  iterator end() { return v_.end(); }
253 
254  const_iterator end() const { return v_.end(); }
256 
258 
260  iterator it = v_.begin();
261  for (int g = 0; g < h; ++g) it += dimpi_[h];
262  return it;
263  }
264  // The following won't compile with clang++ and c++11
265  // const_iterator begin_irrep(int h) const
266  // { return const_iterator(vector_[h]); }
268 
270 
272  iterator it = v_.begin();
273  for (int g = 0; g <= h; ++g) it += dimpi_[h];
274  return it;
275  }
276  // The following won't compile with clang++ and c++11
277  // const_iterator end_irrep(int h) const
278  // { return const_iterator(vector_[h]) + dimpi_[h]; }
280 
284  void set_numpy_shape(std::vector<int> shape) { numpy_shape_ = shape; }
285  std::vector<int> numpy_shape() { return numpy_shape_; }
286 
287  friend class Matrix;
288 };
289 
291 class IntVector {
292  protected:
294  int **vector_;
296  int nirrep_;
298  int *dimpi_;
300  std::string name_;
301 
303  void alloc();
304 
306  void release();
307 
309  void copy_from(int **);
310 
311  public:
313  IntVector();
314 
316  IntVector(const IntVector &copy);
317 
319  IntVector(int nirrep, int *dimpi);
320 
322  IntVector(int dim);
323 
325  IntVector(const std::string &name, int nirrep, int *dimpi);
326 
328  IntVector(const std::string &name, int dim);
329 
331  virtual ~IntVector();
332 
333  void init(int nirrep, int *dimpi);
334 
336  void set(int *vec);
337 
339  int *pointer(int h = 0) { return vector_[h]; }
340 
342  int get(int h, int m) { return vector_[h][m]; }
343 
345  void set(int h, int m, int val) { vector_[h][m] = val; }
346 
348  int *to_block_vector();
349 
351  int dim(int h = 0) const { return dimpi_[h]; }
352 
354  int *dimpi() const { return dimpi_; }
355 
357  int nirrep() const { return nirrep_; }
358 
364  void set_name(const std::string &name) { name_ = name; }
365 
369  std::string name() const { return name_; }
370 
372  void print_out() { print("outfile"); }
373 
380  void print(std::string outfile = "outfile", const char *extra = nullptr) const;
381 
383  void copy(const IntVector *rhs);
384 
386  void copy(const IntVector &rhs);
387 
388  friend class VectorIterator;
389 };
390 
391 } // namespace psi
392 
393 #endif
std::string name() const
Definition: vector.h:205
void copy_from(int **)
Copies data to vector_.
Definition: intvector.cc:120
iterator begin_irrep(int h)
Definition: vector.h:259
void set(int m, double val)
Sets a single element value.
Definition: vector.h:137
double dot(BlockMatrix *A, BlockMatrix *B)
Definition: mcscf/block_matrix.cc:199
void set_block(Slice rows, Slice cols, SharedMatrix block)
void set_name(const std::string &name)
Definition: vector.h:364
void add(int h, int m, double val)
Definition: vector.h:141
Dimension dimpi_
Dimensions per irrep.
Definition: vector.h:57
void set(int h, int m, double val)
Sets a single element value.
Definition: vector.h:131
iterator end_irrep(int h)
Definition: vector.h:271
std::vector< double * > vector_
Pointer offsets into v_, of size dimpi_.n()
Definition: vector.h:53
Definition: matrixtmp.h:42
int nirrep() const
Returns the number of irreps.
Definition: vector.h:193
std::vector< int > numpy_shape()
Definition: vector.h:285
void init(int nirrep, int *dimpi)
Definition: intvector.cc:90
Definition: pointgrp.h:104
double & operator()(int i)
Definition: vector.h:175
void release()
Releases vector_.
Definition: intvector.cc:110
friend class VectorIterator
Definition: vector.h:388
const_iterator end() const
Definition: vector.h:254
std::shared_ptr< PsiOutStream > outfile
Definition: core.cc:102
const_iterator begin() const
Definition: vector.h:247
int nirrep_
Number of irreps.
Definition: vector.h:296
Slicing for Matrices and Vectors objects.
Definition: dimension.h:125
double * pointer(int h=0)
Returns a pointer to irrep h.
Definition: vector.h:123
const double * pointer(int h=0) const
Definition: vector.h:125
int * to_block_vector()
Returns a copy of the vector_.
Definition: intvector.cc:177
int ** vector_
IntVector data.
Definition: vector.h:294
iterator begin()
Definition: vector.h:245
std::string name_
Name.
Definition: vector.h:59
const Dimension & dimpi() const
Returns the dimension array.
Definition: vector.h:190
virtual ~IntVector()
Destructor, frees memory.
Definition: intvector.cc:85
void set_numpy_shape(std::vector< int > shape)
Definition: vector.h:284
int * dimpi_
Dimensions per irrep.
Definition: vector.h:298
void print_out()
Python compatible printer.
Definition: vector.h:372
std::vector< double > v_
Actual data, of size dimpi_.sum()
Definition: vector.h:51
double & operator[](int i)
Definition: vector.h:179
void add(int m, double val)
Definition: vector.h:139
void set_name(const std::string &name)
Definition: vector.h:200
Definition: dimension.h:40
Makes using matrices just a little easier.
Definition: libmints/matrix.h:59
std::string name_
Name of the IntVector.
Definition: vector.h:300
const double & operator[](int i) const
Definition: vector.h:181
int dim(int h=0) const
Returns the dimension per irrep h.
Definition: vector.h:187
void set(int *vec)
Sets the vector_ to the data in vec.
Definition: intvector.cc:151
void set(int h, int m, int val)
Sets a single element value.
Definition: vector.h:345
std::vector< double >::const_iterator const_iterator
Definition: vector.h:241
const double & operator()(int i) const
Definition: vector.h:177
iterator end()
Definition: vector.h:252
IntVector()
Default constructor, zeros everything out.
Definition: intvector.cc:37
#define PSI_API
Definition: pragma.h:155
int * pointer(int h=0)
Returns a pointer to irrep h.
Definition: vector.h:339
void print_out()
Python compatible printer.
Definition: vector.h:208
std::string name() const
Definition: vector.h:369
int nirrep() const
Returns the number of irreps.
Definition: vector.h:357
std::vector< int > numpy_shape_
Numpy Shape.
Definition: vector.h:74
int dim(int h=0) const
Returns the dimension per irrep h.
Definition: vector.h:351
Definition: vector.h:291
int nirrep_
Number of irreps.
Definition: vector.h:55
int * dimpi() const
Returns the dimension array.
Definition: vector.h:354
std::vector< double >::iterator iterator
Definition: vector.h:240
void print(std::string outfile="outfile", const char *extra=nullptr) const
Definition: intvector.cc:162
Definition: vector.h:48
std::shared_ptr< Vector > SharedVector
Definition: adc.h:51
void alloc()
Allocates vector_.
Definition: intvector.cc:98
void copy(const IntVector *rhs)
Copies rhs to this.
Definition: intvector.cc:128