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 215 216 217 218
|
#ifndef _LINALG_H
#define _LINALG_H
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
extern "C" {
#include "f2c.h"
#include "blaswrap.h"
#include "clapack.h"
}
#include "Fmatrix.h"
//these scalar ops (+-*/) won't work for complex
template <class T>
const Fmatrix<T> operator + (const Fmatrix<T>& A, const Fmatrix<T>& B)
{ return Fmatrix<T>(A)+=B; }
template <class T>
const Fmatrix<T> operator - (const Fmatrix<T>& A, const Fmatrix<T>& B)
{ return Fmatrix<T>(A)+=B; }
template <class T>
const Fmatrix<T> operator * (const T alpha, const Fmatrix<T>& A)
{ return Fmatrix<T>(A)*=alpha; }
template <class T>
const Fmatrix<T> operator / (const Fmatrix<T>& A, const T beta)
{ return Fmatrix<T>(A)/=beta; }
template <class T>
const Fvector<T> operator * ( Fmatrix<T>& A, Fvector<T>& x)
{
Fvector<T> y(A.numrows());
gmv(A,x,y);
return y;
}
template <class T>
const Fmatrix<T> operator * ( Fmatrix<T>& A, Fmatrix<T>& B)
{
Fmatrix<T> C(A.numrows(),B.numcols());
gmm(A,B,C);
return C;
}
template <class T>
int gmv(Fmatrix<T>& A, Fvector<T>& x, Fvector<T>& y, char TRANSA='N',
T alpha=1, T beta=0, integer INCX=1, integer INCY=1)
{
integer M, N, LDA;
M=(TRANSA=='N') ? A.numrows():A.numcols();
N=(TRANSA=='N') ? A.numcols():A.numrows();
LDA=M;
//this still needs a switch on data type
dgemv_(&TRANSA, &M, &N,
(doublereal*)&alpha,
(doublereal*)A.begin(), &LDA,
(doublereal*)x.begin(), &INCX,
(doublereal*)&beta,
(doublereal*)y.begin(), &INCY);
}
template <class T>
int gmm(Fmatrix<T>& A, Fmatrix<T>& B, Fmatrix<T>& C,
char TRANSA='N', char TRANSB='N', T alpha=1, T beta=0)
{
integer M, N, K, LDA, LDB, LDC;
M=(TRANSA=='N') ? A.numrows():A.numcols();
N=(TRANSB=='N') ? B.numcols():B.numrows();
K=(TRANSA=='N') ? A.numcols():A.numrows();
LDA=(TRANSA=='N') ? M:K;
LDB=(TRANSB=='N') ? K:N;
LDC=C.numrows();
switch(A.gettype()){//cast explicitly otherwise compiler is cranky
case 'f':
sgemm_(&TRANSA, &TRANSB, &M, &N, &K,
(real*)&alpha,
(real*)A.begin(), &LDA,
(real*)B.begin(), &LDB,
(real*)&beta,
(real*)C.begin(), &LDC);
break;
case 'd':
dgemm_(&TRANSA, &TRANSB, &M, &N, &K,
(doublereal*)&alpha,
(doublereal*)A.begin(), &LDA,
(doublereal*)B.begin(), &LDB,
(doublereal*)&beta,
(doublereal*)C.begin(), &LDC);
break;
case 'c':
cgemm_(&TRANSA, &TRANSB, &M, &N, &K,
(complex*)&alpha,
(complex*)A.begin(), &LDA,
(complex*)B.begin(), &LDB,
(complex*)&beta,
(complex*)C.begin(), &LDC);
break;
case 'z':
zgemm_(&TRANSA, &TRANSB, &M, &N, &K,
(doublecomplex*)&alpha,
(doublecomplex*)A.begin(), &LDA,
(doublecomplex*)B.begin(), &LDB,
(doublecomplex*)&beta,
(doublecomplex*)C.begin(), &LDC);
break;
default:
std::cout << "\nERROR: Something wrong in GMM switch.\n\n";
}
return 0;
}
template <class T>
int pca(Fmatrix<T>& A, Fmatrix<T>& U, Fmatrix<T>& S, Fmatrix<T>& PC)
{
std::ofstream PCoutx("pcx"), PCouty("pcy"), PCoutz("pcz"), PCoutw("pcw");
PCoutx.precision(16); PCouty.precision(16);
PCoutz.precision(16); PCoutw.precision(16);
Fmatrix<T> Y(A.numcols(),A.numrows());
T s,*mean;
mean = new T[A.numrows()];
for(int i=0; i<A.numrows(); i++) mean<i>=0;
//find means
for(int i=0; i<A.numrows(); i++){ for(int j=0; j<A.numcols(); j++){
mean<i>+=A(i,j);
} }
for(int i=0; i<A.numrows(); i++) mean<i>/=A.numcols();
//subtract means from data
for(int i=0; i<A.numrows(); i++){ for(int j=0; j<A.numcols(); j++){
A(i,j)-=mean<i>;
} }
//form matrix for SVD
s=sqrt(A.numcols()-1);
for(int i=0; i<A.numrows(); i++){ for(int j=0; j<A.numcols(); j++){
Y(j,i)=A(i,j)/s;
} }
return svd(Y,U,S,PC);
}
template <class T>
int svd(Fmatrix<T>& A, Fmatrix<T>& U, Fmatrix<T>& S, Fmatrix<T>& V,
char JOBU='A', char JOBVT='A')
{
float *wkf,*rwkc;
double *wkd,*rwkz;
complex *wkc;
doublecomplex *wkz;
integer M, N, mn, MN, LDA, LDU, LDVT, LWORK, INFO;
M=A.numrows(); N=A.numcols(); LDA=M; LDU=M; LDVT=N;
mn=min(M,N);
MN=max(M,N);
LWORK=2*max(3*mn+MN,5*mn);
switch(A.gettype()){//cast explicitly otherwise compiler is cranky
case 'f':
wkf = new real[LWORK];
sgesvd_(&JOBU, &JOBVT, &M, &N,
(real*)A.begin(), &LDA,
(real*)S.begin(),
(real*)U.begin(), &LDU,
(real*)V.begin(), &LDVT,
wkf, &LWORK, &INFO);
delete[] wkf;
break;
case 'd':
wkd = new doublereal[LWORK];
dgesvd_(&JOBU, &JOBVT, &M, &N,
(doublereal*)A.begin(), &LDA,
(doublereal*)S.begin(),
(doublereal*)U.begin(), &LDU,
(doublereal*)V.begin(), &LDVT,
wkd, &LWORK, &INFO);
delete[] wkd;
break;
case 'c':
wkc = new complex[LWORK];
rwkc = new real[5*mn];
cgesvd_(&JOBU, &JOBVT, &M, &N,
(complex*)A.begin(), &LDA,
(real*)S.begin(),
(complex*)U.begin(), &LDU,
(complex*)V.begin(), &LDVT,
wkc, &LWORK, rwkc, &INFO);
delete[] wkc; delete[] rwkc;
break;
case 'z':
wkz = new doublecomplex[LWORK];
rwkz = new doublereal[5*mn];
zgesvd_(&JOBU, &JOBVT, &M, &N,
(doublecomplex*)A.begin(), &LDA,
(doublereal*)S.begin(),
(doublecomplex*)U.begin(), &LDU,
(doublecomplex*)V.begin(), &LDVT,
wkz, &LWORK, rwkz, &INFO);
delete[] wkz; delete[] rwkz;
break;
default:
std::cout << "\nERROR: Something wrong in SVD switch.\n\n";
}
return INFO;
}
#endif /* _LINALG_H */
|