35 template<
typename Real>
42 if (tmp.Cond() < 100)
break;
43 KALDI_LOG <<
"Condition number of random matrix large " 44 <<
static_cast<float>(tmp.Cond())
45 <<
", trying again (this is normal)";
62 printf(
"Condition number of random matrix large %f, trying again (this is normal)\n",
178 for (
int p = 0; p < 2; p++) {
184 Mcopy(
i,
i) += alpha;
191 for (
int p = 0; p < 2; p++) {
193 Real alpha = 0.43243, beta = 1.423;
205 for (
int32 r = 0; r < dimM; r++) {
210 Mcheckrow.
Scale(beta);
211 Mcheckrow.
AddVec(alpha * V(r), Nrow);
214 M.AddDiagVecMat(alpha, V, N, trans, beta);
222 for (
int p = 0; p < 2; p++) {
224 Real alpha = 0.43243, beta = 1.423;
226 Matrix<Real> M(dimM, dimN), N(dimM, dimN), buf(dimM, dimN);
242 M.AddMatDiagVec(alpha, N, trans, V, beta);
251 Real alpha = 0.43243, beta = 1.423;
252 Matrix<Real> M(dimM, dimN), A(dimM, dimN), B(dimM, dimN), buf(dimM, dimN);
261 M.AddMatMatElements(alpha, A, B, beta);
278 template<
typename Real,
typename OtherReal>
290 T(
i,
i) += alpha * v(
i);
296 template<
typename Real>
328 M.CopyColsFromVec(v);
345 Vector<Real> Vs(std::min(dimM, dimN)), Vs10(std::min(dimM, dimN));
448 template <
typename Real>
460 Real observed_moment = Mpow.
Sum() / (rows * cols);
467 Real allowed_deviation = k * pow / sqrt(static_cast<Real>(rows * cols));
472 Real lower_bound = expected_moment - allowed_deviation,
473 upper_bound = expected_moment + allowed_deviation;
474 KALDI_ASSERT(observed_moment >= lower_bound && observed_moment <= upper_bound);
480 template <
typename Real>
491 central_moments(0) = 0.0;
492 central_moments(1) = 0.0;
493 central_moments(2) = 1.0 / 12;
494 central_moments(3) = 0.0;
495 central_moments(4) = 1.0 / 80;
500 Real observed_moment = Mpow.
Sum() / (rows * cols);
503 Real expected_moment = central_moments(pow);
507 Real allowed_deviation = k / sqrt(static_cast<Real>(rows * cols));
508 Real lower_bound = expected_moment - allowed_deviation,
509 upper_bound = expected_moment + allowed_deviation;
510 KALDI_ASSERT(observed_moment >= lower_bound && observed_moment <= upper_bound);
516 template <
typename Real>
520 Vector<Real> V(100), V1(100), V2(100), V3(100), V4(100);
539 V3.AddVecVec(1.0, V, V2, 0.0);
546 V7.AddVecVec(1.0, V, V, 1.0);
552 std::vector<MatrixIndexT> sizes;
554 sizes.push_back(128);
555 for(
int i = 0;
i < sizes.size();
i++) {
560 Vr.AddRowSumMat(0.4, M);
561 Vr.AddRowSumMat(0.3, M, 0.5);
571 V2r.CopyRowFromMat(M, k);
587 Real a = V.LogSumExp();
588 V2.
Set(
Exp(V.LogSumExp()));
592 Real b = V.ApplySoftMax();
607 V1.AddVecVec(1.0, V, V, 0.0);
630 template<
typename Real>
636 for (
int32 i = 1;
i < dimM;
i++) m = std::max(m, V(
i));
643 template<
typename Real>
649 for (
int32 i = 1;
i < dimM;
i++) m = std::min(m, V(
i));
656 template<
typename Real>
660 Real orig = 0.1 * (
Rand() % 100), changed = 0.1 * (
Rand() % 50);
666 if (V1(
i) == orig) V1(
i) = changed;
673 template<
typename Real>
678 if (scalar == 0.0)
continue;
679 if (scalar < 0) scalar *= -1.0;
690 Vnorm1 = V.
Norm(1.0),
691 Vnorm2 = V.
Norm(2.0),
692 Vnorm3 = V.
Norm(3.0);
712 template<
typename Real>
716 num_rows2 = 10 +
Rand() % 10,
717 num_cols = 10 +
Rand() % 10;
722 N2(num_rows2, num_cols), O(num_rows2, num_cols);
723 std::vector<int32> reorder(num_rows2);
724 std::vector<const Real*> reorder_src(num_rows2,
725 static_cast<const Real*>(NULL));
726 for (
int32 i = 0;
i < num_rows2;
i++) {
727 reorder[
i] = -1 + (
Rand() % (num_rows1 + 1));
728 if (reorder[
i] != -1)
732 N1.CopyRows(M, &(reorder[0]));
733 N2.CopyRows(&(reorder_src[0]));
735 for (
int32 i = 0;
i < num_rows2;
i++)
736 for (
int32 j = 0;
j < num_cols;
j++)
737 if (reorder[
i] < 0) O(
i,
j) = 0;
738 else O(
i,
j) = M(reorder[
i],
j);
745 template<
typename Real>
749 num_rows2 = 10 +
Rand() % 10,
750 num_cols = 10 +
Rand() % 10;
754 Matrix<Real> N(num_rows2, num_cols), O(num_rows2, num_cols);
755 std::vector<Real*> reorder_dst(num_rows1,
756 static_cast<Real*>(NULL));
757 unordered_map<MatrixIndexT, bool> used_index;
758 for (
int32 i = 0;
i < num_rows1;
i++) {
760 if (used_index.find(index) == used_index.end()) {
761 used_index[index] =
true;
766 reorder_dst[
i] = N.RowData(index);
767 for (
int32 j = 0;
j < num_cols;
j++)
768 O(index,
j) = M(
i,
j);
778 template<
typename Real>
782 num_rows2 = 10 +
Rand() % 10,
783 num_cols = 10 +
Rand() % 10;
788 N2(num_rows2, num_cols), O(num_rows2, num_cols);
789 std::vector<int32> reorder(num_rows2);
790 std::vector<const Real*> reorder_src(num_rows2,
791 static_cast<const Real*>(NULL));
792 for (
int32 i = 0;
i < num_rows2;
i++) {
793 reorder[
i] = -1 + (
Rand() % (num_rows1 + 1));
794 if (reorder[
i] != -1)
799 static_cast<Real
>((
Rand() % num_rows2)) /
static_cast<Real
>(num_rows1);
801 N1.AddRows(alpha, M, &(reorder[0]));
802 N2.AddRows(alpha, &(reorder_src[0]));
804 for (
int32 i = 0;
i < num_rows2;
i++) {
805 if (reorder[
i] != -1) {
806 for (
int32 j = 0;
j < num_cols;
j++) {
807 O(
i,
j) += alpha * M(reorder[
i],
j);
817 template<
typename Real>
821 num_rows2 = 10 +
Rand() % 10,
822 num_cols = 10 +
Rand() % 10;
827 static_cast<Real
>((
Rand() % num_rows2)) /
static_cast<Real
>(num_rows1);
829 Matrix<Real> N(num_rows2, num_cols), O(num_rows2, num_cols);
830 std::vector<Real*> reorder_dst(num_rows1, static_cast<Real*>(NULL));
831 unordered_map<MatrixIndexT, bool> used_index;
832 for (
int32 i = 0;
i < num_rows1;
i++) {
834 if (used_index.find(index) == used_index.end()) {
835 used_index[index] =
true;
840 reorder_dst[
i] = N.RowData(index);
841 for (
int32 j = 0;
j < num_cols;
j++)
842 O(index,
j) += alpha * M(
i,
j);
852 template<
typename Real>
856 num_cols2 = 10 +
Rand() % 10,
857 num_rows = 10 +
Rand() % 10;
861 Matrix<Real> N(num_rows, num_cols2), O(num_rows, num_cols2);
862 std::vector<int32> reorder(num_cols2);
863 for (
int32 i = 0;
i < num_cols2;
i++)
864 reorder[
i] = -1 + (
Rand() % (num_cols1 + 1));
866 N.CopyCols(M, &(reorder[0]));
868 for (
int32 i = 0;
i < num_rows;
i++)
869 for (
int32 j = 0;
j < num_cols2;
j++)
870 if (reorder[
j] < 0) O(
i,
j) = 0;
871 else O(
i,
j) = M(
i, reorder[
j]);
877 template<
typename Real>
902 Real tmp1 = sqrt(M1.
Sum());
903 Real tmp2 = N.FrobeniusNorm();
910 M(
i,
j) = std::abs(M(
i,
j));
913 V(
i) = M(
i, dimN - 1);
915 std::sort(V.
Data(), V.
Data() + dimM);
916 KALDI_ASSERT(std::abs(V(dimM - 1) - N.LargestAbsElem()) < 0.00001);
922 std::stringstream ss;
925 ss <<
"4.6863" <<
'\n';
926 ss <<
"3.7062 4.6032" <<
'\n';
927 ss <<
"3.4160 3.7256 5.2474" <<
'\n';
940 Real *DetSign = &tmp;
974 Md.
AddVec2(static_cast<Real>(0.5), V);
1005 v1.CopyRowFromSp(S, dim2);
1024 Matrix<Real> M(dimM, dimN), N(dimM, dimN), O(dimN, dimM);
1032 M.CopyFromMat(Morig);
1033 M.AddMat(0.5, O,
kTrans);
1092 V1.AddVecVec(1.0, V, V, 0.0);
1105 V1.AddVecVec(1.0, V, V, 0.0);
1126 Real x = M(r, c), y = N(r, c);
1147 Real alpha = 0.333 * ((
Rand() % 10) - 5);
1148 M.AddVecVec(alpha, v1, v2);
1169 M.AddVecVec(1.0f, v1, v2);
1183 int dimM = 10 +
Rand() % 10;
1191 Real logdet2, logdet3, sign2, sign3;
1192 logdet2 = N.
LogDet(&sign2);
1193 logdet3 = S.
LogDet(&sign3);
1194 KALDI_ASSERT(sign2 == 1.0 && sign3 == 1.0 && std::abs(logdet2-logdet) < 0.1 && std::abs(logdet2 - logdet3) < 0.1);
1199 if ( ! tmp.
IsZero(1.0e-04)) {
1200 printf(
"KALDI_ERR: matrix is not zero\n");
1206 KALDI_ASSERT(std::abs(a-b) < 0.1 && std::abs(b-c) < 0.1);
1214 int dimM = 10 +
Rand() % 10;
1222 Real logdet2, logdet3, sign2, sign3;
1223 logdet2 = N.
LogDet(&sign2);
1224 logdet3 = S.
LogDet(&sign3);
1225 KALDI_ASSERT(sign2 == 1.0 && sign3 == 1.0 && std::abs(logdet2-logdet) < 0.01 && std::abs(logdet2 - logdet3) < 0.01);
1228 for (
MatrixIndexT change = 0; change < num_sign_changes; change++) {
1235 s(
Rand() % dimM) *= -1;
1241 N.
Row(
Rand() % dimM).Scale(-1.0);
1245 Real tmp = 1.0 + ((
Rand() % 5) * 0.01);
1246 Real logdet_factor = dimM *
Log(tmp);
1250 Real logdet4, logdet5, sign4, sign5;
1251 logdet4 = N.
LogDet(&sign4);
1252 logdet5 = S.
LogDet(&sign5);
1255 if (num_sign_changes % 2 == 0) {
1279 int dimM = 10 +
Rand() % 10, dimN = 10 +
Rand() % 10;
1286 printf(
"m = %d, n = %d\n", dimM, dimN);
1297 Matrix<Real> U(dimM, std::min(dimM, dimN)), Vt(std::min(dimM, dimN), dimN);
1324 M(0, 0) *= 1.000001;
1332 M(
i,
j) = 1591.3614306764898;
1334 M(10, 10) *= 1.00001;
1351 bool ans = M.Svd(&v, &U, &Vt);
1422 M.
Eig(&P, &real_eigs, &imag_eigs);
1444 M.
Eig(&P, &real_eigs, &imag_eigs);
1449 logdet_check += 0.5 *
Log(real_eigs(
i)*real_eigs(
i) + imag_eigs(
i)*imag_eigs(
i));
1506 if (
Rand() % 10 == 0) s(
i) = 0;
1507 else if (
Rand() % 10 < 2) {
1510 s(
i) = (
Rand()%2 == 0 ? 1 : -1) * s(
Rand() % dimM);
1515 Real *data = s.
Data();
1516 std::sort(data, data+dimM);
1528 diff.
AddSp(-1.0, S2);
1531 KALDI_LOG <<
"Eigs of difference are " << s;
1537 template <
typename Real>
1541 S.AddMat2(-1.0, M, transM, 1.0);
1545 max = std::max(max, std::abs(S(
i,
j)));
1549 template<
typename Real>
1551 Real max_diag = 0.0, max_offdiag = 0.0;
1554 if (
i ==
j) { max_diag = std::max(max_diag, std::abs(S(
i,
j))); }
1555 else { max_offdiag = std::max(max_offdiag, std::abs(S(
i,
j))); }
1557 if (max_diag == 0.0) {
1558 if (max_offdiag == 0.0)
return 0.0;
1561 return max_offdiag / max_diag;
1566 template<
typename Real>
1574 max = std::max(max, std::abs(tmp(
i,
j)));
1578 template<
typename Real>
1585 tmp+1, 1, 0.0, tmp+4, 1);
1592 if (
i == 0 ||
i == 1)
1599 if (
Rand() % 3 == 0)
1601 else if (
Rand() % 2 == 0)
1603 if (
i == 0 ||
i == 1) {
1608 temp.
Set(-1.61558713e-27);
1609 S.CopyFromMat(temp);
1613 KALDI_LOG <<
"S trace " << S.Trace() <<
", T trace " << T.
Trace();
1620 if (S.LogDet() > -50.0) {
1628 S2.AddMat2Sp(1.0, Q,
kTrans, T, 0.0);
1639 template<
typename Real>
1648 tmp+1, 1, 0.0, tmp+4, 1);
1659 KALDI_LOG <<
"S trace " << S.Trace() <<
", T trace " << T.
Trace();
1670 S2.AddMat2Sp(1.0, Q,
kTrans, T, 0.0);
1671 S3.AddMat2Sp(1.0, Q,
kNoTrans, S, 0.0);
1697 Matrix<Real> A(dimM, dimN), B(dimN, dimO), C(dimM, dimO);
1709 sum += A(
i, k) * B(k,
j);
1724 Matrix<Real> A(dimM, dimM), B(dimM, dimM), C(dimM, dimM), tmp(dimM, dimM), tmp2(dimM, dimM);
1731 tmp.CopyFromMat(A); A.AddMat(1.0, tmp,
kTrans);
1732 tmp.CopyFromMat(B); B.AddMat(1.0, tmp,
kTrans);
1733 tmp.CopyFromMat(C); C.AddMat(1.0, tmp,
kTrans);
1741 tmp2.
AddSpSp(1.0, sA, sB, 0.0);
1743 tmp2.
AddSpSp(1.0, sA, sB, 0.0);
1759 float alpha = 0.2 * (
Rand() % 10);
1764 N(dimX, dimY) + alpha * v(dimX) * w(dimY));
1774 Real alpha = 0.333, beta = 0.5;
1783 x.AddMatVec(alpha, A, trans, y, beta);
1787 double sum = beta * orig_x(
i);
1790 sum += alpha * A(
i,
j) * y(
j);
1792 sum += alpha * A(
j,
i) * y(
j);
1805 Matrix<Real> A(dimM, dimM), B(dimM, dimM), C(dimM, dimM);
1825 Matrix<Real> A(dimM, dimN), B(dimM, dimN), C(dimM, dimN);
1857 template<
class Real>
1862 for (
int32 r = 0; r + 1 < num_rows; r++) {
1863 for (
int32 j = num_cols;
j < stride;
j++) {
1865 (mat->
RowData(r))[
j] = not_a_number;
1878 template<
typename Real>
1880 for (
size_t i = 0;
i < 10;
i++) {
1882 if (num_rows * num_cols == 0) num_rows = num_cols = 0;
1898 template<
typename Real>
1900 for (
size_t i = 0;
i < 10;
i++) {
1902 dimM2 =
Rand() % 10, dimN2 =
Rand() % 10;
1903 if (dimM1*dimN1 == 0) dimM1 = dimN1 = 0;
1904 if (dimM2*dimN2 == 0) dimM2 = dimN2 = 0;
1916 M.
Resize(dimM2, dimN2, resize_type);
1917 v.
Resize(dimM2, resize_type);
1918 S.
Resize(dimM2, resize_type);
1932 if (
i < dimM1 && i2 < dimM1)
AssertEqual(S(
i, i2), Scopy(
i, i2));
1942 template<
typename Real>
1967 template<
typename Real>
1985 template<
typename Real>
1989 dimN = 1 +
Rand() % 4;
1993 if (iter % 2 == 1) {
2019 template<
typename Real>
2024 dimN = 5 +
Rand() % 4;
2041 MN.
AddMatMat(1.0, M, transM, N, transN, 0.0);
2051 template<
typename Real>
2057 if (
Rand() % 5 != 0) M.Row(
i).SetRandn();
2059 if (
Rand() % 2 != 0) {
2068 M.OrthogonalizeRows();
2075 template<
typename Real>
2139 template<
typename Real>
2168 if (
Rand() % 2 == 0)
2172 Matrix<Real> Mm(dimM, dimM), Nm(dimM, dimM), Om(dimM, dimM);
2173 Mm.CopyFromSp(M); Nm.CopyFromSp(N);
2186 M(i, i) = 20 * std::max((Real)0.1, (Real)
RandGauss());
2193 Matrix<Real> Mm(dimM, dimM), Nm(dimM, dimM), Om(dimM, dimM);
2194 Mm.CopyFromTp(M); Nm.CopyFromTp(N);
2234 float alpha = (
Rand() % 10) + 0.5;
2250 Real ip_a =
VecSpVec(v, BFloored, v);
2251 Real ip_c = alpha *
VecSpVec(v, C, v);
2252 if (
i < 3)
KALDI_LOG <<
"alpha = " << alpha <<
", ip_a = " << ip_a <<
" ip_b = " << ip_b <<
" ip_c = " << ip_c <<
'\n';
2265 float floor = (
Rand() % 10) - 3;
2292 f.ApplyFloor(pivot, &floored2);
2294 c.ApplyCeiling(pivot, &ceiled2);
2297 if (f2(
d) < pivot) { f2(
d) = pivot; floored++; }
2298 if (c2(
d) > pivot) { c2(
d) = pivot; ceiled++; }
2359 Matrix<Real> M(dimM, dimN), P(dimM, dimN), Q(dimM, dimN), R(dimM, dimN);
2363 for(
int32 r = 0; r < dimM; r++) {
2364 for (
int32 c = 0; c < dimN; c++) {
2367 x = -1.0 + 2.0 / (1.0 +
Exp(-2.0 * x));
2369 x = 1.0 - 2.0 / (1.0 +
Exp(2.0 * x));
2372 Real out_diff = P(r, c), in_diff = out_diff * (1.0 - x * x);
2386 Matrix<Real> M(dimM, dimN), P(dimM, dimN), Q(dimM, dimN), R(dimM, dimN);
2390 for(
int32 r = 0; r < dimM; r++) {
2391 for (
int32 c = 0; c < dimN; c++) {
2393 y = 1.0 / (1 +
Exp(-x));
2395 Real out_diff = P(r, c), in_diff = out_diff * y * (1.0 - y);
2409 Matrix<Real> M(dimM, dimN), N(dimM, dimN), O(dimM, dimN);
2413 for(
int32 r = 0; r < dimM; r++) {
2414 for (
int32 c = 0; c < dimN; c++) {
2417 if (x > 10.0) y = x;
2491 bool binary = (
i%2 == 0);
2509 std::ofstream outs(
"tmpf", std::ios_base::out |std::ios_base::binary);
2511 M.
Write(outs, binary);
2512 S.Write(outs, binary);
2513 v1.
Write(outs, binary);
2514 M.
Write(outs, binary);
2515 S.Write(outs, binary);
2516 v1.
Write(outs, binary);
2521 bool either_way = (
i%2 == 0);
2522 std::ifstream ins(
"tmpf", std::ios_base::in | std::ios_base::binary);
2529 N.
Read(ins, binary_in, either_way);
2530 T.
Read(ins, binary_in, either_way);
2531 v2.
Read(ins, binary_in, either_way);
2535 N.
Read(ins, binary_in,
true);
2536 T.
Read(ins, binary_in,
true);
2540 v2.
Read(ins, binary_in,
true);
2558 bool binary = (
i%2 == 0);
2577 std::ofstream outs(
"tmpf", std::ios_base::out |std::ios_base::binary);
2580 M.
Write(outs, binary);
2581 S.
Write(outs, binary);
2582 v.
Write(outs, binary);
2583 M.
Write(outs, binary);
2584 S.
Write(outs, binary);
2585 v.
Write(outs, binary);
2588 std::ifstream ins(
"tmpf", std::ios_base::in | std::ios_base::binary);
2594 MO.
Read(ins, binary_in);
2595 SO.
Read(ins, binary_in);
2596 vO.
Read(ins, binary_in);
2597 MO.
Read(ins, binary_in,
true);
2598 SO.
Read(ins, binary_in,
true);
2599 vO.
Read(ins, binary_in,
true);
2630 std::ofstream os(
"tmpf", std::ios::out|std::ios::binary);
2637 std::ifstream is(
"tmpf", std::ios::in|std::ios::binary);
2665 MatrixIndexT dimMEnd = dimMStart + 1 + (
Rand()%10);
if (dimMEnd > dimM) dimMEnd = dimM;
2666 MatrixIndexT dimNEnd = dimNStart + 1 + (
Rand()%10);
if (dimNEnd > dimN) dimNEnd = dimN;
2669 SubMatrix<Real> sub(M, dimMStart, dimMEnd-dimMStart, dimNStart, dimNEnd-dimNStart);
2671 KALDI_ASSERT(sub.
Sum() == M.
Range(dimMStart, dimMEnd-dimMStart, dimNStart, dimNEnd-dimNStart).Sum());
2679 KALDI_ASSERT(sub.
Sum() == M.
Range(dimMStart, dimMEnd-dimMStart, dimNStart, dimNEnd-dimNStart).Sum());
2694 MatrixIndexT lenEnd = lenStart + 1 + (
Rand()%10);
if (lenEnd > length) lenEnd = length;
2754 float alpha = 1.0, beta =0;
2755 if (
i%3 == 0) beta = 0.5;
2756 if (
i%5 == 0) alpha = 0.7;
2769 v2.
AddTpVec(alpha, T, trans, v, beta);
2775 v.
AddTpVec(alpha, T, trans, v, beta);
2781 float alpha = 1.0, beta =0;
2782 if (
i%3 == 0) beta = 0.5;
2783 if (
i%5 == 0) alpha = 0.7;
2825 f2 += v(
i) * v(
j) * S(
i,
j);
2883 dimN = (
Rand()%10 + 1),
2884 dimO = (
Rand()%10 + 1);
2888 B(dimM, dimO), C(dimO, dimN);
2889 A.SetRandn(); B.SetRandn(); C.
SetRandn();
2890 if (transB ==
kTrans) B.Transpose();
2894 A.AddMatMat(alpha, B, transB, C, transC, beta);
2895 A2.AddMatSmat(alpha, B, transB, C, transC, beta);
2896 A3.
AddSmatMat(alpha, B, transB, C, transC, beta);
2906 dimN = (
Rand()%10 + 1);
2916 S.AddMat2Sp(alpha, M, trans, T, beta);
2926 S2.AddSp(alpha, tmp);
2940 M.AddMat(0.5, M,
kTrans);
2949 dimN = (
Rand() % 10) + extra;
2950 Real alpha = 0.2 * (
Rand() % 6),
2951 beta = 0.2 * (
Rand() % 6);
2958 else M.
Resize(dimN, dimM);
2963 S.
AddMat2(alpha, M, trans, beta);
2965 Sfull.AddMatMat(alpha, M, trans, M, other_trans, beta);
2983 KALDI_LOG <<
"dimM = " << dimM <<
", dimN = " << dimN;
2997 Real alpha = 0.2 * (
Rand() % 6),
2998 beta = 0.2 * (
Rand() % 6);
3005 M2.
AddMatMat(alpha, N, trans, N, other_trans, beta);
3012 KALDI_ASSERT(dimM == 0 || X1.Trace() != 0 || (alpha == 0 && beta == 0));
3044 double observed_impr2 = (
VecVec(x2, g) -0.5*
VecSpVec(x2, H, x2)) -
3052 KALDI_ASSERT(std::abs(ans2 - ans3) / std::max(ans2, ans3) < 0.01);
3066 Q.AddMat2(1.0, Mtmp,
kNoTrans, 0.0);
3079 if (Q.Cond() < 1000.0) Qinv.
Invert();
3088 if (Q.Cond() < 1000.0) {
3135 a4 = a1 - 0.5 * a2 - 0.5 * a3,
3136 b4 = b1 - 0.5 * b2 - 0.5 * b3;
3137 KALDI_LOG <<
"a4 = " << a4 <<
", b4 = " << b4;
3151 C(r, c) = std::max(A(r, c), B(r, c));
3165 Real max_eig = std::max(-s.
Min(), s.
Max());
3174 bool minimize = (iter % 2 == 0);
3199 Real sign = (minimize ? -1.0 : 1.0);
3205 dlogf_dx.
AddSpVec(-1.0, S, x, 1.0);
3207 Real f =
Exp(c * logf);
3212 opt_lbfgs.
DoStep(f, df_dx);
3217 diff.
AddVec(-1.0, x_opt);
3218 KALDI_VLOG(2) <<
"L-BFGS finished after " << num_iters <<
" function evaluations.";
3231 for (
int i = 0;
i < 20 ;
i++) {
3239 if (
Rand() % 2 == 0)
3241 if (
Rand() % 2 == 0)
3246 b.AddSpVec(1.0, A, x, 0.0);
3255 residual_error.
AddVec(-1.0, b2);
3262 KALDI_LOG <<
"error = " << error <<
", b norm is " << b.Norm(2.0)
3263 <<
", A max-abs-eig is " << max_abs;
3286 Real min = 1.0e+10, max = -1.0e+10;
3288 min = std::min(min, v(
i));
3289 max = std::max(max, v(
i));
3297 Real min = 1.0e+10, max = -1.0e+10;
3300 min = std::min(min, S(
i,
j));
3301 max = std::max(max, S(
i,
j));
3310 Real min = 1.0e+10, max = -1.0e+10;
3313 min = std::min(min, mat(
i,
j));
3314 max = std::max(max, mat(
i,
j));
3322 template<
typename Real>
3324 return ( std::abs(a-b) <= 1.0e-03 * (std::abs(a)+std::abs(b)));
3331 Matrix<Real> A(dimM, dimN), B(dimN, dimO), C(dimO, dimP);
3332 A.SetRandn(); B.SetRandn(); C.
SetRandn();
3333 Matrix<Real> AT(dimN, dimM), BT(dimO, dimN), CT(dimP, dimO);
3366 KALDI_ASSERT((std::abs(t1-t2) < tol) && (std::abs(t2-t3) < tol) && (std::abs(t3-t4) < tol)
3367 && (std::abs(t4-t5) < tol) && (std::abs(t5-t6) < tol) && (std::abs(t6-t7) < tol)
3368 && (std::abs(t7-t8) < tol) && (std::abs(t8-t9) < tol));
3404 if (N>0) x.
Scale(1.0/static_cast<Real>(N));
3428 N = (
Rand() % 150);
3433 Vector<Real> v(twoN), w_base(twoN), w_alg(twoN), x_base(twoN), x_alg(twoN);
3437 if (N< 100)
ComplexFt(v, &w_base,
true);
3438 w_alg.CopyFromVec(v);
3442 if (N< 100)
ComplexFt(w_base, &x_base,
false);
3461 std::vector<Real> temp_buffer;
3464 Vector<Real> v(twoN), w_base(twoN), w_alg(twoN), x_base(twoN), x_alg(twoN);
3468 if (N< 100)
ComplexFt(v, &w_base,
true);
3469 w_alg.CopyFromVec(v);
3471 if (
Rand() % 2 == 0)
3472 srfft.Compute(w_alg.Data(),
true);
3474 srfft2.
Compute(w_alg.Data(),
true, &temp_buffer);
3478 if (N< 100)
ComplexFt(w_base, &x_base,
false);
3480 srfft.Compute(x_alg.
Data(),
false);
3501 std::vector<Real> sizes;
3502 sizes.push_back(16);
3503 sizes.push_back(128);
3504 for (
int i = 0;
i < 2;
i++) {
3532 Real alpha = 0.33, beta = 4.5;
3541 std::vector<Real> sizes;
3542 sizes.push_back(16);
3543 sizes.push_back(128);
3544 for (
int i = 0;
i < 2;
i++) {
3568 vorig.CopyFromVec(v);
3602 srfft.
Compute(v.Data(),
true);
3603 srfft.
Compute(v.Data(),
false);
3616 if (N >90) N *=
Rand() % 60;
3631 if (N != 0) x.Scale(1.0/N);
3632 if (N != 0) y.
Scale(1.0/N);
3646 std::vector<Real> temp_buffer;
3653 if (
Rand() % 2 == 0)
3654 srfft.Compute(y.
Data(),
true);
3664 srfft.Compute(y.
Data(),
false);
3682 if (
i % 1000 == 0)
KALDI_LOG <<
"done 1000 [ == ten seconds of speech]";
3694 KALDI_LOG <<
"done 1000 [ == ten seconds of speech, split-radix]";
3700 template<
typename Real>
3706 Real x = 2.0, y = 0.0;
3709 AssertEqual(std::pow(static_cast<Real>(2.0), power), x);
3733 x = 1.0/std::sqrt(2.0); y = -1.0/std::sqrt(2.0);
3753 template<
typename Real>
3767 bool b = MM2.
Power(1.0);
3771 b = MMMM2.
Power(2.0);
3788 if (!MM2.
Power(2.0)) {
3789 KALDI_LOG <<
"Could not take matrix to power (not an error)";
3794 if (!MMMM2.
Power(4.0)) {
3795 KALDI_LOG <<
"Could not take matrix to power (not an error)";
3800 if (!MMMM3.
Power(2.0)) {
3801 KALDI_ERR <<
"Could not take matrix to power (should have been able to)";
3807 if (!MM4.
Power(-1.0))
3808 KALDI_ERR <<
"Could not take matrix to power (should have been able to)";
3825 if (
i == 0) f = 2.0;
3853 template<
typename Real>
3858 bool exact =
i % 2 == 0;
3860 feat_dim = true_dim +
Rand() % 5,
3861 num_points = true_dim +
Rand() % 5,
3862 G = std::min(feat_dim,
3863 std::min(num_points,
3864 static_cast<MatrixIndexT>(true_dim +
Rand() % 5)));
3900 template<
typename Real>
3905 num_points = (
i%2 == 0 ? feat_dim * 2 : feat_dim / 2);
3914 bool print_eigs =
true, exact =
false;
3919 KALDI_LOG <<
"Non-orthogonality of U is " << non_orth;
3927 Scatter.
Eig(&l, &V);
3932 KALDI_LOG <<
"Non-orthogonality of U2 is " << non_orth;
3935 ScatterProjU.AddMat2Sp(1.0, U,
kNoTrans, Scatter, 0.0);
3937 KALDI_LOG <<
"Non-diagonality of proj with U is " 3939 KALDI_LOG <<
"Non-diagonality of proj with U2 is " 3944 KALDI_LOG <<
"Trace proj with U is " << ScatterProjU.Trace()
3945 <<
" with U2 is " << ScatterProjU2.
Trace();
3952 template<
typename Real>
3954 std::vector<MatrixIndexT> sizes;
3955 sizes.push_back(100);
3956 sizes.push_back(150);
3957 sizes.push_back(200);
3958 sizes.push_back(300);
3959 sizes.push_back(500);
3960 sizes.push_back(750);
3961 sizes.push_back(1000);
3962 sizes.push_back(2000);
3964 for (
size_t i = 0;
i < sizes.size();
i++) {
3972 double diff = difftime(end, start);
3973 KALDI_LOG <<
"For size " << size <<
", Eig without eigenvectors took " << diff
3984 double diff = difftime(end, start);
3985 KALDI_LOG <<
"For size " << size <<
", Eig with eigenvectors took " << diff
3993 M.
Svd(&l, NULL, NULL);
3995 double diff = difftime(end, start);
3996 KALDI_LOG <<
"For size " << size <<
", SVD without eigenvectors took " << diff
4001 Matrix<Real> M(size, size), U(size, size), V(size, size);
4006 double diff = difftime(end, start);
4007 KALDI_LOG <<
"For size " << size <<
", SVD with eigenvectors took " << diff
4021 for (
int32 j = 0;
j < num_rows;
j++) {
4022 for (
int32 k = 0; k < num_cols; k++) {
4023 mat(
j, k) =
RandInt(-32768, 32767);
4040 for (
int32 j = 0;
j < num_rows;
j++) {
4041 for (
int32 k = 0; k < num_cols; k++) {
4042 mat(
j, k) =
RandInt(0, 255) / 255.0;
4065 MatrixIndexT num_failure = 0, num_tot = 1000, max_failure = 1;
4068 if (num_rows * num_cols == 0) {
4072 if (rand() % 2 == 0 && num_cols != 0) {
4074 num_cols = 1 +
Rand() % 3;
4081 if (
Rand() % 2 == 0 && num_rows != 0) {
4093 if (
Rand() % modulus != 0) M(r, c) = rand_val;
4122 if (!M2.ApproxEqual(M3, 1.0e-04)) {
4124 cmat.
Write(std::cout,
false);
4126 cmat2.
Write(std::cout,
false);
4127 KALDI_WARN <<
"Matrices differ " << M2 <<
" vs. " << M3 <<
", M2 range is " 4128 << M2.Min() <<
" to " << M2.Max() <<
", M3 range is " 4129 << M3.Min() <<
" to " << M3.Max();
4140 bool allow_row_padding =
true;
4142 col_offset =
RandInt(0, num_cols - 1),
4143 num_rows_sub =
RandInt(1, 4 + num_rows - row_offset),
4144 num_cols_sub =
RandInt(1, num_cols - col_offset);
4146 col_offset, num_cols_sub, allow_row_padding);
4148 for (
int32 row = 0; row < num_rows_sub; row++) {
4149 int32 old_row = row + row_offset;
4150 if (old_row < 0) old_row = 0;
4151 else if (old_row >= num_rows) { old_row = num_rows - 1; }
4153 M2_row(M2, old_row),
4154 M2_row_part(M2_row, col_offset, num_cols_sub);
4155 M2_sub_row.CopyFromVec(M2_row_part);
4158 M3_sub.
AddMat(-1.0, M2_sub);
4163 col_offset =
RandInt(0, num_cols - 1),
4164 num_rows_sub =
RandInt(1, num_rows - row_offset),
4165 num_cols_sub =
RandInt(1, num_cols - col_offset);
4167 col_offset, num_cols_sub);
4169 col_offset, num_cols_sub);
4171 M3_sub.
AddMat(-1.0, M2_sub);
4198 if(num_rows != 0 && num_cols != 0){
4200 sub_col_offset =
Rand() % num_cols;
4203 num_subcols =
Rand() % (num_cols-sub_col_offset);
4204 if(num_subrows == 0 || num_subcols == 0){
4210 cmat.
CopyToMat(sub_row_offset, sub_col_offset, &Msub);
4213 AssertEqual(M2(
i+sub_row_offset, k+sub_col_offset), Msub(
i, k));
4219 for (
int32 t = 0; t < 10; t++) {
4222 num_cols = 10 +
Rand() % 50;
4228 scaled_mat.
Scale(alpha);
4235 bool binary = (
n % 2 == 1);
4237 std::ofstream outs(
"tmpf", std::ios_base::out |std::ios_base::binary);
4239 cmat.
Write(outs, binary);
4244 std::ifstream ins(
"tmpf", std::ios_base::in | std::ios_base::binary);
4246 cmat2.
Read(ins, binary_in);
4251 std::ifstream ins(
"tmpf", std::ios_base::in | std::ios_base::binary);
4254 mat1.
Read(ins, binary_in);
4264 std::ofstream outs(
"tmpf", std::ios_base::out |std::ios_base::binary);
4266 mat1.
Write(outs, binary);
4269 std::ifstream ins(
"tmpf", std::ios_base::in | std::ios_base::binary);
4272 cmat2.
Read(ins, binary_in);
4291 KALDI_LOG <<
"Compressed matrix, tot = " << tot <<
", diff = " 4293 if (err > 0.015 * tot) {
4294 KALDI_WARN <<
"Failure in compressed-matrix test.";
4298 if (num_failure > max_failure)
4299 KALDI_ERR <<
"Too many failures in compressed matrix test " << num_failure
4300 <<
" > " << max_failure;
4313 MatrixIndexT num_failure = 0, num_tot = 1000, max_failure = 1;
4316 if (num_rows * num_cols == 0) {
4320 if (rand() % 2 == 0 && num_cols != 0) {
4322 num_cols = 1 +
Rand() % 3;
4325 if (
Rand() % 3 != 0)
4330 if (
Rand() % 2 == 0 && num_rows != 0) {
4342 if (
Rand() % modulus != 0) M(r, c) = rand_val;
4361 pmat2.GetMatrix(&M2);
4366 if (pmat2.NumRows() > 0) {
4373 mat_B(num_rows, pmat.
NumCols());
4374 pmat3.GetMatrix(&mat_A);
4375 for (
int32 row_out = 0; row_out < num_rows; row_out++) {
4376 int32 row_in = row_out + row_offset;
4377 if (row_in < 0) row_in = 0;
4378 if (row_in >= pmat2.NumRows())
4382 vec_out.CopyFromVec(vec_in);
4384 if (mat_A.NumRows() >= 8) {
4391 mat_A.AddMat(-1.0, mat_B);
4393 (mat_A.NumRows() * mat_A.NumCols()) < 0.001);
4398 bool binary = (
n % 2 == 1);
4400 std::ofstream outs(
"tmpf", std::ios_base::out |std::ios_base::binary);
4402 pmat.
Write(outs, binary);
4407 std::ifstream ins(
"tmpf", std::ios_base::in | std::ios_base::binary);
4409 pmat3.
Read(ins, binary_in);
4420 KALDI_LOG <<
"Compressed matrix, tot = " << tot <<
", diff = " 4422 if (err > 0.015 * tot) {
4423 KALDI_WARN <<
"Failure in possibly compressed-matrix test.";
4427 if (num_failure > max_failure)
4428 KALDI_ERR <<
"Too many failures in possibly compressed matrix test " << num_failure
4429 <<
" > " << max_failure;
4434 template<
typename Real>
4438 if (num_rows * num_cols == 0) {
4449 sub_num_cols =
Rand() % (num_cols - col_offset) + 1;
4450 KALDI_VLOG(3) <<
"Whole matrix size: " << num_rows <<
"," << num_cols;
4451 KALDI_VLOG(3) <<
"Sub-matrix size: " << sub_num_rows <<
"," << sub_num_cols
4452 <<
" with offsets " << row_offset <<
"," << col_offset;
4454 col_offset, sub_num_cols);
4459 SubMatrix<Real> sub_mat(mat3, row_offset, sub_num_rows, col_offset, sub_num_cols);
4461 KALDI_ERR <<
"Matrices differ " << sub_mat <<
" vs. " << mat2;
4467 template<
typename Real>
4480 template<
typename Real>
4485 vec(
n) =
Rand() % 3;
4488 Real sum = vec.
Sum();
4489 int32 num_samples = 100000;
4490 std::vector<int32> counts(N, 0);
4491 for (
int32 i = 0;
i < num_samples;
i++)
4494 Real a = counts[
n] / (1.0 * num_samples),
4496 KALDI_LOG <<
"a = " << a <<
", b = " << b;
4502 template <
class Real>
4506 Matrix<Real> mat1(num_rows, mid), mat2(mid, num_cols), prod(num_rows, num_cols);
4512 "The BLAS library that you are linking against has an issue that might " 4513 "cause problems later on.");
4517 template<
class Real>
4538 SP.AddSpMat(1.0, mat, P,
kNoTrans, 0.0);
4548 mat.
Eig(&fulls, &fullP);
4549 KALDI_LOG <<
"Approximate eigs are " << s;
4552 std::sort(fulls.
Data(), fulls.
Data() + dim);
4554 KALDI_LOG <<
"abs(real eigs) are " << tmp;
4558 KALDI_LOG <<
"real sum is " << real_sum <<
", approx_sum = " << approx_sum;
4573 if (fabs(T(
i,
i)) < 0.2)
4585 b2.
AddTpVec((Real)1.0, T, trans, x, (Real)0.0);
4586 KALDI_LOG <<
"b is " << b <<
", b2 is " << b2;
4593 UnitTestLinearCgd<Real>();
4594 UnitTestGeneralMatrix<BaseFloat>();
4595 UnitTestTridiagonalize<Real>();
4596 UnitTestTridiagonalizeAndQr<Real>();
4597 UnitTestAddMatSmat<Real>();
4598 UnitTestFloorChol<Real>();
4599 UnitTestFloorUnit<Real>();
4600 UnitTestAddMat2Sp<Real>();
4601 UnitTestLbfgs<Real>();
4603 UnitTestCompressedMatrix<Real>();
4604 UnitTestCompressedMatrix2<Real>();
4605 UnitTestExtractCompressedMatrix<Real>();
4606 UnitTestResize<Real>();
4607 UnitTestResizeCopyDataDifferentStrideType<Real>();
4608 UnitTestNonsymmetricPower<Real>();
4609 UnitTestEigSymmetric<Real>();
4611 UnitTestComplexPower<Real>();
4612 UnitTestEig<Real>();
4613 UnitTestEigSp<Real>();
4617 UnitTestComplexFt<Real>();
4618 KALDI_LOG <<
" Point B";
4619 UnitTestComplexFft2<Real>();
4620 UnitTestComplexFft<Real>();
4621 UnitTestSplitRadixComplexFft<Real>();
4622 UnitTestSplitRadixComplexFft2<Real>();
4623 UnitTestDct<Real>();
4624 UnitTestRealFft<Real>();
4625 KALDI_LOG <<
" Point C";
4626 UnitTestSplitRadixRealFft<Real>();
4627 UnitTestSvd<Real>();
4628 UnitTestSvdNodestroy<Real>();
4629 UnitTestSvdJustvec<Real>();
4630 UnitTestSpAddDiagVec<Real, float>();
4631 UnitTestSpAddDiagVec<Real, double>();
4632 UnitTestSpAddVecVec<Real>();
4633 UnitTestSpInvert<Real>();
4634 KALDI_LOG <<
" Point D";
4635 UnitTestTpInvert<Real>();
4637 UnitTestIoCross<Real>();
4638 UnitTestHtkIo<Real>();
4639 UnitTestScale<Real>();
4640 UnitTestTrace<Real>();
4641 KALDI_LOG <<
" Point E";
4642 CholeskyUnitTestTr<Real>();
4643 UnitTestAxpy<Real>();
4644 UnitTestSimple<Real>();
4645 UnitTestMmul<Real>();
4646 UnitTestMmulSym<Real>();
4647 UnitTestVecmul<Real>();
4648 UnitTestInverse<Real>();
4649 UnitTestMulElements<Real>();
4650 UnitTestDotprod<Real>();
4652 UnitTestPower<Real>();
4653 UnitTestPowerAbs<Real>();
4654 UnitTestHeaviside<Real>();
4655 UnitTestCopySp<Real>();
4656 UnitTestDeterminant<Real>();
4657 KALDI_LOG <<
" Point F";
4658 UnitTestDeterminantSign<Real>();
4659 UnitTestSger<Real>();
4660 UnitTestAddOuterProductPlusMinus<Real>();
4661 UnitTestTraceProduct<Real>();
4662 UnitTestTransposeScatter<Real>();
4663 UnitTestRankNUpdate<Real>();
4664 UnitTestSpVec<Real>();
4665 UnitTestLimitCondInvert<Real>();
4666 KALDI_LOG <<
" Point G";
4667 UnitTestLimitCond<Real>();
4668 UnitTestMat2Vec<Real>();
4669 UnitTestFloorCeiling<Real>();
4670 KALDI_LOG <<
" Point H";
4671 UnitTestCopyRowsAndCols<Real>();
4672 UnitTestSpliceRows<Real>();
4673 UnitTestAddSp<Real>();
4674 UnitTestRemoveRow<Real>();
4675 UnitTestRow<Real>();
4676 UnitTestSubvector<Real>();
4677 UnitTestRange<Real>();
4678 UnitTestSimpleForVec<Real>();
4679 UnitTestSetRandn<Real>();
4680 UnitTestSetRandUniform<Real>();
4681 UnitTestVectorMax<Real>();
4682 UnitTestVectorMin<Real>();
4683 UnitTestSimpleForMat<Real>();
4684 UnitTestTanh<Real>();
4685 UnitTestSigmoid<Real>();
4686 UnitTestSoftHinge<Real>();
4687 UnitTestNorm<Real>();
4688 UnitTestCopyCols<Real>();
4689 UnitTestCopyRows<Real>();
4690 UnitTestCopyToRows<Real>();
4691 UnitTestAddRows<Real>();
4692 UnitTestAddToRows<Real>();
4693 UnitTestMul<Real>();
4694 KALDI_LOG <<
" Point I";
4695 UnitTestSolve<Real>();
4696 UnitTestAddMat2<Real>();
4697 UnitTestSymAddMat2<Real>();
4698 UnitTestAddMatSelf<Real>();
4699 UnitTestMaxMin<Real>();
4700 UnitTestInnerProd<Real>();
4701 UnitTestApplyExpSpecial<Real>();
4702 UnitTestScaleDiag<Real>();
4703 UnitTestSetDiag<Real>();
4704 UnitTestSetRandn<Real>();
4705 KALDI_LOG <<
" Point J";
4706 UnitTestTraceSpSpLower<Real>();
4707 UnitTestTranspose<Real>();
4708 UnitTestAddVec2Sp<Real>();
4709 UnitTestAddVecToRows<Real>();
4710 UnitTestAddVecToCols<Real>();
4712 UnitTestTp2Sp<Real>();
4713 UnitTestTp2<Real>();
4714 UnitTestAddDiagMat2<Real>();
4715 UnitTestAddDiagMatMat<Real>();
4717 UnitTestTopEigs<Real>();
4718 UnitTestRandCategorical<Real>();
4719 UnitTestTridiag<Real>();
4720 UnitTestTridiag<Real>();
4722 UnitTestAddDiagVecMat<Real>();
4723 UnitTestAddMatDiagVec<Real>();
4724 UnitTestAddMatMatElements<Real>();
4725 UnitTestAddMatMatNans<Real>();
4726 UnitTestAddToDiagMatrix<Real>();
4727 UnitTestAddToDiag<Real>();
4728 UnitTestMaxAbsEig<Real>();
4729 UnitTestMax2<Real>();
4730 UnitTestPca<Real>(full_test);
4731 UnitTestPca2<Real>(full_test);
4732 UnitTestAddVecVec<Real>();
4733 UnitTestReplaceValue<Real>();
4737 KALDI_LOG <<
" Point K";
4738 UnitTestTriVecSolver<Real>();
4747 using namespace kaldi;
4748 bool full_test =
false;
4750 kaldi::MatrixUnitTest<float>(full_test);
4751 kaldi::MatrixUnitTest<double>(full_test);
void CopyColsFromMat(const MatrixBase< Real > &M)
Performs a column stack of the matrix M.
static void UnitTestAddMatMatElements()
bool ApproxEqual(const VectorBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).Norm(2.0) <= tol * (*this).Norm(2.0).
static void UnitTestTraceProduct()
static void UnitTestAddVecVec()
void AddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
rank-N update: if (transM == kNoTrans) (*this) = beta*(*this) + alpha * M * M^T, or (if transM == kTr...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
static void UnitTestInverse()
static void UnitTestSvdJustvec()
void AddVec2Sp(const Real alpha, const VectorBase< Real > &v, const SpMatrix< Real > &S, const Real beta)
Does *this = beta * *thi + alpha * diag(v) * S * diag(v)
static void UnitTestHeaviside()
void Add(const Real alpha)
Add a scalar to each element.
static void UnitTestCopyCols()
void DoStep(Real function_value, const VectorBase< Real > &gradient)
The user calls this function to provide the class with the function and gradient info at the point Ge...
bool IsUnit(Real cutoff=1.0e-05) const
void CopyFromVec(const SubVector< OtherReal > &orig)
CopyFromVec just interprets the vector as having the same layout as the packed matrix.
static void UnitTestEig()
Real Min() const
Returns minimum element of matrix.
This class provides a way for switching between double and float types.
Real TraceMatMatMat(const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const MatrixBase< Real > &C, MatrixTransposeType transC)
Returns tr(A B C)
bool IsDiagonal(Real cutoff=1.0e-05) const
static void UnitTestMul()
void UnitTestComplexPower()
Packed symetric matrix class.
void Write(std::ostream &out, bool binary) const
write to stream.
This class is a wrapper that enables you to store a matrix in one of three forms: either as a Matrix<...
static void UnitTestMaxAbsEig()
bool InitKaldiInputStream(std::istream &is, bool *binary)
Initialize an opened stream for reading by detecting the binary header and.
void CopyColFromVec(const VectorBase< Real > &v, const MatrixIndexT col)
Copy vector into specific column of matrix.
void CopyDiagFromVec(const VectorBase< Real > &v)
Copy vector into diagonal of matrix.
void Tanh(const MatrixBase< Real > &src)
Set each element to the tanh of the corresponding element of "src".
bool IsDiagonal(Real cutoff=1.0e-05) const
Returns true if matrix is Diagonal.
void CopyRowToVec(MatrixIndexT row, VectorBase< Real > *v) const
Copies row #row of the matrix into vector v.
void CopyColToVec(MatrixIndexT col, VectorBase< Real > *v) const
Copies column #col of the matrix into vector v.
static int32 DoubleFactorial(int32 i)
static void UnitTestAddMatSelf()
This class describes the options for maximizing various quadratic objective functions.
static void CholeskyUnitTestTr()
double SolveQuadraticProblem(const SpMatrix< double > &H, const VectorBase< double > &g, const SolverOptions &opts, VectorBase< double > *x)
bool ApproxEqual(const SpMatrix< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).FrobeniusNorm() <= tol*(*this).FrobeniusNorma()
void Read(std::istream &in, bool binary, bool add=false)
bool Power(Real pow)
The Power method attempts to take the matrix to a power using a method that works in general for frac...
void SymAddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA, Real beta)
*this = beta * *this + alpha * M M^T, for symmetric matrices.
static void UnitTestMmulSym()
void DiffTanh(const MatrixBase< Real > &value, const MatrixBase< Real > &diff)
void UnitTestNonsymmetricPower()
void Transpose()
Transpose the matrix.
void Write(std::ostream &out, bool binary) const
static void UnitTestSpInvert()
bool IsZero(Real cutoff=1.0e-06) const
Returns true if matrix is all zeros.
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
void RealFftInefficient(VectorBase< Real > *v, bool forward)
Inefficient version of Fourier transform, for testing purposes.
static void UnitTestOrthogonalizeRows()
static void UnitTestDeterminantSign()
static void UnitTestDeterminant()
void GetMatrix(Matrix< BaseFloat > *mat) const
Outputs the contents as a matrix.
void Qr(MatrixBase< Real > *Q)
The symmetric QR algorithm.
void CopyDiagFromMat(const MatrixBase< Real > &M)
Extracts the diagonal of the matrix M.
void AddVecVec(const Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &w)
rank-two update, this <– this + alpha (v w' + w v').
void AddMat2Vec(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const VectorBase< Real > &v, const Real beta=0.0)
Extension of rank-N update: this <– beta*this + alpha * M * diag(v) * M^T.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Real Cond() const
Returns condition number by computing Svd.
double TraceMat(const MatrixBase< Real > &A)
Returns trace of matrix.
Real SolveQuadraticMatrixProblem(const SpMatrix< Real > &Q, const MatrixBase< Real > &Y, const SpMatrix< Real > &SigmaInv, const SolverOptions &opts, MatrixBase< Real > *M)
Maximizes the auxiliary function : Like a numerically stable version of .
Base class which provides matrix operations not involving resizing or allocation. ...
static void UnitTestRealFftSpeed()
static void UnitTestComplexFt()
static void UnitTestComplexFft2()
void Solve(const TpMatrix< Real > &M, const MatrixTransposeType trans)
If trans == kNoTrans, solves M x = b, where b is the value of *this at input and x is the value of *t...
static void UnitTestMat2Vec()
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
Real Max() const
Returns maximum element of matrix.
void ApplyCeiling(Real ceil_val, MatrixIndexT *ceiled_count=nullptr)
Applies ceiling to all elements.
bool ApproxEqual(const MatrixBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).FrobeniusNorm() <= tol * (*this).FrobeniusNorm(). ...
void ApplyLogAndCopy(const VectorBase< Real > &v)
Apply natural log to another vector and put result in *this.
static void UnitTestAddVecToRows()
void InvertDouble(Real *logdet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
LatticeWeightTpl< FloatType > Plus(const LatticeWeightTpl< FloatType > &w1, const LatticeWeightTpl< FloatType > &w2)
static void UnitTestSimple()
void ComputeDctMatrix(Matrix< Real > *M)
ComputeDctMatrix computes a matrix corresponding to the DCT, such that M * v equals the DCT of vector...
static void InitRandNonsingular(MatrixBase< Real > *M)
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
Real FrobeniusNorm() const
sqrt of sum of square elements.
static void MatrixUnitTest(bool full_test)
static void UnitTestCompressedMatrix2()
void AddMatSvec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
This is as AddMatVec, except optimized for where v contains a lot of zeros.
Real Trace(bool check_square=true) const
Returns trace of matrix.
static void UnitTestRandCategorical()
static void UnitTestRankNUpdate()
static void UnitTestSplitRadixComplexFft()
Real TraceSpMat(const SpMatrix< Real > &A, const MatrixBase< Real > &B)
Returns tr(A B).
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
static void UnitTestInnerProd()
static void UnitTestTridiagonalizeAndQr()
void AddDiagMat2(Real alpha, const MatrixBase< Real > &M, MatrixTransposeType trans=kNoTrans, Real beta=1.0)
Add the diagonal of a matrix times itself: *this = diag(M M^T) + beta * *this (if trans == kNoTrans)...
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
static void UnitTestPowerAbs()
static void UnitTestEigSp()
float RandGauss(struct RandomState *state=NULL)
static void UnitTestSplitRadixRealFft()
void ApplyPow(Real exponent)
Takes matrix to a fraction power via Svd.
const VectorBase< Real > & GetValue(Real *objf_value=NULL) const
This returns the value of the variable x that has the best objective function so far, and the corresponding objective function value if requested.
void Eig(VectorBase< Real > *s, MatrixBase< Real > *P=NULL) const
Solves the symmetric eigenvalue problem: at end we should have (*this) = P * diag(s) * P^T...
static void UnitTestHtkIo()
static void UnitTestSvdNodestroy()
A class for storing matrices.
bool diagonal_precondition
void ScaleDiag(const Real alpha)
static void UnitTestMmul()
void SetUnit()
< Set to zero
static void UnitTestSimpleForMat()
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void UnitTestIo(bool binary)
void AddSpVec(const Real alpha, const SpMatrix< Real > &M, const VectorBase< Real > &v, const Real beta)
Add symmetric positive definite matrix times vector: this <– beta*this + alpha*M*v.
void Write(std::ostream &os, bool binary) const
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
void SwapSparseMatrix(SparseMatrix< BaseFloat > *smat)
Swaps the with the given SparseMatrix.
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
bool IsTridiagonal(Real cutoff=1.0e-05) const
MatrixIndexT NumRows() const
void PlaceNansInGaps(Matrix< Real > *mat)
static void UnitTestRealFft()
static void UnitTestAddVecToCols()
static void UnitTestSpVec()
static void UnitTestResize()
void Read(std::istream &is, bool binary)
static bool approx_equal(Real a, Real b)
static void UnitTestDotprod()
void SetUnit()
Sets to zero, except ones along diagonal [for non-square matrices too].
void Read(std::istream &is, bool binary)
Note: if you write a compressed matrix in text form, it will be read as a regular full matrix...
Real SolveDoubleQuadraticMatrixProblem(const MatrixBase< Real > &G, const SpMatrix< Real > &P1, const SpMatrix< Real > &P2, const SpMatrix< Real > &Q1, const SpMatrix< Real > &Q2, const SolverOptions &opts, MatrixBase< Real > *M)
Maximizes the auxiliary function : Encountered in matrix update with a prior.
void CopyFromSp(const SpMatrix< Real > &other)
Real Norm(Real p) const
Compute the p-th norm of the vector.
void Tridiagonalize(MatrixBase< Real > *Q)
Tridiagonalize the matrix with an orthogonal transformation.
void SetRandUniform()
Sets to numbers uniformly distributed on (0, 1)
void CopyRowFromMat(const MatrixBase< Real > &M, MatrixIndexT row)
Extracts a row of the matrix M.
static void UnitTestMulElements()
static void UnitTestSymAddMat2()
static void UnitTestCopyToRows()
static void UnitTestTriVecSolver()
static void UnitTestFloorCeiling()
void SetRandn()
< Set to unit matrix.
void AddSpSp(const Real alpha, const SpMatrix< Real > &A, const SpMatrix< Real > &B, const Real beta)
this <– beta*this + alpha*A*B.
void CopyFromSp(const SpMatrix< OtherReal > &M)
Copy given spmatrix. (no resize is done).
void CopyToMat(MatrixBase< OtherReal > *other, MatrixTransposeType t=kNoTrans) const
Copy to matrix. It must already have the correct size.
void SetVerboseLevel(int32 i)
This should be rarely used, except by programs using Kaldi as library; command-line programs set the ...
static void UnitTestAxpy()
Real LogSumExp(Real prune=-1.0) const
Returns log(sum(exp())) without exp overflow If prune > 0.0, it uses a pruning beam, discarding terms less than (max - prune).
void AddVec2(const Real alpha, const VectorBase< Real > &v)
Add vector : *this = *this + alpha * rv^2 [element-wise squaring].
int ApplyFloor(const SpMatrix< Real > &Floor, Real alpha=1.0, bool verbose=false)
Floors this symmetric matrix to the matrix alpha * Floor, where the matrix Floor is positive definite...
void ApplyFloor(Real floor_val, MatrixIndexT *floored_count=nullptr)
Applies floor to all elements.
void ComplexFt(const VectorBase< Real > &in, VectorBase< Real > *out, bool forward)
ComplexFt is the same as ComplexFft but it implements the Fourier transform in an inefficient way...
bool IsZero(Real cutoff=1.0e-05) const
Returns true if matrix is all zeros.
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void AddToDiag(const Real r)
MatrixIndexT NumCols() const
void SoftHinge(const MatrixBase< Real > &src)
Set each element to y = log(1 + exp(x))
void AddVec2(const Real alpha, const VectorBase< OtherReal > &v)
rank-one update, this <– this + alpha v v'
void UnitTestAddVecCross()
static void UnitTestTridiag()
void CopyToRows(Real *const *dst) const
Copies row r of this matrix to the array of floats at the location given by dst[r].
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
bool IsSymmetric(Real cutoff=1.0e-05) const
Returns true if matrix is Symmetric.
void Cholesky(const SpMatrix< Real > &orig)
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
static void UnitTestNorm()
void Compute(Real *xr, Real *xi, bool forward) const
static void UnitTestSigmoid()
static void UnitTestAddMatMatNans()
void AddSmat2Sp(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const SpMatrix< Real > &A, const Real beta=0.0)
This is a version of AddMat2Sp specialized for when M is fairly sparse.
static void UnitTestReplaceValue()
Real LogPosDefDet() const
Computes log determinant but only for +ve-def matrices (it uses Cholesky).
Real TraceMatSpMat(const MatrixBase< Real > &A, MatrixTransposeType transA, const SpMatrix< Real > &B, const MatrixBase< Real > &C, MatrixTransposeType transC)
Returns tr(A B C) (A and C may be transposed as specified by transA and transC).
static void UnitTestEigSymmetric()
static void UnitTestCopyRows()
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
MatrixIndexT LimitCond(Real maxCond=1.0e+5, bool invert=false)
void Scale(Real alpha)
Multiply each element with a scalar value.
static void UnitTestSplitRadixRealFftSpeed()
void MulElements(const VectorBase< Real > &v)
Multiply element-by-element by another vector.
static void UnitTestExtractCompressedMatrix()
void AddSp(const Real alpha, const SpMatrix< Real > &Ma)
static void UnitTestSplitRadixComplexFft2()
static void UnitTestAddDiagMatMat()
static void UnitTestSvdZero()
void ApplyPowAbs(Real power, bool include_sign=false)
Take the absolute value of all elements of a vector to a power.
void AddVecToRows(const Real alpha, const VectorBase< OtherReal > &v)
[each row of *this] += alpha * v
static void UnitTestComplexFft()
static void UnitTestLimitCond()
static void UnitTestAddSp()
static void UnitTestGeneralMatrix()
static void UnitTestTp2()
static void UnitTestTridiagonalize()
static void UnitTestSvdSpeed()
void RandPosdefSpMatrix(MatrixIndexT dim, SpMatrix< Real > *matrix)
static void UnitTestSetRandn()
void AddMatSp(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const SpMatrix< Real > &B, const Real beta)
this <– beta*this + alpha*A*B.
static void UnitTestTanh()
void SetDiag(const Real alpha)
void CopyFromTp(const TpMatrix< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given tpmatrix. (no resize is done).
void SetRandn()
Sets to random values of a normal distribution.
static void UnitTestPca(bool full_test)
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
static void UnitTestAddDiagMat2()
static void UnitTestPca2(bool full_test)
void AddToRows(Real alpha, Real *const *dst) const
For each row r of this matrix, adds it (times alpha) to the array of floats at the location given by ...
void ExtractRowRangeWithPadding(const GeneralMatrix &in, int32 row_offset, int32 num_rows, GeneralMatrix *out)
This function extracts a row-range of a GeneralMatrix and writes as a GeneralMatrix containing the sa...
void TopEigs(VectorBase< Real > *s, MatrixBase< Real > *P, MatrixIndexT lanczos_dim=0) const
This function gives you, approximately, the largest eigenvalues of the symmetric matrix and the corre...
static void UnitTestAddMat2()
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
void ComputePca(const MatrixBase< Real > &X, MatrixBase< Real > *U, MatrixBase< Real > *A, bool print_eigs, bool exact)
ComputePCA does a PCA computation, using either outer products or inner products, whichever is more e...
double TraceSpSp(const SpMatrix< double > &A, const SpMatrix< double > &B)
static void UnitTestSpliceRows()
static void UnitTestApplyExpSpecial()
void AddMatMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const MatrixBase< Real > &C, MatrixTransposeType transC, const Real beta)
this <– beta*this + alpha*A*B*C.
Real VecSpVec(const VectorBase< Real > &v1, const SpMatrix< Real > &M, const VectorBase< Real > &v2)
Computes v1^T * M * v2.
Real TraceSpSpLower(const SpMatrix< Real > &A, const SpMatrix< Real > &B)
Packed symetric matrix class.
static void UnitTestAddMat2Sp()
void Scale(float alpha)
scales all elements of matrix by alpha.
static void UnitTestTrace()
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
Real * Data()
Returns a pointer to the start of the vector's data.
static void UnitTestSubvector()
void AddOuterProductPlusMinus(Real alpha, const VectorBase< Real > &a, const VectorBase< Real > &b, MatrixBase< Real > *plus, MatrixBase< Real > *minus)
static void UnitTestAddDiagVecMat()
static void UnitTestVectorMin()
static void UnitTestIoCross()
static void UnitTestSolve()
static void UnitTestTransposeScatter()
MatrixIndexT Dim() const
Returns the dimension of the vector.
Real Sum() const
Returns sum of all elements in matrix.
void SetZero()
Sets matrix to zero.
static Real NonOrthogonality(const MatrixBase< Real > &M, MatrixTransposeType transM)
void Scale(Real alpha)
Multiplies all elements by this constant.
static void UnitTestAddMatDiagVec()
static void UnitTestTranspose()
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
static void UnitTestAddRows()
static void UnitTestDct()
void MulElements(const MatrixBase< Real > &A)
Element by element multiplication with a given matrix.
Real RecentStepLength() const
Returns the average magnitude of the last n steps (but not more than the number we have stored)...
int Rand(struct RandomState *state)
Real Sum() const
Returns sum of the elements.
static void UnitTestMax2()
void SetRandn()
Set vector to random normally-distributed noise.
static Real NonUnitness(const SpMatrix< Real > &S)
void AddVecDivVec(Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &r, Real beta)
Add element-by-element quotient of two vectors.
Real TraceMatSpMatSp(const MatrixBase< Real > &A, MatrixTransposeType transA, const SpMatrix< Real > &B, const MatrixBase< Real > &C, MatrixTransposeType transC, const SpMatrix< Real > &D)
Returns tr (A B C D) (A and C may be transposed as specified by transA and transB).
void Eig(MatrixBase< Real > *P, VectorBase< Real > *eigs_real, VectorBase< Real > *eigs_imag) const
Eigenvalue Decomposition of a square NxN matrix into the form (*this) = P D P^{-1}.
void ComplexFft(VectorBase< Real > *v, bool forward, Vector< Real > *tmp_in)
The function ComplexFft does an Fft on the vector argument v.
void MulColsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = (*this) * diag(scale).
static void UnitTestVectorMax()
void MulRowsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = diag(scale) * (*this).
void AddSmatMat(Real alpha, const SparseMatrix< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, Real beta)
(*this) = alpha * op(A) * B + beta * (*this), where A is sparse.
static void UnitTestVecmul()
void CopyFromMat(const MatrixBase< Real > &orig, SpCopyType copy_type=kTakeMean)
void CopyFromTp(const TpMatrix< Real > &other)
CopyFromTp copies another triangular matrix into this one.
static void UnitTestAddToDiag()
void CopyColFromMat(const MatrixBase< OtherReal > &M, MatrixIndexT col)
Extracts a column of the matrix M.
void DiffSigmoid(const MatrixBase< Real > &value, const MatrixBase< Real > &diff)
static void UnitTestAddVec2Sp()
static void UnitTestAddToDiagMatrix()
static void UnitTestAddOuterProductPlusMinus()
void AddTpVec(const Real alpha, const TpMatrix< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add triangular matrix times vector: this <– beta*this + alpha*M*v.
A class representing a vector.
void SetRandn(BaseFloat zero_prob)
Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob an...
static void UnitTestMaxMin()
static void UnitTestCompressedMatrix()
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
static void UnitTestScaleDiag()
static void UnitTestTp2Sp()
void AddVecToCols(const Real alpha, const VectorBase< OtherReal > &v)
[each col of *this] += alpha * v
#define KALDI_ASSERT(cond)
int32 g_kaldi_verbose_level
This is set by util/parse-options.
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
MatrixIndexT NumRows() const
void Set(Real f)
Set all members of a vector to a specified value.
void AddMat2Sp(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const SpMatrix< Real > &A, const Real beta=0.0)
Extension of rank-N update: this <– beta*this + alpha * M * A * M^T.
Real FrobeniusNorm() const
Frobenius norm, which is the sqrt of sum of square elements.
Real LogDet(Real *det_sign=NULL) const
Returns logdet of matrix.
void ApplyPow(Real power)
Take all elements of vector to a power.
static void UnitTestSimpleForVec()
void CreateEigenvalueMatrix(const VectorBase< Real > &re, const VectorBase< Real > &im, MatrixBase< Real > *D)
Creates the eigenvalue matrix D that is part of the decomposition used Matrix::Eig.
static void UnitTestSpAddVecVec()
static void UnitTestTopEigs()
Real ApplySoftMax()
Apply soft-max to the collection of all elements of the matrix and return normalizer (log sum of expo...
void AddSp(const Real alpha, const SpMatrix< OtherReal > &S)
*this += alpha * S
Real Cond() const
Returns maximum ratio of singular values.
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
int32 LinearCgd(const LinearCgdOptions &opts, const SpMatrix< Real > &A, const VectorBase< Real > &b, VectorBase< Real > *x)
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
static void UnitTestRemoveRow()
void CopyRowFromVec(const VectorBase< Real > &v, const MatrixIndexT row)
Copy vector into specific row of matrix.
static void UnitTestRange()
void CopyRowsFromMat(const MatrixBase< Real > &M)
Performs a row stack of the matrix M.
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
static void UnitTestSvd()
static void UnitTestSetDiag()
void cblas_Xspmv(const float alpha, const int num_rows, const float *Mdata, const float *v, const int v_inc, const float beta, float *y, const int y_inc)
void AddDiagMatMat(Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const MatrixBase< Real > &N, MatrixTransposeType transN, Real beta=1.0)
Add the diagonal of a matrix product: *this = diag(M N), assuming the "trans" arguments are both kNoT...
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
Real ApplyLogSoftMax()
Applies log soft-max to vector and returns normalizer (log sum of exponentials).
void RemoveRow(MatrixIndexT i)
Remove a specified row.
static void UnitTestFloorUnit()
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...
void ReplaceValue(Real orig, Real changed)
Set each element to y = (x == orig ? changed : x).
static void UnitTestScale()
This is an implementation of L-BFGS.
static void UnitTestSetRandUniform()
void Svd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt) const
Compute SVD (*this) = U diag(s) Vt.
void CopyFromMat(const MatrixBase< Real > &M, MatrixTransposeType Trans=kNoTrans)
CopyFromMat copies the lower triangle of M into *this (or the upper triangle, if Trans == kTrans)...
static void UnitTestLimitCondInvert()
static void UnitTestCopySp()
static void UnitTestSoftHinge()
MatrixIndexT RandCategorical() const
This function returns a random index into this vector, chosen with probability proportional to the co...
static void UnitTestCopyRowsAndCols()
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
void ApplyAbs()
Take absolute value of each of the elements.
void RemoveElement(MatrixIndexT i)
Remove one element and shifts later elements down.
void Invert(Real *log_det=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
void SymPosSemiDefEig(VectorBase< Real > *s, MatrixBase< Real > *P, Real tolerance=0.001) const
This is the version of SVD that we implement for symmetric positive definite matrices.
static void UnitTestLinearCgd()
static void UnitTestSger()
Provides a vector abstraction class.
void AddColSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of columns of M) + beta * *this.
void ApplyFloor(Real floor_val)
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
bool IsUnit(Real cutoff=1.0e-05) const
Returns true if the matrix is all zeros, except for ones on diagonal.
void ApplyPow(Real power)
void CopyRowsFromVec(const VectorBase< Real > &v)
This function has two modes of operation.
static void UnitTestAddMatSmat()
void Invert(Real *logdet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
bool IsZero(Real cutoff=1.0e-05) const
void CopyToMat(MatrixBase< Real > *mat, MatrixTransposeType trans=kNoTrans) const
Copies contents to matrix.
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
Real MaxAbsEig() const
Returns the maximum of the absolute values of any of the eigenvalues.
Sub-matrix representation.
static void UnitTestTpInvert()
void AddDiagVec(const Real alpha, const VectorBase< OtherReal > &v)
diagonal update, this <– this + diag(v)
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
void CopyColsFromVec(const VectorBase< Real > &v)
Copies vector into matrix, column-by-column.
const VectorBase< Real > & GetProposedValue() const
This returns the value at which the function wants us to compute the objective function and gradient...
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
static Real NonDiagonalness(const SpMatrix< Real > &S)
Real LogDet(Real *det_sign=NULL) const
bool AttemptComplexPower(Real *x_re, Real *x_im, Real power)
The following function is used in Matrix::Power, and separately tested, so we declare it here mainly ...
static void UnitTestPower()
static void UnitTestSpAddDiagVec()
static void UnitTestLbfgs()
static void UnitTestRow()
void Compute(Real *x, bool forward)
If forward == true, this function transforms from a sequence of N real points to its complex fourier ...
void SortSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt, bool sort_on_absolute_value)
Function to ensure that SVD is sorted.
static void UnitTestAddToRows()
void Set(Real)
Sets all elements to a specific value.
static void UnitTestFloorChol()
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
static void UnitTestResizeCopyDataDifferentStrideType()
Make sure that when Resize() is called with resize_type = kCopyData and a stride_type different from ...
Real TraceMatMatMatMat(const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const MatrixBase< Real > &C, MatrixTransposeType transC, const MatrixBase< Real > &D, MatrixTransposeType transD)
Returns tr(A B C D)
static void UnitTestSvdBad()
void Sigmoid(const MatrixBase< Real > &src)
Set each element to the sigmoid of the corresponding element of "src".
void Write(std::ostream &os, bool binary) const
void RealFft(VectorBase< Real > *v, bool forward)
RealFft is a fourier transform of real inputs.
static void UnitTestTraceSpSpLower()
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).