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 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 #include "psi4/pybind11.h"
42 
43 namespace psi {
44 
45 class Matrix;
46 
47 class VectorIterator;
48 
50 class Vector
51 {
52 protected:
54  std::vector<double> v_;
56  std::vector<double *> vector_;
58  int nirrep_;
62  std::string name_;
63 
65  void alloc();
66 
68  void release();
69 
71  void copy_from(const Vector &other);
72 
75 
77  std::vector<int> numpy_shape_;
78 
79 public:
81  Vector();
82 
84  Vector(const Vector &copy);
85 
88  Vector(int nirrep, int *dimpi);
89 
91  Vector(int dim);
92 
95  Vector(const std::string &name, int nirrep, int *dimpi);
96 
98  Vector(const std::string &name, int dim);
99 
101  Vector(const Dimension &dimpi);
102 
104  Vector(const std::string &name, const Dimension &dimpi);
105 
107  virtual ~Vector();
108 
112  static SharedVector create(const std::string &name,
113  const Dimension &dim);
114 
115  void init(int nirrep, int *dimpi);
116 
117  void init(int nirrep, const int *dimpi, const std::string &name = "");
118 
119  void init(const Dimension &v);
120 
121  Vector *clone();
122 
124  void set(double *vec);
125 
127  double *pointer(int h = 0) { return vector_[h]; }
128 
129  const double *pointer(int h = 0) const { return vector_[h]; }
130 
132  double get(int h, int m) { return vector_[h][m]; }
133 
135  void set(int h, int m, double val) { vector_[h][m] = val; }
136 
138  double get(int m) { return vector_[0][m]; }
139 
141  void set(int m, double val) { vector_[0][m] = val; }
142 
143  void add(int m, double val) { vector_[0][m] += val; }
144 
145  void add(int h, int m, double val) { vector_[h][m] += val; }
146 
147  void add(const std::vector<double> &rhs);
148 
150  void add(const SharedVector &other);
151  void add(const Vector &other);
152 
154  void subtract(const SharedVector &other);
155  void subtract(const Vector &other);
156 
157  void axpy(double scale, const SharedVector &other);
158  void axpy(double scale, const Vector &other);
159 
161  void zero();
162 
169  SharedVector get_block(const Slice& slice);
170 
177  void set_block(const Slice& slice,SharedVector block);
178 
179  double &operator()(int i) { return vector_[0][i]; }
180 
181  const double &operator()(int i) const { return vector_[0][i]; }
182 
183  double &operator[](int i) { return vector_[0][i]; }
184 
185  const double &operator[](int i) const { return vector_[0][i]; }
186 
187  double pyget(const py::tuple &key);
188 
189  void pyset(const py::tuple &key, double value);
190 
191  double pyget(int key);
192 
193  void pyset(int key, double value);
194 
196  double *to_block_vector();
197 
199  int dim(int h = 0) const { return dimpi_[h]; }
200 
202  const Dimension& dimpi() const { return dimpi_; }
203 
205  int nirrep() const { return nirrep_; }
206 
212  void set_name(const std::string &name) { name_ = name; }
213 
217  std::string name() const { return name_; }
218 
220  void print_out() { print("outfile"); }
221 
228  void print(std::string outfile = "outfile", const char *extra = NULL) const;
229 
231  void copy(const Vector *rhs);
232 
234  void copy(const Vector &rhs);
235 
237  void gemv(bool transa, double alpha, Matrix *A, Vector *X, double beta);
238 
240  double vector_dot(const SharedVector &other);
241  double vector_dot(const Vector &other);
242  double dot(Vector *X);
243 
245  double norm();
246  double sum_of_squares();
247  double rms();
248 
250  void scale(const double &sc);
251 
252  // Serializable pure virtual functions:
253  void send();
254 
255  void recv();
256 
257  void bcast(int broadcaster);
258 
262  void sum();
263 
264  typedef std::vector<double>::iterator iterator;
265  typedef std::vector<double>::const_iterator const_iterator;
266 
268 
269  iterator begin() { return v_.begin(); }
270 
271  const_iterator begin() const { return v_.begin(); }
273 
275 
276  iterator end() { return v_.end(); }
277 
278  const_iterator end() const { return v_.end(); }
280 
282 
284  iterator it = v_.begin();
285  for (int g = 0; g < h; ++g) it += dimpi_[h];
286  return it;
287  }
288  // The following won't compile with clang++ and c++11
289  // const_iterator begin_irrep(int h) const
290  // { return const_iterator(vector_[h]); }
292 
294 
296  iterator it = v_.begin();
297  for (int g = 0; g <= h; ++g) it += dimpi_[h];
298  return it;
299  }
300  // The following won't compile with clang++ and c++11
301  // const_iterator end_irrep(int h) const
302  // { return const_iterator(vector_[h]) + dimpi_[h]; }
304 
308  void set_numpy_shape(std::vector<int> shape) { numpy_shape_ = shape; }
309  std::vector<int> numpy_shape() { return numpy_shape_; }
310  std::vector<py::buffer_info> array_interface();
311 
312  friend class Matrix;
313 };
314 
317 {
318 protected:
320  int **vector_;
322  int nirrep_;
324  int *dimpi_;
326  std::string name_;
327 
329  void alloc();
330 
332  void release();
333 
335  void copy_from(int **);
336 
337 public:
339  IntVector();
340 
342  IntVector(const IntVector &copy);
343 
345  IntVector(int nirrep, int *dimpi);
346 
348  IntVector(int dim);
349 
351  IntVector(const std::string &name, int nirrep, int *dimpi);
352 
354  IntVector(const std::string &name, int dim);
355 
357  virtual ~IntVector();
358 
359  void init(int nirrep, int *dimpi);
360 
362  void set(int *vec);
363 
365  int *pointer(int h = 0)
366  {
367  return vector_[h];
368  }
369 
371  int get(int h, int m)
372  {
373  return vector_[h][m];
374  }
375 
377  void set(int h, int m, int val)
378  {
379  vector_[h][m] = val;
380  }
381 
383  int *to_block_vector();
384 
386  int dim(int h = 0) const
387  {
388  return dimpi_[h];
389  }
390 
392  int *dimpi() const
393  {
394  return dimpi_;
395  }
396 
398  int nirrep() const
399  {
400  return nirrep_;
401  }
402 
408  void set_name(const std::string &name)
409  {
410  name_ = name;
411  }
412 
416  std::string name() const
417  {
418  return name_;
419  }
420 
422  void print_out()
423  { print("outfile"); }
424 
431  void print(std::string outfile = "outfile", const char *extra = NULL) const;
432 
434  void copy(const IntVector *rhs);
435 
437  void copy(const IntVector &rhs);
438 
439  friend class VectorIterator;
440 };
441 
442 //class VectorIterator : public std::iterator<std::forward_iterator_tag, double>
443 //{
444 // pointer v_;
445 
446 //public:
447 // VectorIterator(pointer v) : v_(v) {}
448 
449 // VectorIterator(const VectorIterator& mit) : v_(mit.v_) {}
450 
451 // VectorIterator& operator++() {v_++; return *this;} // prefix (++a)
452 // VectorIterator operator++(int) { VectorIterator tmp(*this); operator++(); return tmp; } // suffix (a++)
453 
454 // bool operator==(const VectorIterator& rhs) { return v_ == rhs.v_; }
455 // bool operator!=(const VectorIterator& rhs) { return v_ != rhs.v_; }
456 
457 // reference operator*() { return *v_; }
458 //};
459 
460 typedef std::shared_ptr <Vector> SharedVector;
461 typedef std::shared_ptr <IntVector> SharedIntVector;
462 
463 }
464 
465 #endif
void assign_pointer_offsets()
Assign pointer offsets in vector_ from v_.
Definition: libmints/vector.cc:169
std::string name() const
Definition: vector.h:217
void copy_from(int **)
Copies data to vector_.
Definition: intvector.cc:129
iterator begin_irrep(int h)
Definition: vector.h:283
Vector()
Default constructor, zeros everything out.
Definition: libmints/vector.cc:46
void release()
Releases vector_.
Definition: libmints/vector.cc:184
void set(int m, double val)
Sets a single element value.
Definition: vector.h:141
void set_name(const std::string &name)
Definition: vector.h:408
void add(int h, int m, double val)
Definition: vector.h:145
Dimension dimpi_
Dimensions per irrep.
Definition: vector.h:60
void set(int h, int m, double val)
Sets a single element value.
Definition: vector.h:135
iterator end_irrep(int h)
Definition: vector.h:295
double dot(Vector *X)
Definition: libmints/vector.cc:345
std::vector< double * > vector_
Pointer offsets into v_, of size dimpi_.n()
Definition: vector.h:56
void bcast(int broadcaster)
Definition: libmints/vector.cc:378
double * to_block_vector()
Returns a copy of the vector_.
Definition: libmints/vector.cc:310
void print(std::string outfile="outfile", const char *extra=NULL) const
Definition: libmints/vector.cc:292
int nirrep() const
Returns the number of irreps.
Definition: vector.h:205
double sum_of_squares()
Definition: libmints/vector.cc:353
std::vector< int > numpy_shape()
Definition: vector.h:309
void init(int nirrep, int *dimpi)
Definition: intvector.cc:94
Definition: pointgrp.h:106
double & operator()(int i)
Definition: vector.h:179
void zero()
Zeros the vector out.
Definition: libmints/vector.cc:257
std::vector< py::buffer_info > array_interface()
Definition: libmints/vector.cc:393
void release()
Releases vector_.
Definition: intvector.cc:117
SharedVector get_block(const Slice &slice)
Definition: libmints/vector.cc:213
friend class VectorIterator
Definition: vector.h:439
const_iterator end() const
Definition: vector.h:278
static SharedVector create(const std::string &name, const Dimension &dim)
Definition: libmints/vector.cc:119
const_iterator begin() const
Definition: vector.h:271
int nirrep_
Number of irreps.
Definition: vector.h:322
void sum()
Definition: libmints/vector.cc:388
Slicing for Matrices and Vectors objects.
Definition: dimension.h:120
double norm()
Vector norm.
Definition: libmints/vector.cc:354
double * pointer(int h=0)
Returns a pointer to irrep h.
Definition: vector.h:127
const double * pointer(int h=0) const
Definition: vector.h:129
int * to_block_vector()
Returns a copy of the vector_.
Definition: intvector.cc:193
int ** vector_
IntVector data.
Definition: vector.h:320
iterator begin()
Definition: vector.h:269
std::string name_
Name.
Definition: vector.h:62
virtual ~Vector()
Destructor, frees memory.
Definition: libmints/vector.cc:114
const Dimension & dimpi() const
Returns the dimension array.
Definition: vector.h:202
virtual ~IntVector()
Destructor, frees memory.
Definition: intvector.cc:88
void set_numpy_shape(std::vector< int > shape)
Definition: vector.h:308
void pyset(const py::tuple &key, double value)
Definition: libmints/vector.cc:271
int * dimpi_
Dimensions per irrep.
Definition: vector.h:324
void print_out()
Python compatible printer.
Definition: vector.h:422
std::vector< double > v_
Actual data, of size dimpi_.sum()
Definition: vector.h:54
double & operator[](int i)
Definition: vector.h:183
void add(int m, double val)
Definition: vector.h:143
void set_name(const std::string &name)
Definition: vector.h:212
Definition: dimension.h:38
Makes using matrices just a little easier.
Definition: libmints/matrix.h:70
void init(int nirrep, int *dimpi)
Definition: libmints/vector.cc:124
void set(double *vec)
Sets the vector_ to the data in vec.
Definition: libmints/vector.cc:208
void send()
Definition: libmints/vector.cc:374
std::string name_
Name of the IntVector.
Definition: vector.h:326
const double & operator[](int i) const
Definition: vector.h:185
int dim(int h=0) const
Returns the dimension per irrep h.
Definition: vector.h:199
void set(int *vec)
Sets the vector_ to the data in vec.
Definition: intvector.cc:165
void set(int h, int m, int val)
Sets a single element value.
Definition: vector.h:377
void alloc()
Allocates vector_.
Definition: libmints/vector.cc:155
double pyget(const py::tuple &key)
Definition: libmints/vector.cc:262
std::vector< double >::const_iterator const_iterator
Definition: vector.h:265
const double & operator()(int i) const
Definition: vector.h:181
iterator end()
Definition: vector.h:276
void gemv(bool transa, double alpha, Matrix *A, Vector *X, double beta)
General matrix vector multiplication.
Definition: libmints/vector.cc:328
IntVector()
Default constructor, zeros everything out.
Definition: intvector.cc:37
std::shared_ptr< IntVector > SharedIntVector
Definition: vector.h:461
int * pointer(int h=0)
Returns a pointer to irrep h.
Definition: vector.h:365
void print_out()
Python compatible printer.
Definition: vector.h:220
void recv()
Definition: libmints/vector.cc:376
std::shared_ptr< PsiOutStream > outfile
Definition: core.cc:101
double rms()
Definition: libmints/vector.cc:355
std::string name() const
Definition: vector.h:416
int nirrep() const
Returns the number of irreps.
Definition: vector.h:398
Vector * clone()
Definition: libmints/vector.cc:148
std::vector< int > numpy_shape_
Numpy Shape.
Definition: vector.h:77
int dim(int h=0) const
Returns the dimension per irrep h.
Definition: vector.h:386
Definition: vector.h:316
void copy(const Vector *rhs)
Copies rhs to this.
Definition: libmints/vector.cc:198
double vector_dot(const SharedVector &other)
Vector dot product.
Definition: libmints/vector.cc:337
int nirrep_
Number of irreps.
Definition: vector.h:58
void print(std::string outfile="outfile", const char *extra=NULL) const
Definition: intvector.cc:176
void set_block(const Slice &slice, SharedVector block)
Definition: libmints/vector.cc:236
Definition: PsiFileImpl.h:39
int * dimpi() const
Returns the dimension array.
Definition: vector.h:392
std::vector< double >::iterator iterator
Definition: vector.h:264
void copy_from(const Vector &other)
Copies data to vector_.
Definition: libmints/vector.cc:190
void subtract(const SharedVector &other)
Subtracts other vector from this.
Definition: libmints/vector.cc:362
Definition: vector.h:50
void scale(const double &sc)
Scale the elements of the vector.
Definition: libmints/vector.cc:357
void axpy(double scale, const SharedVector &other)
Definition: libmints/vector.cc:365
std::shared_ptr< Vector > SharedVector
Definition: adc.h:51
void alloc()
Allocates vector_.
Definition: intvector.cc:104
void copy(const IntVector *rhs)
Copies rhs to this.
Definition: intvector.cc:138