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-2017 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  *
25  * @END LICENSE
26  */
27 
28 #ifndef _psi_src_lib_libmints_vector_h
29 #define _psi_src_lib_libmints_vector_h
30 
32 
33 #include <cstdlib>
34 #include <cstdio>
35 #include <vector>
36 #include <iterator>
37 #include <memory>
38 
39 #include "psi4/pybind11.h"
40 
41 namespace psi {
42 
43 class Matrix;
44 
45 class VectorIterator;
46 
48 class Vector
49 {
50 protected:
52  std::vector<double> v_;
54  std::vector<double *> vector_;
56  int nirrep_;
60  std::string name_;
61 
63  void alloc();
64 
66  void release();
67 
69  void copy_from(const Vector &other);
70 
73 
75  std::vector<int> numpy_shape_;
76 
77 public:
79  Vector();
80 
82  Vector(const Vector &copy);
83 
86  Vector(int nirrep, int *dimpi);
87 
89  Vector(int dim);
90 
93  Vector(const std::string &name, int nirrep, int *dimpi);
94 
96  Vector(const std::string &name, int dim);
97 
99  Vector(const Dimension &dimpi);
100 
102  Vector(const std::string &name, const Dimension &dimpi);
103 
105  virtual ~Vector();
106 
110  static std::shared_ptr<Vector> create(const std::string &name,
111  const Dimension &dim);
112 
113  void init(int nirrep, int *dimpi);
114 
115  void init(int nirrep, const int *dimpi, const std::string &name = "");
116 
117  void init(const Dimension &v);
118 
119  Vector *clone();
120 
122  void set(double *vec);
123 
125  double *pointer(int h = 0) { return vector_[h]; }
126 
127  const double *pointer(int h = 0) const { return vector_[h]; }
128 
130  double get(int h, int m) { return vector_[h][m]; }
131 
133  void set(int h, int m, double val) { vector_[h][m] = val; }
134 
136  double get(int m) { return vector_[0][m]; }
137 
139  void set(int m, double val) { vector_[0][m] = val; }
140 
141  void add(int m, double val) { vector_[0][m] += val; }
142 
143  void add(int h, int m, double val) { vector_[h][m] += val; }
144 
145  void add(const std::vector<double> &rhs);
146 
148  void add(const std::shared_ptr<Vector> &other);
149  void add(const Vector &other);
150 
152  void subtract(const std::shared_ptr<Vector> &other);
153  void subtract(const Vector &other);
154 
155  void axpy(double scale, const std::shared_ptr<Vector> &other);
156  void axpy(double scale, const Vector &other);
157 
159  void zero();
160 
161 
162  double &operator()(int i) { return vector_[0][i]; }
163 
164  const double &operator()(int i) const { return vector_[0][i]; }
165 
166  double &operator[](int i) { return vector_[0][i]; }
167 
168  const double &operator[](int i) const { return vector_[0][i]; }
169 
170  double pyget(const py::tuple &key);
171 
172  void pyset(const py::tuple &key, double value);
173 
174  double pyget(int key);
175 
176  void pyset(int key, double value);
177 
179  double *to_block_vector();
180 
182  int dim(int h = 0) const { return dimpi_[h]; }
183 
185  const Dimension& dimpi() const { return dimpi_; }
186 
188  int nirrep() const { return nirrep_; }
189 
195  void set_name(const std::string &name) { name_ = name; }
196 
200  std::string name() const { return name_; }
201 
203  void print_out() { print("outfile"); }
204 
211  void print(std::string outfile = "outfile", const char *extra = NULL) const;
212 
214  void copy(const Vector *rhs);
215 
217  void copy(const Vector &rhs);
218 
220  void gemv(bool transa, double alpha, Matrix *A, Vector *X, double beta);
221 
223  double vector_dot(const std::shared_ptr<Vector> &other);
224  double vector_dot(const Vector &other);
225  double dot(Vector *X);
226 
228  double norm();
229  double sum_of_squares();
230  double rms();
231 
233  void scale(const double &sc);
234 
235  // Serializable pure virtual functions:
236  void send();
237 
238  void recv();
239 
240  void bcast(int broadcaster);
241 
245  void sum();
246 
247  typedef std::vector<double>::iterator iterator;
248  typedef std::vector<double>::const_iterator const_iterator;
249 
251 
252  iterator begin() { return v_.begin(); }
253 
254  const_iterator begin() const { return v_.begin(); }
256 
258 
259  iterator end() { return v_.end(); }
260 
261  const_iterator end() const { return v_.end(); }
263 
265 
267  iterator it = v_.begin();
268  for (int g = 0; g < h; ++g) it += dimpi_[h];
269  return it;
270  }
271  // The following won't compile with clang++ and c++11
272  // const_iterator begin_irrep(int h) const
273  // { return const_iterator(vector_[h]); }
275 
277 
279  iterator it = v_.begin();
280  for (int g = 0; g <= h; ++g) it += dimpi_[h];
281  return it;
282  }
283  // The following won't compile with clang++ and c++11
284  // const_iterator end_irrep(int h) const
285  // { return const_iterator(vector_[h]) + dimpi_[h]; }
287 
291  void set_numpy_shape(std::vector<int> shape) { numpy_shape_ = shape; }
292  std::vector<int> numpy_shape() { return numpy_shape_; }
293  std::vector<py::buffer_info> array_interface();
294 
295  friend class Matrix;
296 };
297 
300 {
301 protected:
303  int **vector_;
305  int nirrep_;
307  int *dimpi_;
309  std::string name_;
310 
312  void alloc();
313 
315  void release();
316 
318  void copy_from(int **);
319 
320 public:
322  IntVector();
323 
325  IntVector(const IntVector &copy);
326 
328  IntVector(int nirrep, int *dimpi);
329 
331  IntVector(int dim);
332 
334  IntVector(const std::string &name, int nirrep, int *dimpi);
335 
337  IntVector(const std::string &name, int dim);
338 
340  virtual ~IntVector();
341 
342  void init(int nirrep, int *dimpi);
343 
345  void set(int *vec);
346 
348  int *pointer(int h = 0)
349  {
350  return vector_[h];
351  }
352 
354  int get(int h, int m)
355  {
356  return vector_[h][m];
357  }
358 
360  void set(int h, int m, int val)
361  {
362  vector_[h][m] = val;
363  }
364 
366  int *to_block_vector();
367 
369  int dim(int h = 0) const
370  {
371  return dimpi_[h];
372  }
373 
375  int *dimpi() const
376  {
377  return dimpi_;
378  }
379 
381  int nirrep() const
382  {
383  return nirrep_;
384  }
385 
391  void set_name(const std::string &name)
392  {
393  name_ = name;
394  }
395 
399  std::string name() const
400  {
401  return name_;
402  }
403 
405  void print_out()
406  { print("outfile"); }
407 
414  void print(std::string outfile = "outfile", const char *extra = NULL) const;
415 
417  void copy(const IntVector *rhs);
418 
420  void copy(const IntVector &rhs);
421 
422  friend class VectorIterator;
423 };
424 
425 //class VectorIterator : public std::iterator<std::forward_iterator_tag, double>
426 //{
427 // pointer v_;
428 
429 //public:
430 // VectorIterator(pointer v) : v_(v) {}
431 
432 // VectorIterator(const VectorIterator& mit) : v_(mit.v_) {}
433 
434 // VectorIterator& operator++() {v_++; return *this;} // prefix (++a)
435 // VectorIterator operator++(int) { VectorIterator tmp(*this); operator++(); return tmp; } // suffix (a++)
436 
437 // bool operator==(const VectorIterator& rhs) { return v_ == rhs.v_; }
438 // bool operator!=(const VectorIterator& rhs) { return v_ != rhs.v_; }
439 
440 // reference operator*() { return *v_; }
441 //};
442 
443 typedef std::shared_ptr <Vector> SharedVector;
444 typedef std::shared_ptr <IntVector> SharedIntVector;
445 
446 }
447 
448 #endif
void assign_pointer_offsets()
Assign pointer offsets in vector_ from v_.
Definition: libmints/vector.cc:168
std::string name() const
Definition: vector.h:200
void copy_from(int **)
Copies data to vector_.
Definition: intvector.cc:128
iterator begin_irrep(int h)
Definition: vector.h:266
Vector()
Default constructor, zeros everything out.
Definition: libmints/vector.cc:45
void release()
Releases vector_.
Definition: libmints/vector.cc:183
void set(int m, double val)
Sets a single element value.
Definition: vector.h:139
void set_name(const std::string &name)
Definition: vector.h:391
void add(int h, int m, double val)
Definition: vector.h:143
Dimension dimpi_
Dimensions per irrep.
Definition: vector.h:58
void set(int h, int m, double val)
Sets a single element value.
Definition: vector.h:133
iterator end_irrep(int h)
Definition: vector.h:278
double dot(Vector *X)
Definition: libmints/vector.cc:300
std::vector< double * > vector_
Pointer offsets into v_, of size dimpi_.n()
Definition: vector.h:54
void axpy(double scale, const std::shared_ptr< Vector > &other)
Definition: libmints/vector.cc:320
void bcast(int broadcaster)
Definition: libmints/vector.cc:333
double * to_block_vector()
Returns a copy of the vector_.
Definition: libmints/vector.cc:265
void print(std::string outfile="outfile", const char *extra=NULL) const
Definition: libmints/vector.cc:247
int nirrep() const
Returns the number of irreps.
Definition: vector.h:188
double sum_of_squares()
Definition: libmints/vector.cc:308
void subtract(const std::shared_ptr< Vector > &other)
Subtracts other vector from this.
Definition: libmints/vector.cc:317
std::vector< int > numpy_shape()
Definition: vector.h:292
void init(int nirrep, int *dimpi)
Definition: intvector.cc:93
Definition: pointgrp.h:105
double & operator()(int i)
Definition: vector.h:162
void zero()
Zeros the vector out.
Definition: libmints/vector.cc:212
std::vector< py::buffer_info > array_interface()
Definition: libmints/vector.cc:348
void release()
Releases vector_.
Definition: intvector.cc:116
friend class VectorIterator
Definition: vector.h:422
const_iterator end() const
Definition: vector.h:261
static std::shared_ptr< Vector > create(const std::string &name, const Dimension &dim)
Definition: libmints/vector.cc:118
const_iterator begin() const
Definition: vector.h:254
int nirrep_
Number of irreps.
Definition: vector.h:305
void sum()
Definition: libmints/vector.cc:343
double norm()
Vector norm.
Definition: libmints/vector.cc:309
double * pointer(int h=0)
Returns a pointer to irrep h.
Definition: vector.h:125
const double * pointer(int h=0) const
Definition: vector.h:127
int * to_block_vector()
Returns a copy of the vector_.
Definition: intvector.cc:192
int ** vector_
IntVector data.
Definition: vector.h:303
iterator begin()
Definition: vector.h:252
std::string name_
Name.
Definition: vector.h:60
virtual ~Vector()
Destructor, frees memory.
Definition: libmints/vector.cc:113
const Dimension & dimpi() const
Returns the dimension array.
Definition: vector.h:185
virtual ~IntVector()
Destructor, frees memory.
Definition: intvector.cc:87
void set_numpy_shape(std::vector< int > shape)
Definition: vector.h:291
void pyset(const py::tuple &key, double value)
Definition: libmints/vector.cc:226
int * dimpi_
Dimensions per irrep.
Definition: vector.h:307
void print_out()
Python compatible printer.
Definition: vector.h:405
std::vector< double > v_
Actual data, of size dimpi_.sum()
Definition: vector.h:52
double & operator[](int i)
Definition: vector.h:166
void add(int m, double val)
Definition: vector.h:141
void set_name(const std::string &name)
Definition: vector.h:195
double vector_dot(const std::shared_ptr< Vector > &other)
Vector dot product.
Definition: libmints/vector.cc:292
Definition: dimension.h:39
Makes using matrices just a little easier.
Definition: libmints/matrix.h:71
void init(int nirrep, int *dimpi)
Definition: libmints/vector.cc:123
void set(double *vec)
Sets the vector_ to the data in vec.
Definition: libmints/vector.cc:207
void send()
Definition: libmints/vector.cc:329
std::string name_
Name of the IntVector.
Definition: vector.h:309
const double & operator[](int i) const
Definition: vector.h:168
int dim(int h=0) const
Returns the dimension per irrep h.
Definition: vector.h:182
void set(int *vec)
Sets the vector_ to the data in vec.
Definition: intvector.cc:164
void set(int h, int m, int val)
Sets a single element value.
Definition: vector.h:360
void alloc()
Allocates vector_.
Definition: libmints/vector.cc:154
double pyget(const py::tuple &key)
Definition: libmints/vector.cc:217
std::vector< double >::const_iterator const_iterator
Definition: vector.h:248
const double & operator()(int i) const
Definition: vector.h:164
iterator end()
Definition: vector.h:259
void gemv(bool transa, double alpha, Matrix *A, Vector *X, double beta)
General matrix vector multiplication.
Definition: libmints/vector.cc:283
IntVector()
Default constructor, zeros everything out.
Definition: intvector.cc:36
std::shared_ptr< IntVector > SharedIntVector
Definition: vector.h:444
int * pointer(int h=0)
Returns a pointer to irrep h.
Definition: vector.h:348
void print_out()
Python compatible printer.
Definition: vector.h:203
void recv()
Definition: libmints/vector.cc:331
std::shared_ptr< PsiOutStream > outfile
Definition: core.cc:99
double rms()
Definition: libmints/vector.cc:310
std::string name() const
Definition: vector.h:399
int nirrep() const
Returns the number of irreps.
Definition: vector.h:381
Vector * clone()
Definition: libmints/vector.cc:147
std::vector< int > numpy_shape_
Numpy Shape.
Definition: vector.h:75
int dim(int h=0) const
Returns the dimension per irrep h.
Definition: vector.h:369
Definition: vector.h:299
void copy(const Vector *rhs)
Copies rhs to this.
Definition: libmints/vector.cc:197
int nirrep_
Number of irreps.
Definition: vector.h:56
void print(std::string outfile="outfile", const char *extra=NULL) const
Definition: intvector.cc:175
Definition: PsiFileImpl.h:38
int * dimpi() const
Returns the dimension array.
Definition: vector.h:375
std::vector< double >::iterator iterator
Definition: vector.h:247
void copy_from(const Vector &other)
Copies data to vector_.
Definition: libmints/vector.cc:189
Definition: vector.h:48
void scale(const double &sc)
Scale the elements of the vector.
Definition: libmints/vector.cc:312
std::shared_ptr< Vector > SharedVector
Definition: adc.h:52
void alloc()
Allocates vector_.
Definition: intvector.cc:103
void copy(const IntVector *rhs)
Copies rhs to this.
Definition: intvector.cc:137