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 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 SharedVector 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 SharedVector &other);
149  void add(const Vector &other);
150 
152  void subtract(const SharedVector &other);
153  void subtract(const Vector &other);
154 
155  void axpy(double scale, const SharedVector &other);
156  void axpy(double scale, const Vector &other);
157 
159  void zero();
160 
167  SharedVector get_block(const Slice& slice);
168 
175  void set_block(const Slice& slice,SharedVector block);
176 
177  double &operator()(int i) { return vector_[0][i]; }
178 
179  const double &operator()(int i) const { return vector_[0][i]; }
180 
181  double &operator[](int i) { return vector_[0][i]; }
182 
183  const double &operator[](int i) const { return vector_[0][i]; }
184 
186  double *to_block_vector();
187 
189  int dim(int h = 0) const { return dimpi_[h]; }
190 
192  const Dimension& dimpi() const { return dimpi_; }
193 
195  int nirrep() const { return nirrep_; }
196 
202  void set_name(const std::string &name) { name_ = name; }
203 
207  std::string name() const { return name_; }
208 
210  void print_out() { print("outfile"); }
211 
218  void print(std::string outfile = "outfile", const char *extra = NULL) const;
219 
221  void copy(const Vector *rhs);
222 
224  void copy(const Vector &rhs);
225 
227  void gemv(bool transa, double alpha, Matrix *A, Vector *X, double beta);
228 
230  double vector_dot(const SharedVector &other);
231  double vector_dot(const Vector &other);
232  double dot(Vector *X);
233 
235  double norm();
236  double sum_of_squares();
237  double rms();
238 
240  void scale(const double &sc);
241 
242  typedef std::vector<double>::iterator iterator;
243  typedef std::vector<double>::const_iterator const_iterator;
244 
246 
247  iterator begin() { return v_.begin(); }
248 
249  const_iterator begin() const { return v_.begin(); }
251 
253 
254  iterator end() { return v_.end(); }
255 
256  const_iterator end() const { return v_.end(); }
258 
260 
262  iterator it = v_.begin();
263  for (int g = 0; g < h; ++g) it += dimpi_[h];
264  return it;
265  }
266  // The following won't compile with clang++ and c++11
267  // const_iterator begin_irrep(int h) const
268  // { return const_iterator(vector_[h]); }
270 
272 
274  iterator it = v_.begin();
275  for (int g = 0; g <= h; ++g) it += dimpi_[h];
276  return it;
277  }
278  // The following won't compile with clang++ and c++11
279  // const_iterator end_irrep(int h) const
280  // { return const_iterator(vector_[h]) + dimpi_[h]; }
282 
286  void set_numpy_shape(std::vector<int> shape) { numpy_shape_ = shape; }
287  std::vector<int> numpy_shape() { return numpy_shape_; }
288 
289  friend class Matrix;
290 };
291 
294 {
295 protected:
297  int **vector_;
299  int nirrep_;
301  int *dimpi_;
303  std::string name_;
304 
306  void alloc();
307 
309  void release();
310 
312  void copy_from(int **);
313 
314 public:
316  IntVector();
317 
319  IntVector(const IntVector &copy);
320 
322  IntVector(int nirrep, int *dimpi);
323 
325  IntVector(int dim);
326 
328  IntVector(const std::string &name, int nirrep, int *dimpi);
329 
331  IntVector(const std::string &name, int dim);
332 
334  virtual ~IntVector();
335 
336  void init(int nirrep, int *dimpi);
337 
339  void set(int *vec);
340 
342  int *pointer(int h = 0)
343  {
344  return vector_[h];
345  }
346 
348  int get(int h, int m)
349  {
350  return vector_[h][m];
351  }
352 
354  void set(int h, int m, int val)
355  {
356  vector_[h][m] = val;
357  }
358 
360  int *to_block_vector();
361 
363  int dim(int h = 0) const
364  {
365  return dimpi_[h];
366  }
367 
369  int *dimpi() const
370  {
371  return dimpi_;
372  }
373 
375  int nirrep() const
376  {
377  return nirrep_;
378  }
379 
385  void set_name(const std::string &name)
386  {
387  name_ = name;
388  }
389 
393  std::string name() const
394  {
395  return name_;
396  }
397 
399  void print_out()
400  { print("outfile"); }
401 
408  void print(std::string outfile = "outfile", const char *extra = NULL) const;
409 
411  void copy(const IntVector *rhs);
412 
414  void copy(const IntVector &rhs);
415 
416  friend class VectorIterator;
417 };
418 
419 }
420 
421 #endif
void assign_pointer_offsets()
Assign pointer offsets in vector_ from v_.
Definition: libmints/vector.cc:166
std::string name() const
Definition: vector.h:207
void copy_from(int **)
Copies data to vector_.
Definition: intvector.cc:129
iterator begin_irrep(int h)
Definition: vector.h:261
Vector()
Default constructor, zeros everything out.
Definition: libmints/vector.cc:43
void release()
Releases vector_.
Definition: libmints/vector.cc:181
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:385
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:273
double dot(Vector *X)
Definition: libmints/vector.cc:311
std::vector< double * > vector_
Pointer offsets into v_, of size dimpi_.n()
Definition: vector.h:54
double * to_block_vector()
Returns a copy of the vector_.
Definition: libmints/vector.cc:276
void print(std::string outfile="outfile", const char *extra=NULL) const
Definition: libmints/vector.cc:259
int nirrep() const
Returns the number of irreps.
Definition: vector.h:195
double sum_of_squares()
Definition: libmints/vector.cc:319
std::vector< int > numpy_shape()
Definition: vector.h:287
void init(int nirrep, int *dimpi)
Definition: intvector.cc:94
Definition: pointgrp.h:106
double & operator()(int i)
Definition: vector.h:177
void zero()
Zeros the vector out.
Definition: libmints/vector.cc:254
void release()
Releases vector_.
Definition: intvector.cc:117
SharedVector get_block(const Slice &slice)
Definition: libmints/vector.cc:210
friend class VectorIterator
Definition: vector.h:416
const_iterator end() const
Definition: vector.h:256
static SharedVector create(const std::string &name, const Dimension &dim)
Definition: libmints/vector.cc:116
const_iterator begin() const
Definition: vector.h:249
int nirrep_
Number of irreps.
Definition: vector.h:299
Slicing for Matrices and Vectors objects.
Definition: dimension.h:120
double norm()
Vector norm.
Definition: libmints/vector.cc:320
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:193
int ** vector_
IntVector data.
Definition: vector.h:297
iterator begin()
Definition: vector.h:247
std::string name_
Name.
Definition: vector.h:60
virtual ~Vector()
Destructor, frees memory.
Definition: libmints/vector.cc:111
const Dimension & dimpi() const
Returns the dimension array.
Definition: vector.h:192
virtual ~IntVector()
Destructor, frees memory.
Definition: intvector.cc:88
void set_numpy_shape(std::vector< int > shape)
Definition: vector.h:286
int * dimpi_
Dimensions per irrep.
Definition: vector.h:301
void print_out()
Python compatible printer.
Definition: vector.h:399
std::vector< double > v_
Actual data, of size dimpi_.sum()
Definition: vector.h:52
double & operator[](int i)
Definition: vector.h:181
void add(int m, double val)
Definition: vector.h:141
void set_name(const std::string &name)
Definition: vector.h:202
Definition: dimension.h:38
Makes using matrices just a little easier.
Definition: libmints/matrix.h:66
void init(int nirrep, int *dimpi)
Definition: libmints/vector.cc:121
void set(double *vec)
Sets the vector_ to the data in vec.
Definition: libmints/vector.cc:205
std::string name_
Name of the IntVector.
Definition: vector.h:303
const double & operator[](int i) const
Definition: vector.h:183
int dim(int h=0) const
Returns the dimension per irrep h.
Definition: vector.h:189
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:354
void alloc()
Allocates vector_.
Definition: libmints/vector.cc:152
std::vector< double >::const_iterator const_iterator
Definition: vector.h:243
const double & operator()(int i) const
Definition: vector.h:179
iterator end()
Definition: vector.h:254
void gemv(bool transa, double alpha, Matrix *A, Vector *X, double beta)
General matrix vector multiplication.
Definition: libmints/vector.cc:294
IntVector()
Default constructor, zeros everything out.
Definition: intvector.cc:37
int * pointer(int h=0)
Returns a pointer to irrep h.
Definition: vector.h:342
void print_out()
Python compatible printer.
Definition: vector.h:210
std::shared_ptr< PsiOutStream > outfile
Definition: core.cc:102
double rms()
Definition: libmints/vector.cc:321
std::string name() const
Definition: vector.h:393
int nirrep() const
Returns the number of irreps.
Definition: vector.h:375
Vector * clone()
Definition: libmints/vector.cc:145
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:363
Definition: vector.h:293
void copy(const Vector *rhs)
Copies rhs to this.
Definition: libmints/vector.cc:195
double vector_dot(const SharedVector &other)
Vector dot product.
Definition: libmints/vector.cc:303
int nirrep_
Number of irreps.
Definition: vector.h:56
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:233
int * dimpi() const
Returns the dimension array.
Definition: vector.h:369
std::vector< double >::iterator iterator
Definition: vector.h:242
void copy_from(const Vector &other)
Copies data to vector_.
Definition: libmints/vector.cc:187
void subtract(const SharedVector &other)
Subtracts other vector from this.
Definition: libmints/vector.cc:328
Definition: vector.h:48
void scale(const double &sc)
Scale the elements of the vector.
Definition: libmints/vector.cc:323
void axpy(double scale, const SharedVector &other)
Definition: libmints/vector.cc:331
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