LA_library/nonclass.cc

1924 lines
63 KiB
C++
Raw Normal View History

2008-02-26 14:55:23 +01:00
/*
LA: linear algebra C++ interface library
Copyright (C) 2008 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2009-10-08 16:01:15 +02:00
//this can be safely included since it contains ifdefs NONCBLAS and NONCLAPACK anyway
2013-11-04 15:56:39 +01:00
#include "la_traits.h"
2005-09-04 21:34:10 +02:00
#include "noncblas.h"
2005-02-18 23:08:15 +01:00
#include "vec.h"
#include "smat.h"
#include "mat.h"
#include "nonclass.h"
2005-02-25 17:26:47 +01:00
#include "qsort.h"
2010-06-25 17:28:19 +02:00
#include "fortran.h"
2005-02-18 23:08:15 +01:00
2009-11-12 22:01:19 +01:00
namespace LA {
2004-03-17 04:07:21 +01:00
#define INSTANTIZE(T) \
template void lawritemat(FILE *file,const T *a,int r,int c,const char *form0, \
int nodim,int modulo, int issym);
INSTANTIZE(double)
2020-01-06 21:50:34 +01:00
INSTANTIZE(std::complex<double>)
2004-03-17 06:34:59 +01:00
INSTANTIZE(int)
2005-09-06 17:55:07 +02:00
INSTANTIZE(short)
2004-03-17 06:34:59 +01:00
INSTANTIZE(char)
2009-11-12 22:01:19 +01:00
INSTANTIZE(long)
INSTANTIZE(long long)
2006-04-01 06:48:01 +02:00
INSTANTIZE(unsigned char)
2009-11-12 22:01:19 +01:00
INSTANTIZE(unsigned short)
2006-09-04 01:31:00 +02:00
INSTANTIZE(unsigned int)
2009-11-12 22:01:19 +01:00
INSTANTIZE(unsigned long)
INSTANTIZE(unsigned long long)
2004-03-17 04:07:21 +01:00
2005-09-10 18:57:41 +02:00
#define EPSDET 1e-300
2004-03-17 04:07:21 +01:00
template <typename T>
void lawritemat(FILE *file,const T *a,int r,int c,const char *form0,
int nodim,int modulo, int issym)
{
int i,j;
const char *f;
/*print out title before %*/
f=form0;
skiptext:
while (*f && *f !='%' ) {fputc(*f++,file);}
if (*f=='%' && f[1]=='%') {
fputc(*f,file); f+=2;
goto skiptext;
}
/* this has to be avoided when const arguments should be allowed *f=0; */
/*use the rest as a format for numbers*/
if (modulo) nodim=0;
if (nodim==2) fprintf(file,"%d %d\n",r,c);
if (nodim==1) fprintf(file,"%d\n",c);
if (modulo) {
int n1, n2, l, m;
char ff[32];
/* prepare integer format for column numbering */
if (sscanf(f+1,"%d",&l) != 1) l=128/modulo;
l -= 2;
m = l/2;
l = l-m;
sprintf(ff,"%%%ds%%3d%%%ds", l, m);
n1 = 1;
while(n1 <= c) {
n2=n1+modulo-1;
if (n2 > c) n2 = c;
/*write block between columns n1 and n2 */
fprintf(file,"\n ");
for (i=n1; i<=n2; i++) fprintf(file,ff," ",i," ");
fprintf(file,"\n\n");
for (i=1; i<=r; i++) {
fprintf(file, "%3d ", i);
for (j=n1; j<=n2; j++) {
if(issym) {
int ii,jj;
if (i >= j) {
ii=i;
jj=j;
} else {
ii=j;
jj=i;
}
2020-01-06 21:50:34 +01:00
fprintf(file, f, ((std::complex<double>)a[ii*(ii+1)/2+jj]).real(), ((std::complex<double>)a[ii*(ii+1)/2+jj]).imag());
} else fprintf(file, f, ((std::complex<double>)a[(i-1)*c+j-1]).real(), ((std::complex<double>)a[(i-1)*c+j-1]).imag());
2004-03-17 04:07:21 +01:00
if (j < n2) fputc(' ',file);
}
fprintf(file, "\n");
}
n1 = n2+1;
}
} else {
for (i=1; i<=r; i++) {
for (j=1; j<=c; j++) {
if (issym) {
int ii,jj;
if (i >= j) {
ii=i;
jj=j;
} else {
ii=j;
jj=i;
}
2020-01-06 21:50:34 +01:00
fprintf(file, f, ((std::complex<double>)a[ii*(ii+1)/2+jj]).real(), ((std::complex<double>)a[ii*(ii+1)/2+jj]).imag());
} else fprintf(file,f,((std::complex<double>)a[(i-1)*c+j-1]).real(), ((std::complex<double>)a[(i-1)*c+j-1]).imag());
2004-03-17 04:07:21 +01:00
putc(j<c?' ':'\n',file);
}
}
}
}
//////////////////////
// LAPACK interface //
//////////////////////
// A will be overwritten, B will contain the solutions, A is nxn, B is rhs x n
2005-02-17 23:54:27 +01:00
static void linear_solve_do(NRMat<double> &A, double *B, const int nrhs, const int ldb, double *det, int n)
2004-03-17 04:07:21 +01:00
{
int r, *ipiv;
2016-06-28 15:07:54 +02:00
int iswap=0;
2005-02-17 23:54:27 +01:00
2004-03-17 04:07:21 +01:00
2005-02-17 23:54:27 +01:00
if (n==A.nrows() && A.nrows() != A.ncols()) laerror("linear_solve() call for non-square matrix");
2004-03-17 04:07:21 +01:00
A.copyonwrite();
ipiv = new int[A.nrows()];
2005-02-17 23:54:27 +01:00
r = clapack_dgesv(CblasRowMajor, n, nrhs, A[0], A.ncols(), ipiv, B , ldb);
2004-03-17 04:07:21 +01:00
if (r < 0) {
delete[] ipiv;
laerror("illegal argument in lapack_gesv");
}
2005-09-10 18:57:41 +02:00
if (det && r==0) {
*det = 1.;
//take into account some numerical instabilities in dgesv for singular matrices
2009-11-12 22:01:19 +01:00
for (int i=0; i<n; ++i) {double t=A[i][i]; if(!finite(t) || std::abs(t) < EPSDET ) {*det=0.; break;} else *det *=t;}
2016-06-28 15:07:54 +02:00
//find out whether ipiv are numbered from 0 or from 1
int shift=1;
for (int i=0; i<n; ++i) if(ipiv[i]==0) shift=0;
2004-03-17 04:07:21 +01:00
//change sign of det by parity of ipiv permutation
2016-06-28 15:07:54 +02:00
if(*det) for (int i=0; i<n; ++i) if(i+shift != ipiv[i]) {*det = -(*det); ++iswap;}
2004-03-17 04:07:21 +01:00
}
2007-06-22 16:24:55 +02:00
/*
2016-06-28 15:07:54 +02:00
std::cout <<"iswap = "<<iswap<<std::endl;
if(det && r>0) *det = 0;
2009-11-12 22:01:19 +01:00
std::cout <<"ipiv = ";
for (int i=0; i<n; ++i) std::cout <<ipiv[i]<<" ";
std::cout <<std::endl;
2007-06-22 16:24:55 +02:00
*/
2016-06-28 15:07:54 +02:00
2004-03-17 04:07:21 +01:00
delete [] ipiv;
if (r>0 && B) laerror("singular matrix in lapack_gesv");
}
2005-02-17 23:54:27 +01:00
void linear_solve(NRMat<double> &A, NRMat<double> *B, double *det, int n)
2005-02-04 10:58:36 +01:00
{
2005-02-17 23:54:27 +01:00
if(n<=0) n=A.nrows(); //default - whole matrix
if (n==A.nrows() && B && A.nrows() != B->ncols() || B && n>B->ncols() ||n>A.nrows()) laerror("incompatible matrices in linear_solve()");
2005-02-04 10:58:36 +01:00
if(B) B->copyonwrite();
2008-03-02 17:40:22 +01:00
linear_solve_do(A,B?(*B)[0]:NULL,B?B->nrows() : 0, B?B->ncols():A.nrows(), det,n);
2005-02-04 10:58:36 +01:00
}
2004-03-17 04:07:21 +01:00
2005-02-17 23:54:27 +01:00
void linear_solve(NRMat<double> &A, NRVec<double> &B, double *det, int n)
2005-02-04 10:58:36 +01:00
{
2005-02-17 23:54:27 +01:00
if(n<=0) n=A.nrows(); //default - whole matrix
if(n==A.nrows() && A.nrows() != B.size() || n>B.size()||n>A.nrows() ) laerror("incompatible matrices in linear_solve()");
2005-02-04 10:58:36 +01:00
B.copyonwrite();
2008-03-02 17:40:22 +01:00
linear_solve_do(A,&B[0],1,A.nrows(),det,n);
2005-02-04 10:58:36 +01:00
}
// Next routines are not available in clapack, fotran ones will be used with an
2004-03-17 04:07:21 +01:00
// additional swap/transpose of outputs when needed
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dspsv)(const char *UPLO, const FINT *N, const FINT *NRHS,
double *AP, FINT *IPIV, double *B, const FINT *LDB, FINT *INFO);
2004-03-17 04:07:21 +01:00
2005-02-17 23:54:27 +01:00
static void linear_solve_do(NRSMat<double> &a, double *b, const int nrhs, const int ldb, double *det, int n)
2004-03-17 04:07:21 +01:00
{
2010-06-25 17:28:19 +02:00
FINT r, *ipiv;
2004-03-17 04:07:21 +01:00
a.copyonwrite();
2010-06-25 17:28:19 +02:00
ipiv = new FINT[n];
2013-11-04 15:56:39 +01:00
char U = LAPACK_FORTRANCASE('u');
2010-06-25 17:28:19 +02:00
#ifdef FORINT
const FINT ntmp=n;
const FINT nrhstmp=nrhs;
const FINT ldbtmp=ldb;
FORNAME(dspsv)(&U, &ntmp, &nrhstmp, a, ipiv, b, &ldbtmp,&r);
#else
2005-02-04 10:58:36 +01:00
FORNAME(dspsv)(&U, &n, &nrhs, a, ipiv, b, &ldb,&r);
2010-06-25 17:28:19 +02:00
#endif
2004-03-17 04:07:21 +01:00
if (r < 0) {
delete[] ipiv;
laerror("illegal argument in spsv() call of linear_solve()");
}
2005-09-10 18:57:41 +02:00
if (det && r == 0) {
*det = 1.;
2009-11-12 22:01:19 +01:00
for (int i=1; i<n; i++) {double t=a(i,i); if(!finite(t) || std::abs(t) < EPSDET ) {*det=0.; break;} else *det *= t;}
2005-09-04 21:34:10 +02:00
//do not use ipiv, since the permutation matrix occurs twice in the decomposition and signs thus cancel (man dspsv)
2004-03-17 04:07:21 +01:00
}
2005-09-10 18:57:41 +02:00
if (det && r>0) *det = 0;
2004-03-17 04:07:21 +01:00
delete[] ipiv;
if (r > 0 && b) laerror("singular matrix in linear_solve(SMat&, Mat*, double*");
}
2005-02-17 23:54:27 +01:00
void linear_solve(NRSMat<double> &a, NRMat<double> *B, double *det, int n)
2005-02-04 10:58:36 +01:00
{
2005-02-17 23:54:27 +01:00
if(n<=0) n=a.nrows();
if (n==a.nrows() && B && a.nrows() != B->ncols() || B && n>B->ncols() || n>a.nrows())
2005-02-04 10:58:36 +01:00
laerror("incompatible matrices in symmetric linear_solve()");
if (B) B->copyonwrite();
2005-02-17 23:54:27 +01:00
linear_solve_do(a,B?(*B)[0]:NULL,B?B->nrows() : 0, B?B->ncols():a.nrows(),det,n);
2005-02-04 10:58:36 +01:00
}
2005-02-17 23:54:27 +01:00
void linear_solve(NRSMat<double> &a, NRVec<double> &B, double *det, int n)
2005-02-04 10:58:36 +01:00
{
2005-02-17 23:54:27 +01:00
if(n<=0) n=a.nrows();
if (n==a.nrows() && a.nrows()!= B.size() || n>B.size() || n>a.nrows())
2005-02-04 10:58:36 +01:00
laerror("incompatible matrices in symmetric linear_solve()");
B.copyonwrite();
2005-02-17 23:54:27 +01:00
linear_solve_do(a,&B[0],1,a.nrows(),det,n);
2005-02-04 10:58:36 +01:00
}
2005-02-17 23:54:27 +01:00
2011-01-18 15:37:05 +01:00
// Roman, complex version of linear_solve()
extern "C" void FORNAME(zgesv)(const int *N, const int *NRHS, double *A, const int *LDA,
int *IPIV, double *B, const int *LDB, int *INFO);
2020-01-06 21:50:34 +01:00
void linear_solve(NRMat< std::complex<double> > &A, NRMat< std::complex<double> > *B, std::complex<double> *det, int n)
2011-01-18 15:37:05 +01:00
{
int r, *ipiv;
if (A.nrows() != A.ncols()) laerror("linear_solve() call for non-square matrix");
if (B && A.nrows() != B->ncols()) laerror("incompatible matrices in linear_solve()");
A.copyonwrite();
if (B) B->copyonwrite();
ipiv = new int[A.nrows()];
n = A.nrows();
int nrhs = B ? B->nrows() : 0;
int lda = A.ncols();
int ldb = B ? B->ncols() : A.nrows();
FORNAME(zgesv)(&n, &nrhs, (double *)A[0], &lda, ipiv,
B ? (double *)(*B)[0] : (double *)0, &ldb, &r);
if (r < 0) {
delete[] ipiv;
laerror("illegal argument in lapack_gesv");
}
if (det && r>=0) {
*det = A[0][0];
for (int i=1; i<A.nrows(); ++i) *det *= A[i][i];
//change sign of det by parity of ipiv permutation
for (int i=0; i<A.nrows(); ++i) *det = -(*det);
}
delete [] ipiv;
if (r>0 && B) laerror("singular matrix in zgesv");
}
2010-01-17 21:28:38 +01:00
//other version of linear solver based on gesvx
2010-02-25 21:47:01 +01:00
//------------------------------------------------------------------------------
2020-01-06 21:50:34 +01:00
extern "C" void FORNAME(zgesvx)(const char *fact, const char *trans, const FINT *n, const FINT *nrhs, std::complex<double> *A, const FINT *lda, std::complex<double> *AF, const FINT *ldaf, const FINT *ipiv, char *equed, double *R,double *C, std::complex<double> *B, const FINT *ldb, std::complex<double> *X, const FINT *ldx, double *rcond, double *ferr, double *berr, std::complex<double> *work, double *rwork, FINT *info);
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dgesvx)(const char *fact, const char *trans, const FINT *n, const FINT *nrhs, double *A, const FINT *lda, double *AF, const FINT *ldaf, const FINT *ipiv, char *equed, double *R,double *C, double *B, const FINT *ldb, double *X, const FINT *ldx, double *rcond, double *ferr, double *berr, double *work, FINT *iwork, FINT *info);
2010-02-25 21:47:01 +01:00
//------------------------------------------------------------------------------
// solves set of linear equations using dgesvx
// input:
// _A double precision matrix of dimension nn x mm, where min(nn, mm) >= n
// _B double prec. array dimensioned as nrhs x n
// _rhsCount nrhs - count of right hand sides
// _eqCount n - count of equations
// _eq use equilibration of matrix A before solving
// _saveA if set, do no overwrite A if equilibration in effect
// _rcond if not NULL, store the returned rcond value from dgesvx
// output:
// solution is stored in _B
// the info parameter of dgesvx is returned (see man dgesvx)
//------------------------------------------------------------------------------
int linear_solve_x(NRMat<double> &_A, double *_B, const int _rhsCount, const int _eqCount, const bool _eq, const bool _saveA, double *_rcond){
const int A_rows = _A.nrows();
const int A_cols = _A.ncols();
2013-11-04 15:56:39 +01:00
const char fact = LAPACK_FORTRANCASE(_eq?'E':'N');
const char trans = LAPACK_FORTRANCASE('T');//because of c-order
char equed = LAPACK_FORTRANCASE('B');//if fact=='N' then equed is an output argument, therefore not declared as const
2010-02-25 21:47:01 +01:00
if(_eqCount < 0 || _eqCount > A_rows || _eqCount > A_cols || _rhsCount < 0){
laerror("linear_solve_x: invalid input matrices");
}
2010-01-17 21:28:38 +01:00
2010-02-25 21:47:01 +01:00
double *A;
double * const _A_data = (double*)_A;
2010-06-25 17:28:19 +02:00
FINT info;
const FINT nrhs = _rhsCount;
const FINT n = _eqCount;
FINT lda = A_cols;
const FINT ldaf = lda;
2010-02-25 21:47:01 +01:00
double rcond;
double ferr[nrhs], berr[nrhs], work[4*n];
double R[n], C[n];
2010-06-25 17:28:19 +02:00
FINT *const iwork = new FINT[n];
FINT *const ipiv = new FINT[n];
2010-02-25 21:47:01 +01:00
double *X = new double[n*nrhs];
double *AF = new double[ldaf*n];
A = _A_data;
if(_eq){
if(_saveA){//store the corresponding submatrix of _A (not needed provided fact=='N')
A = new double[n*n];
int offset1 = 0;int offset2 = 0;
for(register int i=0;i<n;i++){
cblas_dcopy(n, _A_data + offset1, 1, A + offset2, 1);
offset1 += A_cols;
offset2 += n;
}
lda = n;//!!!
}else{
_A.copyonwrite();
}
}
2010-01-17 21:28:38 +01:00
2010-02-25 21:47:01 +01:00
FORNAME(dgesvx)(&fact, &trans, &n, &nrhs, A, &lda, AF, &ldaf, &ipiv[0], &equed, &R[0], &C[0], _B, &n, X, &n, &rcond, ferr, berr, work, iwork, &info);
2010-06-25 17:28:19 +02:00
2010-02-25 21:47:01 +01:00
if(_rcond)*_rcond = rcond;
cblas_dcopy(n*nrhs, X, 1, _B, 1);//store the solution
2010-01-17 21:28:38 +01:00
2010-02-25 21:47:01 +01:00
delete[] iwork;delete[] ipiv;
delete[] AF;delete[] X;
if(_saveA){
delete[] A;
}
2010-06-25 17:28:19 +02:00
return (int)info;
2010-01-17 21:28:38 +01:00
}
2010-02-25 21:47:01 +01:00
//------------------------------------------------------------------------------
// solves set of linear equations using zgesvx
// input:
// _A double precision complex matrix of dimension nn x mm, where min(nn, mm) >= n
// _B double prec. complex array dimensioned as nrhs x n
// _rhsCount nrhs - count of right hand sides
// _eqCount n - count of equations
// _eq use equilibration
// _saveA if set, do no overwrite A if equilibration in effect
// _rcond if not NULL, store the returned rcond value from dgesvx
// output:
// solution is stored in _B
// the info parameter of dgesvx is returned (see man dgesvx)
//------------------------------------------------------------------------------
2020-01-06 21:50:34 +01:00
int linear_solve_x(NRMat<std::complex<double> > &_A, std::complex<double> *_B, const int _rhsCount, const int _eqCount, const bool _eq, const bool _saveA, double *_rcond){
2010-02-25 21:47:01 +01:00
const int A_rows = _A.nrows();
const int A_cols = _A.ncols();
2013-11-04 15:56:39 +01:00
const char fact = LAPACK_FORTRANCASE(_eq?'E':'N');
const char trans = LAPACK_FORTRANCASE('T');//because of c-order
char equed = LAPACK_FORTRANCASE('B');//if fact=='N' then equed is an output argument, therefore not declared as const
2010-02-25 21:47:01 +01:00
if(_eqCount < 0 || _eqCount > A_rows || _eqCount > A_cols || _rhsCount < 0){
laerror("linear_solve_x: invalid input matrices");
}
2010-01-17 21:28:38 +01:00
2020-01-06 21:50:34 +01:00
std::complex<double> *A;
std::complex<double> * const _A_data = (std::complex<double>*)_A;
2010-02-25 21:47:01 +01:00
2010-06-25 17:28:19 +02:00
FINT info;
const FINT nrhs = _rhsCount;
const FINT n = _eqCount;
FINT lda = A_cols;
const FINT ldaf = lda;
2010-02-25 21:47:01 +01:00
double rcond;
double ferr[nrhs], berr[nrhs];
double R[n], C[n], rwork[2*n];
2020-01-06 21:50:34 +01:00
std::complex<double> work[2*n];
2010-02-25 21:47:01 +01:00
2010-06-25 17:28:19 +02:00
FINT *const ipiv = new FINT[n];
2010-02-25 21:47:01 +01:00
2020-01-06 21:50:34 +01:00
std::complex<double> *X = new std::complex<double>[n*nrhs];
std::complex<double> *AF = new std::complex<double>[ldaf*n];
2010-02-25 21:47:01 +01:00
A = _A_data;
if(_eq){
if(_saveA){//store the corresponding submatrix of _A (not needed provided fact=='N')
2020-01-06 21:50:34 +01:00
A = new std::complex<double>[n*n];
2010-02-25 21:47:01 +01:00
int offset1 = 0;int offset2 = 0;
for(register int i=0;i<n;i++){
cblas_zcopy(n, _A_data + offset1, 1, A + offset2, 1);
offset1 += A_cols;
offset2 += n;
}
lda = n;//!!!
}else{
_A.copyonwrite();
}
}
2010-01-17 21:28:38 +01:00
2010-02-25 21:47:01 +01:00
FORNAME(zgesvx)(&fact, &trans, &n, &nrhs, A, &lda, AF, &ldaf, &ipiv[0], &equed, &R[0], &C[0], _B, &n, X, &n, &rcond, ferr, berr, work, rwork, &info);
2010-01-17 21:28:38 +01:00
2010-02-25 21:47:01 +01:00
if(_rcond)*_rcond = rcond;
cblas_zcopy(n*nrhs, X, 1, _B, 1);//store the solution
delete[] ipiv;
delete[] AF;delete[] X;
if(_saveA){
delete[] A;
}
2010-06-25 17:28:19 +02:00
return (int)info;
2010-02-25 21:47:01 +01:00
}
//------------------------------------------------------------------------------
// for given square matrices A, B computes X = AB^{-1} as follows
// XB = A => B^TX^T = A^T
// input:
// _A double precision matrix of dimension nn x nn
// _B double prec. matrix of dimension nn x nn
// _useEq use equilibration suitable for badly conditioned matrices
// _rcond if not NULL, store the returned value of rcond fromd dgesvx
// output:
// solution is stored in _B
// the info parameter of dgesvx is returned (see man dgesvx)
//------------------------------------------------------------------------------
2020-01-06 21:50:34 +01:00
template<>
int multiply_by_inverse<double>(NRMat<double> &_A, NRMat<double> &_B, bool _useEq, double *_rcond){
2010-02-25 21:47:01 +01:00
2010-06-25 17:28:19 +02:00
const FINT n = _A.nrows();
const FINT m = _A.ncols();
2010-02-25 21:47:01 +01:00
if(n != m || n != _B.nrows() || n != _B.ncols()){
laerror("multiply_by_inverse: incompatible matrices");
}
const char fact = _useEq?'E':'N';
const char trans = 'N';//because of c-order
char equed = 'B';//if fact=='N' then equed is an output argument, therefore not declared as const
const int n2 = n*n;
double * const A = (double*)_A;
double * const B = (double*)_B;
_B.copyonwrite();//even if fact='N', call copyonwrite because the solution is going to be stored in _B
2010-06-25 17:28:19 +02:00
FINT info;
2010-02-25 21:47:01 +01:00
double rcond;
double ferr[n], berr[n], work[4*n];
double R[n], C[n];
2010-06-25 17:28:19 +02:00
FINT *const iwork = new FINT[n];
FINT *const ipiv = new FINT[n];
2010-02-25 21:47:01 +01:00
double *X = new double[n2];
double *AF = new double[n2];
FORNAME(dgesvx)(&fact, &trans, &n, &n, B, &n, AF, &n, &ipiv[0], &equed, &R[0], &C[0], A, &n, X, &n, &rcond, ferr, berr, work, iwork, &info);
2010-01-17 21:28:38 +01:00
2010-02-25 21:47:01 +01:00
if(_rcond)*_rcond = rcond;
cblas_dcopy(n2, X, 1, B, 1);//store the solution
delete[] iwork;delete[] ipiv;
delete[] AF;delete[] X;
2010-06-25 17:28:19 +02:00
return (int)info;
2010-01-17 21:28:38 +01:00
}
2010-02-25 21:47:01 +01:00
//------------------------------------------------------------------------------
// for given square matrices A, B computes X = AB^{-1} as follows
// XB = A => B^TX^T = A^T
// input:
// _A double precision matrix of dimension nn x nn
// _B double prec. matrix of dimension nn x nn
// _useEq use equilibration suitable for badly conditioned matrices
// _rcond if not NULL, store the returned value of rcond fromd zgesvx
// output:
// solution is stored in _B
// the info parameter of zgesvx is returned (see man zgesvx)
//------------------------------------------------------------------------------
2020-01-06 21:50:34 +01:00
template<>
int multiply_by_inverse<std::complex<double> >(NRMat<std::complex<double> > &_A, NRMat<std::complex<double> > &_B, bool _useEq, double *_rcond){
2010-02-25 21:47:01 +01:00
2010-06-25 17:28:19 +02:00
const FINT n = _A.nrows();
const FINT m = _A.ncols();
2010-02-25 21:47:01 +01:00
if(n != m || n != _B.nrows() || n != _B.ncols()){
laerror("multiply_by_inverse: incompatible matrices");
}
const int n2 = n*n;
const char fact = _useEq?'E':'N';
const char trans = 'N';//because of c-order
char equed = 'B';//if fact=='N' then equed is an output argument, therefore not declared as const
2020-01-06 21:50:34 +01:00
std::complex<double> * const A = (std::complex<double>*)_A;
std::complex<double> * const B = (std::complex<double>*)_B;
2010-02-25 21:47:01 +01:00
_B.copyonwrite();//even if fact='N', call copyonwrite because the solution is going to be stored in _B
2010-06-25 17:28:19 +02:00
FINT info;
2010-02-25 21:47:01 +01:00
double rcond;
double ferr[n], berr[n];
double R[n], C[n], rwork[2*n];
2020-01-06 21:50:34 +01:00
std::complex<double> work[2*n];
2010-02-25 21:47:01 +01:00
2010-06-25 17:28:19 +02:00
FINT *const ipiv = new FINT[n];
2010-02-25 21:47:01 +01:00
2020-01-06 21:50:34 +01:00
std::complex<double> *X = new std::complex<double>[n2];
std::complex<double> *AF = new std::complex<double>[n2];
2010-02-25 21:47:01 +01:00
FORNAME(zgesvx)(&fact, &trans, &n, &n, B, &n, AF, &n, &ipiv[0], &equed, &R[0], &C[0], A, &n, X, &n, &rcond, ferr, berr, work, rwork, &info);
2010-01-17 21:28:38 +01:00
2010-02-25 21:47:01 +01:00
if(_rcond)*_rcond = rcond;
cblas_zcopy(n2, X, 1, B, 1);//store the solution
delete[] ipiv;
delete[] AF;delete[] X;
2010-06-25 17:28:19 +02:00
return (int)info;
2010-02-25 21:47:01 +01:00
}
//------------------------------------------------------------------------------
2010-01-17 21:28:38 +01:00
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dsyev)(const char *JOBZ, const char *UPLO, const FINT *N,
double *A, const FINT *LDA, double *W, double *WORK, const FINT *LWORK, FINT *INFO);
2004-03-17 04:07:21 +01:00
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dsygv)(const FINT *ITYPE, const char *JOBZ, const char *UPLO, const FINT *N,
double *A, const FINT *LDA, double *B, const FINT *LDB, double *W, double *WORK, const FINT *LWORK, FINT *INFO);
2005-02-17 23:54:27 +01:00
2005-01-31 00:49:50 +01:00
// a will contain eigenvectors (columns if corder==1), w eigenvalues
2004-03-17 04:07:21 +01:00
void diagonalize(NRMat<double> &a, NRVec<double> &w, const bool eivec,
2005-02-17 23:54:27 +01:00
const bool corder, int n, NRMat<double> *b, const int itype)
2004-03-17 04:07:21 +01:00
{
2010-06-25 17:28:19 +02:00
FINT m = a.nrows();
2005-02-01 00:08:03 +01:00
if (m != a.ncols()) laerror("diagonalize() call with non-square matrix");
2004-03-17 04:07:21 +01:00
if (a.nrows() != w.size())
laerror("inconsistent dimension of eigenvalue vector in diagonalize()");
2005-02-01 00:08:03 +01:00
if(n==0) n=m;
if(n<0||n>m) laerror("actual dimension out of range in diagonalize");
2005-02-17 23:54:27 +01:00
if(b) if(n>b->nrows() || n> b->ncols()) laerror("wrong B matrix dimension in diagonalize");
2004-03-17 04:07:21 +01:00
a.copyonwrite();
w.copyonwrite();
2005-02-17 23:54:27 +01:00
if(b) b->copyonwrite();
2004-03-17 04:07:21 +01:00
2010-06-25 17:28:19 +02:00
FINT r = 0;
2013-11-04 15:56:39 +01:00
char U =LAPACK_FORTRANCASE('u');
char vectors = LAPACK_FORTRANCASE('v');
if (!eivec) vectors = LAPACK_FORTRANCASE('n');
2010-06-25 17:28:19 +02:00
FINT LWORK = -1;
2004-03-17 04:07:21 +01:00
double WORKX;
2010-06-25 17:28:19 +02:00
FINT ldb=0; if(b) ldb=b->ncols();
2004-03-17 04:07:21 +01:00
2010-06-25 17:28:19 +02:00
#ifdef FORINT
const FINT itypetmp = itype;
FINT ntmp = n;
// First call is to determine size of workspace
if(b) FORNAME(dsygv)(&itypetmp,&vectors, &U, &ntmp, a, &m, *b, &ldb, w, &WORKX, &LWORK, &r );
else FORNAME(dsyev)(&vectors, &U, &ntmp, a, &m, w, &WORKX, &LWORK, &r );
#else
2004-03-17 04:07:21 +01:00
// First call is to determine size of workspace
2005-02-25 17:26:47 +01:00
if(b) FORNAME(dsygv)(&itype,&vectors, &U, &n, a, &m, *b, &ldb, w, &WORKX, &LWORK, &r );
else FORNAME(dsyev)(&vectors, &U, &n, a, &m, w, &WORKX, &LWORK, &r );
2010-06-25 17:28:19 +02:00
#endif
LWORK = (FINT)WORKX;
2004-03-17 04:07:21 +01:00
double *WORK = new double[LWORK];
2010-06-25 17:28:19 +02:00
#ifdef FORINT
if(b) FORNAME(dsygv)(&itypetmp,&vectors, &U, &ntmp, a, &m, *b, &ldb, w, &WORKX, &LWORK, &r );
else FORNAME(dsyev)(&vectors, &U, &ntmp, a, &m, w, &WORKX, &LWORK, &r );
#else
if(b) FORNAME(dsygv)(&itype,&vectors, &U, &n, a, &m, *b,&ldb, w, WORK, &LWORK, &r );
2005-02-17 23:54:27 +01:00
else FORNAME(dsyev)(&vectors, &U, &n, a, &m, w, WORK, &LWORK, &r );
2010-06-25 17:28:19 +02:00
#endif
delete[] WORK;
2013-11-04 15:56:39 +01:00
if (LAPACK_FORTRANCASE(vectors) == LAPACK_FORTRANCASE('v') && corder) a.transposeme(n);
2004-03-17 04:07:21 +01:00
2005-02-17 23:54:27 +01:00
if (r < 0) laerror("illegal argument in sygv/syev in diagonalize()");
if (r > 0) laerror("convergence problem in sygv/syev in diagonalize()");
2004-03-17 04:07:21 +01:00
}
2005-02-01 00:08:03 +01:00
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(zheev)(const char *JOBZ, const char *UPLO, const FINT *N,
2020-01-06 21:50:34 +01:00
std::complex<double> *A, const FINT *LDA, double *W, std::complex<double> *WORK, const FINT *LWORK, double *RWORK, FINT *INFO);
2009-05-28 14:40:38 +02:00
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(zhegv)(const FINT *ITYPE, const char *JOBZ, const char *UPLO, const FINT *N,
2020-01-06 21:50:34 +01:00
std::complex<double> *A, const FINT *LDA, std::complex<double> *B, const FINT *LDB, double *W, std::complex<double> *WORK, const FINT *LWORK, double *RWORK, FINT *INFO);
2009-05-28 14:40:38 +02:00
// a will contain eigenvectors (columns if corder==1), w eigenvalues
2020-01-06 21:50:34 +01:00
void diagonalize(NRMat<std::complex<double> > &a, NRVec<double> &w, const bool eivec,
const bool corder, int n, NRMat<std::complex<double> > *b, const int itype)
2009-05-28 14:40:38 +02:00
{
2010-06-25 17:28:19 +02:00
FINT m = a.nrows();
2009-05-28 14:40:38 +02:00
if (m != a.ncols()) laerror("diagonalize() call with non-square matrix");
if (a.nrows() != w.size())
laerror("inconsistent dimension of eigenvalue vector in diagonalize()");
if(n==0) n=m;
if(n<0||n>m) laerror("actual dimension out of range in diagonalize");
if(b) if(n>b->nrows() || n> b->ncols()) laerror("wrong B matrix dimension in diagonalize");
a.copyonwrite();
w.copyonwrite();
if(b) b->copyonwrite();
2010-06-25 17:28:19 +02:00
FINT r = 0;
2013-11-04 15:56:39 +01:00
char U =LAPACK_FORTRANCASE('U');
char vectors = LAPACK_FORTRANCASE('V');
if (!eivec) vectors = LAPACK_FORTRANCASE('n');
2010-06-25 17:28:19 +02:00
FINT LWORK = -1;
2020-01-06 21:50:34 +01:00
std::complex<double> WORKX;
2010-06-25 17:28:19 +02:00
FINT ldb=0; if(b) ldb=b->ncols();
2009-05-28 14:40:38 +02:00
// First call is to determine size of workspace
2009-05-29 12:02:57 +02:00
double *RWORK = new double[3*n+2];
2010-06-25 17:28:19 +02:00
#ifdef FORINT
const FINT itypetmp = itype;
FINT ntmp = n;
if(b) FORNAME(zhegv)(&itypetmp,&vectors, &U, &ntmp, a, &m, *b, &ldb, w, &WORKX, &LWORK, RWORK, &r );
else FORNAME(zheev)(&vectors, &U, &ntmp, a, &m, w, &WORKX, &LWORK, RWORK, &r );
#else
if(b) FORNAME(zhegv)(&itype,&vectors, &U, &n, a, &m, *b, &ldb, w, &WORKX, &LWORK, RWORK, &r );
2009-05-28 16:14:12 +02:00
else FORNAME(zheev)(&vectors, &U, &n, a, &m, w, &WORKX, &LWORK, RWORK, &r );
2010-06-25 17:28:19 +02:00
#endif
LWORK = (FINT)WORKX.real();
2020-01-06 21:50:34 +01:00
std::complex<double> *WORK = new std::complex<double>[LWORK];
2010-06-25 17:28:19 +02:00
#ifdef FORINT
if(b) FORNAME(zhegv)(&itypetmp,&vectors, &U, &ntmp, a, &m, *b, &ldb, w, &WORKX, &LWORK, RWORK, &r );
else FORNAME(zheev)(&vectors, &U, &ntmp, a, &m, w, &WORKX, &LWORK, RWORK, &r );
#else
if(b) FORNAME(zhegv)(&itype,&vectors, &U, &n, a, &m, *b,&ldb, w, WORK, &LWORK, RWORK, &r );
2009-05-28 16:14:12 +02:00
else FORNAME(zheev)(&vectors, &U, &n, a, &m, w, WORK, &LWORK, RWORK, &r );
2010-06-25 17:28:19 +02:00
#endif
delete[] WORK;
2009-05-28 16:14:12 +02:00
delete[] RWORK;
2013-11-04 15:56:39 +01:00
if (LAPACK_FORTRANCASE(vectors) == LAPACK_FORTRANCASE('v') && corder) {a.transposeme(n); a.conjugateme();}
2009-05-28 14:40:38 +02:00
2009-05-28 16:14:12 +02:00
if (r < 0) laerror("illegal argument in hegv/heev in diagonalize()");
if (r > 0) laerror("convergence problem in hegv/heev in diagonalize()");
2009-05-28 14:40:38 +02:00
}
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dspev)(const char *JOBZ, const char *UPLO, const FINT *N,
double *AP, double *W, double *Z, const FINT *LDZ, double *WORK, FINT *INFO);
2004-03-17 04:07:21 +01:00
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dspgv)(const FINT *ITYPE, const char *JOBZ, const char *UPLO, const FINT *N,
double *AP, double *BP, double *W, double *Z, const FINT *LDZ, double *WORK, FINT *INFO);
2005-02-17 23:54:27 +01:00
2004-03-17 04:07:21 +01:00
// v will contain eigenvectors, w eigenvalues
void diagonalize(NRSMat<double> &a, NRVec<double> &w, NRMat<double> *v,
2005-02-17 23:54:27 +01:00
const bool corder, int n, NRSMat<double> *b, const int itype)
2004-03-17 04:07:21 +01:00
{
2005-02-17 23:54:27 +01:00
if(n<=0) n = a.nrows();
if (v) if (v->nrows() != v ->ncols() || n > v->nrows() || n > a.nrows())
2004-03-17 04:07:21 +01:00
laerror("diagonalize() call with inconsistent dimensions");
2005-02-17 23:54:27 +01:00
if (n==a.nrows() && n != w.size() || n>w.size()) laerror("inconsistent dimension of eigenvalue vector");
if(b) if(n>b->nrows() || n> b->ncols()) laerror("wrong B matrix dimension in diagonalize");
2004-03-17 04:07:21 +01:00
a.copyonwrite();
w.copyonwrite();
2005-02-17 23:54:27 +01:00
if(v) v->copyonwrite();
if(b) b->copyonwrite();
2004-03-17 04:07:21 +01:00
2010-06-25 17:28:19 +02:00
FINT r = 0;
2013-11-04 15:56:39 +01:00
char U = LAPACK_FORTRANCASE('u');
char job = LAPACK_FORTRANCASE(v ? 'v' : 'n');
2004-03-17 04:07:21 +01:00
double *WORK = new double[3*n];
2010-06-25 17:28:19 +02:00
FINT ldv=v?v->ncols():n;
#ifdef FORINT
const FINT itypetmp = itype;
FINT ntmp = n;
if(b) FORNAME(dspgv)(&itypetmp,&job, &U, &ntmp, a, *b, w, v?(*v)[0]:(double *)0, &ldv, WORK, &r );
else FORNAME(dspev)(&job, &U, &ntmp, a, w, v?(*v)[0]:(double *)0, &ldv, WORK, &r );
#else
if(b) FORNAME(dspgv)(&itype,&job, &U, &n, a, *b, w, v?(*v)[0]:(double *)0, &ldv, WORK, &r );
2005-02-17 23:54:27 +01:00
else FORNAME(dspev)(&job, &U, &n, a, w, v?(*v)[0]:(double *)0, &ldv, WORK, &r );
2010-06-25 17:28:19 +02:00
#endif
2004-03-17 04:07:21 +01:00
delete[] WORK;
2005-02-17 23:54:27 +01:00
if (v && corder) v->transposeme(n);
2004-03-17 04:07:21 +01:00
2005-02-17 23:54:27 +01:00
if (r < 0) laerror("illegal argument in spgv/spev in diagonalize()");
if (r > 0) laerror("convergence problem in spgv/spev in diagonalize()");
2004-03-17 04:07:21 +01:00
}
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(zhpev)(const char *JOBZ, const char *UPLO, const FINT *N,
2020-01-06 21:50:34 +01:00
std::complex<double> *AP, double *W, std::complex<double> *Z, const FINT *LDZ, std::complex<double> *WORK, double *RWORK, FINT *INFO);
2009-05-28 14:40:38 +02:00
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(zhpgv)(const FINT *ITYPE, const char *JOBZ, const char *UPLO, const FINT *N,
2020-01-06 21:50:34 +01:00
std::complex<double> *AP, std::complex<double> *BP, double *W, std::complex<double> *Z, const FINT *LDZ, std::complex<double> *WORK, double *RWORK, FINT *INFO);
2009-05-28 14:40:38 +02:00
// v will contain eigenvectors, w eigenvalues
2020-01-06 21:50:34 +01:00
void diagonalize(NRSMat<std::complex<double> > &a, NRVec<double> &w, NRMat<std::complex<double> > *v,
const bool corder, int n, NRSMat<std::complex<double> > *b, const int itype)
2009-05-28 14:40:38 +02:00
{
if(n<=0) n = a.nrows();
if (v) if (v->nrows() != v ->ncols() || n > v->nrows() || n > a.nrows())
laerror("diagonalize() call with inconsistent dimensions");
if (n==a.nrows() && n != w.size() || n>w.size()) laerror("inconsistent dimension of eigenvalue vector");
if(b) if(n>b->nrows() || n> b->ncols()) laerror("wrong B matrix dimension in diagonalize");
a.copyonwrite();
w.copyonwrite();
if(v) v->copyonwrite();
if(b) b->copyonwrite();
2010-06-25 17:28:19 +02:00
FINT r = 0;
2013-11-04 15:56:39 +01:00
char U = LAPACK_FORTRANCASE('u');
char job = LAPACK_FORTRANCASE(v ? 'v' : 'n');
2009-05-28 14:40:38 +02:00
2020-01-06 21:50:34 +01:00
std::complex<double> *WORK = new std::complex<double>[2*n];
2009-05-28 14:40:38 +02:00
double *RWORK = new double[3*n];
2010-06-25 17:28:19 +02:00
FINT ldv=v?v->ncols():n;
#ifdef FORINT
const FINT itypetmp = itype;
FINT ntmp = n;
2020-01-06 21:50:34 +01:00
if(b) FORNAME(zhpgv)(&itypetmp,&job, &U, &ntmp, a, *b, w, v?(*v)[0]:(std::complex<double> *)0, &ldv, WORK, RWORK, &r );
else FORNAME(zhpev)(&job, &U, &ntmp, a, w, v?(*v)[0]:(std::complex<double> *)0, &ldv, WORK, RWORK, &r );
2010-06-25 17:28:19 +02:00
#else
2020-01-06 21:50:34 +01:00
if(b) FORNAME(zhpgv)(&itype,&job, &U, &n, a, *b, w, v?(*v)[0]:(std::complex<double> *)0, &ldv, WORK, RWORK, &r );
else FORNAME(zhpev)(&job, &U, &n, a, w, v?(*v)[0]:(std::complex<double> *)0, &ldv, WORK, RWORK, &r );
2010-06-25 17:28:19 +02:00
#endif
2009-05-28 14:40:38 +02:00
delete[] WORK;
delete[] RWORK;
if (v && corder) v->transposeme(n);
2009-05-28 16:14:12 +02:00
if (r < 0) laerror("illegal argument in hpgv/hpev in diagonalize()");
if (r > 0) laerror("convergence problem in hpgv/hpev in diagonalize()");
2009-05-28 14:40:38 +02:00
}
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dgesvd)(const char *JOBU, const char *JOBVT, const FINT *M,
const FINT *N, double *A, const FINT *LDA, double *S, double *U, const FINT *LDU,
double *VT, const FINT *LDVT, double *WORK, const FINT *LWORK, FINT *INFO );
2004-03-17 04:07:21 +01:00
2021-04-21 15:04:37 +02:00
//normally in v returns vtransposed for default vnotdagger=0
2004-03-17 04:07:21 +01:00
void singular_decomposition(NRMat<double> &a, NRMat<double> *u, NRVec<double> &s,
2016-06-28 15:07:54 +02:00
NRMat<double> *v, const bool vnotdagger, int m, int n)
2004-03-17 04:07:21 +01:00
{
2010-06-25 17:28:19 +02:00
FINT m0 = a.nrows();
FINT n0 = a.ncols();
if(m<=0) m=(int)m0;
if(n<=0) n=(int)n0;
2005-02-17 23:54:27 +01:00
if(n>n0 || m>m0) laerror("bad dimension in singular_decomposition");
if (u) if (m > u->nrows() || m> u->ncols())
2004-03-17 04:07:21 +01:00
laerror("inconsistent dimension of U Mat in singular_decomposition()");
if (s.size() < m && s.size() < n)
laerror("inconsistent dimension of S Vec in singular_decomposition()");
2005-02-17 23:54:27 +01:00
if (v) if (n > v->nrows() || n > v->ncols())
2004-03-17 04:07:21 +01:00
laerror("inconsistent dimension of V Mat in singular_decomposition()");
a.copyonwrite();
s.copyonwrite();
if (u) u->copyonwrite();
if (v) v->copyonwrite();
// C-order (transposed) input and swap u,v matrices,
// v should be transposed at the end
char jobu = u ? 'A' : 'N';
char jobv = v ? 'A' : 'N';
double work0;
2010-06-25 17:28:19 +02:00
FINT lwork = -1;
FINT r;
#ifdef FORINT
FINT ntmp = n;
FINT mtmp = m;
FORNAME(dgesvd)(&jobv, &jobu, &ntmp, &mtmp, a, &n0, s, v?(*v)[0]:0, &n0,
u?(*u)[0]:0, &m0, &work0, &lwork, &r);
#else
2005-02-17 23:54:27 +01:00
FORNAME(dgesvd)(&jobv, &jobu, &n, &m, a, &n0, s, v?(*v)[0]:0, &n0,
u?(*u)[0]:0, &m0, &work0, &lwork, &r);
2010-06-25 17:28:19 +02:00
#endif
lwork = (FINT) work0;
2004-03-17 04:07:21 +01:00
double *work = new double[lwork];
2010-06-25 17:28:19 +02:00
#ifdef FORINT
FORNAME(dgesvd)(&jobv, &jobu, &ntmp, &mtmp, a, &n0, s, v?(*v)[0]:0, &n0,
u?(*u)[0]:0, &m0, work, &lwork, &r);
#else
2005-02-17 23:54:27 +01:00
FORNAME(dgesvd)(&jobv, &jobu, &n, &m, a, &n0, s, v?(*v)[0]:0, &n0,
2005-02-25 17:26:47 +01:00
u?(*u)[0]:0, &m0, work, &lwork, &r);
2010-06-25 17:28:19 +02:00
#endif
2004-03-17 04:07:21 +01:00
delete[] work;
2016-06-28 15:07:54 +02:00
if (v && vnotdagger) v->transposeme(n);
if (r < 0) laerror("illegal argument in gesvd() of singular_decomposition()");
if (r > 0) laerror("convergence problem in gesvd() of singular_decomposition()");
}
extern "C" void FORNAME(zgesvd)(const char *JOBU, const char *JOBVT, const FINT *M,
2020-01-06 21:50:34 +01:00
const FINT *N, std::complex<double> *A, const FINT *LDA, double *S, std::complex<double> *U, const FINT *LDU,
std::complex<double> *VT, const FINT *LDVT, std::complex<double> *WORK, const FINT *LWORK, double *RWORK, FINT *INFO );
2016-06-28 15:07:54 +02:00
2020-01-06 21:50:34 +01:00
void singular_decomposition(NRMat<std::complex<double> > &a, NRMat<std::complex<double> > *u, NRVec<double> &s,
NRMat<std::complex<double> > *v, const bool vnotdagger, int m, int n)
2016-06-28 15:07:54 +02:00
{
FINT m0 = a.nrows();
FINT n0 = a.ncols();
if(m<=0) m=(int)m0;
if(n<=0) n=(int)n0;
if(n>n0 || m>m0) laerror("bad dimension in singular_decomposition");
if (u) if (m > u->nrows() || m> u->ncols())
laerror("inconsistent dimension of U Mat in singular_decomposition()");
if (s.size() < m && s.size() < n)
laerror("inconsistent dimension of S Vec in singular_decomposition()");
if (v) if (n > v->nrows() || n > v->ncols())
laerror("inconsistent dimension of V Mat in singular_decomposition()");
int nmin = n<m?n:m;
a.copyonwrite();
s.copyonwrite();
if (u) u->copyonwrite();
if (v) v->copyonwrite();
// C-order (transposed) input and swap u,v matrices,
// v should be transposed at the end
char jobu = u ? 'A' : 'N';
char jobv = v ? 'A' : 'N';
2020-01-06 21:50:34 +01:00
std::complex<double> work0;
2016-06-28 15:07:54 +02:00
FINT lwork = -1;
FINT r;
double *rwork = new double[5*nmin];
#ifdef FORINT
FINT ntmp = n;
FINT mtmp = m;
FORNAME(zgesvd)(&jobv, &jobu, &ntmp, &mtmp, a, &n0, s, v?(*v)[0]:0, &n0,
u?(*u)[0]:0, &m0, &work0, &lwork, rwork, &r);
#else
FORNAME(zgesvd)(&jobv, &jobu, &n, &m, a, &n0, s, v?(*v)[0]:0, &n0,
u?(*u)[0]:0, &m0, &work0, &lwork, rwork, &r);
#endif
lwork = (FINT) work0.real();
2020-01-06 21:50:34 +01:00
std::complex<double> *work = new std::complex<double>[lwork];
2016-06-28 15:07:54 +02:00
#ifdef FORINT
FORNAME(zgesvd)(&jobv, &jobu, &ntmp, &mtmp, a, &n0, s, v?(*v)[0]:0, &n0,
u?(*u)[0]:0, &m0, work, &lwork, rwork, &r);
#else
FORNAME(zgesvd)(&jobv, &jobu, &n, &m, a, &n0, s, v?(*v)[0]:0, &n0,
u?(*u)[0]:0, &m0, work, &lwork, rwork, &r);
#endif
delete[] work;
delete[] rwork;
if (v && vnotdagger) {v->transposeme(n); v->conjugateme();}
2004-03-17 04:07:21 +01:00
if (r < 0) laerror("illegal argument in gesvd() of singular_decomposition()");
2016-06-28 15:07:54 +02:00
if (r > 0) laerror("convergence problem in gesvd() of singular_decomposition()");
2004-03-17 04:07:21 +01:00
}
2016-06-28 15:07:54 +02:00
2009-11-12 22:01:19 +01:00
//QR decomposition
//extern "C" void FORNAME(dgeqrf)(const int *M, const int *N, double *A, const int *LDA, double *TAU, double *WORK, int *LWORK, int *INFO);
2004-03-17 04:07:21 +01:00
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dgeev)(const char *JOBVL, const char *JOBVR, const FINT *N,
double *A, const FINT *LDA, double *WR, double *WI, double *VL, const FINT *LDVL,
double *VR, const FINT *LDVR, double *WORK, const FINT *LWORK, FINT *INFO );
2004-03-17 04:07:21 +01:00
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dggev)(const char *JOBVL, const char *JOBVR, const FINT *N,
double *A, const FINT *LDA, double *B, const FINT *LDB, double *WR, double *WI, double *WBETA,
double *VL, const FINT *LDVL, double *VR, const FINT *LDVR,
double *WORK, const FINT *LWORK, FINT *INFO );
2005-02-17 23:54:27 +01:00
2011-01-18 15:37:05 +01:00
extern "C" void FORNAME(zgeev)(const char *JOBVL, const char *JOBVR, const FINT *N,
2020-01-06 21:50:34 +01:00
std::complex<double> *A, const FINT *LDA, std::complex<double> *W, std::complex<double> *VL, const FINT *LDVL,
std::complex<double> *VR, const FINT *LDVR, std::complex<double> *WORK, const FINT *LWORK,
2011-01-18 15:37:05 +01:00
double *RWORK, FINT *INFO );
extern "C" void FORNAME(zggev)(const char *JOBVL, const char *JOBVR, const FINT *N,
2020-01-06 21:50:34 +01:00
std::complex<double> *A, const FINT *LDA, std::complex<double> *B, const FINT *LDB, std::complex<double> *W, std::complex<double> *WBETA,
std::complex<double> *VL, const FINT *LDVL, std::complex<double> *VR, const FINT *LDVR,
std::complex<double> *WORK, const FINT *LWORK, double *RWORK, FINT *INFO );
2011-01-18 15:37:05 +01:00
2005-02-25 17:26:47 +01:00
//statics for sorting
static int *gdperm;
2005-02-26 00:37:26 +01:00
static double *gdwr, *gdwi, *gdbeta;
2005-02-25 17:26:47 +01:00
//compare methods
static double realonly(const int i, const int j)
{
2005-02-26 00:37:26 +01:00
if(gdbeta)
{
if(gdbeta[i]==0. && gdbeta[j]!=0) return 1.;
if(gdbeta[j]==0. && gdbeta[i]!=0) return -1.;
if(gdbeta[i]==0. && gdbeta[j]==0) return 0.;
double tmp = gdwr[i]/gdbeta[i]-gdwr[j]/gdbeta[j];
if(tmp) return tmp;
return gdwi[j]/gdbeta[j]-gdwi[i]/gdbeta[i];
}
//else
2005-02-25 17:26:47 +01:00
double tmp = gdwr[i]-gdwr[j];
if(tmp) return tmp;
return gdwi[j]-gdwi[i];
}
static double realfirst(const int i, const int j)
{
if(gdwi[i] && ! gdwi[j]) return 1.;
if(!gdwi[i] && gdwi[j]) return -1.;
2005-02-26 00:37:26 +01:00
return realonly(i,j);
2005-02-25 17:26:47 +01:00
}
static double (* gdcompar[2])(const int, const int) = {&realonly, &realfirst};
//swap method
static void gdswap(const int i, const int j)
{
double tmp;
int itmp;
itmp=gdperm[i]; gdperm[i]=gdperm[j]; gdperm[j]=itmp;
tmp=gdwr[i]; gdwr[i]=gdwr[j]; gdwr[j]=tmp;
tmp=gdwi[i]; gdwi[i]=gdwi[j]; gdwi[j]=tmp;
2005-02-26 00:37:26 +01:00
if(gdbeta) {tmp=gdbeta[i]; gdbeta[i]=gdbeta[j]; gdbeta[j]=tmp;}
2005-02-25 17:26:47 +01:00
}
2004-03-17 04:07:21 +01:00
void gdiagonalize(NRMat<double> &a, NRVec<double> &wr, NRVec<double> &wi,
2005-02-17 23:54:27 +01:00
NRMat<double> *vl, NRMat<double> *vr, const bool corder, int n,
2008-03-02 17:40:22 +01:00
const int sorttype, const int biorthonormalize,
2005-02-17 23:54:27 +01:00
NRMat<double> *b, NRVec<double> *beta)
2004-03-17 04:07:21 +01:00
{
2013-11-04 15:56:39 +01:00
if(n<=0) {n = a.nrows(); if(a.ncols()!=a.nrows() ) laerror("gdiagonalize() call for a non-square matrix");}
if (n > a.ncols() || n>a.nrows() ) laerror("gdiagonalize() of too big submatrix");
2005-02-17 23:54:27 +01:00
if (n > wr.size())
2004-03-17 04:07:21 +01:00
laerror("inconsistent dimension of eigen vector in gdiagonalize()");
2005-02-17 23:54:27 +01:00
if (vl) if (n > vl->nrows() || n > vl->ncols())
2004-03-17 04:07:21 +01:00
laerror("inconsistent dimension of vl in gdiagonalize()");
2005-02-17 23:54:27 +01:00
if (vr) if (n > vr->nrows() || n > vr->ncols())
2004-03-17 04:07:21 +01:00
laerror("inconsistent dimension of vr in gdiagonalize()");
2005-02-17 23:54:27 +01:00
if (beta) if(n > beta ->size()) laerror("inconsistent dimension of beta in gdiagonalize()");
if(b) if(n > b->nrows() || n > b->ncols())
laerror("inconsistent dimension of b in gdiagonalize()");
if(b && !beta || beta && !b) laerror("missing array for generalized diagonalization");
2004-03-17 04:07:21 +01:00
a.copyonwrite();
wr.copyonwrite();
wi.copyonwrite();
if (vl) vl->copyonwrite();
if (vr) vr->copyonwrite();
2005-02-17 23:54:27 +01:00
if (beta) beta->copyonwrite();
if (b) b->copyonwrite();
2004-03-17 04:07:21 +01:00
2013-11-04 15:56:39 +01:00
char jobvl = LAPACK_FORTRANCASE(vl ? 'v' : 'n');
char jobvr = LAPACK_FORTRANCASE(vr ? 'v' : 'n');
2004-03-17 04:07:21 +01:00
double work0;
2010-06-25 17:28:19 +02:00
FINT lwork = -1;
FINT r;
FINT lda=a.ncols();
FINT ldb=0;
2005-02-17 23:54:27 +01:00
if(b) ldb=b->ncols();
2010-06-25 17:28:19 +02:00
FINT ldvl= vl?vl->ncols():lda;
FINT ldvr= vr?vr->ncols():lda;
#ifdef FORINT
FINT ntmp = n;
if(b) FORNAME(dggev)(&jobvr, &jobvl, &ntmp, a, &lda, *b, &ldb, wr, wi, *beta, vr?vr[0]:(double *)0,
&ldvr, vl?vl[0]:(double *)0, &ldvl, &work0, &lwork, &r);
else FORNAME(dgeev)(&jobvr, &jobvl, &ntmp, a, &lda, wr, wi, vr?vr[0]:(double *)0,
&ldvr, vl?vl[0]:(double *)0, &ldvl, &work0, &lwork, &r);
#else
if(b) FORNAME(dggev)(&jobvr, &jobvl, &n, a, &lda, *b, &ldb, wr, wi, *beta, vr?vr[0]:(double *)0,
2005-02-17 23:54:27 +01:00
&ldvr, vl?vl[0]:(double *)0, &ldvl, &work0, &lwork, &r);
else FORNAME(dgeev)(&jobvr, &jobvl, &n, a, &lda, wr, wi, vr?vr[0]:(double *)0,
&ldvr, vl?vl[0]:(double *)0, &ldvl, &work0, &lwork, &r);
2010-06-25 17:28:19 +02:00
#endif
lwork = (FINT) work0;
2004-03-17 04:07:21 +01:00
double *work = new double[lwork];
2010-06-25 17:28:19 +02:00
#ifdef FORINT
if(b) FORNAME(dggev)(&jobvr, &jobvl, &ntmp, a, &lda, *b, &ldb, wr, wi, *beta, vr?vr[0]:(double *)0,
&ldvr, vl?vl[0]:(double *)0, &ldvl, work, &lwork, &r);
else FORNAME(dgeev)(&jobvr, &jobvl, &ntmp, a, &lda, wr, wi, vr?vr[0]:(double *)0,
&ldvr, vl?vl[0]:(double *)0, &ldvl, work, &lwork, &r);
#else
2005-02-17 23:54:27 +01:00
if(b) FORNAME(dggev)(&jobvr, &jobvl, &n, a, &lda, *b, &ldb, wr, wi, *beta, vr?vr[0]:(double *)0,
2005-02-25 17:26:47 +01:00
&ldvr, vl?vl[0]:(double *)0, &ldvl, work, &lwork, &r);
2005-02-17 23:54:27 +01:00
else FORNAME(dgeev)(&jobvr, &jobvl, &n, a, &lda, wr, wi, vr?vr[0]:(double *)0,
2005-02-25 17:26:47 +01:00
&ldvr, vl?vl[0]:(double *)0, &ldvl, work, &lwork, &r);
2010-06-25 17:28:19 +02:00
#endif
2004-03-17 04:07:21 +01:00
delete[] work;
2008-03-02 17:40:22 +01:00
2004-03-17 04:07:21 +01:00
2005-02-25 17:26:47 +01:00
if (r < 0) laerror("illegal argument in ggev/geev in gdiagonalize()");
if (r > 0) laerror("convergence problem in ggev/geev in gdiagonalize()");
2011-01-18 15:37:05 +01:00
//std::cout <<"TEST dgeev\n"<<wr<<wi<<*vr<<*vl<<std::endl;
2005-02-25 17:26:47 +01:00
if(biorthonormalize && vl && vr)
{
2005-02-26 00:37:26 +01:00
if(b || beta) laerror("@@@ biorthonormalize not implemented yet for generalized non-symmetric eigenproblem");//metric b would be needed
2005-02-25 17:26:47 +01:00
int i=0;
while(i<n)
{
if(wi[i]==0) //real
{
//calculate scaling paramter
double tmp;
tmp=cblas_ddot(n,(*vl)[i],1,(*vr)[i], 1);
2008-03-02 17:40:22 +01:00
if(biorthonormalize==1) cblas_dscal(n,1./tmp,(*vl)[i],1);
if(biorthonormalize==2) cblas_dscal(n,1./tmp,(*vr)[i],1);
2005-02-25 17:26:47 +01:00
i++;
}
else //complex pair
{
//calculate rotation parameters
double s11,s12;
//double s21,s22;
s11=cblas_ddot(n,(*vl)[i],1,(*vr)[i], 1);
s12=cblas_ddot(n,(*vl)[i],1,(*vr)[i+1], 1);
//s21=cblas_ddot(n,(*vl)[i+1],1,(*vr)[i], 1);
//s22=cblas_ddot(n,(*vl)[i+1],1,(*vr)[i+1], 1);
double t,x,y;
t=1/(s11*s11+s12*s12);
x=.5*t*s11;
y=.5*t*s12;
double alp,bet;
2009-11-12 22:01:19 +01:00
t=.5*std::sqrt(t);
alp=std::sqrt(.5*(t+x));
bet=std::sqrt(.5*(t-x));
2005-02-25 17:26:47 +01:00
if(y<0.) bet= -bet;
//rotate left ev
memcpy(a[i],(*vl)[i],n*sizeof(double));
cblas_dscal(n,alp,a[i],1);
cblas_daxpy(n,-bet,(*vl)[i+1],1,a[i],1);
memcpy(a[i+1],(*vl)[i+1],n*sizeof(double));
cblas_dscal(n,alp,a[i+1],1);
cblas_daxpy(n,bet,(*vl)[i],1,a[i+1],1);
memcpy((*vl)[i],a[i],n*sizeof(double));
memcpy((*vl)[i+1],a[i+1],n*sizeof(double));
//rotate right ev
memcpy(a[i],(*vr)[i],n*sizeof(double));
cblas_dscal(n,alp,a[i],1);
cblas_daxpy(n,bet,(*vr)[i+1],1,a[i],1);
memcpy(a[i+1],(*vr)[i+1],n*sizeof(double));
cblas_dscal(n,alp,a[i+1],1);
cblas_daxpy(n,-bet,(*vr)[i],1,a[i+1],1);
memcpy((*vr)[i],a[i],n*sizeof(double));
memcpy((*vr)[i+1],a[i+1],n*sizeof(double));
i+=2;
}
}
}
2011-01-18 15:37:05 +01:00
2005-02-25 17:26:47 +01:00
if(sorttype>0)
{
NRVec<int> perm(n);
for(int i=0; i<n;++i) perm[i]=i;
gdperm= perm;
2005-02-26 00:37:26 +01:00
if(beta) gdbeta= *beta; else gdbeta= NULL;
2005-02-25 17:26:47 +01:00
gdwr=wr, gdwi=wi;
genqsort(0,n-1,gdcompar[sorttype-1],gdswap);
if(vl)
{
for(int i=0; i<n;++i) memcpy(a[i],(*vl)[perm[i]],n*sizeof(double));
*vl |= a;
}
if(vr)
{
for(int i=0; i<n;++i) memcpy(a[i],(*vr)[perm[i]],n*sizeof(double));
*vr |= a;
}
}
2004-03-17 04:07:21 +01:00
if (corder) {
2005-02-17 23:54:27 +01:00
if (vl) vl->transposeme(n);
if (vr) vr->transposeme(n);
2004-03-17 04:07:21 +01:00
}
}
2005-02-25 17:26:47 +01:00
2011-01-18 15:37:05 +01:00
//most general complex routine
template<>
2020-01-06 21:50:34 +01:00
void gdiagonalize(NRMat<std::complex<double> > &a, NRVec< std::complex<double> > &w,
NRMat< std::complex<double> >*vl, NRMat< std::complex<double> > *vr,
2011-01-18 15:37:05 +01:00
const bool corder, int n, const int sorttype, const int biorthonormalize,
2020-01-06 21:50:34 +01:00
NRMat<std::complex<double> > *b, NRVec<std::complex<double> > *beta)
2011-01-18 15:37:05 +01:00
{
2013-11-04 15:56:39 +01:00
if(n<=0) {n = a.nrows(); if(a.ncols()!=a.nrows() ) laerror("gdiagonalize() call for a non-square matrix");}
if (n > a.ncols() || n>a.nrows() ) laerror("gdiagonalize() of too big submatrix");
2011-01-18 15:37:05 +01:00
if (n > w.size())
laerror("inconsistent dimension of eigen vector in gdiagonalize()");
if (vl) if (n > vl->nrows() || n > vl->ncols())
laerror("inconsistent dimension of vl in gdiagonalize()");
if (vr) if (n > vr->nrows() || n > vr->ncols())
laerror("inconsistent dimension of vr in gdiagonalize()");
if (beta) if(n > beta ->size()) laerror("inconsistent dimension of beta in gdiagonalize()");
if(b) if(n > b->nrows() || n > b->ncols())
laerror("inconsistent dimension of b in gdiagonalize()");
if(b && !beta || beta && !b) laerror("missing array for generalized diagonalization");
a.copyonwrite();
w.copyonwrite();
if (vl) vl->copyonwrite();
if (vr) vr->copyonwrite();
if (beta) beta->copyonwrite();
if (b) b->copyonwrite();
2013-11-04 15:56:39 +01:00
char jobvl = LAPACK_FORTRANCASE(vl ? 'v' : 'n');
char jobvr = LAPACK_FORTRANCASE(vr ? 'v' : 'n');
2020-01-06 21:50:34 +01:00
std::complex<double> work0;
2011-01-18 15:37:05 +01:00
FINT lwork = -1;
FINT r;
FINT lda=a.ncols();
FINT ldb=0;
if(b) ldb=b->ncols();
FINT ldvl= vl?vl->ncols():lda;
FINT ldvr= vr?vr->ncols():lda;
double *rwork = new double[n*(b?8:2)];
#ifdef FORINT
FINT ntmp = n;
2020-01-06 21:50:34 +01:00
if(b) FORNAME(zggev)(&jobvr, &jobvl, &ntmp, a, &lda, *b, &ldb, w, *beta, vr?vr[0]:(std::complex<double> *)0,
&ldvr, vl?vl[0]:(std::complex<double> *)0, &ldvl, &work0, &lwork, rwork, &r);
else FORNAME(zgeev)(&jobvr, &jobvl, &ntmp, a, &lda, w, vr?vr[0]:(std::complex<double> *)0,
&ldvr, vl?vl[0]:(std::complex<double> *)0, &ldvl, &work0, &lwork, rwork, &r);
2011-01-18 15:37:05 +01:00
#else
2020-01-06 21:50:34 +01:00
if(b) FORNAME(zggev)(&jobvr, &jobvl, &n, a, &lda, *b, &ldb, w, *beta, vr?vr[0]:(std::complex<double> *)0,
&ldvr, vl?vl[0]:(std::complex<double> *)0, &ldvl, &work0, &lwork, rwork, &r);
else FORNAME(zgeev)(&jobvr, &jobvl, &n, a, &lda, w, vr?vr[0]:(std::complex<double> *)0,
&ldvr, vl?vl[0]:(std::complex<double> *)0, &ldvl, &work0, &lwork, rwork, &r);
2011-01-18 15:37:05 +01:00
#endif
lwork = (FINT) work0.real();
2020-01-06 21:50:34 +01:00
std::complex<double> *work = new std::complex<double>[lwork];
2011-01-18 15:37:05 +01:00
#ifdef FORINT
2020-01-06 21:50:34 +01:00
if(b) FORNAME(zggev)(&jobvr, &jobvl, &ntmp, a, &lda, *b, &ldb, w, *beta, vr?vr[0]:(std::complex<double> *)0,
&ldvr, vl?vl[0]:(std::complex<double> *)0, &ldvl, work, &lwork, rwork, &r);
else FORNAME(zgeev)(&jobvr, &jobvl, &ntmp, a, &lda, w, vr?vr[0]:(std::complex<double> *)0,
&ldvr, vl?vl[0]:(std::complex<double> *)0, &ldvl, work, &lwork, rwork, &r);
2011-01-18 15:37:05 +01:00
#else
2020-01-06 21:50:34 +01:00
if(b) FORNAME(zggev)(&jobvr, &jobvl, &n, a, &lda, *b, &ldb, w, *beta, vr?vr[0]:(std::complex<double> *)0,
&ldvr, vl?vl[0]:(std::complex<double> *)0, &ldvl, work, &lwork, rwork, &r);
else FORNAME(zgeev)(&jobvr, &jobvl, &n, a, &lda, w, vr?vr[0]:(std::complex<double> *)0,
&ldvr, vl?vl[0]:(std::complex<double> *)0, &ldvl, work, &lwork, rwork, &r);
2011-01-18 15:37:05 +01:00
#endif
delete[] work;
delete[] rwork;
//std::cout <<"TEST zg(g|e)ev\n"<<w<<*vr<<*vl<<std::endl;
if (r < 0) laerror("illegal argument in ggev/geev in gdiagonalize()");
if (r > 0) laerror("convergence problem in ggev/geev in gdiagonalize()");
if(biorthonormalize && vl && vr)
{
if(b || beta) laerror("@@@ biorthonormalize not implemented yet for generalized non-hermitian eigenproblem");//metric b would be needed
for(int i=0; i<n; ++i)
{
//calculate scaling paramter
2020-01-06 21:50:34 +01:00
std::complex<double> tmp;
2011-01-18 15:37:05 +01:00
cblas_zdotc_sub(n,(*vr)[i],1,(*vl)[i], 1, &tmp);
tmp = 1./tmp;
std::cout <<"scaling by "<<tmp<<"\n";
if(biorthonormalize==1) cblas_zscal(n,&tmp,(*vl)[i],1);
if(biorthonormalize==2) cblas_zscal(n,&tmp,(*vr)[i],1);
}
}
if(sorttype>0)
{
laerror("sorting not implemented in complex gdiagonalize");
}
if (corder) {
2013-11-04 15:56:39 +01:00
if (vl) {vl->transposeme(n); vl->conjugateme();}
if (vr) {vr->transposeme(n); vr->conjugateme();}
2011-01-18 15:37:05 +01:00
}
}
template<>
2020-01-06 21:50:34 +01:00
void gdiagonalize(NRMat<double> &a, NRVec< std::complex<double> > &w,
NRMat< std::complex<double> >*vl, NRMat< std::complex<double> > *vr,
2008-03-02 17:40:22 +01:00
const bool corder, int n, const int sorttype, const int biorthonormalize,
2005-02-25 17:26:47 +01:00
NRMat<double> *b, NRVec<double> *beta)
2004-03-17 04:07:21 +01:00
{
2013-11-04 15:56:39 +01:00
if(n<=0) {n = a.nrows(); if(a.ncols()!=a.nrows() ) laerror("gdiagonalize() call for a non-square matrix");}
if(n> a.nrows() || n == a.nrows() && n != a.ncols()) laerror("gdiagonalize() of too big submatrix");
2004-03-17 04:07:21 +01:00
NRVec<double> wr(n), wi(n);
NRMat<double> *rvl = 0;
NRMat<double> *rvr = 0;
if (vl) rvl = new NRMat<double>(n, n);
if (vr) rvr = new NRMat<double>(n, n);
2005-02-25 17:26:47 +01:00
gdiagonalize(a, wr, wi, rvl, rvr, 0, n, sorttype, biorthonormalize, b, beta);
2004-03-17 04:07:21 +01:00
//process the results into complex matrices
int i;
2020-01-06 21:50:34 +01:00
for (i=0; i<n; i++) w[i] = std::complex<double>(wr[i], wi[i]);
2004-03-17 04:07:21 +01:00
if (rvl || rvr) {
i = 0;
while (i < n) {
if (wi[i] == 0) {
2011-01-18 15:37:05 +01:00
if(corder)
{
if (vl) for (int j=0; j<n; j++) (*vl)[j][i] = (*rvl)[i][j];
if (vr) for (int j=0; j<n; j++) (*vr)[j][i] = (*rvr)[i][j];
}
else
{
2004-03-17 04:07:21 +01:00
if (vl) for (int j=0; j<n; j++) (*vl)[i][j] = (*rvl)[i][j];
if (vr) for (int j=0; j<n; j++) (*vr)[i][j] = (*rvr)[i][j];
2011-01-18 15:37:05 +01:00
}
2004-03-17 04:07:21 +01:00
i++;
} else {
if (vl)
for (int j=0; j<n; j++) {
2011-01-18 15:37:05 +01:00
if(corder)
{
2020-01-06 21:50:34 +01:00
(*vl)[j][i] = std::complex<double>((*rvl)[i][j], (*rvl)[i+1][j]);
(*vl)[j][i+1] = std::complex<double>((*rvl)[i][j], -(*rvl)[i+1][j]);
2011-01-18 15:37:05 +01:00
}
else
{
2020-01-06 21:50:34 +01:00
(*vl)[i][j] = std::complex<double>((*rvl)[i][j], (*rvl)[i+1][j]);
(*vl)[i+1][j] = std::complex<double>((*rvl)[i][j], -(*rvl)[i+1][j]);
2011-01-18 15:37:05 +01:00
}
2004-03-17 04:07:21 +01:00
}
if (vr)
for (int j=0; j<n; j++) {
2011-01-18 15:37:05 +01:00
if(corder)
{
2020-01-06 21:50:34 +01:00
(*vr)[j][i] = std::complex<double>((*rvr)[i][j], (*rvr)[i+1][j]);
(*vr)[j][i+1] = std::complex<double>((*rvr)[i][j], -(*rvr)[i+1][j]);
2011-01-18 15:37:05 +01:00
}
else
{
2020-01-06 21:50:34 +01:00
(*vr)[i][j] = std::complex<double>((*rvr)[i][j], (*rvr)[i+1][j]);
(*vr)[i+1][j] = std::complex<double>((*rvr)[i][j], -(*rvr)[i+1][j]);
2011-01-18 15:37:05 +01:00
}
2004-03-17 04:07:21 +01:00
}
i += 2;
}
}
}
if (rvl) delete rvl;
if (rvr) delete rvr;
}
//for compatibility in davidson
void gdiagonalize(NRMat<std::complex<double> > &a, NRVec<double> &wr, NRVec<double> &wi,
NRMat<std::complex<double> > *vl, NRMat<std::complex<double> > *vr, const bool corder, int n, const int sorttype, const int biorthonormalize,
NRMat<std::complex<double> > *b, NRVec<std::complex<double> > *beta)
{
if(wr.size()!=wi.size()) laerror("length mismatch in gdiagonalize");
NRVec<std::complex<double> > w(wr.size());
gdiagonalize(a,w,vl,vr,corder,n,sorttype,biorthonormalize,b,beta);
wr.copyonwrite();
wi.copyonwrite();
for(int i=0; i<w.size(); ++i)
{
wr[i]=w[i].real();
wi[i]=w[i].imag();
}
}
2022-08-11 15:52:58 +02:00
template<>
const SparseMat<double> realpart<SparseMat< std::complex<double> > >(const SparseMat< std::complex<double> > &a)
{
SparseMat<double> result(a.nrows(), a.ncols());
for(SparseMat< std::complex<double> >::iterator p=a.begin(); p!=a.end(); ++p)
{
result.add(p->row,p->col,p->elem.real());
}
if(a.issymmetric()) result.setsymmetric();
return result;
}
template<>
const SparseMat<double> imagpart<SparseMat< std::complex<double> > >(const SparseMat< std::complex<double> > &a)
{
SparseMat<double> result(a.nrows(), a.ncols());
for(SparseMat< std::complex<double> >::iterator p=a.begin(); p!=a.end(); ++p)
{
result.add(p->row,p->col,p->elem.imag());
}
if(a.issymmetric()) result.setsymmetric();
return result;
}
2011-01-18 15:37:05 +01:00
template<>
2020-01-06 21:50:34 +01:00
const NRMat<double> realpart<NRMat< std::complex<double> > >(const NRMat< std::complex<double> > &a)
2004-03-17 04:07:21 +01:00
{
2013-11-04 15:56:39 +01:00
NRMat<double> result(a.nrows(), a.ncols());
2011-01-18 15:37:05 +01:00
#ifdef CUDALA
2013-11-04 15:56:39 +01:00
if(a.location == cpu){
2011-01-18 15:37:05 +01:00
#endif
2013-11-04 15:56:39 +01:00
// NRMat<double> result(a.nrows(), a.ncols());
2004-03-17 04:07:21 +01:00
cblas_dcopy(a.nrows()*a.ncols(), (const double *)a[0], 2, result, 1);
2011-01-18 15:37:05 +01:00
#ifdef CUDALA
}else{
laerror("not implemented for cuda yet");
}
#endif
2004-03-17 04:07:21 +01:00
return result;
}
2011-01-18 15:37:05 +01:00
template<>
2020-01-06 21:50:34 +01:00
const NRMat<double> imagpart<NRMat< std::complex<double> > >(const NRMat< std::complex<double> > &a)
2004-03-17 04:07:21 +01:00
{
2013-11-04 15:56:39 +01:00
NRMat<double> result(a.nrows(), a.ncols());
2011-01-18 15:37:05 +01:00
#ifdef CUDALA
2013-11-04 15:56:39 +01:00
if(a.location == cpu){
2011-01-18 15:37:05 +01:00
#endif
2013-11-04 15:56:39 +01:00
// NRMat<double> result(a.nrows(), a.ncols());
2004-03-17 04:07:21 +01:00
cblas_dcopy(a.nrows()*a.ncols(), (const double *)a[0]+1, 2, result, 1);
2011-01-18 15:37:05 +01:00
#ifdef CUDALA
}else{
laerror("not implemented for cuda yet");
}
#endif
2004-03-17 04:07:21 +01:00
return result;
}
2011-01-18 15:37:05 +01:00
template<>
2020-01-06 21:50:34 +01:00
const NRMat< std::complex<double> > realmatrix<NRMat<double> > (const NRMat<double> &a)
2004-03-17 04:07:21 +01:00
{
2013-11-04 15:56:39 +01:00
2020-01-06 21:50:34 +01:00
NRMat <std::complex<double> > result(a.nrows(), a.ncols());
2013-11-04 15:56:39 +01:00
2011-01-18 15:37:05 +01:00
#ifdef CUDALA
2013-11-04 15:56:39 +01:00
if(a.location == cpu){
2011-01-18 15:37:05 +01:00
#endif
2020-01-06 21:50:34 +01:00
// NRMat <std::complex<double> > result(a.nrows(), a.ncols());
2004-03-17 04:07:21 +01:00
cblas_dcopy(a.nrows()*a.ncols(), a, 1, (double *)result[0], 2);
2011-01-18 15:37:05 +01:00
#ifdef CUDALA
}else{
laerror("not implemented for cuda yet");
}
#endif
2004-03-17 04:07:21 +01:00
return result;
}
2011-01-18 15:37:05 +01:00
template<>
2020-01-06 21:50:34 +01:00
const NRMat< std::complex<double> > imagmatrix<NRMat<double> > (const NRMat<double> &a)
2004-03-17 04:07:21 +01:00
{
2020-01-06 21:50:34 +01:00
NRMat< std::complex<double> > result(a.nrows(), a.ncols());
2013-11-04 15:56:39 +01:00
2011-01-18 15:37:05 +01:00
#ifdef CUDALA
2013-11-04 15:56:39 +01:00
if(a.location == cpu){
2011-01-18 15:37:05 +01:00
#endif
2020-01-06 21:50:34 +01:00
// NRMat< std::complex<double> > result(a.nrows(), a.ncols());
2004-03-17 04:07:21 +01:00
cblas_dcopy(a.nrows()*a.ncols(), a, 1, (double *)result[0]+1, 2);
2011-01-18 15:37:05 +01:00
#ifdef CUDALA
}else{
laerror("not implemented for cuda yet");
}
#endif
2004-03-17 04:07:21 +01:00
return result;
}
2011-01-18 15:37:05 +01:00
template<>
2020-01-06 21:50:34 +01:00
const NRMat< std::complex<double> > complexmatrix<NRMat<double> > (const NRMat<double> &re, const NRMat<double> &im)
2009-11-12 22:01:19 +01:00
{
if(re.nrows()!=im.nrows() || re.ncols() != im.ncols()) laerror("incompatible sizes of real and imaginary parts");
2020-01-06 21:50:34 +01:00
NRMat< std::complex<double> > result(re.nrows(), re.ncols());
2009-11-12 22:01:19 +01:00
cblas_dcopy(re.nrows()*re.ncols(), re, 1, (double *)result[0], 2);
cblas_dcopy(re.nrows()*re.ncols(), im, 1, (double *)result[0]+1, 2);
return result;
}
2011-01-18 15:37:05 +01:00
template<>
2020-01-06 21:50:34 +01:00
const SparseSMat< std::complex<double> > complexmatrix<SparseSMat<double> >(const SparseSMat<double> &re, const SparseSMat<double> &im) {
2011-01-18 15:37:05 +01:00
if(re.nrows()!=im.nrows() || re.ncols() != im.ncols()) laerror("incompatible sizes of real and imaginary parts");
2020-01-06 21:50:34 +01:00
SparseSMat< std::complex<double> > result(re.nrows(),re.ncols());
std::complex<double> tmp;
2011-01-18 15:37:05 +01:00
SparseSMat<double>::iterator pre(re);
for(; pre.notend(); ++pre) {
tmp = pre->elem;
result.add(pre->row,pre->col,tmp,false);
}
SparseSMat<double>::iterator pim(im);
for(; pim.notend(); ++pim) {
2020-01-06 21:50:34 +01:00
tmp = std::complex<double>(0,1)*(pim->elem);
2011-01-18 15:37:05 +01:00
result.add(pim->row,pim->col,tmp,false);
}
return result;
}
2009-11-12 22:01:19 +01:00
2011-01-18 15:37:05 +01:00
template<>
2020-01-06 21:50:34 +01:00
const SparseSMat< std::complex<double> > realmatrix<SparseSMat<double> >(const SparseSMat<double> &re) {
SparseSMat< std::complex<double> > result(re.nrows(),re.ncols());
std::complex<double> tmp;
2004-03-17 04:07:21 +01:00
2011-01-18 15:37:05 +01:00
SparseSMat<double>::iterator pre(re);
for(; pre.notend(); ++pre) {
tmp = pre->elem;
result.add(pre->row,pre->col,tmp,false);
}
2004-03-17 04:07:21 +01:00
2011-01-18 15:37:05 +01:00
return result;
2004-03-17 04:07:21 +01:00
}
2011-01-18 15:37:05 +01:00
template<>
2020-01-06 21:50:34 +01:00
const SparseSMat< std::complex<double> > imagmatrix<SparseSMat<double> >(const SparseSMat<double> &im) {
SparseSMat< std::complex<double> > result(im.nrows(),im.ncols());
std::complex<double> tmp;
2011-01-18 15:37:05 +01:00
SparseSMat<double>::iterator pim(im);
for(; pim.notend(); ++pim) {
2020-01-06 21:50:34 +01:00
tmp = std::complex<double>(0,1)*(pim->elem);
2011-01-18 15:37:05 +01:00
result.add(pim->row,pim->col,tmp,false);
}
return result;
2004-03-17 04:07:21 +01:00
}
2011-01-18 15:37:05 +01:00
2006-04-01 06:48:01 +02:00
NRMat<double> realmatrixfunction(NRMat<double> a, double (*f) (const double))
2005-09-11 22:04:24 +02:00
{
int n = a.nrows();
NRVec<double> w(n);
diagonalize(a, w, true, false);
for (int i=0; i<a.nrows(); i++) w[i] = (*f)(w[i]);
NRMat<double> u = a;
a.diagmultl(w);
NRMat<double> r(n, n);
r.gemm(0.0, u, 't', a, 'n', 1.0);
return r;
}
2011-01-18 15:37:05 +01:00
2020-01-06 21:50:34 +01:00
NRMat<std::complex<double> > complexmatrixfunction(NRMat<double> a, double (*fre) (const double), double (*fim) (const double))
2006-04-01 06:48:01 +02:00
{
int n = a.nrows();
NRVec<double> wre(n),wim(n);
diagonalize(a, wre, true, false);
for (int i=0; i<a.nrows(); i++) wim[i] = (*fim)(wre[i]);
for (int i=0; i<a.nrows(); i++) wre[i] = (*fre)(wre[i]);
NRMat<double> u = a;
NRMat<double> b = a;
a.diagmultl(wre);
b.diagmultl(wim);
NRMat<double> t(n,n),tt(n,n);
t.gemm(0.0, u, 't', a, 'n', 1.0);
tt.gemm(0.0, u, 't', b, 'n', 1.0);
2020-01-06 21:50:34 +01:00
NRMat<std::complex<double> > r(n, n);
for (int i=0; i<a.nrows(); i++) for(int j=0; j<a.ncols(); ++j) r(i,j)=std::complex<double>(t(i,j),tt(i,j));
2006-04-01 06:48:01 +02:00
return r;
}
2005-09-11 22:04:24 +02:00
2004-03-17 04:07:21 +01:00
// instantize template to an addresable function
2020-01-06 21:50:34 +01:00
std::complex<double> myccopy (const std::complex<double> &x)
2011-01-18 15:37:05 +01:00
{
return x;
}
double mycopy (const double x)
{
return x;
}
2020-01-06 21:50:34 +01:00
std::complex<double> myclog (const std::complex<double> &x)
2004-03-17 04:07:21 +01:00
{
return log(x);
}
2020-01-06 21:50:34 +01:00
std::complex<double> mycexp (const std::complex<double> &x)
2006-09-04 01:31:00 +02:00
{
2009-11-12 22:01:19 +01:00
return std::exp(x);
2006-09-04 01:31:00 +02:00
}
2020-01-06 21:50:34 +01:00
std::complex<double> sqrtinv (const std::complex<double> &x)
2006-04-01 06:48:01 +02:00
{
2009-11-12 22:01:19 +01:00
return 1./std::sqrt(x);
2006-04-01 06:48:01 +02:00
}
double sqrtinv (const double x)
{
2009-11-12 22:01:19 +01:00
return 1./std::sqrt(x);
2006-04-01 06:48:01 +02:00
}
2004-03-17 04:07:21 +01:00
NRMat<double> log(const NRMat<double> &a)
{
2011-01-18 15:37:05 +01:00
return matrixfunction(a, &myclog);
2004-03-17 04:07:21 +01:00
}
2020-01-06 21:50:34 +01:00
NRMat<std::complex<double> > log(const NRMat<std::complex<double> > &a)
2011-01-18 15:37:05 +01:00
{
return matrixfunction(a, &myclog);
}
2006-09-04 01:31:00 +02:00
NRMat<double> exp0(const NRMat<double> &a)
{
2011-01-18 15:37:05 +01:00
return matrixfunction(a, &mycexp);
}
2020-01-06 21:50:34 +01:00
NRMat<std::complex<double> > exp0(const NRMat<std::complex<double> > &a)
2011-01-18 15:37:05 +01:00
{
return matrixfunction(a, &mycexp);
2006-09-04 01:31:00 +02:00
}
2020-01-06 21:50:34 +01:00
NRMat<std::complex<double> > copytest(const NRMat<std::complex<double> > &a)
2011-01-18 15:37:05 +01:00
{
return matrixfunction(a, &myccopy);
}
NRMat<double> copytest(const NRMat<double> &a)
{
return matrixfunction(a, &myccopy);
}
2006-09-04 01:31:00 +02:00
2004-03-17 04:07:21 +01:00
const NRVec<double> diagofproduct(const NRMat<double> &a, const NRMat<double> &b,
bool trb, bool conjb)
{
if (trb && (a.nrows() != b.nrows() || a.ncols() != b.ncols()) ||
!trb && (a.nrows() != b.ncols() || a.ncols() != b.nrows()))
laerror("incompatible Mats in diagofproduct<double>()");
NRVec<double> result(a.nrows());
if (trb)
for(int i=0; i<a.nrows(); i++)
result[i] = cblas_ddot(a.ncols(), a[i], 1, b[i], 1);
else
for(int i=0; i<a.nrows(); i++)
result[i] = cblas_ddot(a.ncols(), a[i], 1, b[0]+i, b.ncols());
return result;
}
2020-01-06 21:50:34 +01:00
const NRVec< std::complex<double> > diagofproduct(const NRMat< std::complex<double> > &a,
const NRMat< std::complex<double> > &b, bool trb, bool conjb)
2004-03-17 04:07:21 +01:00
{
if (trb && (a.nrows() != b.nrows() || a.ncols() != b.ncols()) ||
!trb && (a.nrows() != b.ncols() || a.ncols() != b.nrows()))
laerror("incompatible Mats in diagofproduct<complex>()");
2020-01-06 21:50:34 +01:00
NRVec< std::complex<double> > result(a.nrows());
2004-03-17 04:07:21 +01:00
if (trb) {
if (conjb) {
for(int i=0; i<a.nrows(); i++)
cblas_zdotc_sub(a.ncols(), b[i], 1, a[i], 1, &result[i]);
} else {
for(int i=0; i<a.nrows(); i++)
cblas_zdotu_sub(a.ncols(), b[i], 1, a[i], 1, &result[i]);
}
} else {
if (conjb) {
for(int i=0; i<a.nrows(); i++)
cblas_zdotc_sub(a.ncols(), b[0]+i, b.ncols(), a[i], 1, &result[i]);
} else {
for(int i=0; i<a.nrows(); i++)
cblas_zdotu_sub(a.ncols(), b[0]+i, b.ncols(), a[i], 1, &result[i]);
}
}
return result;
}
double trace2(const NRMat<double> &a, const NRMat<double> &b, bool trb)
{
if (trb && (a.nrows() != b.nrows() || a.ncols() != b.ncols()) ||
!trb && (a.nrows() != b.ncols() || a.ncols() != b.nrows()))
2006-10-22 10:39:24 +02:00
laerror("incompatible Mats in trace2()");
2004-03-17 04:07:21 +01:00
if (trb) return cblas_ddot(a.nrows()*a.ncols(), a, 1, b, 1);
double sum = 0.0;
for (int i=0; i<a.nrows(); i++)
sum += cblas_ddot(a.ncols(), a[i], 1, b[0]+i, b.ncols());
return sum;
}
2011-02-02 10:55:59 +01:00
// LV
2020-01-06 21:50:34 +01:00
std::complex<double> trace2(const NRMat<std::complex<double> > &a, const NRMat<std::complex<double> > &b, bool adjb)
2011-02-02 10:55:59 +01:00
{
if (adjb && (a.nrows() != b.nrows() || a.ncols() != b.ncols()) ||
!adjb && (a.nrows() != b.ncols() || a.ncols() != b.nrows()))
laerror("incompatible Mats in trace2()");
2020-01-06 21:50:34 +01:00
std::complex<double> dot;
2011-02-02 10:55:59 +01:00
if (adjb) { cblas_zdotc_sub(a.nrows()*a.ncols(), b, 1, a, 1, &dot); return dot; }
2020-01-06 21:50:34 +01:00
std::complex<double> sum = std::complex<double>(0.,0.);
2011-02-02 10:55:59 +01:00
for (int i=0; i<a.nrows(); i++) {
cblas_zdotu_sub(a.ncols(), a[i], 1, b[0]+i, b.ncols(), &dot);
sum += dot;
}
return sum;
}
2004-03-17 04:07:21 +01:00
double trace2(const NRSMat<double> &a, const NRSMat<double> &b,
const bool diagscaled)
{
if (a.nrows() != b.nrows()) laerror("incompatible SMats in trace2()");
2006-10-22 10:39:24 +02:00
//double r = 0; for (int i=0; i<a.nrows()*(a.nrows()+1)/2; ++i) r += a[i]*b[i]; r+=r;
2004-03-17 04:07:21 +01:00
double r = 2.0*cblas_ddot(a.nrows()*(a.nrows()+1)/2, a, 1, b, 1);
if (diagscaled) return r;
2006-10-22 10:39:24 +02:00
for (int i=0; i<a.nrows(); i++) r -= a(i,i)*b(i,i);
//r -= cblas_ddot(a.nrows(),a,a.nrows()+1,b,a.nrows()+1); //@@@this was errorneous in one version of ATLAS
2004-03-17 04:07:21 +01:00
return r;
}
2006-09-04 01:31:00 +02:00
double trace2(const NRSMat<double> &a, const NRMat<double> &b, const bool diagscaled)
{
if (a.nrows() != b.nrows()||b.nrows()!=b.ncols()) laerror("incompatible SMats in trace2()");
double r=0;
int i, j, k=0;
for (i=0; i<a.nrows(); i++)
for (j=0; j<=i;j++) r += a[k++] * (b[i][j] + (i!=j||diagscaled ? b[j][i] : 0 ));
return r;
}
2019-11-13 23:22:25 +01:00
inline double trace2(const NRMat<double> &a, const NRSMat<double> &b, const bool diagscaled)
{
return trace2(b,a,diagscaled);
}
2006-09-04 01:31:00 +02:00
2010-01-07 17:10:12 +01:00
//Cholesky interface
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dpotrf)(const char *UPLO, const FINT *N, double *A, const FINT *LDA, FINT *INFO);
2020-01-06 21:50:34 +01:00
extern "C" void FORNAME(zpotrf)(const char *UPLO, const FINT *N, std::complex<double> *A, const FINT *LDA, FINT *INFO);
2010-01-07 17:10:12 +01:00
void cholesky(NRMat<double> &a, bool upper)
{
if(a.nrows()!=a.ncols()) laerror("matrix must be square in Cholesky");
2010-06-25 17:28:19 +02:00
FINT lda=a.ncols();
FINT n=a.nrows();
2013-11-04 15:56:39 +01:00
char uplo= LAPACK_FORTRANCASE(upper?'u':'l');
2010-06-25 17:28:19 +02:00
FINT info;
2010-01-07 17:10:12 +01:00
a.copyonwrite();
FORNAME(dpotrf)(&uplo, &n, a, &lda, &info);
if(info) {std::cerr << "Lapack error "<<info<<std::endl; laerror("error in Cholesky");}
//zero the other triangle and switch to C array order
if(upper)
for(int i=0; i<n; ++i) for(int j=0; j<i; ++j) {a(j,i)=a(i,j); a(i,j)=0.;}
else
for(int i=0; i<n; ++i) for(int j=0; j<i; ++j) {a(i,j)=a(j,i); a(j,i)=0.;}
}
2020-01-06 21:50:34 +01:00
void cholesky(NRMat<std::complex<double> > &a, bool upper)
2010-01-07 17:10:12 +01:00
{
if(a.nrows()!=a.ncols()) laerror("matrix must be square in Cholesky");
2010-06-25 17:28:19 +02:00
FINT lda=a.ncols();
FINT n=a.nrows();
2013-11-04 15:56:39 +01:00
char uplo= LAPACK_FORTRANCASE(upper?'u':'l');
2010-06-25 17:28:19 +02:00
FINT info;
2010-01-07 17:10:12 +01:00
a.copyonwrite();
a.transposeme();//switch to Fortran order
FORNAME(zpotrf)(&uplo, &n, a, &lda, &info);
if(info) {std::cerr << "Lapack error "<<info<<std::endl; laerror("error in Cholesky");}
//zero the other triangle and switch to C array order
if(upper)
for(int i=0; i<n; ++i) for(int j=0; j<i; ++j) {a(j,i)=a(i,j); a(i,j)=0.;}
else
for(int i=0; i<n; ++i) for(int j=0; j<i; ++j) {a(i,j)=a(j,i); a(j,i)=0.;}
}
2010-01-17 21:28:38 +01:00
//various norms
2020-01-06 21:50:34 +01:00
extern "C" double FORNAME(zlange)( const char *NORM, const FINT *M, const FINT *N, std::complex<double> *A, const FINT *LDA, double *WORK);
2010-06-25 17:28:19 +02:00
extern "C" double FORNAME(dlange)( const char *NORM, const FINT *M, const FINT *N, double *A, const FINT *LDA, double *WORK);
2010-01-17 21:28:38 +01:00
2020-01-06 21:50:34 +01:00
double MatrixNorm(NRMat<std::complex<double> > &A, const char norm)
2010-01-17 21:28:38 +01:00
{
const char TypNorm = (tolower(norm) == 'o')?'I':'O'; //switch c-order/fortran-order
2010-06-25 17:28:19 +02:00
const FINT M = A.nrows();
const FINT N = A.ncols();
2010-01-17 21:28:38 +01:00
double work[M];
const double ret = FORNAME(zlange)(&TypNorm, &M, &N, A[0], &M, &work[0]);
return ret;
}
double MatrixNorm(NRMat<double > &A, const char norm)
{
const char TypNorm = (tolower(norm) == 'o')?'I':'O'; //switch c-order/fortran-order
2010-06-25 17:28:19 +02:00
const FINT M = A.nrows();
const FINT N = A.ncols();
2010-01-17 21:28:38 +01:00
double work[M];
const double ret = FORNAME(dlange)(&TypNorm, &M, &N, A[0], &M, &work[0]);
return ret;
}
2010-01-07 17:10:12 +01:00
2005-01-31 00:49:50 +01:00
2010-01-17 21:28:38 +01:00
//condition number
2020-01-06 21:50:34 +01:00
extern "C" void FORNAME(zgecon)( const char *norm, const FINT *n, std::complex<double> *A, const FINT *LDA, const double *anorm, double *rcond, std::complex<double> *work, double *rwork, FINT *info);
2010-06-25 17:28:19 +02:00
extern "C" void FORNAME(dgecon)( const char *norm, const FINT *n, double *A, const FINT *LDA, const double *anorm, double *rcond, double *work, double *rwork, FINT *info);
2010-01-17 21:28:38 +01:00
2020-01-06 21:50:34 +01:00
double CondNumber(NRMat<std::complex<double> > &A, const char norm)
2010-01-17 21:28:38 +01:00
{
const char TypNorm = (tolower(norm) == 'o')?'I':'O'; //switch c-order/fortran-order
2010-06-25 17:28:19 +02:00
const FINT N = A.nrows();
2010-01-17 21:28:38 +01:00
double Norma(0.0), ret(0.0);
2010-06-25 17:28:19 +02:00
FINT info;
2020-01-06 21:50:34 +01:00
std::complex<double> *work;
2010-01-17 21:28:38 +01:00
double *rwork;
if(N != A.ncols()){
laerror("nonsquare matrix in zgecon");
return 0.0;
}
2020-01-06 21:50:34 +01:00
work = new std::complex<double>[2*N];
2010-01-17 21:28:38 +01:00
rwork = new double[2*N];
Norma = MatrixNorm(A, norm);
FORNAME(zgecon)(&TypNorm, &N, A[0], &N, &Norma, &ret, &work[0], &rwork[0], &info);
delete[] work;
delete[] rwork;
return ret;
}
double CondNumber(NRMat<double> &A, const char norm)
{
const char TypNorm = (tolower(norm) == 'o')?'I':'O'; //switch c-order/fortran-order
2010-06-25 17:28:19 +02:00
const FINT N = A.nrows();
2010-01-17 21:28:38 +01:00
double Norma(0.0), ret(0.0);
2010-06-25 17:28:19 +02:00
FINT info;
2010-01-17 21:28:38 +01:00
double *work;
double *rwork;
if(N != A.ncols()){
laerror("nonsquare matrix in zgecon");
return 0.0;
}
work = new double[2*N];
rwork = new double[2*N];
Norma = MatrixNorm(A, norm);
FORNAME(dgecon)(&TypNorm, &N, A[0], &N, &Norma, &ret, &work[0], &rwork[0], &info);
delete[] work;
delete[] rwork;
return ret;
}
2005-02-17 23:54:27 +01:00
#ifdef obsolete
2005-02-01 00:08:03 +01:00
void gendiagonalize(NRMat<double> &a, NRVec<double> &w, NRMat<double> b, int n)
2005-01-31 00:49:50 +01:00
{
if(a.nrows()!=a.ncols() || a.nrows()!=w.size() || a.nrows()!=b.nrows() || b.nrows()!=b.ncols() ) laerror("incompatible Mats in gendiagonalize");
a.copyonwrite();
w.copyonwrite();
b.copyonwrite();
2005-02-01 00:08:03 +01:00
int m=w.size();
NRVec<double> dl(m);
2005-01-31 00:49:50 +01:00
int i,j;
double x;
2005-02-01 00:08:03 +01:00
if(n==0) n=m;
if(n<0 || n>m) laerror("actual dimension in gendiagonalize out of range");
2005-01-31 00:49:50 +01:00
//transform the problem to usual diagonalization
2005-02-04 10:58:36 +01:00
//cholesky decompose in b and dl
2005-01-31 00:49:50 +01:00
for(i=0; i<n; ++i)
{
for(j=i; j<n; ++j)
{
x = b(i,j) - cblas_ddot(i,&b(i,0),1,&b(j,0),1);
if(i==j)
{
if(x<=0) laerror("not positive definite metric in gendiagonalize");
2009-11-12 22:01:19 +01:00
dl[i] = std::sqrt(x);
2005-01-31 00:49:50 +01:00
}
else
b(j,i) = x / dl[i];
}
}
2005-02-04 10:58:36 +01:00
// form the transpose of the upper triangle of inv(l)*a in the lower triangle of a
2005-01-31 00:49:50 +01:00
for(i=0; i<n; ++i)
{
for(j=i; j<n ; ++j)
{
x = a(i,j) - cblas_ddot(i,&b(i,0),1,&a(j,0),1);
a(j,i) = x/dl[i];
}
}
2005-02-04 10:58:36 +01:00
//pre-multiply by l^-1
2005-01-31 00:49:50 +01:00
for(j=0; j<n ; ++j)
{
for(i=j;i<n;++i)
{
2005-02-01 00:08:03 +01:00
x = a(i,j) - cblas_ddot(i-j,&a(j,j),m,&b(i,j),1)
2005-01-31 00:49:50 +01:00
- cblas_ddot(j,&a(j,0),1,&b(i,0),1);
a(i,j) = x/dl[i];
}
}
//fill in upper triangle of a for the diagonalize procedure (would not be needed with tred2,tql2)
for(i=1;i<n;++i) for(j=0; j<i; ++j) a(j,i)=a(i,j);
//diagonalize by a standard procedure
2005-02-01 00:08:03 +01:00
diagonalize(a,w,1,1,n);
2005-01-31 00:49:50 +01:00
//transform the eigenvectors back
for(j=0; j<n; ++j)//eigenvector loop
{
for(int i=n-1; i>=0; --i)//component loop
{
2005-02-01 00:08:03 +01:00
if(i<n-1) a(i,j) -= cblas_ddot(n-1-i,&b(i+1,i),m,&a(i+1,j),m);
2005-01-31 00:49:50 +01:00
a(i,j) /= dl[i];
}
}
}
2005-02-17 23:54:27 +01:00
#endif
//obsolete
2005-02-06 15:01:27 +01:00
//auxiliary routine to adjust eigenvectors to guarantee real logarithm
//at the moment not rigorous yet
void adjustphases(NRMat<double> &v)
{
int n=v.nrows();
double det=determinant(v);
int nchange=0;
for(int i=0; i<n;++i) if(v[i][i]<0.)
{
cblas_dscal(n,-1.,v[i],1);
nchange++;
}
if(det<0) nchange++;
if(nchange&1)//still adjust to get determinant=1
{
int imin=-1; double min=1e200;
for(int i=0; i<n;++i)
2009-11-12 22:01:19 +01:00
if(std::abs(v[i][i])<min)
2005-02-06 15:01:27 +01:00
{
imin=i;
2009-11-12 22:01:19 +01:00
min=std::abs(v[i][i]);
2005-02-06 15:01:27 +01:00
}
cblas_dscal(n,-1.,v[imin],1);
}
}
2009-11-12 22:01:19 +01:00
}//namespace
2013-11-04 15:56:39 +01:00