ergo
template_lapack_larnv.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_LAPACK_LARNV_HEADER
36 #define TEMPLATE_LAPACK_LARNV_HEADER
37 
38 
39 template<class Treal>
40 int template_lapack_larnv(const integer *idist, integer *iseed, const integer *n,
41  Treal *x)
42 {
43 /* -- LAPACK auxiliary routine (version 3.0) --
44  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
45  Courant Institute, Argonne National Lab, and Rice University
46  September 30, 1994
47 
48 
49  Purpose
50  =======
51 
52  DLARNV returns a vector of n random real numbers from a uniform or
53  normal distribution.
54 
55  Arguments
56  =========
57 
58  IDIST (input) INTEGER
59  Specifies the distribution of the random numbers:
60  = 1: uniform (0,1)
61  = 2: uniform (-1,1)
62  = 3: normal (0,1)
63 
64  ISEED (input/output) INTEGER array, dimension (4)
65  On entry, the seed of the random number generator; the array
66  elements must be between 0 and 4095, and ISEED(4) must be
67  odd.
68  On exit, the seed is updated.
69 
70  N (input) INTEGER
71  The number of random numbers to be generated.
72 
73  X (output) DOUBLE PRECISION array, dimension (N)
74  The generated random numbers.
75 
76  Further Details
77  ===============
78 
79  This routine calls the auxiliary routine DLARUV to generate random
80  real numbers from a uniform (0,1) distribution, in batches of up to
81  128 using vectorisable code. The Box-Muller method is used to
82  transform numbers from a uniform to a normal distribution.
83 
84  =====================================================================
85 
86 
87  Parameter adjustments */
88  /* System generated locals */
89  integer i__1, i__2, i__3;
90  /* Local variables */
91  integer i__;
92  Treal u[128];
93  integer il, iv;
94  integer il2;
95 
96  --x;
97  --iseed;
98 
99  /* Function Body */
100  i__1 = *n;
101  for (iv = 1; iv <= i__1; iv += 64) {
102 /* Computing MIN */
103  i__2 = 64, i__3 = *n - iv + 1;
104  il = minMACRO(i__2,i__3);
105  if (*idist == 3) {
106  il2 = il << 1;
107  } else {
108  il2 = il;
109  }
110 
111 /* Call DLARUV to generate IL2 numbers from a uniform (0,1)
112  distribution (IL2 <= LV) */
113 
114  dlaruv_(&iseed[1], &il2, u);
115 
116  if (*idist == 1) {
117 
118 /* Copy generated numbers */
119 
120  i__2 = il;
121  for (i__ = 1; i__ <= i__2; ++i__) {
122  x[iv + i__ - 1] = u[i__ - 1];
123 /* L10: */
124  }
125  } else if (*idist == 2) {
126 
127 /* Convert generated numbers to uniform (-1,1) distribution */
128 
129  i__2 = il;
130  for (i__ = 1; i__ <= i__2; ++i__) {
131  x[iv + i__ - 1] = u[i__ - 1] * 2. - 1.;
132 /* L20: */
133  }
134  } else if (*idist == 3) {
135 
136 /* Convert generated numbers to normal (0,1) distribution */
137 
138  i__2 = il;
139  for (i__ = 1; i__ <= i__2; ++i__) {
140  x[iv + i__ - 1] = template_blas_sqrt(template_blas_log(u[(i__ << 1) - 2]) * -2.) * cos(u[(
141  i__ << 1) - 1] * 6.2831853071795864769252867663);
142 /* L30: */
143  }
144  }
145 /* L40: */
146  }
147  return 0;
148 
149 /* End of DLARNV */
150 
151 } /* dlarnv_ */
152 
153 #endif
int template_lapack_larnv(const integer *idist, integer *iseed, const integer *n, Treal *x)
Definition: template_lapack_larnv.h:40
int integer
Definition: template_blas_common.h:38
#define minMACRO(a, b)
Definition: template_blas_common.h:44
Treal template_blas_log(Treal x)
Treal template_blas_sqrt(Treal x)
int dlaruv_(integer *iseed, const integer *n, Treal *x)
Definition: template_lapack_laruv.h:40