6 for (i = 0; i <
RowN; i++) ResV[i] = 0.0;
7 for (j = 0; j <
ColN; j++) {
9 for (i = 0; i < len; i++) {
10 ResV[ColV[i].Key] += ColV[i].Dat * B(j,ColId);
17 int i, j;
TFlt *ResV = Result.
BegI();
18 for (i = 0; i <
RowN; i++) ResV[i] = 0.0;
19 for (j = 0; j <
ColN; j++) {
21 for (i = 0; i < len; i++) {
22 ResV[ColV[i].Key] += ColV[i].Dat * Vec[j];
29 int i, j, len;
TFlt *ResV = Result.
BegI();
30 for (j = 0; j <
ColN; j++) {
32 len = ColV.
Len(); ResV[j] = 0.0;
33 for (i = 0; i < len; i++) {
34 ResV[j] += ColV[i].Dat * B(ColV[i].Key, ColId);
41 int i, j, len;
TFlt *VecV = Vec.
BegI(), *ResV = Result.
BegI();
42 for (j = 0; j <
ColN; j++) {
44 len = ColV.
Len(); ResV[j] = 0.0;
45 for (i = 0; i < len; i++) {
46 ResV[j] += ColV[i].Dat * VecV[ColV[i].Key];
54 FILE *F = fopen(MatlabMatrixFNm.
CStr(),
"rt");
IAssert(F != NULL);
58 int row=-1, col=-1;
float val;
59 if (fscanf(F,
"%d %d %f\n", &row, &col, &val) == 3) {
71 for (
int row = 1; row <=
RowN; row++) {
72 while (cnt < MtxV.
Len() && MtxV[cnt].Val1 == row) {
81 for (
int i = 0; i <
ColN; i++) Result[i] = 0.0;
82 for (
int j = 0; j <
RowN; j++) {
84 for (
int i = 0; i < len; i++) {
85 Result[RowV[i].Key] += RowV[i].Dat * B(j,ColId);
92 for (
int i = 0; i <
ColN; i++) Result[i] = 0.0;
93 for (
int j = 0; j <
RowN; j++) {
95 for (
int i = 0; i < len; i++) {
96 Result[RowV[i].Key] += RowV[i].Dat * Vec[j];
103 for (
int j = 0; j <
RowN; j++) {
105 int len = RowV.
Len(); Result[j] = 0.0;
106 for (
int i = 0; i < len; i++) {
107 Result[j] += RowV[i].Dat * B(RowV[i].Key, ColId);
114 for (
int j = 0; j <
RowN; j++) {
116 int len = RowV.
Len(); Result[j] = 0.0;
117 for (
int i = 0; i < len; i++) {
118 Result[j] += RowV[i].Dat * Vec[RowV[i].Key];
128 for (
int i = 0; i <
ColN; i++) {
135 for (
int i = 0; i <
ColN; i++) {
142 for (
int i = 0; i <
ColN; i++) {
149 for (
int i = 0; i <
RowN; i++) { Result[i] = 0.0; }
150 for (
int i = 0; i <
ColN; i++) {
157 for (
int i = 0; i <
RowN; i++) { Result[i] = 0.0; }
158 for (
int i = 0; i <
ColN; i++) {
167 double result = 0.0;
int Len = x.
Len();
168 for (
int i = 0; i < Len; i++)
169 result += x[i] * y[i];
175 double result = 0.0, len = X.
GetRows();
176 for (
int i = 0; i < len; i++)
177 result = result + X(i,ColIdX) * Y(i,ColIdY);
183 double result = 0.0;
int Len = X.
GetRows();
184 for (
int i = 0; i < Len; i++)
185 result += X(i,ColId) * Vec[i];
190 const int xLen = x.
Len(), yLen = y.
Len();
191 double Res = 0.0;
int i1 = 0, i2 = 0;
192 while (i1 < xLen && i2 < yLen) {
193 if (x[i1].Key < y[i2].Key) i1++;
194 else if (x[i1].Key > y[i2].Key) i2++;
195 else { Res += x[i1].Dat * y[i2].Dat; i1++; i2++; }
201 double Res = 0.0;
const int xLen = x.
Len(), yLen = y.
Len();
202 for (
int i = 0; i < yLen; i++) {
203 const int key = y[i].Key;
204 if (key < xLen) Res += y[i].Dat * x[key];
210 double Res = 0.0;
const int n = X.
GetRows(), yLen = y.
Len();
211 for (
int i = 0; i < yLen; i++) {
212 const int key = y[i].Key;
213 if (key < n) Res += y[i].Dat * X(key,ColId);
222 const int Len = x.
Len();
223 for (
int i = 0; i < Len; i++) {
224 z[i] = p * x[i] + q * y[i]; }
240 const int xLen = x.
Len(), yLen = y.
Len();
241 for (
int i = 0; i < xLen; i++) {
242 const int ii = x[i].Key;
244 z[ii] = k * x[i].Dat + y[ii];
250 const int xLen = x.
Len(), yLen = y.
Len();
251 for (
int i = 0; i < xLen; i++) {
252 const int ii = x[i].Key;
254 y[ii] += k * x[i].Dat;
262 for (
int i = 0; i < len; i++) {
263 Y(i,ColIdY) = Y(i,ColIdY) + k * X(i, ColIdX);
269 const int len = Result.
Len();
270 for (
int i = 0; i < len; i++) {
271 Result[i] = Result[i] + k * X(i, ColId);
280 const int len = x.
Len();
282 for (
int i = 0; i < len; i++) {
290 const int len = x.
Len();
292 for (
int i = 0; i < len; i++) {
293 Res += k * x[i] + y[i];
300 const int len = x.
Len();
302 for (
int i = 0; i < len; i++) {
325 return sqrt(
Norm2(x));
329 const double xNorm =
Norm(x);
335 for (
int i = 0; i < x.
Len(); i++) {
342 return sqrt(
Norm2(x));
354 return sqrt(
Norm2(X, ColId));
358 double norm = 0.0;
const int Len = x.
Len();
359 for (
int i = 0; i < Len; i++)
366 double norm = 0.0;
const int len = x.
Len();
367 for (
int i = 0; i < len; i++) {
374 double norm = 0.0;
const int Len = x.
Len();
375 for (
int i = 0; i < Len; i++)
381 const double xNorm =
NormL1(x);
386 const double xNorm =
NormL1(x);
391 double norm = 0.0;
const int Len = x.
Len();
392 for (
int i = 0; i < Len; i++)
398 double norm = 0.0;
const int Len = x.
Len();
399 for (
int i = 0; i < Len; i++)
405 const double xNormLinf =
NormLinf(x);
410 const double xNormLInf =
NormLinf(x);
417 for (
int i = 0; i < Len; i++)
424 for (
int i = 0; i < Len; i++)
425 y[i].Dat = k * x[i].Dat;
431 for (
int i = 0; i < n; i++) {
433 for (
int j = 0; j < m; j++)
434 y[i] += A(i,j) * x[j];
441 for (
int i = 0; i < n; i++) {
443 for (
int j = 0; j < m; j++)
444 C(i,ColId) += A(i,j) * x[j];
451 for (
int i = 0; i < n; i++) {
453 for (
int j = 0; j < m; j++)
454 y[i] += A(i,j) * B(j,ColId);
461 for (
int i = 0; i < n; i++) {
463 for (
int j = 0; j < m; j++)
464 C(i,ColIdC) += A(i,j) * B(j,ColIdB);
471 for (
int i = 0; i < n; i++) {
473 for (
int j = 0; j < m; j++)
474 y[i] += A(j,i) * x[j];
482 for (
int i = 0; i < n; i++) {
483 for (
int j = 0; j < m; j++) {
485 for (
int k = 0; k < l; k++)
486 sum += A(i,k)*B(k,j);
494 const TFltVV& C,
TFltVV& D,
const int& TransposeFlags) {
514 bool Cnd = a_i == c_j && b_i == c_i && a_i == b_j && c_i == d_i && c_j == d_j;
519 double Aij, Bij, Cij;
522 for (
int j = 0; j < a_j; j++) {
524 for (
int i = 0; i < a_i; i++) {
527 for (
int k = 0; k < a_i; k++) {
528 Aij = tA ? A.
At(j, k) : A.
At(k, j);
529 Bij = tB ? B.
At(k, i) : B.
At(i, k);
530 sum += Alpha * Aij * Bij;
532 Cij = tC ? C.
At(i, j) : C.
At(j, i);
541 for (
int i = 0; i < A.
GetCols(); i++) {
542 for (
int j = 0; j < A.
GetRows(); j++) {
543 B.
At(i, j) = A.
At(j, i);
549 switch (DecompType) {
568 for (
int i = 0; i < E.
Len(); i++) {
573 for (
int i = 0; i < U.
GetCols(); i++) {
574 for (
int j = 0; j < V.
GetRows(); j++) {
576 for (
int k = 0; k < U.
GetCols(); k++) {
577 sum += E[k] * V.
At(i, k) * U.
At(j, k);
587 for (
int i = 0; i < m; i++) {
589 for (
int j = 0; j < i; j++) {
600 for (
int i = 0; i < m; i++) {
601 for (
int j = 0; j < i; j++) {
606 for (
int k = 0; k < n; k++)
607 Q(k,i) = Q(k,i) / nr;
611 void TLinAlg::Rotate(
const double& OldX,
const double& OldY,
const double& Angle,
double& NewX,
double& NewY) {
612 NewX = OldX*cos(Angle) - OldY*sin(Angle);
613 NewY = OldX*sin(Angle) + OldY*cos(Angle);
618 for (
int i = 0; i < m; i++) {
619 for (
int j = 0; j < i; j++) {
622 printf(
"<%d,%d> = %.5f", i,j,res);
624 double norm =
Norm2(Vecs[i]);
626 printf(
"||%d|| = %.5f", i, norm);
632 for (
int i = 0; i < m; i++) {
633 for (
int j = 0; j < i; j++) {
636 printf(
"<%d,%d> = %.5f", i, j, res);
638 double norm =
Norm2(Vecs, i);
640 printf(
"||%d|| = %.5f", i, norm);
648 return a == 0.0 ? 0.0 : a*a;
652 return b >= 0.0 ? fabs(a) : -fabs(a);
656 printf(
"NR_ERROR: %s", error_text.
CStr());
661 double absa = fabs(a), absb = fabs(b);
663 return absa*sqrt(1.0+
sqr(absb/absa));
665 return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+
sqr(absa/absb)));
670 double scale,hh,h,g,f;
676 scale += fabs(a(i-1,k-1).Val);
682 h += a(i-1,k-1)*a(i-1,k-1);
685 g=(f >= 0.0 ? -sqrt(h) : sqrt(h));
693 a(j-1,i-1)=a(i-1,j-1)/h;
696 g += a(j-1,k-1)*a(i-1,k-1);
698 g += a(k-1,j-1)*a(i-1,k-1);
700 f += e[j]*a(i-1,j-1);
707 a(j-1,k-1) -= (f*e[k]+g*a(i-1,k-1));
708 Assert(_isnan(static_cast<double>(a(j-1,k-1))) == 0);
727 g += a(i-1,k-1)*a(k-1,j-1);
729 a(k-1,j-1) -= g*a(k-1,i-1);
730 Assert(_isnan(static_cast<double>(a(k-1,j-1))) == 0);
736 for (j=1;j<=l;j++) a(j-1,i-1)=a(i-1,j-1)=0.0;
742 double s,r,p,g,f,dd,c,b;
744 for (i=2;i<=n;i++) e[i-1]=e[i];
750 for (m=l;m<=n-1;m++) {
752 if ((
double)(
TFlt::Abs(e[m])+dd) == dd)
break;
755 if (iter++ == 60)
nrerror(
"Too many iterations in EigSymmetricTridiag");
757 g=(d[l+1]-d[l])/(2.0*e[l]);
760 g=d[m]-d[l]+e[l]/(g+
sign(r,g));
765 for (i=m-1;i>=l;i--) {
778 r=(d[i]-g)*s+2.0*c*b;
784 z(k,i)=s*z(k,i-1)+c*f;
785 z(k,i-1)=c*z(k,i-1)-s*f;
788 if (r == 0.0 && i >= l)
continue;
805 for (sum=A(i-1,j-1),k=i-1;k>=1;k--) sum -= A(i-1,k-1)*A(j-1,k-1);
810 }
else A(j-1,i-1)=sum/p[i-1];
824 for (sum=b[i-1],k=i-1;k>=1;k--)
825 sum -= A(i-1,k-1)*x[k-1];
831 for (sum=x[i-1],k=i+1;k<=n;k++)
832 sum -= A(k-1,i-1)*x[k-1];
847 int i, j, k;
double sum;
848 for (i = 0; i < n; i++) {
851 for (j = 0; j < i; j++) x[j] = 0.0;
855 for (j = i+1; j < n; j++) {
856 for (sum = 0.0, k = i; k < j; k++)
857 sum -= A(j,k) * x[k];
862 for (j = n-1; j >= i; j--) {
863 for (sum = x[j], k = j+1; k < n; k++)
867 for (
int j = i; j < n; j++) A(i,j) = x[j];
885 int i, j, k;
double sum;
887 for (i = 0; i < n; i++) {
889 for (j = i+1; j < n; j++)
893 for (i = 0; i < n; i++) {
896 for (j = n-1; j > i; j--) x[j] = 0.0;
900 for (j = i-1; j >= 0; j--) {
901 for (sum = 0.0, k = i; k > j; k--)
902 sum -= A(k,j) * x[k];
905 for (
int j = 0; j <= i; j++) A(j,i) = x[j];
913 int i=0,imax=0,j=0,k=0;
914 double big,dum,sum,temp;
922 if ((temp=
TFlt::Abs(A(i-1,j-1))) > big) big=temp;
923 if (big == 0.0)
nrerror(
"Singular matrix in routine LUDecomposition");
930 for (k=1;k<i;k++) sum -= A(i-1,k-1)*A(k-1,j-1);
937 sum -= A(i-1,k-1)*A(k-1,j-1);
941 if ((dum=vv[i-1] *
TFlt::Abs(sum)) >= big) {
952 A(imax-1,k-1)=A(j-1,k-1);
964 if (A(j-1,j-1) == 0.0) A(j-1,j-1)=1e-20;
968 dum=1.0/(A(j-1,j-1));
969 for (i=j+1;i<=n;i++) A(i-1,j-1) *= dum;
984 for (j=ii;j<=i-1;j++) sum -= A(i-1,j-1)*b[j-1];
985 else if (sum) ii=i;b[i-1]=sum;
989 for (j=i+1;j<=n;j++) sum -= A(i-1,j-1)*b[j-1];
990 b[i-1]=sum/A(i-1,i-1);
995 TIntV indx;
double d;
1022 int NumSV,
int IterN,
TFltV& SgnValV) {
1025 int N = Matrix.
GetCols(), M = NumSV;
1030 for (i = 0; i < N; i++) {
1031 for (j = 0; j < M; j++)
1036 for (
int IterC = 0; IterC < IterN; IterC++) {
1037 printf(
"%d..", IterC);
1041 for (
int ColId = 0; ColId < M; ColId++) {
1043 for (k = 0; k < N; k++) Q(k,ColId) = tmp[k];
1048 for (i = 0; i < NumSV; i++)
1054 const int& NumEig,
TFltV& EigValV,
1055 const bool& DoLocalReortoP,
const bool& SvdMatrixProductP) {
1057 if (SvdMatrixProductP) {
1064 const int N = Matrix.
GetCols();
1065 TFltV r(N), v0(N), v1(N);
1066 TFltV alpha(NumEig, 0), beta(NumEig, 0);
1068 printf(
"Calculating %d eigen-values of %d x %d matrix\n", NumEig, N, N);
1072 for (
int i = 0; i < N; i++) {
1073 r[i] = 1/sqrt((
double)N);
1074 v0[i] = v1[i] = 0.0;
1078 for (
int j = 0; j < NumEig; j++) {
1079 printf(
"%d\r", j+1);
1085 if (SvdMatrixProductP) {
1099 if (DoLocalReortoP) { }
1108 TFltV d(NumEig + 1), e(NumEig + 1);
1109 d[1] = alpha[0]; d[0] = e[0] = e[1] = 0.0;
1110 for (
int i = 1; i < NumEig; i++) {
1111 d[i+1] = alpha[i]; e[i+1] = beta[i]; }
1113 TFltVV S(NumEig+1,NumEig+1);
1119 TFltKdV AllEigValV(NumEig, 0);
1120 for (
int i = 1; i <= NumEig; i++) {
1121 const double ResidualNorm =
TFlt::Abs(S(i-1, NumEig-1) * beta.
Last());
1122 if (ResidualNorm < 1e-5)
1127 AllEigValV.
Sort(
false); EigValV.
Gen(NumEig, 0);
1128 for (
int i = 0; i < AllEigValV.
Len(); i++) {
1129 if (i == 0 || (
TFlt::Abs(AllEigValV[i].Dat/AllEigValV[i-1].Dat) < 0.9999))
1130 EigValV.
Add(AllEigValV[i].Dat);
1136 TFltV& EigValV,
TFltVV& EigVecVV,
const bool& SvdMatrixProductP) {
1138 if (SvdMatrixProductP) {
1147 int i, N = Matrix.
GetCols(), K = 0;
1148 double t = 0.0, eps = 1e-6;
1152 double tmp = 1/sqrt((
double)N);
1153 for (i = 0; i < N; i++) {
1158 TIntV CountConvgV(Iters);
1159 for (i = 0; i < Iters; i++) CountConvgV[i] = 0;
1163 TFltV d(Iters+1), e(Iters+1);
1169 TFltV z(N), bb(Iters), aa(Iters), y(N);
1172 if (SvdMatrixProductP) {
1180 for (
int j = 0; j < (Iters-1); j++) {
1195 for (i = 0; i <= j; i++) {
1198 (bb[j-1] *
TFlt::Abs(V(K-1, i)) < eps * t)) {
1200 ConvgQV[i].
Reserve(N,N); CountConvgV[i]++;
1201 TFltV& vec = ConvgQV[i];
1203 for (
int k = 0; k < N; k++) {
1205 for (
int l = 0; l < K; l++) {
1206 vec[k] += Q(k,l) * V(l,i);
1217 if (!(bb[j] > 1e-10)) {
1218 printf(
"Rank of matrix is only %d\n", j+2);
1219 printf(
"Last singular value is %g\n", bb[j].Val);
1223 for (i = 0; i < N; i++) {
1224 Q(i, j+1) = z[i] / bb[j];
1228 if (SvdMatrixProductP) {
1239 for (i = 1; i < K; i++) d[i] = aa[i-1];
1243 for (i = 2; i <= K; i++) e[i] = bb[i-2];
1247 for (i = 2; i < K; i++) {
1253 for (i = 0; i < K; i++) {
1254 for (
int k = 0; k < K; k++) {
1268 for (i = 0; i < K; i++) {
1276 EigValV.
Reserve(FinalNumEig,0);
1278 for (i = 0; i < FinalNumEig; i++) {
1281 double sigma = d[ii+1].Val;
1292 TFltV& EigValV,
TFltVV& EigVecVV,
const bool& SvdMatrixProductP) {
1294 if (SvdMatrixProductP) {
1303 int i, N = Matrix.
GetCols(), K = 0;
1304 double t = 0.0, eps = 1e-6;
1308 double tmp = 1/sqrt((
double)N);
1309 for (i = 0; i < N; i++)
1313 TIntV CountConvgV(MaxNumEig);
1314 for (i = 0; i < MaxNumEig; i++) CountConvgV[i] = 0;
1318 TFltV d(MaxNumEig+1), e(MaxNumEig+1);
1321 TFltVV V(MaxNumEig, MaxNumEig);
1324 TFltV z(N), bb(MaxNumEig), aa(MaxNumEig), y(N);
1327 if (SvdMatrixProductP) {
1335 for (
int j = 0; j < (MaxNumEig-1); j++) {
1336 printf(
"%d [%s]..\r",j+2, ExeTm.
GetStr());
1337 if (ExeTm.
GetSecs() > MaxSecs) {
break; }
1351 for (i = 0; i <= j; i++) {
1354 (bb[j-1] *
TFlt::Abs(V(K-1, i)) < eps * t)) {
1356 ConvgQV[i].
Reserve(N,N); CountConvgV[i]++;
1357 TFltV& vec = ConvgQV[i];
1359 for (
int k = 0; k < N; k++) {
1361 for (
int l = 0; l < K; l++)
1362 vec[k] += Q(k,l) * V(l,i);
1372 if (!(bb[j] > 1e-10)) {
1373 printf(
"Rank of matrix is only %d\n", j+2);
1374 printf(
"Last singular value is %g\n", bb[j].Val);
1377 for (i = 0; i < N; i++)
1378 Q(i, j+1) = z[i] / bb[j];
1381 if (SvdMatrixProductP) {
1392 for (i = 1; i < K; i++) d[i] = aa[i-1];
1396 for (i = 2; i <= K; i++) e[i] = bb[i-2];
1400 for (i = 2; i < K; i++)
1405 for (i = 0; i < K; i++) {
1406 for (
int k = 0; k < K; k++)
1414 printf(
"... calc %d.", K);
1417 for (i = 0; i < K; i++) {
1424 const int FinalNumEig = K;
1425 EigValV.
Reserve(FinalNumEig,0);
1427 for (i = 0; i < FinalNumEig; i++) {
1430 double sigma = d[ii+1].Val;
1441 const int& CalcSV,
TFltV& SngValV,
const bool& DoLocalReorto) {
1443 SimpleLanczos(Matrix, CalcSV, SngValV, DoLocalReorto,
true);
1444 for (
int SngValN = 0; SngValN < SngValV.
Len(); SngValN++) {
1446 if (SngValV[SngValN] < 0.0) {
1447 printf(
"bad sng val: %d %g\n", SngValN, SngValV[SngValN]());
1448 SngValV[SngValN] = 0;
1450 SngValV[SngValN] = sqrt(SngValV[SngValN].Val);
1459 Lanczos(Matrix, NumSV, Iters, ReOrtoType, SgnValV, RightSgnVecVV,
true);
1461 const int FinalNumSV = SgnValV.
Len();
1462 LeftSgnVecVV.
Gen(Matrix.
GetRows(), FinalNumSV);
1464 for (
int i = 0; i < FinalNumSV; i++) {
1465 if (SgnValV[i].Val < 0.0) { SgnValV[i] = 0.0; }
1466 const double SgnVal = sqrt(SgnValV[i]);
1467 SgnValV[i] = SgnVal;
1469 Matrix.
Multiply(RightSgnVecVV, i, LeftSgnVecV);
1470 for (
int j = 0; j < LeftSgnVecV.Len(); j++) {
1471 LeftSgnVecVV(j,i) = LeftSgnVecV[j] / SgnVal; }
1480 for (
int j = 0; j < m; j++) {
1482 for (
int i = 0; i < Vec.
Len(); i++)
1483 x += U(Vec[i].Key, j) * Vec[i].Dat;
1493 for (
int i = 0; i < data.
Len(); i++)
1495 double zi = data[i].Key;
int yi = data[i].Dat;
1496 double e = exp(-A * zi + B);
1497 double denum = 1.0 + e;
1498 double prob = (yi > 0) ? (1.0 / denum) : (e / denum);
1499 J -= log(prob < 1e-20 ? 1e-20 : prob);
1510 J = 0.0;
double sum_all_PyNeg = 0.0, sum_all_ziPyNeg = 0.0, sum_yNeg_zi = 0.0, sum_yNeg_1 = 0.0;
1511 for (
int i = 0; i < data.
Len(); i++)
1513 double zi = data[i].Key;
int yi = data[i].Dat;
1514 double e = exp(-A * zi + B);
1515 double denum = 1.0 + e;
1516 double prob = (yi > 0) ? (1.0 / denum) : (e / denum);
1517 J -= log(prob < 1e-20 ? 1e-20 : prob);
1518 sum_all_PyNeg += e / denum;
1519 sum_all_ziPyNeg += zi * e / denum;
1520 if (yi < 0) { sum_yNeg_zi += zi; sum_yNeg_1 += 1; }
1522 JA = -sum_all_ziPyNeg + sum_yNeg_zi;
1523 JB = sum_all_PyNeg - sum_yNeg_1;
1527 const double V,
const double lambda,
double& J,
double& JJ,
double& JJJ)
1534 J = 0.0; JJ = 0.0; JJJ = 0.0;
1535 for (
int i = 0; i < data.
Len(); i++)
1537 double zi = data[i].Key;
int yi = data[i].Dat;
1538 double e = exp(-A * zi + B);
1539 double denum = 1.0 + e;
1540 double prob = (yi > 0) ? (1.0 / denum) : (e / denum);
1541 J -= log(prob < 1e-20 ? 1e-20 : prob);
1542 double VU = V - U * zi;
1543 JJ += VU * (e / denum);
if (yi < 0) JJ -= VU;
1544 JJJ += VU * VU * e / denum / denum;
1558 double minProj = data[0].Key, maxProj = data[0].Key;
1559 {
for (
int i = 1; i < data.
Len(); i++) {
1560 double zi = data[i].Key;
if (zi < minProj) minProj = zi;
if (zi > maxProj) maxProj = zi; }}
1562 A = 1.0;
B = 0.5 * (minProj + maxProj);
1563 double bestJ = 0.0, bestA = 0.0, bestB = 0.0, lambda = 1.0;
1564 for (
int nIter = 0; nIter < 50; nIter++)
1567 if (nIter == 0 || J < bestJ) { bestJ = J; bestA =
A; bestB =
B; }
1571 if (norm < 1e-10)
break;
1577 while (lambda > 1e-5) {
1578 lambda = 0.5 * lambda;
1583 while (lambda < 1e5) {
1584 double lambda2 = 2 * lambda;
1587 if (Jc2 > Jc)
break;
1588 lambda = lambda2; Jc = Jc2; } }
1590 A += cl * lambda * JA / norm;
B += cl * lambda * JB / norm;
1593 A = bestA;
B = bestB;
1599 for (
int ValN = 0; ValN < Vec.
Len(); ValN++) {
1606 const int Len = SpV.
Len();
1607 for (
int ValN = 0; ValN < Len; ValN++) {
1614 const int RowN = m.
Len();
1615 for (
int RowId = 0; RowId < RowN; RowId++) {
1624 const int RowN = m.
Len();
1625 for (
int RowId = 0; RowId < RowN; RowId++) {
1635 for (
int RowId = 0; RowId < RowN; RowId++) {
1647 for (
int RowId = 0; RowId < RowN; RowId++) {
1648 for (
int ColId = 0; ColId < ColN; ColId++) {
1658 int RowN,
int ColN,
const TStr& FName) {
1661 for (
int RowId = 0; RowId < RowN; RowId++) {
1662 for (
int ColId = 0; ColId < ColN; ColId++) {
1673 int Row = 0, Col = 0; ColV.
Clr();
1678 if (ColV.
Len() > Col) {
1679 IAssert(ColV[Col].Len() == Row);
1703 if (ColV.
Empty()) { MatrixVV.
Clr();
return; }
1704 const int Rows = ColV[0].
Len(), Cols = ColV.
Len();
1705 MatrixVV.
Gen(Rows, Cols);
1706 for (
int RowN = 0; RowN < Rows; RowN++) {
1707 for (
int ColN = 0; ColN < Cols; ColN++) {
1708 MatrixVV(RowN, ColN) = ColV[ColN][RowN];
1715 printf(
"%s = [", VecNm.
CStr());
1716 for (
int i = 0; i < Vec.
Len(); i++) {
1717 printf(
"%.5f", Vec[i]());
1718 if (i < Vec.
Len() - 1) { printf(
", "); }
1725 printf(
"%s = [\n", MatrixNm.
CStr());
1726 for (
int j = 0; j < A.
GetRows(); j++) {
1727 for (
int i = 0; i < A.
GetCols(); i++) {
1728 printf(
"%f\t", A.
At(i, j).
Val);
1736 printf(
"%s = [", VecNm.
CStr());
1737 for (
int i = 0; i < Vec.
Len(); i++) {
1738 printf(
"%d", Vec[i]());
1739 if (i < Vec.
Len() - 1) printf(
", ");
1746 int Len = Vec.
Len();
1747 for (
int i = 0; i < Len; i++)
1754 for (
int i = 0; i < Len; i++) {
1755 for (
int j = 0; j < Len; j++) M(i,j) = 0.0;
1763 for (
int i = 0; i < Len; i++) {
1764 for (
int j = 0; j < Len; j++) M(i,j) = 0.0;
1770 const int Len = Vec.
Len();
1772 for (
int i = 0; i < Len; i++)
1778 const int Len = Vec.
Len();
1780 for (
int i = 0; i < Len; i++)
1786 const double& CutSumPrc) {
1789 IAssert(0.0 <= CutSumPrc && CutSumPrc <= 1.0);
1790 const int Elts = Vec.
Len();
1791 double EltSum = 0.0;
1792 for (
int EltN = 0; EltN < Elts; EltN++) {
1794 const double MnEltVal = CutSumPrc * EltSum;
1797 for (
int EltN = 0; EltN < Elts; EltN++) {
1807 int Elts = SpVec.
Len();
1808 for (
int EltN = 0; EltN < Elts; EltN++) {
1809 if (SpVec[EltN].Key < VecLen) {
1810 Vec[SpVec[EltN].Key] = SpVec[EltN].Dat;
static double EuclDist(const TFltV &x, const TFltV &y)
static void Transpose(const TFltVV &A, TFltVV &B)
static double Norm(const TFltV &x)
#define IAssertR(Cond, Reason)
static PSOut New(const TStr &FNm, const bool &Append=false)
static void AssertOrtogonality(const TVec< TFltV > &Vecs, const double &Threshold)
static void SaveMatlabTFltIntKdV(const TIntFltKdV &SpV, const int &ColN, TSOut &SOut)
virtual int PutCh(const char &Ch)=0
static double SumVec(const TFltV &x)
static double sqr(double a)
static double EvaluateFit(const TFltIntKdV &data, const double A, const double B)
static const T & Mx(const T &LVal, const T &RVal)
virtual void PMultiplyT(const TFltVV &B, int ColId, TFltV &Result) const
static void GS(TVec< TFltV > &Q)
static void FillRnd(TFltV &Vec)
static double GetMx(const double &Flt1, const double &Flt2)
void MultiplyT(const TFltVV &B, int ColId, TFltV &Result) const
static void Lanczos(const TMatrix &Matrix, int NumEig, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &EigValV, TFltVV &EigVecVV, const bool &SvdMatrixProductP=false)
static void ToVec(const TIntFltKdV &SpVec, TFltV &Vec, const int &VecLen)
static void SimpleLanczosSVD(const TMatrix &Matrix, const int &CalcSV, TFltV &SngValV, const bool &DoLocalReortoP=false)
TVec< TIntFltKdV > RowSpVV
static void Lanczos2(const TMatrix &Matrix, int MaxNumEig, int MaxSecs, const TSpSVDReOrtoType &ReOrtoType, TFltV &EigValV, TFltVV &EigVecVV, const bool &SvdMatrixProductP=false)
static void SaveMatlabTFltVV(const TFltVV &m, const TStr &FName)
TSizeTy Len() const
Returns the number of elements in the vector.
TKeyDat< TInt, TFlt > TIntFltKd
static void SolveLinearSystem(TFltVV &A, const TFltV &b, TFltV &x)
static void SaveMatlabTFltVVCol(const TFltVV &m, int ColId, const TStr &FName)
TKeyDat< TFlt, TFlt > TFltKd
static double Sqr(const double &x)
static void LanczosSVD(const TMatrix &Matrix, int NumSV, int Iters, const TSpSVDReOrtoType &ReOrtoType, TFltV &SgnValV, TFltVV &LeftSgnVecVV, TFltVV &RightSgnVecVV)
int PutLn(const int &Lns=1)
static void MultiplyT(const TFltVV &A, const TFltV &x, TFltV &y)
static void InverseSVD(const TFltVV &A, TFltVV &B)
static void InverseTriagonal(TFltVV &A)
bool Empty() const
Tests whether the vector is empty.
static void Normalize(TFltV &x)
static void NormalizeLinf(TFltV &x)
static void Project(const TIntFltKdV &Vec, const TFltVV &U, TFltV &ProjVec)
static void SaveMatlabTFltVVMjrSubMtrx(const TFltVV &m, int rowN, int colN, const TStr &FName)
static void SparseMerge(const TKeyDatV &SrcV1, const TKeyDatV &SrcV2, TKeyDatV &DstV)
static PSIn New(const TStr &FNm)
static int SumVec(const TIntV &Vec)
static void Rotate(const double &OldX, const double &OldY, const double &Angle, double &NewX, double &NewY)
static void MultiplyScalar(const double &k, const TFltV &x, TFltV &y)
static void Svd(const TFltVV &InMtx, TFltVV &LSingV, TFltV &SingValV, TFltVV &RSingV)
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
static void NormalizeL1(TFltV &x)
static int GetMn(const int &Int1, const int &Int2)
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
static void LUSolve(const TFltVV &A, const TIntV &indx, TFltV &b)
static double Norm2(const TFltV &x)
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
static void CholeskyDecomposition(TFltVV &A, TFltV &p)
static double NormLinf(const TFltV &x)
static void AddVec(const double &k, const TFltV &x, const TFltV &y, TFltV &z)
virtual void PMultiply(const TFltVV &B, int ColId, TFltV &Result) const
static void PrintTIntV(const TIntV &Vec, const TStr &VecNm)
const TVal & Last() const
Returns a reference to the last element of the vector.
static void nrerror(const TStr &error_text)
int PutInt(const int &Int)
static void EigSymmetricTridiag(TFltV &d, TFltV &e, int n, TFltVV &z)
TVec< TIntFltKdV > ColSpVV
static void PrintTFltVV(const TFltVV &A, const TStr &MatrixNm)
static void SolveSymetricSystem(TFltVV &A, const TFltV &b, TFltV &x)
static void SaveMatlabTIntV(const TIntV &m, const TStr &FName)
virtual void PMultiply(const TFltVV &B, int ColId, TFltV &Result) const
int PutFlt(const double &Flt)
static void Multiply(const TFltVV &A, const TFltV &x, TFltV &y)
static double Abs(const double &Flt)
static void SymetricToTridiag(TFltVV &a, int n, TFltV &d, TFltV &e)
static void CholeskySolve(const TFltVV &A, const TFltV &p, const TFltV &b, TFltV &x)
static void SimpleLanczos(const TMatrix &Matrix, const int &NumEig, TFltV &EigValV, const bool &DoLocalReortoP=false, const bool &SvdMatrixProductP=false)
void Multiply(const TFltVV &B, int ColId, TFltV &Result) const
static double NormL1(const TFltV &x)
static void LoadMatlabTFltVV(const TStr &FNm, TVec< TFltV > &ColV)
virtual void PMultiplyT(const TFltVV &B, int ColId, TFltV &Result) const
static void SaveCsvTFltV(const TFltV &Vec, TSOut &SOut)
static double pythag(double a, double b)
TIter BegI() const
Returns an iterator pointing to the first element in the vector.
static TStr Fmt(const char *FmtStr,...)
void Pack()
Reduces vector capacity (frees memory) to match its size.
static void SaveMatlabTFltV(const TFltV &m, const TStr &FName)
int PutStr(const char *CStr)
static void FillIdentity(TFltVV &M)
static void LUDecomposition(TFltVV &A, TIntV &indx, double &d)
virtual void PMultiply(const TFltVV &B, int ColId, TFltV &Result) const
static double DotProduct(const TFltV &x, const TFltV &y)
#define AssertR(Cond, Reason)
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
static void InverseSubstitute(TFltVV &A, const TFltV &p)
void Reserve(const TSizeTy &_MxVals)
Reserves enough memory for the vector to store _MxVals elements.
TLxSym GetSym(const TFSet &Expect)
static void Inverse(const TFltVV &A, TFltVV &B, const TLinAlgInverseType &DecompType)
static TVec< TFlt, int > GetV(const TFlt &Val1)
Returns a vector on element Val1.
static void ToSpVec(const TFltV &Vec, TIntFltKdV &SpVec, const double &CutWordWgtSumPrc=0.0)
static void OrtoIterSVD(const TMatrix &Matrix, int NumSV, int IterN, TFltV &SgnValV)
const char * GetStr() const
int PutStrLn(const TStr &Str, const bool &ForceInLn=false)
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
static void Gemm(const double &Alpha, const TFltVV &A, const TFltVV &B, const double &Beta, const TFltVV &C, TFltVV &D, const int &TransposeFlags)
static void LinComb(const double &p, const TFltV &x, const double &q, const TFltV &y, TFltV &z)
static void ConvexComb(const double &p, const TFltV &x, const TFltV &y, TFltV &z)
static void InverseSymetric(TFltVV &A)
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
static void PrintTFltV(const TFltV &Vec, const TStr &VecNm)
virtual void PMultiplyT(const TFltVV &B, int ColId, TFltV &Result) const
static void MultiplyATA(const TMatrix &Matrix, const TFltVV &Vec, int ColId, TFltV &Result)
static double EuclDist2(const TFltV &x, const TFltV &y)
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
static double sign(double a, double b)