ergo
TestMatrix.h
Go to the documentation of this file.
1 /* Ergo, version 3.4, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2014 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 
28 #include "matInclude.h"
29 #include "mat_gblas.h"
30 #include "matrix_proxy.h"
31 #include "Interval.h"
32 template<typename Treal>
33 class TestMatrix {
34  public:
35  typedef Treal real;
36  TestMatrix() :elements(0),n(0) {}
37  ~TestMatrix() {delete[] elements;}
38  TestMatrix(TestMatrix<real> const & other);
40  TestMatrix(int const n, real const * const elem);
41  void get_diag(real * elem) const;
42  real thresh(real chosen_thresh, mat::normType normTruncation);
43  TestMatrix<real>& operator*=(real const alpha);
44  void add_identity(real const alpha);
47  real trace() const;
49  TestMatrix<real> const & B,
50  mat::normType const norm,
51  real const reqAcc,
52  real const maxAbsVal );
53  static real mixed_diff( TestMatrix<real> const & A,
54  TestMatrix<real> const & B,
55  real const reqAcc );
56 
57  void transfer(TestMatrix<real> & dest);
58 
59 
60  real min() const {
61  real min_val = elements[0];
62  for (int ind = 1; ind < n; ++ind) {
63  min_val = min_val < elements[ind] ? min_val : elements[ind];
64  }
65  return min_val;
66  }
67  real max() const {
68  real max_val = elements[0];
69  for (int ind = 1; ind < n; ++ind) {
70  max_val = max_val > elements[ind] ? max_val : elements[ind];
71  }
72  return max_val;
73  }
74 
75  size_t nnz() const {return n;}
76 
77  int get_nrows() const {return n;}
78  private:
79  real * elements; // Diagonal matrix, length of elements vector: n
80  int n;
81 };
82 
83 template<typename Treal>
85 :n(other.n)
86 {
87  elements = new real[n];
88  for (int i = 0; i < n; ++i)
89  elements[i] = other.elements[i];
90 }
91 
92 template<typename Treal>
94  delete[] elements;
95  n = other.n;
96  elements = new real[n];
97  for (int i = 0; i < n; ++i)
98  elements[i] = other.elements[i];
99 }
100 
101 template<typename Treal>
102 TestMatrix<Treal>::TestMatrix(int const n, real const * const elem)
103 :n(n) {
104  elements = new real[n];
105  for (int i = 0; i < n; ++i)
106  elements[i] = elem[i];
107 }
108 
109 template<typename Treal>
110 void TestMatrix<Treal>::get_diag(real * elem) const {
111  for (int i = 0; i < n; ++i)
112  elem[i] = elements[i];
113 }
114 
115 
116 template<typename Treal>
117 Treal TestMatrix<Treal>::thresh(real chosen_thresh,
118  mat::normType normTruncation) {
119  for (int ind = 0; ind < n; ind++)
120  elements[ind] += chosen_thresh;
121  return chosen_thresh;
122 }
123 
124 template<typename Treal>
126  for (int ind = 0; ind < n; ind++)
127  elements[ind] *= alpha;
128  return *this;
129 }
130 
131 template<typename Treal>
133  for (int ind = 0; ind < n; ind++)
134  elements[ind] += alpha;
135 }
136 
137 template<typename Treal>
139  assert(this != &sm2.B);
140  assert(this != &sm2.C);
141  delete[] elements;
142  n = sm2.B.n;
143  elements = new real[n];
144  for (int ind = 0; ind < n; ind++)
145  elements[ind] = sm2.A * sm2.B.elements[ind] * sm2.C.elements[ind];
146  return *this;
147 }
148 
149 template<typename Treal>
152  for (int ind = 0; ind < n; ind++)
153  elements[ind] += sm.A * sm.B.elements[ind];
154  return *this;
155 }
156 
157 template<typename Treal>
159  real tr = 0;
160  for (int ind = 0; ind < n; ind++)
161  tr += elements[ind];
162  return tr;
163 }
164 
165 template<typename Treal>
168  TestMatrix<Treal> const & B,
169  mat::normType const norm,
170  real const reqAcc,
171  real const maxAbsVal ) {
172  real diff = 0;
173  for (int ind = 0; ind < A.n; ind++) {
174  real tmp_val = fabs( A.elements[ind] - B.elements[ind] );
175  diff = diff > tmp_val ? diff : tmp_val;
176  }
177  return mat::Interval<Treal>(diff,diff);
178 }
179 
180 template<typename Treal>
182  TestMatrix<Treal> const & B,
183  real const reqAcc ) {
184  real diff = 0;
185  for (int ind = 0; ind < A.n; ind++) {
186  real tmp_val = fabs( A.elements[ind] - B.elements[ind] );
187  diff = diff > tmp_val ? diff : tmp_val;
188  }
189  return diff;
190 }
191 
192 
193 template<typename Treal>
196  delete[] dest.elements;
197  dest.n = n;
198  dest.elements = elements;
199  elements = 0;
200  n = 0;
201 }
#define A
~TestMatrix()
Definition: TestMatrix.h:37
void get_diag(real *elem) const
Definition: TestMatrix.h:110
real max() const
Definition: TestMatrix.h:67
Proxy structs used by the matrix API.
void add_identity(real const alpha)
Definition: TestMatrix.h:132
int n
Definition: TestMatrix.h:80
size_t nnz() const
Definition: TestMatrix.h:75
TestMatrix< real > & operator+=(mat::XY< real, TestMatrix< real > > const &sm)
Definition: TestMatrix.h:151
TestMatrix< real > & operator*=(real const alpha)
Definition: TestMatrix.h:125
real * elements
Definition: TestMatrix.h:79
C++ interface to a subset of BLAS and LAPACK.
This proxy expresses the result of multiplication of three objects, of possibly different types...
Definition: matrix_proxy.h:65
static real mixed_diff(TestMatrix< real > const &A, TestMatrix< real > const &B, real const reqAcc)
Definition: TestMatrix.h:181
Treal real
Definition: TestMatrix.h:35
Definition: TestMatrix.h:33
static mat::Interval< real > diffIfSmall(TestMatrix< real > const &A, TestMatrix< real > const &B, mat::normType const norm, real const reqAcc, real const maxAbsVal)
Definition: TestMatrix.h:167
int get_nrows() const
Definition: TestMatrix.h:77
TestMatrix< real > & operator=(TestMatrix< real > const &other)
Definition: TestMatrix.h:93
Copyright(c) Emanuel Rubensson 2006.
TestMatrix()
Definition: TestMatrix.h:36
real thresh(real chosen_thresh, mat::normType normTruncation)
Definition: TestMatrix.h:117
This proxy expresses the result of multiplication of two objects, of possibly different types...
Definition: matrix_proxy.h:49
#define B
Interval class.
real min() const
Definition: TestMatrix.h:60
real trace() const
Definition: TestMatrix.h:158
void transfer(TestMatrix< real > &dest)
Definition: TestMatrix.h:195
normType
Definition: matInclude.h:135