HPhi++  3.1.0
matrixlapack.cpp
Go to the documentation of this file.
1 /* HPhi - Quantum Lattice Model Simulator */
2 /* Copyright (C) 2015 The University of Tokyo */
3 
4 /* This program is free software: you can redistribute it and/or modify */
5 /* it under the terms of the GNU General Public License as published by */
6 /* the Free Software Foundation, either version 3 of the License, or */
7 /* (at your option) any later version. */
8 
9 /* This program is distributed in the hope that it will be useful, */
10 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
12 /* GNU General Public License for more details. */
13 
14 /* You should have received a copy of the GNU General Public License */
15 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
16 /*-------------------------------------------------------------
17  *[ver.2009.05.25]
18  *-------------------------------------------------------------
19  * Copyright (C) 2007-2009 Daisuke Tahara. All rights reserved.
20  * Copyright (C) 2009- Takahiro Misawa. All rights reserved.
21  * Some functions are added by TM.
22  *-------------------------------------------------------------*/
23 /*=================================================================================================*/
24 
25 #include "matrixlapack.hpp"
26 #include <cstdlib>
37 extern "C" {
38 #ifdef SR
39  extern int dsyevd_(char *jobz, char *uplo, int *n, double *a, int *lda, double *w, double *work, int *lwork, int *iwork, int *liwork, int *info);
40  extern int zheevd_(char *jobz, char *uplo, int *n, std::complex<double> *a, int *lda, double *w, std::complex<double> *work, int *lwork, double *rwork, int *iwork, int *liwork, int *info);
41 #else
42  extern int zheev_(char *jobz, char *uplo, int *n, std::complex<double> *a, int *lda, double *w, std::complex<double> *work, int *lwork, double *rwork, int *info);
43 #endif
44 
45  extern int dsyevx_(char *jobz, char *range, char *uplo, int *n, double *a, int *lda, double *vl, double *vu,
46  int *il, int *iu, double *abstol, int *m, double *w, double *z__, int *ldz,
47  double *work, int *lwork, int *iwork, int *ifail, int *info);
48 }
49 //added by Misawa 130121
50 //For complex Hermite matrix
63 int ZHEEVall(int xNsize, std::complex<double> **A, double *r, std::complex<double> **vec) {
64 
65  int i, j, k;
66  char jobz, uplo;
67  int n, lda, lwork, info, lrwork;
68  double *rwork;
69  double *w;
70  std::complex<double> *a, *work;
71 #ifdef SR
72  int *iwork, liwork;
73  liwork = 5 * xNsize + 3;
74  iwork = (int*)malloc(liwork * sizeof(double));
75 #endif
76 
77  n = lda = xNsize;
78 #ifdef SR
79  lwork = xNsize * xNsize + 2 * xNsize; /* 3*xNsize OK?*/
80  lrwork = 2 * xNsize*xNsize + 5 * xNsize + 1;
81 #else
82  lwork = 4 * xNsize; /* 3*xNsize OK?*/
83  lrwork = lwork;
84 #endif
85 
86  a = (std::complex<double>*)malloc(xNsize*xNsize * sizeof(std::complex<double>));
87  w = (double*)malloc(xNsize * sizeof(double));
88  work = (std::complex<double>*)malloc(lwork * sizeof(std::complex<double>));
89  rwork = (double*)malloc(lrwork * sizeof(double));
90 
91  k = 0;
92  for (j = 0; j < xNsize; j++) {
93  for (i = 0; i < xNsize; i++) {
94  a[k] = A[i][j];
95  k++;
96  }
97  }
98 
99  jobz = 'V';
100  uplo = 'U';
101 
102 #ifdef SR
103  int zheevd_(char *jobz, char *uplo, int *n, std::complex<double> *a, int *lda, double *w, std::complex<double> *work, int *lwork, double *rwork, int *iwork, int *liwork, int *info);
104  free(iwork);
105 #else
106  zheev_(&jobz, &uplo, &n, a, &lda, w, work, &lwork, rwork, &info);
107 #endif
108 
109  if (info != 0) {
110  free(a);
111  free(w);
112  free(work);
113  free(rwork);
114  return 0;
115  }
116 
117  k = 0;
118  for (i = 0; i < xNsize; i++) {
119  for (j = 0; j < xNsize; j++) {
120  vec[j][i] = a[k];
121  k++;
122  }
123  }
124 
125 
126  for (k = 0; k < xNsize; k++) {
127  r[k] = w[k];
128  }
129 
130  free(a);
131  free(w);
132  free(work);
133  free(rwork);
134 
135  return 1;
136 }
int ZHEEVall(int xNsize, std::complex< double > **A, double *r, std::complex< double > **vec)
obtain eigenvalues and eigenvectors of Hermite matrix A
int zheev_(char *jobz, char *uplo, int *n, std::complex< double > *a, int *lda, double *w, std::complex< double > *work, int *lwork, double *rwork, int *info)
int dsyevx_(char *jobz, char *range, char *uplo, int *n, double *a, int *lda, double *vl, double *vu, int *il, int *iu, double *abstol, int *m, double *w, double *z__, int *ldz, double *work, int *lwork, int *iwork, int *ifail, int *info)
void zheevd_(char *jobz, char *uplo, int *n, std::complex< double > *a, int *lda, double *w, std::complex< double > *work, int *lwork, double *rwork, int *lrwork, int *iwork, int *liwork, int *info)