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-2019 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 #pragma once
30 
31 #include <vector>
32 #include <memory>
33 
34 #include "dimension.h"
35 
36 namespace psi {
37 
38 class Matrix;
39 
40 class Vector;
41 using SharedVector = std::shared_ptr<Vector>;
42 
44 class PSI_API Vector final {
45  protected:
47  std::vector<double> v_;
49  std::vector<double *> vector_;
51  int nirrep_;
55  std::string name_;
56 
58  void alloc();
59 
61  void release();
62 
64  void copy_from(const Vector &other);
65 
67  void assign_pointer_offsets();
68 
70  std::vector<int> numpy_shape_;
71 
72  public:
74  Vector();
75 
77  Vector(const Vector &copy);
78 
81  explicit Vector(int nirrep, int *dimpi);
82 
84  explicit Vector(int dim);
85 
88  explicit Vector(const std::string &name, int nirrep, int *dimpi);
89 
91  explicit Vector(const std::string &name, int dim);
92 
94  explicit Vector(const Dimension &dimpi);
95 
97  explicit Vector(const std::string &name, const Dimension &dimpi);
98 
100  ~Vector();
101 
102  void init(int nirrep, int *dimpi);
103 
104  void init(int nirrep, const int *dimpi, const std::string &name = "");
105 
106  void init(const Dimension &v);
107 
108  Vector *clone();
109 
111  double *pointer(int h = 0) { return vector_[h]; }
112 
113  const double *pointer(int h = 0) const { return vector_[h]; }
114 
116  double get(int m) const { return vector_[0][m]; }
117 
119  void set(int m, double val) { vector_[0][m] = val; }
120 
122  double get(int h, int m) const { return vector_[h][m]; }
123 
125  void set(int h, int m, double val) { vector_[h][m] = val; }
126 
127  void add(int m, double val) { vector_[0][m] += val; }
128 
129  void add(int h, int m, double val) { vector_[h][m] += val; }
130 
132  void add(const SharedVector &other);
133  void add(const Vector &other);
134 
136  void subtract(const SharedVector &other);
137  void subtract(const Vector &other);
138 
139  void axpy(double scale, const SharedVector &other);
140  void axpy(double scale, const Vector &other);
141 
143  void zero();
144 
151  SharedVector get_block(const Slice &slice);
152 
159  void set_block(const Slice &slice, SharedVector block);
160 
161  double &operator()(int i) { return vector_[0][i]; }
162 
163  const double &operator()(int i) const { return vector_[0][i]; }
164 
165  double &operator[](int i) { return vector_[0][i]; }
166 
167  const double &operator[](int i) const { return vector_[0][i]; }
168 
170  int dim(int h = 0) const { return dimpi_[h]; }
171 
173  const Dimension &dimpi() const { return dimpi_; }
174 
176  int nirrep() const { return nirrep_; }
177 
183  void set_name(const std::string &name) { name_ = name; }
184 
188  std::string name() const { return name_; }
189 
191  void print_out() { print("outfile"); }
192 
199  void print(std::string outfile = "outfile", const char *extra = nullptr) const;
200 
202  void copy(const Vector *rhs);
203 
205  void copy(const Vector &rhs);
206 
208  void gemv(bool transa, double alpha, Matrix *A, Vector *X, double beta);
209 
211  double vector_dot(const SharedVector &other);
212  double vector_dot(const Vector &other);
213  double dot(Vector *X);
214 
216  double norm();
217  double sum_of_squares();
218  double rms();
219 
221  void scale(double sc);
222 
226  void set_numpy_shape(std::vector<int> shape) { numpy_shape_ = shape; }
227  std::vector<int> numpy_shape() { return numpy_shape_; }
228 
230  "Using `Vector::create` instead of `auto my_vec = std::make_shared<Vector>(name, dim);` "
231  "is deprecated, and in 1.4 it will "
232  "stop working")
233  static SharedVector create(const std::string &name, const Dimension &dim) {
234  return std::make_shared<Vector>(name, dim);
235  }
236 };
237 
240  protected:
242  int **vector_;
244  int nirrep_;
246  int *dimpi_;
248  std::string name_;
249 
251  void alloc();
252 
254  void release();
255 
257  void copy_from(int **);
258 
259  public:
261  IntVector();
262 
264  IntVector(const IntVector &copy);
265 
267  IntVector(int nirrep, int *dimpi);
268 
270  IntVector(int dim);
271 
273  IntVector(const std::string &name, int nirrep, int *dimpi);
274 
276  IntVector(const std::string &name, int dim);
277 
279  virtual ~IntVector();
280 
281  void init(int nirrep, int *dimpi);
282 
284  void set(int *vec);
285 
287  int *pointer(int h = 0) { return vector_[h]; }
288 
290  int get(int h, int m) { return vector_[h][m]; }
291 
293  void set(int h, int m, int val) { vector_[h][m] = val; }
294 
296  int *to_block_vector();
297 
299  int dim(int h = 0) const { return dimpi_[h]; }
300 
302  int *dimpi() const { return dimpi_; }
303 
305  int nirrep() const { return nirrep_; }
306 
312  void set_name(const std::string &name) { name_ = name; }
313 
317  std::string name() const { return name_; }
318 
320  void print_out() { print("outfile"); }
321 
328  void print(std::string outfile = "outfile", const char *extra = nullptr) const;
329 
331  void copy(const IntVector *rhs);
332 
334  void copy(const IntVector &rhs);
335 };
336 
337 } // namespace psi
std::string name() const
Definition: vector.h:188
void set(int m, double val)
Sets a single element value.
Definition: vector.h:119
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:312
void add(int h, int m, double val)
Definition: vector.h:129
Dimension dimpi_
Dimensions per irrep.
Definition: vector.h:53
void set(int h, int m, double val)
Sets a single element value.
Definition: vector.h:125
std::vector< double * > vector_
Pointer offsets into v_, of size dimpi_.n()
Definition: vector.h:49
Definition: matrixtmp.h:42
int nirrep() const
Returns the number of irreps.
Definition: vector.h:176
std::vector< int > numpy_shape()
Definition: vector.h:227
Definition: pointgrp.h:104
double & operator()(int i)
Definition: vector.h:161
std::shared_ptr< PsiOutStream > outfile
Definition: core.cc:106
int nirrep_
Number of irreps.
Definition: vector.h:244
Slicing for Matrices and Vectors objects.
Definition: dimension.h:125
double * pointer(int h=0)
Returns a pointer to irrep h.
Definition: vector.h:111
const double * pointer(int h=0) const
Definition: vector.h:113
int ** vector_
IntVector data.
Definition: vector.h:242
std::string name_
Name.
Definition: vector.h:55
const Dimension & dimpi() const
Returns the dimension array.
Definition: vector.h:173
void set_numpy_shape(std::vector< int > shape)
Definition: vector.h:226
int * dimpi_
Dimensions per irrep.
Definition: vector.h:246
void print_out()
Python compatible printer.
Definition: vector.h:320
std::vector< double > v_
Actual data, of size dimpi_.sum()
Definition: vector.h:47
double & operator[](int i)
Definition: vector.h:165
void add(int m, double val)
Definition: vector.h:127
void set_name(const std::string &name)
Definition: vector.h:183
Definition: dimension.h:40
Makes using matrices just a little easier.
Definition: libmints/matrix.h:112
std::string name_
Name of the IntVector.
Definition: vector.h:248
const double & operator[](int i) const
Definition: vector.h:167
int dim(int h=0) const
Returns the dimension per irrep h.
Definition: vector.h:170
void set(int h, int m, int val)
Sets a single element value.
Definition: vector.h:293
const double & operator()(int i) const
Definition: vector.h:163
#define PSI_API
Definition: pragma.h:155
int * pointer(int h=0)
Returns a pointer to irrep h.
Definition: vector.h:287
void print_out()
Python compatible printer.
Definition: vector.h:191
std::string name() const
Definition: vector.h:317
int nirrep() const
Returns the number of irreps.
Definition: vector.h:305
std::vector< int > numpy_shape_
Numpy Shape.
Definition: vector.h:70
int dim(int h=0) const
Returns the dimension per irrep h.
Definition: vector.h:299
PSI_DEPRECATED("Using `Vector::create` instead of `auto my_vec = std::make_shared<Vector>(name, dim);` ""is deprecated, and in 1.4 it will ""stop working") static SharedVector create(const std
Definition: vector.h:229
Definition: vector.h:239
int nirrep_
Number of irreps.
Definition: vector.h:51
int * dimpi() const
Returns the dimension array.
Definition: vector.h:302
Definition: vector.h:44
std::shared_ptr< Vector > SharedVector
Definition: adc.h:51