File: mat_utils.h

package info (click to toggle)
ergo 3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 17,044 kB
  • ctags: 6,813
  • sloc: cpp: 91,488; ansic: 15,728; sh: 6,416; makefile: 1,287; yacc: 123; lex: 108
file content (179 lines) | stat: -rw-r--r-- 5,500 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Ergo, version 3.5, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2016 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
 * and Anastasia Kruchinina.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Primary academic reference:
 * Kohn−Sham Density Functional Theory Electronic Structure Calculations 
 * with Linearly Scaling Computational Time and Memory Usage,
 * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
 * J. Chem. Theory Comput. 7, 340 (2011),
 * <http://dx.doi.org/10.1021/ct100611z>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */
#ifndef MAT_UTILS_HEADER
#define MAT_UTILS_HEADER
#include "Interval.h"
#include "matrix_proxy.h"
namespace mat {

  template<typename Tmatrix, typename Treal>
    struct DiffMatrix {
      typedef typename Tmatrix::VectorType VectorType;
      void getCols(SizesAndBlocks & colsCopy) const {
	A.getCols(colsCopy);
      }
      int get_nrows() const { 
	assert( A.get_nrows() == B.get_nrows() );
	return A.get_nrows(); 
      }
      Treal frob() const {
	return Tmatrix::frob_diff(A, B);
      }
      void quickEuclBounds(Treal & euclLowerBound, 
			   Treal & euclUpperBound) const {
	Treal frobTmp = frob();
	euclLowerBound = frobTmp  / template_blas_sqrt( (Treal)get_nrows() );
	euclUpperBound = frobTmp;
      }

      Tmatrix const & A;
      Tmatrix const & B;
      DiffMatrix(Tmatrix const & A_, Tmatrix const & B_)
      : A(A_), B(B_) {}
      template<typename Tvector>
      void matVecProd(Tvector & y, Tvector const & x) const {
	Tvector tmp(y);
	tmp = (Treal)-1.0 * B * x;   // -B * x
	y   = (Treal)1.0 * A * x;    // A * x
	y  += (Treal)1.0 * tmp;        // A * x - B * x  => (A - B) * x
      }
    };


  // ATAMatrix AT*A 
  template<typename Tmatrix, typename Treal>
    struct ATAMatrix {
      typedef typename Tmatrix::VectorType VectorType;
      Tmatrix const & A;
      explicit ATAMatrix(Tmatrix const & A_)
      : A(A_) {}
      void getCols(SizesAndBlocks & colsCopy) const {
	A.getRows(colsCopy);
      }
      void quickEuclBounds(Treal & euclLowerBound, 
			   Treal & euclUpperBound) const {
	Treal frobA = A.frob();
	euclLowerBound = 0;
	euclUpperBound = frobA * frobA;
      }
      
      // y = AT*A*x
      template<typename Tvector>
      void matVecProd(Tvector & y, Tvector const & x) const {
	y = x;
	y = A * y;
	y = transpose(A) * y;
      }
      // Number of rows of A^T * A is the number of columns of A 
      int get_nrows() const { return A.get_ncols(); }       
    };


  template<typename Tmatrix, typename Tmatrix2, typename Treal>
    struct TripleMatrix {
      typedef typename Tmatrix::VectorType VectorType;
      void getCols(SizesAndBlocks & colsCopy) const {
	A.getCols(colsCopy);
      }
      int get_nrows() const { 
	assert( A.get_nrows() == Z.get_nrows() );
	return A.get_nrows(); 
      }
      void quickEuclBounds(Treal & euclLowerBound, 
			   Treal & euclUpperBound) const {
	Treal frobA = A.frob();
	Treal frobZ = Z.frob();
	euclLowerBound = 0;
	euclUpperBound = frobA * frobZ * frobZ;
      }
      
      Tmatrix  const & A;
      Tmatrix2 const & Z;
      TripleMatrix(Tmatrix const & A_, Tmatrix2 const & Z_)
      : A(A_), Z(Z_) {}
      void matVecProd(VectorType & y, VectorType const & x) const {
	VectorType tmp(x);
	tmp = Z * tmp;            // Z * x
	y = (Treal)1.0 * A * tmp; // A * Z * x
	y = transpose(Z) * y;     // Z^T * A * Z * x
      }
    };


  template<typename Tmatrix, typename Tmatrix2, typename Treal>
    struct CongrTransErrorMatrix {
      typedef typename Tmatrix::VectorType VectorType;
      void getCols(SizesAndBlocks & colsCopy) const {
	E.getRows(colsCopy);
      }
      int get_nrows() const { 
	return E.get_ncols(); 
      }
      void quickEuclBounds(Treal & euclLowerBound, 
			   Treal & euclUpperBound) const {
	Treal frobA = A.frob();
	Treal frobZ = Zt.frob();
	Treal frobE = E.frob();
	euclLowerBound = 0;
	euclUpperBound = frobA * frobE * frobE + 2 * frobA * frobE * frobZ;
      }
      
      Tmatrix  const & A;
      Tmatrix2 const & Zt;
      Tmatrix2 const & E;
      
      CongrTransErrorMatrix(Tmatrix const & A_, 
			    Tmatrix2 const & Z_,
			    Tmatrix2 const & E_)
      : A(A_), Zt(Z_), E(E_) {}
      void matVecProd(VectorType & y, VectorType const & x) const {
	
	VectorType tmp(x);
	tmp = E * tmp;               // E * x
	y = (Treal)-1.0 * A * tmp;   // -A * E * x
	y = transpose(E) * y;        // -E^T * A * E * x
	
	VectorType tmp1;
	tmp = x;
	tmp = Zt * tmp;              // Zt * x
	tmp1 = (Treal)1.0 * A * tmp; // A * Zt * x
	tmp1 = transpose(E) * tmp1;  // E^T * A * Zt * x
	y += (Treal)1.0 * tmp1;

	tmp = x;
	tmp = E * tmp;               // E * x
	tmp1 = (Treal)1.0 * A * tmp; // A * E * x
	tmp1 = transpose(Zt) * tmp1; // Zt^T * A * E * x
	y += (Treal)1.0 * tmp1;	
      }
    };



}  /* end namespace mat */
#endif