ergo
template_blas_nrm2.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  /* 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_BLAS_NRM2_HEADER
36 #define TEMPLATE_BLAS_NRM2_HEADER
37 
38 
39 template<class Treal>
40 Treal template_blas_nrm2(const integer *n, const Treal *x, const integer *incx)
41 {
42 /* The following loop is equivalent to this call to the LAPACK
43  auxiliary routine:
44  CALL DLASSQ( N, X, INCX, SCALE, SSQ ) */
45  /* System generated locals */
46  integer i__1, i__2;
47  Treal ret_val, d__1;
48  /* Local variables */
49  Treal norm, scale, absxi;
50  integer ix;
51  Treal ssq;
52 /* DNRM2 returns the euclidean norm of a vector via the function
53  name, so that
54  DNRM2 := sqrt( x'*x )
55  -- This version written on 25-October-1982.
56  Modified on 14-October-1993 to inline the call to DLASSQ.
57  Sven Hammarling, Nag Ltd.
58  Parameter adjustments */
59  --x;
60  /* Function Body */
61  if (*n < 1 || *incx < 1) {
62  norm = 0.;
63  } else if (*n == 1) {
64  norm = absMACRO(x[1]);
65  } else {
66  scale = 0.;
67  ssq = 1.;
68 
69 
70  i__1 = (*n - 1) * *incx + 1;
71  i__2 = *incx;
72  for (ix = 1; i__2 < 0 ? ix >= i__1 : ix <= i__1; ix += i__2) {
73  if (x[ix] != 0.) {
74  absxi = (d__1 = x[ix], absMACRO(d__1));
75  if (scale < absxi) {
76 /* Computing 2nd power */
77  d__1 = scale / absxi;
78  ssq = ssq * (d__1 * d__1) + 1.;
79  scale = absxi;
80  } else {
81 /* Computing 2nd power */
82  d__1 = absxi / scale;
83  ssq += d__1 * d__1;
84  }
85  }
86 /* L10: */
87  }
88  norm = scale * template_blas_sqrt(ssq);
89  }
90 
91  ret_val = norm;
92  return ret_val;
93 
94 /* End of DNRM2. */
95 
96 } /* dnrm2_ */
97 
98 #endif
Treal template_blas_nrm2(const integer *n, const Treal *x, const integer *incx)
Definition: template_blas_nrm2.h:40
#define absMACRO(x)
Definition: template_blas_common.h:45
int integer
Definition: template_blas_common.h:38
Treal template_blas_sqrt(Treal x)