ergo
template_lapack_larft.h
Go to the documentation of this file.
1 /* Ergo, version 3.2, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2012 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  /* This file belongs to the template_lapack part of the Ergo source
29  * code. The source files in the template_lapack directory are modified
30  * versions of files originally distributed as CLAPACK, see the
31  * Copyright/license notice in the file template_lapack/COPYING.
32  */
33 
34 
35 #ifndef TEMPLATE_LAPACK_LARFT_HEADER
36 #define TEMPLATE_LAPACK_LARFT_HEADER
37 
38 
39 template<class Treal>
40 int template_lapack_larft(const char *direct, const char *storev, const integer *n, const integer *
41  k, Treal *v, const integer *ldv, const Treal *tau, Treal *t,
42  const integer *ldt)
43 {
44 /* -- LAPACK auxiliary routine (version 3.0) --
45  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
46  Courant Institute, Argonne National Lab, and Rice University
47  February 29, 1992
48 
49 
50  Purpose
51  =======
52 
53  DLARFT forms the triangular factor T of a real block reflector H
54  of order n, which is defined as a product of k elementary reflectors.
55 
56  If DIRECT = 'F', H = H(1) H(2) . . . H(k) and T is upper triangular;
57 
58  If DIRECT = 'B', H = H(k) . . . H(2) H(1) and T is lower triangular.
59 
60  If STOREV = 'C', the vector which defines the elementary reflector
61  H(i) is stored in the i-th column of the array V, and
62 
63  H = I - V * T * V'
64 
65  If STOREV = 'R', the vector which defines the elementary reflector
66  H(i) is stored in the i-th row of the array V, and
67 
68  H = I - V' * T * V
69 
70  Arguments
71  =========
72 
73  DIRECT (input) CHARACTER*1
74  Specifies the order in which the elementary reflectors are
75  multiplied to form the block reflector:
76  = 'F': H = H(1) H(2) . . . H(k) (Forward)
77  = 'B': H = H(k) . . . H(2) H(1) (Backward)
78 
79  STOREV (input) CHARACTER*1
80  Specifies how the vectors which define the elementary
81  reflectors are stored (see also Further Details):
82  = 'C': columnwise
83  = 'R': rowwise
84 
85  N (input) INTEGER
86  The order of the block reflector H. N >= 0.
87 
88  K (input) INTEGER
89  The order of the triangular factor T (= the number of
90  elementary reflectors). K >= 1.
91 
92  V (input/output) DOUBLE PRECISION array, dimension
93  (LDV,K) if STOREV = 'C'
94  (LDV,N) if STOREV = 'R'
95  The matrix V. See further details.
96 
97  LDV (input) INTEGER
98  The leading dimension of the array V.
99  If STOREV = 'C', LDV >= max(1,N); if STOREV = 'R', LDV >= K.
100 
101  TAU (input) DOUBLE PRECISION array, dimension (K)
102  TAU(i) must contain the scalar factor of the elementary
103  reflector H(i).
104 
105  T (output) DOUBLE PRECISION array, dimension (LDT,K)
106  The k by k triangular factor T of the block reflector.
107  If DIRECT = 'F', T is upper triangular; if DIRECT = 'B', T is
108  lower triangular. The rest of the array is not used.
109 
110  LDT (input) INTEGER
111  The leading dimension of the array T. LDT >= K.
112 
113  Further Details
114  ===============
115 
116  The shape of the matrix V and the storage of the vectors which define
117  the H(i) is best illustrated by the following example with n = 5 and
118  k = 3. The elements equal to 1 are not stored; the corresponding
119  array elements are modified but restored on exit. The rest of the
120  array is not used.
121 
122  DIRECT = 'F' and STOREV = 'C': DIRECT = 'F' and STOREV = 'R':
123 
124  V = ( 1 ) V = ( 1 v1 v1 v1 v1 )
125  ( v1 1 ) ( 1 v2 v2 v2 )
126  ( v1 v2 1 ) ( 1 v3 v3 )
127  ( v1 v2 v3 )
128  ( v1 v2 v3 )
129 
130  DIRECT = 'B' and STOREV = 'C': DIRECT = 'B' and STOREV = 'R':
131 
132  V = ( v1 v2 v3 ) V = ( v1 v1 1 )
133  ( v1 v2 v3 ) ( v2 v2 v2 1 )
134  ( 1 v2 v3 ) ( v3 v3 v3 v3 1 )
135  ( 1 v3 )
136  ( 1 )
137 
138  =====================================================================
139 
140 
141  Quick return if possible
142 
143  Parameter adjustments */
144  /* Table of constant values */
145  integer c__1 = 1;
146  Treal c_b8 = 0.;
147 
148  /* System generated locals */
149  integer t_dim1, t_offset, v_dim1, v_offset, i__1, i__2, i__3;
150  Treal d__1;
151  /* Local variables */
152  integer i__, j;
153  Treal vii;
154 #define t_ref(a_1,a_2) t[(a_2)*t_dim1 + a_1]
155 #define v_ref(a_1,a_2) v[(a_2)*v_dim1 + a_1]
156 
157 
158  v_dim1 = *ldv;
159  v_offset = 1 + v_dim1 * 1;
160  v -= v_offset;
161  --tau;
162  t_dim1 = *ldt;
163  t_offset = 1 + t_dim1 * 1;
164  t -= t_offset;
165 
166  /* Function Body */
167  if (*n == 0) {
168  return 0;
169  }
170 
171  if (template_blas_lsame(direct, "F")) {
172  i__1 = *k;
173  for (i__ = 1; i__ <= i__1; ++i__) {
174  if (tau[i__] == 0.) {
175 
176 /* H(i) = I */
177 
178  i__2 = i__;
179  for (j = 1; j <= i__2; ++j) {
180  t_ref(j, i__) = 0.;
181 /* L10: */
182  }
183  } else {
184 
185 /* general case */
186 
187  vii = v_ref(i__, i__);
188  v_ref(i__, i__) = 1.;
189  if (template_blas_lsame(storev, "C")) {
190 
191 /* T(1:i-1,i) := - tau(i) * V(i:n,1:i-1)' * V(i:n,i) */
192 
193  i__2 = *n - i__ + 1;
194  i__3 = i__ - 1;
195  d__1 = -tau[i__];
196  template_blas_gemv("Transpose", &i__2, &i__3, &d__1, &v_ref(i__, 1),
197  ldv, &v_ref(i__, i__), &c__1, &c_b8, &t_ref(1,
198  i__), &c__1);
199  } else {
200 
201 /* T(1:i-1,i) := - tau(i) * V(1:i-1,i:n) * V(i,i:n)' */
202 
203  i__2 = i__ - 1;
204  i__3 = *n - i__ + 1;
205  d__1 = -tau[i__];
206  template_blas_gemv("No transpose", &i__2, &i__3, &d__1, &v_ref(1, i__)
207  , ldv, &v_ref(i__, i__), ldv, &c_b8, &t_ref(1,
208  i__), &c__1);
209  }
210  v_ref(i__, i__) = vii;
211 
212 /* T(1:i-1,i) := T(1:i-1,1:i-1) * T(1:i-1,i) */
213 
214  i__2 = i__ - 1;
215  template_blas_trmv("Upper", "No transpose", "Non-unit", &i__2, &t[
216  t_offset], ldt, &t_ref(1, i__), &c__1);
217  t_ref(i__, i__) = tau[i__];
218  }
219 /* L20: */
220  }
221  } else {
222  for (i__ = *k; i__ >= 1; --i__) {
223  if (tau[i__] == 0.) {
224 
225 /* H(i) = I */
226 
227  i__1 = *k;
228  for (j = i__; j <= i__1; ++j) {
229  t_ref(j, i__) = 0.;
230 /* L30: */
231  }
232  } else {
233 
234 /* general case */
235 
236  if (i__ < *k) {
237  if (template_blas_lsame(storev, "C")) {
238  vii = v_ref(*n - *k + i__, i__);
239  v_ref(*n - *k + i__, i__) = 1.;
240 
241 /* T(i+1:k,i) :=
242  - tau(i) * V(1:n-k+i,i+1:k)' * V(1:n-k+i,i) */
243 
244  i__1 = *n - *k + i__;
245  i__2 = *k - i__;
246  d__1 = -tau[i__];
247  template_blas_gemv("Transpose", &i__1, &i__2, &d__1, &v_ref(1,
248  i__ + 1), ldv, &v_ref(1, i__), &c__1, &c_b8, &
249  t_ref(i__ + 1, i__), &c__1);
250  v_ref(*n - *k + i__, i__) = vii;
251  } else {
252  vii = v_ref(i__, *n - *k + i__);
253  v_ref(i__, *n - *k + i__) = 1.;
254 
255 /* T(i+1:k,i) :=
256  - tau(i) * V(i+1:k,1:n-k+i) * V(i,1:n-k+i)' */
257 
258  i__1 = *k - i__;
259  i__2 = *n - *k + i__;
260  d__1 = -tau[i__];
261  template_blas_gemv("No transpose", &i__1, &i__2, &d__1, &v_ref(
262  i__ + 1, 1), ldv, &v_ref(i__, 1), ldv, &c_b8,
263  &t_ref(i__ + 1, i__), &c__1);
264  v_ref(i__, *n - *k + i__) = vii;
265  }
266 
267 /* T(i+1:k,i) := T(i+1:k,i+1:k) * T(i+1:k,i) */
268 
269  i__1 = *k - i__;
270  template_blas_trmv("Lower", "No transpose", "Non-unit", &i__1, &t_ref(
271  i__ + 1, i__ + 1), ldt, &t_ref(i__ + 1, i__), &
272  c__1);
273  }
274  t_ref(i__, i__) = tau[i__];
275  }
276 /* L40: */
277  }
278  }
279  return 0;
280 
281 /* End of DLARFT */
282 
283 } /* dlarft_ */
284 
285 #undef v_ref
286 #undef t_ref
287 
288 
289 #endif