29 int Choleski(
int N,
double** L,
double** A);
32 double**
Copy(
int M,
int N,
double** Z,
double** A,
MList* List=0);
33 double**
Copy(
int M,
int N,
double** A,
MList* List=0);
34 double**
Copy(
int N,
double** Z,
double ** A,
MList* List=0);
35 double**
Copy(
int N,
double ** A,
MList* List=0);
40 double*
Copy(
int N,
double* z,
double* a,
MList* List=0);
41 double*
Copy(
int N,
double* a,
MList* List=0);
46 double det(
int N,
double** A,
int mode=0);
50 int EigPower(
int N,
double& l,
double* x,
double** A,
double ep=1e-6,
int maxiter=50);
51 int EigPowerA(
int N,
double& l,
double* x,
double** A,
double ep=1e-6,
int maxiter=50);
52 int EigPowerI(
int N,
double& l,
double* x,
double** A,
double ep=1e-6,
int maxiter=50);
53 int EigPowerS(
int N,
double& l,
double* x,
double** A,
double ep=1e-6,
int maxiter=50);
54 int EigPowerWielandt(
int N,
double& m,
double* u,
double l,
double* v,
double** A,
double ep=1e-06,
int maxiter=50);
56 int EigSym(
int N,
double** A,
double* d,
double** Q);
59 int GEB(
int N,
double* x,
double** A,
double* b);
60 int GESCP(
int N,
double* x,
double** A,
double*b);
61 void GExL(
int N,
double* x,
double** L,
double* a);
62 void GExLAdd(
int N,
double* x,
double** L,
double* a);
63 void GExL1(
int N,
double* x,
double** L,
double a);
64 void GExL1Add(
int N,
double* x,
double** L,
double a);
74 template<
class T,
class Ta>
int GECP(
int N, T* x, Ta** A, T *b, Ta* logdet=0)
76 if (logdet) *logdet=1E-302;
int c, p, ip, *rp=
new int[N];
for (
int i=0; i<N; i++) rp[i]=i;
79 for (
int i=0; i<N-1; i++)
82 while (ip<N){
if (fabs(A[rp[ip]][i])>fabs(A[rp[p]][i])) p=ip; ip++;}
83 if (A[rp[p]][i]==0) {
delete[] rp;
return 1;}
84 if (p!=i) {c=rp[i]; rp[i]=rp[p]; rp[p]=c;}
85 for (
int j=i+1; j<N; j++)
87 m=A[rp[j]][i]/A[rp[i]][i];
89 for (
int k=i+1; k<N; k++) A[rp[j]][k]-=m*A[rp[i]][k];
93 if (A[rp[N-1]][N-1]==0) {
delete[] rp;
return 1;}
95 x[N-1]=b[rp[N-1]]/A[rp[N-1]][N-1];
96 for (
int i=N-2; i>=0; i--)
98 x[i]=b[rp[i]];
for (
int j=i+1; j<N; j++) x[i]-=A[rp[i]][j]*x[j]; x[i]/=A[rp[i]][i];
100 if (logdet){*logdet=log(fabs(A[rp[0]][0]));
for (
int n=1; n<N; n++) *logdet+=log(fabs(A[rp[n]][n]));}
106 double GILT(
int N,
double** A);
107 double GIUT(
int N,
double** A);
110 double GICP(
int N,
double** X,
double** A);
111 double GICP(
int N,
double** A);
113 double GISCP(
int N,
double** X,
double** A);
114 double GISCP(
int N,
double** A);
117 int GSI(
int N,
double* x0,
double** A,
double* b,
double ep=1e-4,
int maxiter=50);
120 void Hessenb(
int n,
double** A);
124 void HouseHolder(
int N,
double** T,
double** Q,
double** A);
125 void HouseHolder(
int n,
double **A,
double* d,
double* sd);
128 double Inner(
int N,
double* x,
double* y);
133 double Inner(
int M,
int N,
double** X,
double** Y);
145 for (
int i=0; i<N; i++) result+=(x[i]*w[i])**y[i];
148 template<
class Tx,
class Tw>
cdouble Inner(
int N, Tx* x, Tw* w,
double* y)
151 for (
int i=0; i<N; i++) result+=x[i]*w[i]*y[i];
156 int JI(
int N,
double* x,
double** A,
double* b,
double ep=1e-4,
int maxiter=50);
159 int LDL(
int N,
double** L,
double* d,
double** A);
162 void LQ_GS(
int M,
int N,
double** A,
double** L,
double** Q);
173 template <
class T> T LSLinear(
int M,
int N, T* x, T** A, T* y)
176 T** AtA=MultiplyXtX(N, M, A, mlist);
177 T* Aty=MultiplyXty(N, M, A, y, mlist);
178 T result; GECP(N, x, AtA, Aty, &result);
184 void LSLinear2(
int M,
int N,
double* x,
double** A,
double* y);
187 int LU_Direct(
int mode,
int N,
double* diag,
double** a);
188 int LU(
int N,
double** l,
double** u,
double** a);
void LU_DiagL(
int N,
double** l,
double* diagl,
double** u,
double** a);
189 int LU_PD(
int N,
double** b);
190 int LU_PD(
int N,
double** b,
double* c);
191 double LUCP(
double **a,
int N,
int *ind,
int& parity,
int mode=1);
194 void LU(
int N,
double* x,
double** A,
double* y,
int* ind=0);
197 int maxind(
double* data,
int from,
int to);
208 template<
class Tz,
class Tx,
class Ty,
class Ta> Tz**
MultiAdd(
int M,
int N, Tz** Z, Tx** X, Ty** Y, Ta a,
MList* List=0)
210 if (!Z){Allocate2(Tz, M, N, Z);
if (List) List->Add(Z, 2);}
211 for (
int i=0; i<M; i++)
for (
int j=0; j<N; j++) Z[i][j]=X[i][j]+a*Y[i][j];
223 template<
class Tz,
class Tx,
class Ty,
class Ta> Tz*
MultiAdd(
int N, Tz* z, Tx* x, Ty* y, Ta a,
MList* List=0)
225 if (!z){z=
new Tz[N];
if (List) List->Add(z, 1);}
226 for (
int i=0; i<N; i++) z[i]=x[i]+Ty(a)*y[i];
229 template<
class Tx,
class Ty,
class Ta> Tx*
MultiAdd(
int N, Tx* x, Ty* y, Ta a,
MList* List=0)
231 return MultiAdd(N, (Tx*)0, x, y, a, List);
243 template<
class Tz,
class Tx,
class Ta> Tz** Multiply(
int M,
int N, Tz** Z, Tx** X, Ta a,
MList* List=0)
245 if (!Z){Allocate2(Tz, M, N, Z);
if (List) List->Add(Z, 2);}
246 for (
int i=0; i<M; i++)
for (
int j=0; j<N; j++) Z[i][j]=a*X[i][j];
258 template<
class Tz,
class Tx,
class Ta> Tz* Multiply(
int N, Tz* z, Tx* x, Ta a,
MList* List=0)
260 if (!z){z=
new Tz[N];
if (List) List->Add(z, 1);}
261 for (
int i=0; i<N; i++) z[i]=x[i]*a;
264 template<
class Tx,
class Ta>Tx* Multiply(
int N, Tx* x, Ta a,
MList* List=0)
266 return Multiply(N, (Tx*)0, x, a, List);
286 #define Multiply_full(MULTIPLY, MULTIADD, xx, yy) \ 287 template<class Tx, class Ty>Tx** MULTIPLY(int N, int M, int K, Tx** z, Tx** x, Ty** y, MList* List=0){ \ 288 if (!z) {Allocate2(Tx, N, K, z); if (List) List->Add(z, 2);} \ 289 for (int n=0; n<N; n++) for (int k=0; k<K; k++){ \ 290 Tx zz=0; for (int m=0; m<M; m++) zz+=xx*yy; z[n][k]=zz;} return z;} \ 291 template<class Tx, class Ty>Tx** MULTIPLY(int N, int M, int K, Tx** x, Ty** y, MList* List=0){ \ 292 Tx** Allocate2(Tx, N, K, z); if (List) List->Add(z, 2); \ 293 for (int n=0; n<N; n++) for (int k=0; k<K; k++){ \ 294 Tx zz=0; for (int m=0; m<M; m++) zz+=xx*yy; z[n][k]=zz;} return z;} \ 295 template<class Tx, class Ty>void MULTIADD(int N, int M, int K, Tx** z, Tx** x, Ty** y){ \ 296 for (int n=0; n<N; n++) for (int k=0; k<K; k++){ \ 297 Tx zz=0; for (int m=0; m<M; m++) zz+=xx*yy; z[n][k]+=zz;}} 298 Multiply_full(MultiplyXY, MultiAddXY, x[n][m], y[m][k])
299 Multiply_full(MultiplyXYc, MultiAddXYc, x[n][m], *y[m][k])
300 Multiply_full(MultiplyXYt, MultiAddXYt, x[n][m], y[k][m])
301 Multiply_full(MultiplyXYh, MultiAddXYh, x[n][m], *y[k][m])
312 #define Multiply_square(MULTIPLY, MULTIADD) \ 313 template<class T>T** MULTIPLY(int N, T** z, T** x, T** y, MList* List=0){ \ 314 if (!z){Allocate2(T, N, N, z); if (List) List->Add(z, 2);} \ 315 if (z!=x) MULTIPLY(N, N, N, z, x, y); else{ \ 316 T* tmp=new T[N]; int sizeN=sizeof(T)*N; for (int i=0; i<N; i++){ \ 317 MULTIPLY(1, N, N, &tmp, &x[i], y); memcpy(z[i], tmp, sizeN);} delete[] tmp;} \ 319 template<class T>T** MULTIPLY(int N, T** x, T** y, MList* List=0){return MULTIPLY(N, N, N, x, y, List);}\ 320 template<class T>void MULTIADD(int N, T** z, T** x, T** y){MULTIADD(N, N, N, z, x, y);} 321 Multiply_square(MultiplyXY, MultiAddXY)
331 #define Multiply_xx(MULTIPLY, MULTIADD, xx1, xx2, zzt) \ 332 template<class T>T** MULTIPLY(int M, int N, T** z, T** x, MList* List=0){ \ 333 if (!z){Allocate2(T, M, M, z); if (List) List->Add(z, 2);} \ 334 for (int m=0; m<M; m++) for (int k=0; k<=m; k++){ \ 335 T zz=0; for (int n=0; n<N; n++) zz+=xx1*xx2; z[m][k]=zz; if (m!=k) z[k][m]=zzt;} \ 337 template<class T>T** MULTIPLY(int M, int N, T ** x, MList* List=0){ \ 338 T** Allocate2(T, M, M, z); if (List) List->Add(z, 2); \ 339 for (int m=0; m<M; m++) for (int k=0; k<=m; k++){ \ 340 T zz=0; for (int n=0; n<N; n++) zz+=xx1*xx2; z[m][k]=zz; if (m!=k) z[k][m]=zzt;} \ 342 template<class T>void MULTIADD(int M, int N, T** z, T** x){ \ 343 for (int m=0; m<M; m++) for (int k=0; k<=m; k++){ \ 344 T zz=0; for (int n=0; n<N; n++) zz+=xx1*xx2; z[m][k]+=zz; if (m!=k) z[k][m]+=zzt;}} 345 Multiply_xx(MultiplyXtX, MultiAddXtX, x[n][m], x[n][k], zz)
346 Multiply_xx(MultiplyXXt, MultiAddXXt, x[m][n], x[k][n], zz)
347 Multiply_xx(MultiplyXhX, MultiAddXhX, *x[n][m], x[n][k], *zz)
348 Multiply_xx(MultiplyXXh, MultiAddXXh, x[m][n], *x[k][n], *zz)
349 Multiply_xx(MultiplyXcXt, MultiAddXcXt, *x[m][n], x[k][n], *zz)
352 double* MultiplyXy(
int M,
int N,
double* z,
double** x,
double* y,
MList* List=0);
353 double* MultiplyXy(
int M,
int N,
double** x,
double* y,
MList* List=0);
358 double* MultiplyxY(
int M,
int N,
double* zt,
double* xt,
double** y,
MList* List=0);
359 double* MultiplyxY(
int M,
int N,
double* xt,
double** y,
MList* List=0);
362 double* MultiplyXty(
int M,
int N,
double* z,
double** x,
double* y,
MList* List=0);
363 double* MultiplyXty(
int M,
int N,
double** x,
double* y,
MList* List=0);
372 double* MultiplyxYt(
int M,
int N,
double* zt,
double* xt,
double** y,
MList*
MList=0);
373 double* MultiplyxYt(
int M,
int N,
double* xt,
double** y, MList* MList=0);
376 double Norm1(
int N,
double** A);
379 void QR_GS(
int M,
int N,
double** A,
double** Q,
double** R);
380 void QR_householder(
int M,
int N,
double** A,
double** Q,
double** R);
383 int QL(
int N,
double* d,
double* sd,
double** z);
387 void QU(
int N,
double** Q,
double** A);
390 double**
Real(
int M,
int N,
double** z,
cdouble** x, MList* List);
391 double**
Real(
int M,
int N,
cdouble** x, MList* List);
394 int Roots(
int N,
double* p,
double* rr,
double* ri);
397 int SorI(
int N,
double* x0,
double** a,
double* b,
double w=1,
double ep=1e-4,
int maxiter=50);
400 void SetSubMatrix(
double** z,
double** x,
int Y1,
int Y,
int X1,
int X);
410 double**
transpose(
int N,
int M,
double** ta,
double** a, MList* List=0);
411 double**
transpose(
int N,
int M,
double** a, MList* List=0);
414 double**
Unitary(
int N,
double** P,
double* x,
double* y, MList* List=0);
415 double**
Unitary(
int N,
double* x,
double* y, MList* List=0);
void QR_householder(int M, int N, double **A, double **Q, double **R)
Definition: matrix.cpp:1950
void SetSubMatrix(double **z, double **x, int Y1, int Y, int X1, int X)
Definition: matrix.cpp:2104
cdouble * SubVector(cdouble *z, cdouble *x, int X1, int X, MList *List=0)
Definition: matrix.cpp:2142
void GExL1Add(int N, double *x, double **L, double a)
Definition: matrix.cpp:619
void GExLAdd(int N, double *x, double **L, double *a)
Definition: matrix.cpp:583
int EigPowerI(int N, double &l, double *x, double **A, double ep=1e-6, int maxiter=50)
Definition: matrix.cpp:329
int LU(int N, double **l, double **u, double **a)
Definition: matrix.cpp:1318
int EigPowerA(int N, double &l, double *x, double **A, double ep=1e-6, int maxiter=50)
Definition: matrix.cpp:295
void LSLinear2(int M, int N, double *x, double **A, double *y)
Definition: matrix.cpp:1277
double Inner(int N, double *x, double *y)
Definition: matrix.cpp:1120
Definition: arrayalloc.h:83
void QU(int N, double **Q, double **A)
Definition: matrix.cpp:1988
double GILT(int N, double **A)
Definition: matrix.cpp:730
int Choleski(int N, double **L, double **A)
Definition: matrix.cpp:89
int GSI(int N, double *x0, double **A, double *b, double ep=1e-4, int maxiter=50)
Definition: matrix.cpp:860
double det(int N, double **A, int mode=0)
Definition: matrix.cpp:179
double ** Copy(int M, int N, double **Z, double **A, MList *List=0)
Definition: matrix.cpp:120
double GIUT(int N, double **A)
Definition: matrix.cpp:756
int EigPowerWielandt(int N, double &m, double *u, double l, double *v, double **A, double ep=1e-06, int maxiter=50)
Definition: matrix.cpp:403
double ** Real(int M, int N, double **z, cdouble **x, MList *List)
Definition: matrix.cpp:2018
double LUCP(double **a, int N, int *ind, int &parity, int mode=1)
Definition: matrix.cpp:1555
void GExL1(int N, double *x, double **L, double a)
Definition: matrix.cpp:599
int SorI(int N, double *x0, double **a, double *b, double w=1, double ep=1e-4, int maxiter=50)
Definition: matrix.cpp:2070
void BalanceSim(int n, double **A)
Definition: matrix.cpp:31
int EigPowerS(int N, double &l, double *x, double **A, double ep=1e-6, int maxiter=50)
Definition: matrix.cpp:369
int JI(int N, double *x, double **A, double *b, double ep=1e-4, int maxiter=50)
Definition: matrix.cpp:1179
int EigenValues(int N, double **A, cdouble *ev)
Definition: matrix.cpp:442
int QL(int N, double *d, double *sd, double **z)
Definition: matrix.cpp:1723
void HouseHolder(int N, double **A)
Definition: matrix.cpp:948
int GESCP(int N, double *x, double **A, double *b)
Definition: matrix.cpp:518
double ** Unitary(int N, double **P, double *x, double *y, MList *List=0)
Definition: matrix.cpp:2205
int LU_PD(int N, double **b)
Definition: matrix.cpp:1458
void MultiAdd(double *out, double *in, double *adder, int count, double mul)
Definition: procedures.cpp:2148
int maxind(double *data, int from, int to)
Definition: matrix.cpp:1640
cdouble ** SubMatrix(cdouble **z, cdouble **x, int Y1, int Y, int X1, int X, MList *List=0)
Definition: matrix.cpp:2122
void Hessenb(int n, double **A)
Definition: matrix.cpp:892
Definition: xcomplex.h:26
int EigSym(int N, double **A, double *d, double **Q)
Definition: matrix.cpp:457
int EigPower(int N, double &l, double *x, double **A, double ep=1e-6, int maxiter=50)
Definition: matrix.cpp:262
int QR(int N, double **A, cdouble *ev)
Definition: matrix.cpp:1791
double GISCP(int N, double **X, double **A)
Definition: matrix.cpp:841
int GEB(int N, double *x, double **A, double *b)
Definition: matrix.cpp:476
int LDL(int N, double **L, double *d, double **A)
Definition: matrix.cpp:1216
void transpose(int N, double **a)
Definition: matrix.cpp:2163
void LQ_GS(int M, int N, double **A, double **L, double **Q)
Definition: matrix.cpp:1247
double GICP(int N, double **A)
Definition: matrix.cpp:636
int LU_Direct(int mode, int N, double *diag, double **a)
Definition: matrix.cpp:1403
void QR_GS(int M, int N, double **A, double **Q, double **R)
Definition: matrix.cpp:1922
void GExL(int N, double *x, double **L, double *a)
Definition: matrix.cpp:565