File: mat_utils.h

package info (click to toggle)
ergo 3.8.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,568 kB
  • sloc: cpp: 94,763; ansic: 17,785; sh: 10,701; makefile: 1,403; yacc: 127; lex: 116; awk: 23
file content (204 lines) | stat: -rw-r--r-- 6,072 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Ergo, version 3.8.2, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2023 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:
 * Ergo: An open-source program for linear-scaling electronic structure
 * calculations,
 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
 * Kruchinina,
 * SoftwareX 7, 107 (2018),
 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */

/** @file mat_utils.h

    \brief Utilities used by other parts of the hierarchical matrix
    library.
*/

#ifndef MAT_UTILS_HEADER
#define MAT_UTILS_HEADER
#include "Interval.h"
#include "matrix_proxy.h"
#include <random>
namespace mat {

  template<class RandomIt>
  static void
  random_shuffle(RandomIt first, RandomIt last) {
#if 1
    /* Doing this instead of the deprecated std::random_shuffle() --
       see
       https://meetingcpp.com/blog/items/stdrandom_shuffle-is-deprecated.html */
    std::random_device rng;
    std::mt19937 urng(rng());
    std::shuffle(first, last, urng);
#else
    /* Old behavior using deprecated std::random_shuffle() */
    std::random_shuffle(first, last);
#endif
  }

  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