33 "kaldi::kNoTrans and kaldi::kTrans must be equal to the appropriate CBLAS library constants!");
37 template<
typename Real>
39 bool inverse_needed) {
42 if (det_sign) *det_sign = 1;
43 if (log_det) *log_det = 0.0;
47 KaldiBlasInt *pivot =
new KaldiBlasInt[num_rows_];
48 KaldiBlasInt M = num_rows_;
49 KaldiBlasInt N = num_cols_;
50 KaldiBlasInt LDA = stride_;
51 KaldiBlasInt result = -1;
52 KaldiBlasInt l_work = std::max<KaldiBlasInt>(1, N);
55 if ((p_work = static_cast<Real*>(
58 throw std::bad_alloc();
62 const int pivot_offset = 1;
64 int *pivot =
new int[num_rows_];
66 clapack_Xgetrf(num_rows_, num_cols_,
data_, stride_, pivot, &result);
67 const int pivot_offset = 0;
69 KALDI_ASSERT(result >= 0 &&
"Call to CLAPACK sgetrf_ or ATLAS clapack_sgetrf " 70 "called with wrong arguments");
73 KALDI_ERR <<
"Cannot invert: matrix is singular";
75 if (log_det) *log_det = -std::numeric_limits<Real>::infinity();
76 if (det_sign) *det_sign = 0;
84 if (det_sign != NULL) {
87 if (pivot[
i] != static_cast<int>(
i) + pivot_offset) sign *= -1;
90 if (log_det != NULL || det_sign != NULL) {
91 if (log_det != NULL) *log_det = 0.0;
94 prod *= (*this)(
i,
i);
95 if (i == num_rows_ - 1 || std::fabs(prod) < 1.0e-10 ||
96 std::fabs(prod) > 1.0e+10) {
97 if (log_det != NULL) *log_det +=
kaldi::Log(std::fabs(prod));
98 if (det_sign != NULL) *det_sign *= (prod > 0 ? 1.0 : -1.0);
110 clapack_Xgetri(num_rows_,
data_, stride_, pivot, &result);
113 KALDI_ASSERT(result == 0 &&
"Call to CLAPACK sgetri_ or ATLAS clapack_sgetri " 114 "called with wrong arguments");
127 template<
typename Real>
128 template<
typename OtherReal>
133 if (num_rows_ * num_cols_ > 100) {
136 cblas_Xger(num_rows_, num_cols_, alpha, temp_a.Data(), 1,
140 Real *row_data =
data_;
144 row_data[
j] += alpha_ai * b_data[
j];
161 void MatrixBase<double>::AddVecVec(
const double alpha,
165 if (num_rows_ == 0)
return;
170 template<
typename Real>
182 if (num_rows_ == 0)
return;
188 template<
typename Real>
196 BaseFloat i = C(r, c), o = B(r, c), od = A(r, c),
211 template<
typename Real>
216 for (
int32 i = 0;
i < num_rows;
i++)
218 data[
j * stride + i ] = data[i * stride +
j];
222 template<
typename Real>
227 for (
int32 i = 0;
i < num_rows;
i++)
229 data[i * stride +
j] = data[
j * stride + i];
232 template<
typename Real>
241 if (num_rows_ == 0)
return;
252 if (transA ==
kTrans && num_rows_ >= 56) {
266 template<
typename Real>
291 Bdata + c, Bstride, beta, data + c, stride);
299 Bdata + (c * Bstride), 1, beta, data + c, stride);
304 template<
typename Real>
328 Adata + (r * Astride), 1, beta, data + (r * stride), 1);
336 Adata + r, Astride, beta, data + (r * stride), 1);
341 template<
typename Real>
352 cblas_Xsymm(alpha, sz, A.data_, A.stride_, B.data_, B.stride_, beta,
data_, stride_);
355 template<
typename Real>
362 KALDI_ASSERT(num_rows_ == num_cols_ &&
"AddMat: adding to self (transposed): not symmetric.");
367 Real *lower = data + (row * stride_) + col, *upper = data + (col
369 Real sum = *lower + *upper;
370 *lower = *upper = sum;
372 *(data + (row * stride_) + row) *= 2.0;
377 Real *lower = data + (row * stride_) + col, *upper = data + (col
379 Real lower_tmp = *lower;
380 *lower += alpha * *upper;
381 *upper += alpha * lower_tmp;
383 *(data + (row * stride_) + row) *= (1.0 + alpha);
388 int aStride = (int) A.
stride_, stride = stride_;
392 if (num_rows_ == 0)
return;
393 for (
MatrixIndexT row = 0; row < num_rows_; row++, adata += aStride,
399 if (num_rows_ == 0)
return;
400 for (
MatrixIndexT row = 0; row < num_rows_; row++, adata++, data += stride)
401 cblas_Xaxpy(num_cols_, alpha, adata, aStride, data, 1);
406 template<
typename Real>
436 template<
typename Real>
447 this_num_cols = this->NumCols();
449 Real *this_row_i = this->RowData(
i);
453 const std::pair<MatrixIndexT, Real> &p = A_row_i.
GetElement(e);
455 Real alpha_A_ik = alpha * p.second;
456 const Real *b_row_k = B.
RowData(k);
471 this_num_cols = this->NumCols();
472 for (
int k = 0; k < a_num_rows; ++k) {
473 const Real *b_row_k = B.
RowData(k);
477 const std::pair<MatrixIndexT, Real> &p = A_row_k.
GetElement(e);
479 Real alpha_A_ki = alpha * p.second;
480 Real *this_row_i = this->RowData(i);
490 template<
typename Real>
501 this_num_rows = this->NumRows();
506 const Real *a_col_k = A.
Data() + k;
508 const std::pair<MatrixIndexT, Real> &p = B_row_k.
GetElement(e);
510 Real alpha_B_kj = alpha * p.second;
511 Real *this_col_j = this->Data() +
j;
515 this_col_j, this->stride_);
527 this_num_rows = this->NumRows();
532 Real *this_col_j = this->Data() +
j;
534 const std::pair<MatrixIndexT, Real> &p = B_row_j.
GetElement(e);
536 Real alpha_B_jk = alpha * p.second;
537 const Real *a_col_k = A.
Data() + k;
541 this_col_j, this->stride_);
549 template<
typename Real>
550 template<
typename OtherReal>
557 data[i*stride +
j] += alpha * *sdata;
558 data[
j*stride +
i] += alpha * *sdata;
560 data[i*stride +
i] += alpha * *sdata++;
570 void MatrixBase<float>::AddSp(
const float alpha,
const SpMatrix<double> &S);
572 void MatrixBase<double>::AddSp(
const double alpha,
const SpMatrix<float> &S);
575 template<
typename Real>
581 if (beta != 1.0) this->Scale(beta);
591 num_rows = num_rows_, num_cols = num_cols_;
594 const Real *Mdata = M.
Data(), *vdata = v.
Data();
595 if (num_rows_ == 0)
return;
596 for (
MatrixIndexT i = 0;
i < num_rows;
i++, data += stride, Mdata += M_row_stride, vdata++)
597 cblas_Xaxpy(num_cols, alpha * *vdata, Mdata, M_col_stride, data, 1);
600 template<
typename Real>
607 if (beta != 1.0) this->Scale(beta);
619 num_rows = num_rows_,
620 num_cols = num_cols_;
626 const Real *Mdata = M.
Data(), *vdata = v.
Data();
627 if (num_rows_ == 0)
return;
630 data[
i*stride +
j] += alpha * vdata[
j] * Mdata[
i*M_row_stride +
j*M_col_stride];
635 template<
typename Real>
643 const Real *dataA = A.
Data();
644 const Real *dataB = B.
Data();
648 data[
j] = beta*data[
j] + alpha*dataA[
j]*dataB[
j];
656 #if !defined(HAVE_ATLAS) && !defined(USE_KALDI_SVD) 659 template<
typename Real>
662 KALDI_ASSERT(s != NULL && U_in !=
this && V_in !=
this);
665 if (U_in == NULL) tmpU.
Resize(this->num_rows_, 1);
666 if (V_in == NULL) tmpV.
Resize(1, this->num_cols_);
672 KaldiBlasInt M = num_cols_;
673 KaldiBlasInt N = num_rows_;
674 KaldiBlasInt LDA = Stride();
689 KaldiBlasInt V_stride = V->
Stride();
690 KaldiBlasInt U_stride = U->Stride();
695 KaldiBlasInt l_work = -1;
700 char *u_job =
const_cast<char*
>(U_in ?
"s" :
"N");
701 char *v_job =
const_cast<char*
>(V_in ?
"s" :
"N");
705 V->
Data(), &V_stride,
706 U->Data(), &U_stride,
707 &work_query, &l_work,
710 KALDI_ASSERT(result >= 0 &&
"Call to CLAPACK dgesvd_ called with wrong arguments");
712 l_work =
static_cast<KaldiBlasInt
>(work_query);
715 if ((p_work = static_cast<Real*>(
717 throw std::bad_alloc();
723 V->
Data(), &V_stride,
724 U->Data(), &U_stride,
728 KALDI_ASSERT(result >= 0 &&
"Call to CLAPACK dgesvd_ called with wrong arguments");
731 KALDI_WARN <<
"CLAPACK sgesvd_ : some weird convergence not satisfied";
739 template<
typename Real>
745 this->CopyFromMat(M);
748 this->CopyFromMat(M,
kTrans);
753 template<
typename Real>
757 this->CopyFromMat(M);
761 template<
typename Real>
762 template<
typename OtherReal>
767 this->CopyFromMat(M);
770 this->CopyFromMat(M,
kTrans);
782 template<
typename Real>
786 if (rows * cols == 0) {
801 skip = ((16 /
sizeof(Real)) - cols % (16 /
sizeof(Real)))
802 % (16 /
sizeof(Real));
803 stride = cols + skip;
804 size =
static_cast<size_t>(rows) * static_cast<size_t>(stride)
814 throw std::bad_alloc();
818 template<
typename Real>
826 if (this->
data_ == NULL || rows == 0) resize_type =
kSetZero;
827 else if (rows == this->num_rows_ && cols == this->num_cols_ &&
828 (stride_type ==
kDefaultStride || this->stride_ == this->num_cols_)) {
return; }
834 Matrix<Real> tmp(rows, cols, new_resize_type, stride_type);
835 MatrixIndexT rows_min = std::min(rows, this->num_rows_),
836 cols_min = std::min(cols, this->num_cols_);
837 tmp.
Range(0, rows_min, 0, cols_min).
838 CopyFromMat(this->Range(0, rows_min, 0, cols_min));
856 Init(rows, cols, stride_type);
860 template<
typename Real>
861 template<
typename OtherReal>
865 static_cast<const void*>(M.
Data()) ==
866 static_cast<const void*>(this->Data())) {
875 (*this).Row(
i).CopyFromVec(M.
Row(
i));
878 int32 this_stride = stride_, other_stride = M.
Stride();
879 Real *this_data =
data_;
883 this_data[
i * this_stride +
j] = other_data[
j * other_stride +
i];
907 const float *Mdata = M.
Data();
910 cblas_scopy(
i+1, Mdata, 1, row_data, 1);
911 cblas_scopy(
i, Mdata, 1, col_data, stride);
924 const double *Mdata = M.
Data();
927 cblas_dcopy(
i+1, Mdata, 1, row_data, 1);
928 cblas_dcopy(
i, Mdata, 1, col_data, stride);
936 template<
typename Real>
937 template<
typename OtherReal>
943 (*this)(
j,
i) = (*
this)(
i,
j) = M(i, j);
945 (*this)(
i,
i) = M(i, i);
956 template<
typename Real>
957 template<
typename OtherReal>
977 out_i[
j*stride] = in_i[
j];
996 template<
typename Real>
998 if (rv.
Dim() == num_rows_*num_cols_) {
999 if (stride_ == num_cols_) {
1001 const Real *rv_data = rv.
Data();
1002 std::memcpy(
data_, rv_data,
sizeof(Real)*num_rows_*num_cols_);
1004 const Real *rv_data = rv.
Data();
1006 Real *row_data = RowData(r);
1008 row_data[c] = rv_data[c];
1010 rv_data += num_cols_;
1013 }
else if (rv.
Dim() == num_cols_) {
1014 const Real *rv_data = rv.
Data();
1016 std::memcpy(RowData(r), rv_data,
sizeof(Real)*num_cols_);
1022 template<
typename Real>
1023 template<
typename OtherReal>
1025 if (rv.
Dim() == num_rows_*num_cols_) {
1028 Real *row_data = RowData(r);
1030 row_data[c] =
static_cast<Real
>(rv_data[c]);
1032 rv_data += num_cols_;
1034 }
else if (rv.
Dim() == num_cols_) {
1036 Real *first_row_data = RowData(0);
1038 first_row_data[c] = rv_data[c];
1040 std::memcpy(RowData(r), first_row_data,
sizeof(Real)*num_cols_);
1052 template<
typename Real>
1054 if (rv.
Dim() == num_rows_*num_cols_) {
1055 const Real *v_inc_data = rv.
Data();
1056 Real *m_inc_data =
data_;
1060 m_inc_data[r * stride_] = v_inc_data[r];
1062 v_inc_data += num_rows_;
1065 }
else if (rv.
Dim() == num_rows_) {
1066 const Real *v_inc_data = rv.
Data();
1067 Real *m_inc_data =
data_;
1069 Real value = *(v_inc_data++);
1071 m_inc_data[c] = value;
1072 m_inc_data += stride_;
1075 KALDI_ERR <<
"Wrong size of arguments.";
1080 template<
typename Real>
1084 static_cast<UnsignedMatrixIndexT>(num_rows_));
1086 const Real *rv_data = rv.
Data();
1087 Real *row_data = RowData(row);
1089 std::memcpy(row_data, rv_data, num_cols_ *
sizeof(Real));
1092 template<
typename Real>
1095 const Real *rv_data = rv.
Data(), *rv_end = rv_data + rv.
Dim();
1096 Real *my_data = this->Data();
1097 for (; rv_data != rv_end; rv_data++, my_data += (this->stride_+1))
1098 *my_data = *rv_data;
1101 template<
typename Real>
1106 static_cast<UnsignedMatrixIndexT>(num_cols_));
1108 const Real *rv_data = rv.
Data();
1109 Real *col_data =
data_ + col;
1112 col_data[r * stride_] = rv_data[r];
1117 template<
typename Real>
1121 &&
"Access out of matrix");
1122 for (
MatrixIndexT j = i + 1; j < MatrixBase<Real>::num_rows_;
j++)
1127 template<
typename Real>
1139 template<
typename Real>
1143 if (num_cols_ == stride_ && num_cols_ == a.
stride_) {
1156 template<
typename Real>
1162 for (i = 0; i < num_rows_; i++) {
1163 for (j = 0; j < num_cols_; j++) {
1164 (*this)(
i,
j) /= a(i, j);
1169 template<
typename Real>
1175 sum += (*this)(
i,
j);
1185 Real *row_data = RowData(row);
1186 const Real *other_row_data = A.
RowData(row);
1189 row_data[col] = std::max(row_data[col],
1190 other_row_data[col]);
1198 Real *row_data = RowData(row);
1199 const Real *other_row_data = A.
RowData(row);
1202 row_data[col] = std::min(row_data[col],
1203 other_row_data[col]);
1210 if (alpha == 1.0)
return;
1211 if (num_rows_ == 0)
return;
1212 if (num_cols_ == stride_) {
1213 cblas_Xscal(static_cast<size_t>(num_rows_) * static_cast<size_t>(num_cols_),
1223 template<
typename Real>
1229 Real this_scale = scale(
i);
1231 (*this)(
i,
j) *= this_scale;
1237 template<
typename Real>
1240 this->NumCols() % src.
NumCols() == 0);
1242 num_groups = this->NumCols() / group_size,
1243 num_rows = this->NumRows();
1246 Real *data = this->RowData(
i);
1248 Real scale = src(
i,
j);
1254 template<
typename Real>
1260 this->NumRows() == output.
NumRows());
1262 int group_size = this->NumCols() / output.
NumCols(),
1263 num_rows = this->NumRows(), num_cols = this->NumCols();
1268 Real input_val = input(
i,
j);
1269 (*this)(
i,
j) = (input_val == 0 ? 0 : (input_val > 0 ? 1 : -1));
1272 }
else if (power == std::numeric_limits<Real>::infinity()) {
1275 Real output_val = output(
i,
j / group_size), input_val = input(
i,
j);
1276 if (output_val == 0)
1279 (*
this)(
i,
j) = (std::abs(input_val) == output_val ? 1.0 : 0.0)
1280 * (input_val >= 0 ? 1 : -1);
1286 Real output_val = output(
i,
j / group_size),
1287 input_val = input(
i,
j);
1288 if (output_val == 0)
1291 (*
this)(
i,
j) = pow(std::abs(input_val), power - 1) *
1292 pow(output_val, 1 - power) * (input_val >= 0 ? 1 : -1) ;
1298 template<
typename Real>
1302 input.
NumRows() == this->NumRows());
1304 this->NumRows() == output.
NumRows());
1306 int group_size = this->NumCols() / output.
NumCols(),
1307 num_rows = this->NumRows(), num_cols = this->NumCols();
1311 Real input_val = input(
i,
j);
1312 Real output_val = output(
i,
j / group_size);
1313 (*this)(
i,
j) = (input_val == output_val ? 1 : 0);
1318 template<
typename Real>
1323 Real this_scale = scale(
j);
1324 (*this)(
i,
j) *= this_scale;
1329 template<
typename Real>
1331 if (num_cols_ == stride_)
1332 memset(
data_, 0,
sizeof(Real)*num_rows_*num_cols_);
1335 memset(
data_ + row*stride_, 0,
sizeof(Real)*num_cols_);
1338 template<
typename Real>
1342 (*this)(row, col) = value;
1347 template<
typename Real>
1350 for (
MatrixIndexT row = 0; row < std::min(num_rows_, num_cols_); row++)
1351 (*
this)(row, row) = 1.0;
1354 template<
typename Real>
1358 Real *row_data = this->RowData(row);
1359 MatrixIndexT nc = (num_cols_ % 2 == 1) ? num_cols_ - 1 : num_cols_;
1363 if (nc != num_cols_) row_data[nc] =
static_cast<Real
>(
kaldi::RandGauss(&rstate));
1367 template<
typename Real>
1371 Real *row_data = this->RowData(row);
1372 for (
MatrixIndexT col = 0; col < num_cols_; col++, row_data++) {
1378 template<
typename Real>
1381 KALDI_ERR <<
"Failed to write matrix to stream: stream not good";
1385 std::string my_token = (
sizeof(Real) == 4 ?
"FM" :
"DM");
1389 int32 rows = this->num_rows_;
1390 int32 cols = this->num_cols_;
1396 if (Stride() == NumCols())
1397 os.write(reinterpret_cast<const char*> (Data()),
sizeof(Real)
1398 * static_cast<size_t>(num_rows_) * static_cast<size_t>(num_cols_));
1401 os.write(reinterpret_cast<const char*> (RowData(
i)),
sizeof(Real)
1404 KALDI_ERR <<
"Failed to write matrix to stream";
1407 if (num_cols_ == 0) {
1414 os << (*
this)(
i,
j) <<
" ";
1422 template<
typename Real>
1426 tmp.
Read(is, binary,
false);
1428 KALDI_ERR <<
"MatrixBase::Read, size mismatch " 1429 << this->num_rows_ <<
", " << this->num_cols_
1431 this->AddMat(1.0, tmp);
1439 tmp.
Read(is, binary,
false);
1441 KALDI_ERR <<
"MatrixBase<Real>::Read, size mismatch " 1442 << NumRows() <<
" x " << NumCols() <<
" versus " 1449 template<
typename Real>
1453 tmp.
Read(is, binary,
false);
1458 else KALDI_ERR <<
"Matrix::Read, size mismatch " 1459 << this->num_rows_ <<
", " << this->num_cols_
1463 this->AddMat(1.0, tmp);
1469 std::ostringstream specific_error;
1472 int peekval =
Peek(is, binary);
1473 if (peekval ==
'C') {
1476 compressed_mat.
Read(is, binary);
1477 this->Resize(compressed_mat.
NumRows(), compressed_mat.
NumCols());
1481 const char *my_token = (
sizeof(Real) == 4 ?
"FM" :
"DM");
1482 char other_token_start = (
sizeof(Real) == 4 ?
'D' :
'F');
1483 if (peekval == other_token_start) {
1486 other.
Read(is, binary,
false);
1488 this->CopyFromMat(other);
1493 if (token != my_token) {
1494 if (token.length() > 20) token = token.substr(0, 17) +
"...";
1495 specific_error <<
": Expected token " << my_token <<
", got " << token;
1502 this->Resize(rows, cols);
1504 if (this->Stride() == this->NumCols() && rows*cols!=0) {
1505 is.read(reinterpret_cast<char*>(this->Data()),
1506 sizeof(Real)*rows*cols);
1507 if (is.fail())
goto bad;
1510 is.read(reinterpret_cast<char*>(this->RowData(
i)),
sizeof(Real)*cols);
1511 if (is.fail())
goto bad;
1514 if (is.eof())
return;
1515 if (is.fail())
goto bad;
1520 if (is.fail()) { specific_error <<
": Expected \"[\", got EOF";
goto bad; }
1526 if (str ==
"[]") { Resize(0, 0);
return; }
1527 else if (str !=
"[") {
1528 if (str.length() > 20) str = str.substr(0, 17) +
"...";
1529 specific_error <<
": Expected \"[\", got \"" << str <<
'"';
1533 std::vector<std::vector<Real>* > data;
1534 std::vector<Real> *cur_row =
new std::vector<Real>;
1537 if (i == -1) { specific_error <<
"Got EOF while reading matrix data";
goto cleanup; }
1538 else if (static_cast<char>(i) ==
']') {
1541 if (static_cast<char>(i) ==
'\r') {
1544 }
else if (static_cast<char>(i) ==
'\n') { is.get(); }
1546 KALDI_WARN <<
"After end of matrix data, read error.";
1550 if (!cur_row->empty()) data.push_back(cur_row);
1551 else delete(cur_row);
1553 if (data.empty()) { this->Resize(0, 0);
return; }
1555 int32 num_rows = data.size(), num_cols = data[0]->size();
1556 this->Resize(num_rows, num_cols);
1557 for (
int32 i = 0; i < num_rows; i++) {
1558 if (static_cast<int32>(data[i]->size()) != num_cols) {
1559 specific_error <<
"Matrix has inconsistent #cols: " << num_cols
1560 <<
" vs." << data[
i]->size() <<
" (processing row" 1564 for (
int32 j = 0;
j < num_cols;
j++)
1565 (*
this)(
i,
j) = (*(data[i]))[
j];
1571 }
else if (static_cast<char>(i) ==
'\n' ||
static_cast<char>(
i) ==
';') {
1574 if (cur_row->size() != 0) {
1575 data.push_back(cur_row);
1576 cur_row =
new std::vector<Real>;
1577 cur_row->reserve(data.back()->size());
1579 }
else if ( (i >=
'0' && i <=
'9') || i ==
'-' ) {
1583 specific_error <<
"Stream failure/EOF while reading matrix data.";
1586 cur_row->push_back(r);
1587 }
else if (isspace(i)) {
1594 cur_row->push_back(std::numeric_limits<Real>::infinity());
1595 KALDI_WARN <<
"Reading infinite value into matrix.";
1597 cur_row->push_back(std::numeric_limits<Real>::quiet_NaN());
1598 KALDI_WARN <<
"Reading NaN value into matrix.";
1600 if (str.length() > 20) str = str.substr(0, 17) +
"...";
1601 specific_error <<
"Expecting numeric matrix data, got " << str;
1611 for (
size_t i = 0;
i < data.size();
i++)
1617 KALDI_ERR <<
"Failed to read matrix from stream. " << specific_error.str()
1618 <<
" File position at start is " 1619 << pos_at_start <<
", currently " << is.tellg();
1626 template<
typename Real>
1632 if (r == 0 || c == 0) {
1636 this->num_cols_ = 0;
1637 this->num_rows_ = 0;
1642 static_cast<UnsignedMatrixIndexT>(M.
num_rows_) &&
1643 static_cast<UnsignedMatrixIndexT>(co) <
1644 static_cast<UnsignedMatrixIndexT>(M.
num_cols_) &&
1645 static_cast<UnsignedMatrixIndexT>(r) <=
1646 static_cast<UnsignedMatrixIndexT>(M.
num_rows_ - ro) &&
1647 static_cast<UnsignedMatrixIndexT>(c) <=
1648 static_cast<UnsignedMatrixIndexT>(M.
num_cols_ - co));
1654 static_cast<size_t>(co) +
1655 static_cast<size_t>(ro) *
static_cast<size_t>(M.
Stride());
1659 template<
typename Real>
1664 MatrixBase<Real>(data, num_cols, num_rows, stride) {
1676 template<
typename Real>
1682 data[c + stride*r] += alpha;
1685 template<
typename Real>
1691 data[r * this_stride] += alpha;
1695 template<
typename Real>
1699 Svd(&singular_values);
1700 Real min = singular_values(0), max = singular_values(0);
1702 min = std::min((Real)std::abs(singular_values(
i)), min); max = std::max((Real)std::abs(singular_values(
i)), max);
1704 if (min > 0)
return max/min;
1705 else return std::numeric_limits<Real>::infinity();
1708 template<
typename Real>
1716 template<
typename Real>
1727 template<
typename Real>
1740 template <
typename Real>
1763 MatrixIndexT AB_C_time = ARows*BRows*CRows + ARows*CRows*CCols;
1764 MatrixIndexT A_BC_time = BRows*CRows*CCols + ARows*BRows*CCols;
1766 if (AB_C_time < A_BC_time) {
1768 AB.
AddMatMat(1.0, A, transA, B, transB, 0.0);
1769 (*this).AddMatMat(alpha, AB,
kNoTrans, C, transC, beta);
1772 BC.
AddMatMat(1.0, B, transB, C, transC, 0.0);
1773 (*this).AddMatMat(alpha, A, transA, BC,
kNoTrans, beta);
1780 template<
typename Real>
1794 Real prescale = 1.0;
1795 if ( std::abs((*
this)(0, 0) ) < 1.0e-30) {
1797 if (max_elem != 0) {
1798 prescale = 1.0 / max_elem;
1799 if (std::abs(prescale) == std::numeric_limits<Real>::infinity()) { prescale = 1.0e+40; }
1800 (*this).Scale(prescale);
1804 #if !defined(HAVE_ATLAS) && !defined(USE_KALDI_SVD) 1815 bool ans = JamaSvd(s, U, Vt);
1821 if (prescale != 1.0) s->
Scale(1.0/prescale);
1824 template<
typename Real>
1840 KALDI_ERR <<
"Error doing Svd (did not converge), first part of matrix is\n" 1843 <<
", min and max are: " <<
Min() <<
", " <<
Max();
1847 template<
typename Real>
1850 if (R != C)
return false;
1851 Real bad_sum = 0.0, good_sum = 0.0;
1854 Real a = (*this)(
i,
j), b = (*
this)(
j,
i), avg = 0.5*(a+b), diff = 0.5*(a-b);
1855 good_sum += std::abs(avg); bad_sum += std::abs(diff);
1857 good_sum += std::abs((*
this)(i, i));
1859 if (bad_sum > cutoff*good_sum)
return false;
1863 template<
typename Real>
1866 Real bad_sum = 0.0, good_sum = 0.0;
1869 if (
i ==
j) good_sum += std::abs((*
this)(
i,
j));
1870 else bad_sum += std::abs((*
this)(
i,
j));
1873 return (!(bad_sum > good_sum * cutoff));
1878 template<
typename Real>
1883 if ((*
this)(
i,
j) > 0.0) positive++;
1884 if (positive > R * C)
1889 template<
typename Real>
1895 bad_max = std::max(bad_max, static_cast<Real>(std::abs( (*
this)(
i,
j) - (
i ==
j?1.0:0.0))));
1896 return (bad_max <= cutoff);
1899 template<
typename Real>
1905 bad_max = std::max(bad_max, static_cast<Real>(std::abs( (*
this)(
i,
j) )));
1906 return (bad_max <= cutoff);
1909 template<
typename Real>
1914 template<
typename Real>
1917 KALDI_ERR <<
"ApproxEqual: size mismatch.";
1924 template<
typename Real>
1930 if ( (*
this)(
i,
j) != other(
i,
j))
1936 template<
typename Real>
1942 largest = std::max(largest, (Real)std::abs((*
this)(
i,
j)));
1947 template<
typename Real>
1955 if (start_prod - start_prod != 0.0 || start_prod == 0.0) {
1956 KALDI_WARN <<
"Self-product of row " <<
i <<
" of matrix is " 1957 << start_prod <<
", randomizing.";
1958 this->
Row(
i).SetRandn();
1964 this->
Row(i).AddVec(-prod, this->
Row(
j));
1967 if (end_prod <= 0.01 * start_prod) {
1972 if (end_prod == 0.0) {
1974 this->
Row(i).SetRandn();
1978 KALDI_ERR <<
"Loop detected while orthogalizing matrix.";
1980 this->
Row(i).Scale(1.0 / std::sqrt(end_prod));
1995 template<
typename Real>
2012 if (sum < 0.0) (*rs)(
i) = 0.0;
2022 float old_norm = (*this).FrobeniusNorm();
2023 tmpThisFull.
AddMat(-1.0, (*
this));
2025 if (!(old_norm == 0 && new_norm == 0)) {
2027 if (std::abs(new_norm-old_norm) > old_norm*check_thresh || diff_norm > old_norm*check_thresh) {
2028 KALDI_WARN <<
"SymPosSemiDefEig seems to have failed " << diff_norm <<
" !<< " 2029 << check_thresh <<
"*" << old_norm <<
", maybe matrix was not " 2030 <<
"positive semi definite. Continuing anyway.";
2037 template<
typename Real>
2041 tmp.
Invert(&log_det, det_sign,
false);
2045 template<
typename Real>
2047 bool inverse_needed) {
2048 double log_det_tmp, det_sign_tmp;
2050 dmat.
Invert(&log_det_tmp, &det_sign_tmp, inverse_needed);
2051 if (inverse_needed) (*this).CopyFromMat(dmat);
2052 if (log_det) *log_det = log_det_tmp;
2053 if (det_sign) *det_sign = det_sign_tmp;
2056 template<
class Real>
2061 template<
class Real>
2069 template<
typename Real>
2073 (*this)(r, c) = static_cast<Real>(1.0 / (*
this)(r, c));
2078 template<
typename Real>
2084 Real &a = (*this)(
i,
j), &b = (*
this)(
j,
i);
2090 template<
typename Real>
2092 if (this->num_rows_ != this->num_cols_) {
2094 Resize(this->num_cols_, this->num_rows_);
2095 this->CopyFromMat(tmp);
2101 template<
typename Real>
2104 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2105 Real *row_data =
data_;
2106 const Real *src_row_data = src.
Data();
2108 row++,row_data += stride_, src_row_data += src.
stride_) {
2110 row_data[col] = (src_row_data[col] > 0 ? 1.0 : 0.0);
2114 template<
typename Real>
2117 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2118 Real *row_data =
data_;
2119 const Real *src_row_data = src.
Data();
2121 row++,row_data += stride_, src_row_data += src.
stride_) {
2123 row_data[col] =
kaldi::Exp(src_row_data[col]);
2127 template<
typename Real>
2130 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2131 Real *row_data =
data_;
2132 const Real *src_row_data = src.
Data();
2134 row++,row_data += stride_, src_row_data += src.
stride_) {
2136 row_data[col] = pow(src_row_data[col], power);
2141 template<
typename Real>
2144 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2145 Real *row_data =
data_;
2146 const Real *src_row_data = src.
Data();
2148 row++,row_data += stride_, src_row_data += src.
stride_) {
2150 if (include_sign ==
true && src_row_data[col] < 0) {
2151 row_data[col] = -pow(std::abs(src_row_data[col]), power);
2153 row_data[col] = pow(std::abs(src_row_data[col]), power);
2159 template<
typename Real>
2162 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2163 Real *row_data =
data_;
2164 const Real *src_row_data = src.
Data();
2166 row++,row_data += stride_, src_row_data += src.
stride_) {
2168 row_data[col] = (src_row_data[col] < floor_val ? floor_val : src_row_data[col]);
2172 template<
typename Real>
2175 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2176 Real *row_data =
data_;
2177 const Real *src_row_data = src.
Data();
2179 row++,row_data += stride_, src_row_data += src.
stride_) {
2181 row_data[col] = (src_row_data[col] > ceiling_val ? ceiling_val : src_row_data[col]);
2185 template<
typename Real>
2188 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2189 Real *row_data =
data_;
2190 const Real *src_row_data = src.
Data();
2192 row++,row_data += stride_, src_row_data += src.
stride_) {
2194 row_data[col] =
kaldi::Log(src_row_data[col]);
2198 template<
typename Real>
2201 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2202 Real *row_data =
data_;
2203 const Real *src_row_data = src.
Data();
2205 row++,row_data += stride_, src_row_data += src.
stride_) {
2207 row_data[col] = (src_row_data[col] < Real(0) ?
kaldi::Exp(src_row_data[col]) : (src_row_data[col] + Real(1)));
2211 template<
typename Real>
2214 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_;
2215 Real *row_data =
data_;
2216 const Real *src_row_data = src.
Data();
2218 row++,row_data += stride_, src_row_data += src.
stride_) {
2220 const Real x = src_row_data[col];
2221 if (!(x >= lower_limit))
2223 else if (x > upper_limit)
2231 template<
typename Real>
2237 this->Eig(&P, &re, &im);
2254 template<
typename Real>
2279 template<
typename Real>
2302 template<
typename Real>
2311 is.read((
char*)&htk_hdr,
sizeof(htk_hdr));
2313 KALDI_WARN <<
"Could not read header from HTK feature file ";
2322 bool has_checksum =
false;
2326 Waveform, Lpc, Lprefc, Lpcepstra, Lpdelcep,
2327 Irefc,
Mfcc,
Fbank, Melspec, User, Discrete,
Plp, Anon };
2329 const int32 IsCompressed = 02000, HasChecksum = 010000, HasVq = 040000,
2330 Problem = IsCompressed | HasVq;
2331 int32 base_parm = htk_hdr.mSampleKind & (077);
2332 has_checksum = (base_parm & HasChecksum) != 0;
2333 htk_hdr.mSampleKind &= ~HasChecksum;
2335 if (htk_hdr.mSampleKind & Problem)
2336 KALDI_ERR <<
"Code to read HTK features does not support compressed " 2337 "features, or features with VQ.";
2338 if (base_parm == Waveform || base_parm == Irefc || base_parm == Discrete)
2339 KALDI_ERR <<
"Attempting to read HTK features from unsupported type " 2340 "(e.g. waveform or discrete features.";
2343 KALDI_VLOG(3) <<
"HTK header: Num Samples: " << htk_hdr.mNSamples
2344 <<
"; Sample period: " << htk_hdr.mSamplePeriod
2345 <<
"; Sample size: " << htk_hdr.mSampleSize
2346 <<
"; Sample kind: " << htk_hdr.mSampleKind;
2348 M.
Resize(htk_hdr.mNSamples, htk_hdr.mSampleSize /
sizeof(
float));
2352 if (
sizeof(Real) ==
sizeof(
float)) {
2353 for (i = 0; i< M.
NumRows(); i++) {
2356 KALDI_WARN <<
"Could not read data from HTK feature file ";
2361 for (j = 0; j < C; j++) {
2367 float *pmem =
new float[M.
NumCols()];
2368 for (i = 0; i < M.
NumRows(); i++) {
2369 is.read((
char*)pmem,
sizeof(
float)*M.
NumCols());
2371 KALDI_WARN <<
"Could not read data from HTK feature file ";
2376 for (j = 0; j < C; j++) {
2379 M(i, j) =
static_cast<Real
>(pmem[
j]);
2384 if (header_ptr) *header_ptr = htk_hdr;
2387 is.read((
char*)&checksum,
sizeof(checksum));
2389 KALDI_WARN <<
"Could not read checksum from HTK feature file ";
2402 template<
typename Real>
2407 static_cast<MatrixIndexT>(
sizeof(
float)));
2414 os.write((
char*)&htk_hdr,
sizeof(htk_hdr));
2415 if (os.fail())
goto bad;
2420 for (i = 0; i< M.
NumRows(); i++) {
2422 if (os.fail())
goto bad;
2425 float *pmem =
new float[M.
NumCols()];
2427 for (i = 0; i < M.
NumRows(); i++) {
2428 const Real *rowData = M.
RowData(i);
2429 for (j = 0;j < M.
NumCols();j++)
2430 pmem[j] = static_cast<float> ( rowData[j] );
2432 for (j = 0;j < M.
NumCols();j++)
2434 os.write((
char*)pmem,
sizeof(
float)*M.
NumCols());
2444 KALDI_WARN <<
"Could not write to HTK feature file ";
2454 template<
class Real>
2461 os.write((
char*)&size,
sizeof(
int));
2462 if (os.fail())
goto bad;
2467 for (i = 0; i< M.
NumRows(); i++) {
2469 if (os.fail())
goto bad;
2472 float *pmem =
new float[M.
NumCols()];
2474 for (i = 0; i < M.
NumRows(); i++) {
2475 const Real *rowData = M.
RowData(i);
2476 for (j = 0;j < M.
NumCols();j++)
2477 pmem[j] = static_cast<float> ( rowData[j] );
2479 for (j = 0;j < M.
NumCols();j++)
2481 os.write((
char*)pmem,
sizeof(
float)*M.
NumCols());
2491 KALDI_WARN <<
"Could not write to Sphinx feature file";
2501 template <
typename Real>
2510 KALDI_ASSERT( CCols == ARows && ACols == BRows && BCols == CRows &&
"TraceMatMatMat: args have mismatched dimensions.");
2511 if (ARows*BCols < std::min(BRows*CCols, CRows*ACols)) {
2513 AB.
AddMatMat(1.0, A, transA, B, transB, 0.0);
2515 }
else if ( BRows*CCols < CRows*ACols) {
2517 BC.
AddMatMat(1.0, B, transB, C, transC, 0.0);
2521 CA.
AddMatMat(1.0, C, transC, A, transA, 0.0);
2537 template <
typename Real>
2548 KALDI_ASSERT( DCols == ARows && ACols == BRows && BCols == CRows && CCols == DRows &&
"TraceMatMatMat: args have mismatched dimensions.");
2549 if (ARows*BCols < std::min(BRows*CCols, std::min(CRows*DCols, DRows*ACols))) {
2551 AB.
AddMatMat(1.0, A, transA, B, transB, 0.0);
2553 }
else if ((BRows*CCols) < std::min(CRows*DCols, DRows*ACols)) {
2555 BC.
AddMatMat(1.0, B, transB, C, transC, 0.0);
2557 }
else if (CRows*DCols < DRows*ACols) {
2559 CD.
AddMatMat(1.0, C, transC, D, transD, 0.0);
2563 DA.
AddMatMat(1.0, D, transD, A, transA, 0.0);
2587 std::vector<std::pair<Real, MatrixIndexT> > vec(num_singval);
2591 sort_val = -(sort_on_absolute_value ? std::abs(val) : val);
2592 vec[
d] = std::pair<Real, MatrixIndexT>(sort_val,
d);
2594 std::sort(vec.begin(), vec.end());
2597 (*s)(
d) = s_copy(vec[
d].second);
2604 (*U)(e,
d) = Utmp(e, oldidx);
2610 (*Vt).Row(
d).CopyFromVec(Vttmp.
Row(vec[
d].second));
2622 template<
typename Real>
2638 Real lambda = re(j), mu = im(j);
2640 (*D)(
j,
j) = lambda;
2643 (*D)(j+1, j+1) = lambda;
2658 template<
typename Real>
2671 if (*x_re < 0.0 && *x_im == 0.0)
return false;
2673 Real r = std::sqrt((*x_re * *x_re) + (*x_im * *x_im));
2674 if (power < 0.0 && r == 0.0)
return false;
2675 Real theta = std::atan2(*x_im, *x_re);
2677 r = std::pow(r, power);
2679 *x_re = r * std::cos(theta);
2680 *x_im = r * std::sin(theta);
2691 template <
typename Real>
2701 for (
MatrixIndexT row = 0;row < arows;row++, adata+=aStride, bdata++)
2702 ans +=
cblas_Xdot(acols, adata, 1, bdata, bStride);
2709 for (
MatrixIndexT row = 0;row < arows;row++, adata+=aStride, bdata+=bStride)
2710 ans +=
cblas_Xdot(acols, adata, 1, bdata, 1);
2727 template<
typename Real>
2732 Real max_elem = Max(), cutoff;
2735 if (prune > 0.0 && max_elem - prune > cutoff)
2736 cutoff = max_elem - prune;
2738 double sum_relto_max_elem = 0.0;
2744 sum_relto_max_elem +=
kaldi::Exp(f - max_elem);
2747 return max_elem +
kaldi::Log(sum_relto_max_elem);
2750 template<
typename Real>
2752 Real max = this->Max(), sum = 0.0;
2757 this->Scale(1.0 / sum);
2761 template<
typename Real>
2767 dst_vec(this->
data_, num_rows_ * num_cols_);
2768 dst_vec.
Tanh(src_vec);
2772 dest_vec.
Tanh(src_vec);
2777 template<
typename Real>
2780 int32 num_rows = num_rows_, num_cols = num_cols_;
2782 Real *row_data = this->RowData(r);
2783 const Real *src_row_data = src.
RowData(r);
2785 Real x = src_row_data[c], y;
2786 if (x > 10.0) y = x;
2794 template<
typename Real>
2797 src.
NumRows() == this->NumRows());
2798 int group_size = src.
NumCols() / this->NumCols(),
2799 num_rows = this->NumRows(), num_cols = this->NumCols();
2802 (*
this)(
i,
j) = src.
Row(
i).Range(
j * group_size, group_size).Norm(power);
2805 template<
typename Real>
2808 src.
NumRows() == this->NumRows());
2809 int group_size = src.
NumCols() / this->NumCols(),
2810 num_rows = this->NumRows(), num_cols = this->NumCols();
2812 const Real *src_row_data = src.
RowData(
i);
2814 Real max_val = -1e20;
2816 Real src_data = src_row_data[
j * group_size + k];
2817 if (src_data > max_val)
2820 (*this)(
i,
j) = max_val;
2825 template<
typename Real>
2829 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2830 this_stride = stride_, src_stride = src.
stride_;
2831 Real *this_data = this->
data_;
2832 const Real *src_data = src.
data_;
2833 #ifdef KALDI_PARANOID 2841 for (
MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride, src_data += src_stride) {
2843 for (
MatrixIndexT c = 0; c < num_cols; c++, index_ptr++) {
2844 if (*index_ptr < 0) this_data[c] = 0;
2845 else this_data[c] = src_data[*index_ptr];
2851 template<
typename Real>
2855 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2856 this_stride = stride_, src_stride = src.
stride_;
2857 Real *this_data = this->
data_;
2858 const Real *src_data = src.
data_;
2859 #ifdef KALDI_PARANOID 2867 for (
MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride, src_data += src_stride) {
2869 for (
MatrixIndexT c = 0; c < num_cols; c++, index_ptr++) {
2870 if (*index_ptr >= 0)
2871 this_data[c] += src_data[*index_ptr];
2876 template<
typename Real>
2880 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2881 this_stride = stride_;
2882 Real *this_data = this->
data_;
2884 for (
MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2886 if (index < 0) memset(this_data, 0,
sizeof(Real) * num_cols_);
2891 template<
typename Real>
2894 num_cols = num_cols_, this_stride = stride_;
2895 Real *this_data = this->
data_;
2897 for (
MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2898 const Real *
const src_data = src[r];
2899 if (src_data == NULL) memset(this_data, 0,
sizeof(Real) * num_cols);
2900 else cblas_Xcopy(num_cols, src_data, 1, this_data, 1);
2904 template<
typename Real>
2907 num_cols = num_cols_, this_stride = stride_;
2908 const Real *this_data = this->
data_;
2910 for (
MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2911 Real *
const dst_data = dst[r];
2912 if (dst_data != NULL)
2917 template<
typename Real>
2923 num_cols = num_cols_, this_stride = stride_;
2924 Real *this_data = this->
data_;
2926 for (
MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2934 template<
typename Real>
2937 num_cols = num_cols_, this_stride = stride_;
2938 Real *this_data = this->
data_;
2940 for (
MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2941 const Real *
const src_data = src[r];
2942 if (src_data != NULL)
2943 cblas_Xaxpy(num_cols, alpha, src_data, 1, this_data, 1);
2947 template<
typename Real>
2953 num_cols = num_cols_, this_stride = stride_;
2954 Real *this_data = this->
data_;
2956 for (
MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2964 template<
typename Real>
2967 num_cols = num_cols_, this_stride = stride_;
2968 const Real *this_data = this->
data_;
2970 for (
MatrixIndexT r = 0; r < num_rows; r++, this_data += this_stride) {
2971 Real *
const dst_data = dst[r];
2972 if (dst_data != NULL)
2973 cblas_Xaxpy(num_cols, alpha, this_data, 1, dst_data, 1);
2977 template<
typename Real>
2983 dst_vec(this->
data_, num_rows_ * num_cols_);
2993 template<
typename Real>
2997 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
2998 stride = stride_, value_stride = value.
stride_, diff_stride = diff.
stride_;
3000 const Real *value_data = value.
data_, *diff_data = diff.
data_;
3003 data[c] = diff_data[c] * value_data[c] * (1.0 - value_data[c]);
3005 value_data += value_stride;
3006 diff_data += diff_stride;
3010 template<
typename Real>
3014 MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
3015 stride = stride_, value_stride = value.
stride_, diff_stride = diff.
stride_;
3017 const Real *value_data = value.
data_, *diff_data = diff.
data_;
3020 data[c] = diff_data[c] * (1.0 - (value_data[c] * value_data[c]));
3022 value_data += value_stride;
3023 diff_data += diff_stride;
3028 template<
typename Real>
3029 template<
typename OtherReal>
3031 const MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
3034 if(num_cols <= 64) {
3039 data[
j] += alpha * vdata[
j];
3045 this->AddVecVec(alpha, ones, v);
3051 template void MatrixBase<float>::AddVecToRows(
const float alpha,
3055 template void MatrixBase<double>::AddVecToRows(
const double alpha,
3059 template<
typename Real>
3060 template<
typename OtherReal>
3062 const MatrixIndexT num_rows = num_rows_, num_cols = num_cols_,
3066 if (num_rows <= 64) {
3070 Real to_add = alpha * vdata[
i];
3078 this->AddVecVec(alpha, v, ones);
3084 template void MatrixBase<float>::AddVecToCols(
const float alpha,
3088 template void MatrixBase<double>::AddVecToCols(
const double alpha,
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...
void Add(const Real alpha)
Add a scalar to each element.
Matrix()
Empty constructor.
void InvertDouble(Real *LogDet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse [double].
Real Min() const
Returns minimum element of matrix.
This class provides a way for switching between double and float types.
template double TraceMatMat(const MatrixBase< double > &A, const MatrixBase< double > &B, MatrixTransposeType trans)
Packed symetric matrix class.
Real LargestAbsElem() const
largest absolute value.
template void CreateEigenvalueMatrix(const VectorBase< double > &re, const VectorBase< double > &im, MatrixBase< double > *D)
void cblas_Xsyrk(const MatrixTransposeType trans, const MatrixIndexT dim_c, const MatrixIndexT other_dim_a, const float alpha, const float *A, const MatrixIndexT a_stride, const float beta, float *C, const MatrixIndexT c_stride)
bool IsDiagonal(Real cutoff=1.0e-05) const
Returns true if matrix is Diagonal.
OfflineFeatureTpl< MfccComputer > Mfcc
void clapack_Xgetri2(KaldiBlasInt *num_rows, float *Mdata, KaldiBlasInt *stride, KaldiBlasInt *pivot, float *p_work, KaldiBlasInt *l_work, KaldiBlasInt *result)
void Sigmoid(const VectorBase< Real > &src)
Sets each element of *this to the sigmoid function of the corresponding element of "src"...
MatrixIndexT stride_
< Number of rows
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Real Cond() const
Returns condition number by computing Svd.
Base class which provides matrix operations not involving resizing or allocation. ...
const Real * Data() const
Gives pointer to raw data (const).
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
const std::pair< MatrixIndexT, Real > & GetElement(MatrixIndexT i) const
get an indexed element (0 <= i < NumElements()).
void Tanh(const VectorBase< Real > &src)
Sets each element of *this to the tanh of the corresponding element of "src".
Real * data_
data memory area
OfflineFeatureTpl< PlpComputer > Plp
Real Max() const
Returns maximum element of matrix.
bool ApproxEqual(const MatrixBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).FrobeniusNorm() <= tol * (*this).FrobeniusNorm(). ...
void Xgemv_sparsevec(MatrixTransposeType trans, MatrixIndexT num_rows, MatrixIndexT num_cols, Real alpha, const Real *Mdata, MatrixIndexT stride, const Real *xdata, MatrixIndexT incX, Real beta, Real *ydata, MatrixIndexT incY)
void SymPosSemiDefEig(VectorBase< Real > *s, MatrixBase< Real > *P, Real check_thresh=0.001)
Uses Svd to compute the eigenvalue decomposition of a symmetric positive semi-definite matrix: (*this...
Real Trace(bool check_square=true) const
Returns trace of matrix.
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
void clapack_Xgetrf2(KaldiBlasInt *num_rows, KaldiBlasInt *num_cols, float *Mdata, KaldiBlasInt *stride, KaldiBlasInt *pivot, KaldiBlasInt *result)
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
void LapackGesvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
float RandGauss(struct RandomState *state=NULL)
void AddToDiag(const Real alpha)
Add a scalar to each diagonal element.
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Real * Data_workaround() const
A workaround that allows SubMatrix to get a pointer to non-const data for const Matrix.
A class for storing matrices.
template double TraceMatMatMatMat(const MatrixBase< double > &A, MatrixTransposeType transA, const MatrixBase< double > &B, MatrixTransposeType transB, const MatrixBase< double > &C, MatrixTransposeType transC, const MatrixBase< double > &D, MatrixTransposeType transD)
void cblas_Xcopy(const int N, const float *X, const int incX, float *Y, const int incY)
void Swap(Matrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
MatrixIndexT NumRows() const
void clapack_Xgesvd(char *v, char *u, KaldiBlasInt *num_cols, KaldiBlasInt *num_rows, float *Mdata, KaldiBlasInt *stride, float *sv, float *Vdata, KaldiBlasInt *vstride, float *Udata, KaldiBlasInt *ustride, float *p_work, KaldiBlasInt *l_work, KaldiBlasInt *result)
void Read(std::istream &is, bool binary)
uint32 UnsignedMatrixIndexT
void mul_elements(const MatrixIndexT dim, const double *a, double *b)
This is not really a wrapper for CBLAS as CBLAS does not have this; in future we could extend this so...
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
template bool ReadHtk(std::istream &is, Matrix< double > *M, HtkHeader *header_ptr)
template double TraceMatMatMat(const MatrixBase< double > &A, MatrixTransposeType transA, const MatrixBase< double > &B, MatrixTransposeType transB, const MatrixBase< double > &C, MatrixTransposeType transC)
void DestructiveSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt)
Singular value decomposition Major limitations: For nonsquare matrices, we assume m>=n (NumRows >= Nu...
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
void TestUninitialized() const
float cblas_Xdot(const int N, const float *const X, const int incX, const float *const Y, const int incY)
bool IsZero(Real cutoff=1.0e-05) const
Returns true if matrix is all zeros.
void GetImagEigenvalues(VectorBase< Real > *i_out)
MatrixIndexT NumCols() const
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.
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
MatrixIndexT num_rows_
< Number of columns
#define KALDI_MEMALIGN(align, size, pp_orig)
static const double kMinLogDiffDouble
void RandGauss2(float *a, float *b, RandomState *state)
template bool WriteHtk(std::ostream &os, const MatrixBase< double > &M, HtkHeader htk_hdr)
OfflineFeatureTpl< FbankComputer > Fbank
void GetV(MatrixBase< Real > *V_out)
void Transpose()
Transpose the matrix.
void cblas_Xsymm(const float alpha, MatrixIndexT sz, const float *Adata, MatrixIndexT a_stride, const float *Bdata, MatrixIndexT b_stride, const float beta, float *Mdata, MatrixIndexT stride)
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
int MachineIsLittleEndian()
#define KALDI_MEMALIGN_FREE(x)
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.
Packed symetric matrix class.
MatrixIndexT num_cols_
these attributes store the real matrix size as it is stored in memory including memalignment ...
MatrixIndexT NumElements() const
Returns the number of nonzero elements.
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.
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
void cblas_Xgemm(const float alpha, MatrixTransposeType transA, const float *Adata, MatrixIndexT a_num_rows, MatrixIndexT a_num_cols, MatrixIndexT a_stride, MatrixTransposeType transB, const float *Bdata, MatrixIndexT b_stride, const float beta, float *Mdata, MatrixIndexT num_rows, MatrixIndexT num_cols, MatrixIndexT stride)
MatrixIndexT Dim() const
Returns the dimension of the vector.
void SetZero()
Sets matrix to zero.
void Scale(Real alpha)
Multiplies all elements by this constant.
template void SortSvd(VectorBase< double > *s, MatrixBase< double > *U, MatrixBase< double > *Vt, bool)
static const float kMinLogDiffFloat
void MulColsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = (*this) * diag(scale).
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
void GetRealEigenvalues(VectorBase< Real > *r_out)
A class representing a vector.
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
void Set(Real f)
Set all members of a vector to a specified value.
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.
MatrixIndexT NumRows() const
void AddVecVec(const Real alpha, const VectorBase< OtherReal > &a, const VectorBase< OtherReal > &b)
*this += alpha * a * b^T
template bool WriteSphinx(std::ostream &os, const MatrixBase< double > &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.
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
bool Equal(const MatrixBase< Real > &other) const
Tests for exact equality. It's usually preferable to use ApproxEqual.
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).
const float kLogZeroFloat
void Svd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt) const
Compute SVD (*this) = U diag(s) Vt.
void OrthogonalizeRows()
This function orthogonalizes the rows of a matrix using the Gram-Schmidt process. ...
void Invert(Real *log_det=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
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 cblas_Xger(MatrixIndexT num_rows, MatrixIndexT num_cols, float alpha, const float *xdata, MatrixIndexT incX, const float *ydata, MatrixIndexT incY, float *Mdata, MatrixIndexT stride)
const double kLogZeroDouble
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
void CopyToMat(MatrixBase< Real > *mat, MatrixTransposeType trans=kNoTrans) const
Copies contents to matrix.
Sub-matrix representation.
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
const SparseVector< Real > & Row(MatrixIndexT r) const
template bool AttemptComplexPower(double *x_re, double *x_im, double power)