-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmathutil.c
214 lines (197 loc) · 5.91 KB
/
mathutil.c
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
205
206
207
208
209
210
211
212
213
214
/* File: mathutil.c */
/*
* mathutil: math functions, some interfacing with LAPACK
* -------------------------------------------------------------------
* compute_vector_norm: compute norm of a vector
* diagmat_dsyevr: diagonalizes a square matrix using dsyevr
* dot_product: computes dot product of two vectors.
* matmul_dgemm: perform A_ij B_jk = C_ik with dgemm
* orthonormalize_vector: orthonormalize vector to space
*
* By Christopher L Malbon
* Dept of Chemistry, The Johns Hopkins University
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "arrayutil.h"
#include "mathutil.h"
/*
* fortran subroutines
*/
extern double _dlamch_fcn_();
/*
* compute_3d_distance: compute eucliden distance of two points.
*/
double compute_3d_distance(double *v, double *u)
{
double dist = 0.0;
double dvec[3];
int i;
init_dbl_array_0(dvec, 3);
for (i = 0; i < 3; i++) {
dvec[i] = v[i] - u[i];
}
dist = compute_vector_norm(dvec, 3);
return dist;
}
/*
* compute_vector_norm: compute euclidean norm of vector.
*/
double compute_vector_norm(double *v, int d)
{
double nrm, dp = 0.0;
dp = dot_product(v, v, d);
nrm = sqrt(dp);
return nrm;
}
/*
* diagmat_dsyevr: diagonalizes a square matrix using the dsyevr
* LAPACK subroutine
*/
int diagmat_dsyevr(double *mat, int dim, double *evecs, double *evals)
{
unsigned char jobz[1] = "v"; /* return eigenvectors and eigenvalues */
unsigned char range[1] = "a"; /* compute all eigenvectors */
unsigned char uplo[1] = "u"; /* upper triangle stored */
long long int n; /* order of matrix */
long long int lda; /* leading dimension of a */
double vl, vu; /* lower and upper bounds of eigenvalue
* interval */
double il, iu; /* indices of smallest and largest
* eigenvalues to find */
double abstol; /* absolute error tolerance */
long long int m; /* total number of eigenvalues found */
long long int ldz; /* leading dimension of eigenvector array */
long long int *isuppz; /* support array */
double *work; /* workspace array */
long long int lwork; /* dimension of workspace array */
long long int *iwork; /* integer workspace array */
long long int liwork; /* dimension of integer workspace array */
long long int *info; /* information */
int err = 0;
/* set size */
n = dim;
lda = dim;
vl = 1;
vu = dim;
il = 1;
iu = dim;
ldz= dim;
abstol = dlamch_fcn_();
lwork = 30 * dim + 10;
liwork = 15 * dim;
info = 0;
isuppz = (long long int *) malloc(2 * dim * sizeof(long long int));
work = (double *) malloc(lwork * sizeof(double));
iwork = (long long int *) malloc(liwork * sizeof(double));
dsyevr_(&jobz, &range, &uplo, &n, mat, &lda, &vl, &vu, &il, &iu,
&abstol, &m, evals, evecs, &ldz, isuppz, work, &lwork,
iwork, &liwork, &info);
free(isuppz);
free(work);
free(iwork);
if (info != 0) {
fprintf(stderr,"*** ERROR in DSYEVR_! info = %lld ***\n", *info);
err = -1;
}
return err;
}
/*
* dot_product: compute the dot product of two vectors
*/
double dot_product(double *u, double *v, int d)
{
int i;
double dp = 0.0;
for (i = 0; i < d; i++) {
dp += u[i] * v[i];
}
return dp;
}
/*
* gauss_fcn: gaussian function. exp(-1*alpha*x^2)
*/
double gauss_fcn(double alpha, double x)
{
double result = 0.0;
double negone = -1.0;
double coeff;
coeff = negone * alpha * pow(x, 2);
result = exp(coeff);
return result;
}
/*
* matmul_dgemm: matrix multiply two arrays with fortran routine DGEMM
*/
int matmul_dgemm(double *mata, int row_a, int col_a, double *matb, int row_b,
int col_b, double *matc, int row_c, int col_c)
{
int error = 0; /* error flag */
/* DGEMM variables */
unsigned char transa[1] = "n"; /* Do not use transpose of matrix A */
unsigned char transb[1] = "n"; /* Do not use transpose of matrix B */
long long int m, n, k;
long long int lda, ldb, ldc;
double alpha, beta;
/* set dgemm variables */
m = (long long int) row_a;
n = (long long int) col_c;
k = (long long int) row_b;
alpha = 1.0;
beta = 0.0;
lda = row_a;
ldb = row_b;
ldc = row_c;
dgemm_(&transa, &transb, &m, &n, &k, &alpha, mata, &lda, matb, &ldb, &beta,
matc, &ldc);
return error;
}
/*
* orthonormalize_vector: orthogonalize and normalize new vector to space
* of basis vectors.
*/
void orthonormalize_vector(double **bvecs, int nbv, int vlen, double *nvec)
{
double nvnorm; /* norm of orthogonalized vector */
double ovrlp; /* overlap of new vector with basis vectors */
int i, j;
/* Loop over space removing the overlap of the new vector with each basis
* vector. */
for (i = 0; i < nbv; i++) {
ovrlp = dot_product(bvecs[i], nvec, vlen);
for (j = 0; j < vlen; j++) {
nvec[j] = nvec[j] - ovrlp * bvecs[i][j];
}
}
/* Normalize this vector */
nvnorm = compute_vector_norm(nvec, vlen);
for (i = 0; i < vlen; i++) {
nvec[i] = nvec[i] / nvnorm;
}
/* check orthogonalization */
for (i = 0; i < nbv; i++) {
ovrlp = dot_product(bvecs[i], nvec, vlen);
if (ovrlp > 0.000001) {
printf("*** Warning: ovrlp = %15.8lf ***\n", ovrlp);
}
}
/* check norm */
nvnorm = compute_vector_norm(nvec, vlen);
if (pow(nvnorm - 1.0, 2) > 0.0000001) {
printf("*** Warning: nvnorm = %15.8lf ***\n", nvnorm);
}
return;
}
/*
* vector_difference: compute the difference between two vectors of length n.
*/
void vector_difference(double *v1, double *v2, int n, double *v3)
{
int i;
init_dbl_array_0(v3, n);
for (i = 0; i < n; i++) {
v3[i] = v1[i] - v2[i];
}
return;
}