ergo
template_lapack_potf2.h
Go to the documentation of this file.
1 /* Ergo, version 3.8, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2019 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4  * and Anastasia Kruchinina.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Primary academic reference:
20  * Ergo: An open-source program for linear-scaling electronic structure
21  * calculations,
22  * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23  * Kruchinina,
24  * SoftwareX 7, 107 (2018),
25  * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26  *
27  * For further information about Ergo, see <http://www.ergoscf.org>.
28  */
29 
30  /* This file belongs to the template_lapack part of the Ergo source
31  * code. The source files in the template_lapack directory are modified
32  * versions of files originally distributed as CLAPACK, see the
33  * Copyright/license notice in the file template_lapack/COPYING.
34  */
35 
36 
37 #ifndef TEMPLATE_LAPACK_POTF2_HEADER
38 #define TEMPLATE_LAPACK_POTF2_HEADER
39 
40 
41 template<class Treal>
42 int template_lapack_potf2(const char *uplo, const integer *n, Treal *a, const integer *
43  lda, integer *info)
44 {
45 /* -- LAPACK routine (version 3.0) --
46  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
47  Courant Institute, Argonne National Lab, and Rice University
48  February 29, 1992
49 
50 
51  Purpose
52  =======
53 
54  DPOTF2 computes the Cholesky factorization of a real symmetric
55  positive definite matrix A.
56 
57  The factorization has the form
58  A = U' * U , if UPLO = 'U', or
59  A = L * L', if UPLO = 'L',
60  where U is an upper triangular matrix and L is lower triangular.
61 
62  This is the unblocked version of the algorithm, calling Level 2 BLAS.
63 
64  Arguments
65  =========
66 
67  UPLO (input) CHARACTER*1
68  Specifies whether the upper or lower triangular part of the
69  symmetric matrix A is stored.
70  = 'U': Upper triangular
71  = 'L': Lower triangular
72 
73  N (input) INTEGER
74  The order of the matrix A. N >= 0.
75 
76  A (input/output) DOUBLE PRECISION array, dimension (LDA,N)
77  On entry, the symmetric matrix A. If UPLO = 'U', the leading
78  n by n upper triangular part of A contains the upper
79  triangular part of the matrix A, and the strictly lower
80  triangular part of A is not referenced. If UPLO = 'L', the
81  leading n by n lower triangular part of A contains the lower
82  triangular part of the matrix A, and the strictly upper
83  triangular part of A is not referenced.
84 
85  On exit, if INFO = 0, the factor U or L from the Cholesky
86  factorization A = U'*U or A = L*L'.
87 
88  LDA (input) INTEGER
89  The leading dimension of the array A. LDA >= max(1,N).
90 
91  INFO (output) INTEGER
92  = 0: successful exit
93  < 0: if INFO = -k, the k-th argument had an illegal value
94  > 0: if INFO = k, the leading minor of order k is not
95  positive definite, and the factorization could not be
96  completed.
97 
98  =====================================================================
99 
100 
101  Test the input parameters.
102 
103  Parameter adjustments */
104  /* Table of constant values */
105  integer c__1 = 1;
106  Treal c_b10 = -1.;
107  Treal c_b12 = 1.;
108 
109  /* System generated locals */
110  integer a_dim1, a_offset, i__1, i__2, i__3;
111  Treal d__1;
112  /* Local variables */
113  integer j;
114  logical upper;
115  Treal ajj;
116 #define a_ref(a_1,a_2) a[(a_2)*a_dim1 + a_1]
117 
118 
119  a_dim1 = *lda;
120  a_offset = 1 + a_dim1 * 1;
121  a -= a_offset;
122 
123  /* Function Body */
124  *info = 0;
125  upper = template_blas_lsame(uplo, "U");
126  if (! upper && ! template_blas_lsame(uplo, "L")) {
127  *info = -1;
128  } else if (*n < 0) {
129  *info = -2;
130  } else if (*lda < maxMACRO(1,*n)) {
131  *info = -4;
132  }
133  if (*info != 0) {
134  i__1 = -(*info);
135  template_blas_erbla("POTF2 ", &i__1);
136  return 0;
137  }
138 
139 /* Quick return if possible */
140 
141  if (*n == 0) {
142  return 0;
143  }
144 
145  if (upper) {
146 
147 /* Compute the Cholesky factorization A = U'*U. */
148 
149  i__1 = *n;
150  for (j = 1; j <= i__1; ++j) {
151 
152 /* Compute U(J,J) and test for non-positive-definiteness. */
153 
154  i__2 = j - 1;
155  ajj = a_ref(j, j) - template_blas_dot(&i__2, &a_ref(1, j), &c__1, &a_ref(1, j)
156  , &c__1);
157  if (ajj <= 0.) {
158  a_ref(j, j) = ajj;
159  goto L30;
160  }
161  ajj = template_blas_sqrt(ajj);
162  a_ref(j, j) = ajj;
163 
164 /* Compute elements J+1:N of row J. */
165 
166  if (j < *n) {
167  i__2 = j - 1;
168  i__3 = *n - j;
169  template_blas_gemv("Transpose", &i__2, &i__3, &c_b10, &a_ref(1, j + 1),
170  lda, &a_ref(1, j), &c__1, &c_b12, &a_ref(j, j + 1),
171  lda);
172  i__2 = *n - j;
173  d__1 = 1. / ajj;
174  template_blas_scal(&i__2, &d__1, &a_ref(j, j + 1), lda);
175  }
176 /* L10: */
177  }
178  } else {
179 
180 /* Compute the Cholesky factorization A = L*L'. */
181 
182  i__1 = *n;
183  for (j = 1; j <= i__1; ++j) {
184 
185 /* Compute L(J,J) and test for non-positive-definiteness. */
186 
187  i__2 = j - 1;
188  ajj = a_ref(j, j) - template_blas_dot(&i__2, &a_ref(j, 1), lda, &a_ref(j, 1),
189  lda);
190  if (ajj <= 0.) {
191  a_ref(j, j) = ajj;
192  goto L30;
193  }
194  ajj = template_blas_sqrt(ajj);
195  a_ref(j, j) = ajj;
196 
197 /* Compute elements J+1:N of column J. */
198 
199  if (j < *n) {
200  i__2 = *n - j;
201  i__3 = j - 1;
202  template_blas_gemv("No transpose", &i__2, &i__3, &c_b10, &a_ref(j + 1, 1),
203  lda, &a_ref(j, 1), lda, &c_b12, &a_ref(j + 1, j), &
204  c__1);
205  i__2 = *n - j;
206  d__1 = 1. / ajj;
207  template_blas_scal(&i__2, &d__1, &a_ref(j + 1, j), &c__1);
208  }
209 /* L20: */
210  }
211  }
212  goto L40;
213 
214 L30:
215  *info = j;
216 
217 L40:
218  return 0;
219 
220 /* End of DPOTF2 */
221 
222 } /* dpotf2_ */
223 
224 #undef a_ref
225 
226 
227 #endif
template_blas_sqrt
Treal template_blas_sqrt(Treal x)
template_blas_dot
Treal template_blas_dot(const integer *n, const Treal *dx, const integer *incx, const Treal *dy, const integer *incy)
Definition: template_blas_dot.h:43
template_blas_scal
int template_blas_scal(const integer *n, const Treal *da, Treal *dx, const integer *incx)
Definition: template_blas_scal.h:43
logical
bool logical
Definition: template_blas_common.h:41
template_blas_erbla
int template_blas_erbla(const char *srname, integer *info)
Definition: template_blas_common.cc:146
template_blas_gemv
int template_blas_gemv(const char *trans, const integer *m, const integer *n, const Treal *alpha, const Treal *a, const integer *lda, const Treal *x, const integer *incx, const Treal *beta, Treal *y, const integer *incy)
Definition: template_blas_gemv.h:43
template_blas_lsame
logical template_blas_lsame(const char *ca, const char *cb)
Definition: template_blas_common.cc:46
template_lapack_potf2
int template_lapack_potf2(const char *uplo, const integer *n, Treal *a, const integer *lda, integer *info)
Definition: template_lapack_potf2.h:42
integer
int integer
Definition: template_blas_common.h:40
maxMACRO
#define maxMACRO(a, b)
Definition: template_blas_common.h:45
a_ref
#define a_ref(a_1, a_2)