36 template<
typename Real>
51 template<
typename Real,
typename OtherReal>
56 const Real *a_data = ra.
Data();
60 sum += a_data[
i]*b_data[
i];
91 template<
typename Real>
104 template<
typename Real>
140 template<
typename Real>
151 template<
typename Real>
158 template<
typename Real>
166 template<
typename Real>
178 size = dim *
sizeof(Real);
181 this->
data_ =
static_cast<Real*
> (data);
184 throw std::bad_alloc();
189 template<
typename Real>
196 else if (this->dim_ == dim) {
return; }
200 if (dim > this->dim_) {
201 memcpy(tmp.
data_, this->data_,
sizeof(Real)*this->dim_);
202 memset(tmp.
data_+this->dim_, 0,
sizeof(Real)*(dim-this->dim_));
204 memcpy(tmp.
data_, this->data_,
sizeof(Real)*dim);
213 if (this->
data_ != NULL) {
214 if (this->dim_ == dim) {
215 if (resize_type ==
kSetZero) this->SetZero();
222 if (resize_type ==
kSetZero) this->SetZero();
227 template<
typename Real>
231 std::memcpy(this->
data_, v.
data_, dim_ *
sizeof(Real));
235 template<
typename Real>
236 template<
typename OtherReal>
239 this->CopyFromVec(v);
248 template<
typename Real>
251 std::memcpy(this->
data_, data, Dim() *
sizeof(Real));
254 template<
typename Real>
255 template<
typename OtherReal>
258 Real * __restrict__ ptr =
data_;
261 ptr[
i] = other_ptr[
i];
268 template<
typename Real>
278 template<
typename Real>
281 if (this->
data_ != NULL)
287 template<
typename Real>
289 std::memset(
data_, 0, dim_ *
sizeof(Real));
292 template<
typename Real>
296 abs_max = std::max(std::abs(
data_[
i]), abs_max);
297 return (abs_max <= cutoff);
300 template<
typename Real>
303 MatrixIndexT last = (Dim() % 2 == 1) ? Dim() - 1 : Dim();
310 template<
typename Real>
318 template<
typename Real>
321 Real sum = this->Sum();
324 Real *data = this->
data_;
326 Real running_sum = 0.0;
328 running_sum += data[
i];
329 if (r < running_sum)
return i;
335 template<
typename Real>
347 template<
typename Real>
351 Real *inc_data =
data_;
355 memcpy(inc_data, mat.
Data(), cols*rows*
sizeof(Real));
359 memcpy(inc_data, mat.
RowData(
i), cols *
sizeof(Real));
366 template<
typename Real>
367 template<
typename OtherReal>
370 Real *vec_data =
data_;
377 vec_data[
j] =
static_cast<Real
>(mat_row[
j]);
389 template<
typename Real>
393 Real* inc_data =
data_;
395 const Real *mat_inc_data = mat.
Data();
399 inc_data[
j] = mat_inc_data[
j*stride];
406 template<
typename Real>
410 const Real *mat_row = mat.
RowData(row);
411 memcpy(
data_, mat_row,
sizeof(Real)*dim_);
414 template<
typename Real>
415 template<
typename OtherReal>
421 data_[
i] = static_cast<Real>(mat_row[
i]);
429 template<
typename Real>
430 template<
typename OtherReal>
437 sp_data += (row*(row+1)) / 2;
439 for (i = 0; i < row; i++)
440 data_[i] = static_cast<Real>(*(sp_data++));
441 for(; i < dim_; ++
i, sp_data +=
i)
442 data_[i] = static_cast<Real>(*sp_data);
467 template<
typename Real>
478 template<
typename Real>
486 }
else if (power == 0.5) {
488 data_[
i] = (include_sign &&
data_[
i] < 0 ? -1 : 1) * std::sqrt(std::abs(
data_[i]));
490 }
else if (power < 0.0) {
494 if (
data_[i] == HUGE_VAL) {
495 KALDI_ERR <<
"Could not raise element " << i <<
"to power " 496 << power <<
": returned value = " <<
data_[
i];
501 data_[
i] = (include_sign &&
data_[
i] < 0 ? -1 : 1) * pow(std::abs(
data_[i]), power);
502 if (
data_[i] == HUGE_VAL) {
503 KALDI_ERR <<
"Could not raise element " << i <<
"to power " 504 << power <<
": returned value = " <<
data_[
i];
511 template<
typename Real>
517 if (
data_[
i] != 0.0) sum += 1.0;
519 }
else if (p == 1.0) {
521 sum += std::abs(
data_[
i]);
523 }
else if (p == 2.0) {
526 return std::sqrt(sum);
527 }
else if (p == std::numeric_limits<Real>::infinity()){
529 sum = std::max(sum, std::abs(
data_[
i]));
535 tmp = pow(std::abs(
data_[
i]), p);
540 tmp = pow(sum, static_cast<Real>(1.0/p));
545 Real maximum = this->Max(), minimum = this->Min(),
546 max_abs = std::max(maximum, -minimum);
549 tmp.
Scale(1.0 / max_abs);
550 return tmp.
Norm(p) * max_abs;
555 template<
typename Real>
557 if (dim_ != other.
dim_)
KALDI_ERR <<
"ApproxEqual: size mismatch " 558 << dim_ <<
" vs. " << other.
dim_;
563 return (tmp.
Norm(2.0) <=
static_cast<Real
>(tol) * this->Norm(2.0));
565 const Real *data =
data_;
566 const Real *other_data = other.
data_;
568 if (data[
i] != other_data[
i])
return false;
573 template<
typename Real>
575 Real ans = - std::numeric_limits<Real>::infinity();
576 const Real *data =
data_;
578 for (i = 0; i + 4 <= dim; i += 4) {
579 Real a1 = data[
i], a2 = data[i+1], a3 = data[i+2], a4 = data[i+3];
580 if (a1 > ans || a2 > ans || a3 > ans || a4 > ans) {
581 Real b1 = (a1 > a2 ? a1 : a2), b2 = (a3 > a4 ? a3 : a4);
582 if (b1 > ans) ans = b1;
583 if (b2 > ans) ans = b2;
587 if (data[i] > ans) ans = data[
i];
591 template<
typename Real>
593 if (dim_ == 0)
KALDI_ERR <<
"Empty vector";
594 Real ans = - std::numeric_limits<Real>::infinity();
596 const Real *data =
data_;
598 for (i = 0; i + 4 <= dim; i += 4) {
599 Real a1 = data[
i], a2 = data[i+1], a3 = data[i+2], a4 = data[i+3];
600 if (a1 > ans || a2 > ans || a3 > ans || a4 > ans) {
601 if (a1 > ans) { ans = a1; index =
i; }
602 if (a2 > ans) { ans = a2; index = i + 1; }
603 if (a3 > ans) { ans = a3; index = i + 2; }
604 if (a4 > ans) { ans = a4; index = i + 3; }
608 if (data[i] > ans) { ans = data[
i]; index =
i; }
613 template<
typename Real>
615 Real ans = std::numeric_limits<Real>::infinity();
616 const Real *data =
data_;
618 for (i = 0; i + 4 <= dim; i += 4) {
619 Real a1 = data[
i], a2 = data[i+1], a3 = data[i+2], a4 = data[i+3];
620 if (a1 < ans || a2 < ans || a3 < ans || a4 < ans) {
621 Real b1 = (a1 < a2 ? a1 : a2), b2 = (a3 < a4 ? a3 : a4);
622 if (b1 < ans) ans = b1;
623 if (b2 < ans) ans = b2;
627 if (data[i] < ans) ans = data[
i];
631 template<
typename Real>
633 if (dim_ == 0)
KALDI_ERR <<
"Empty vector";
634 Real ans = std::numeric_limits<Real>::infinity();
636 const Real *data =
data_;
638 for (i = 0; i + 4 <= dim; i += 4) {
639 Real a1 = data[
i], a2 = data[i+1], a3 = data[i+2], a4 = data[i+3];
640 if (a1 < ans || a2 < ans || a3 < ans || a4 < ans) {
641 if (a1 < ans) { ans = a1; index =
i; }
642 if (a2 < ans) { ans = a2; index = i + 1; }
643 if (a3 < ans) { ans = a3; index = i + 2; }
644 if (a4 < ans) { ans = a4; index = i + 3; }
648 if (data[i] < ans) { ans = data[
i]; index =
i; }
654 template<
typename Real>
655 template<
typename OtherReal>
673 template<
typename Real>
679 template<
typename Real>
687 template<
typename Real>
696 template<
typename Real>
698 double sum_log = 0.0;
704 if (prod < 1.0e-10 || prod > 1.0e+10) {
705 sum_log +=
Log(prod);
709 if (prod != 1.0) sum_log +=
Log(prod);
713 template<
typename Real>
720 if (num_rows <= 64) {
722 const Real *m_data = M.
Data();
729 this->AddMatVec(alpha, M,
kTrans, ones, beta);
733 template<
typename Real>
739 if (num_cols <= 64) {
750 this->AddMatVec(alpha, M,
kNoTrans, ones, beta);
754 template<
typename Real>
759 Real max_elem = Max(), cutoff;
762 if (prune > 0.0 && max_elem - prune > cutoff)
763 cutoff = max_elem - prune;
765 double sum_relto_max_elem = 0.0;
770 sum_relto_max_elem +=
Exp(f - max_elem);
772 return max_elem +
Log(sum_relto_max_elem);
775 template<
typename Real>
782 template<
typename Real>
786 KALDI_ERR <<
"Trying to take log of a negative number.";
791 template<
typename Real>
799 template<
typename Real>
806 template<
typename Real>
811 template<
typename Real>
814 if (floored_count ==
nullptr) {
821 if (v.
data_[
i] < floor_val) {
828 *floored_count = num_floored;
832 template<
typename Real>
835 if (ceiled_count ==
nullptr) {
842 if (v.
data_[
i] > ceil_val) {
849 *ceiled_count = num_changed;
853 template<
typename Real>
866 template<
typename Real>
868 Real max = this->Max(), sum = 0.0;
872 this->Scale(1.0 / sum);
873 return max +
Log(sum);
876 template<
typename Real>
878 Real max = this->Max(), sum = 0.0;
883 this->Add(-1.0 * sum);
899 template<
typename Real>
905 Real inv_expx =
Exp(-x);
906 x = -1.0 + 2.0 / (1.0 + inv_expx * inv_expx);
909 x = 1.0 - 2.0 / (1.0 + expx * expx);
921 this->CopyFromVec(src);
930 this->CopyFromVec(src);
937 template<
typename Real>
944 x = 1.0 / (1.0 +
Exp(-x));
955 template<
typename Real>
962 template<
typename Real>
967 template<
typename Real>
975 template<
typename Real>
979 if (data[
i] == orig) data[
i] = changed;
983 template<
typename Real>
984 template<
typename OtherReal>
999 template<
typename Real>
1006 r.
data_, 1, beta, this->data_, 1);
1010 template<
typename Real>
1018 template<
typename Real>
1019 template<
typename OtherReal>
1033 template<
typename Real>
1042 template<
typename Real>
1043 template<
typename OtherReal>
1047 Real *__restrict__ data =
data_;
1052 data[
i] += alpha * other_data[
i];
1055 data[
i] += other_data[
i];
1063 template<
typename Real>
1064 template<
typename OtherReal>
1068 Real *__restrict__ data =
data_;
1073 data[
i] += alpha * other_data[
i] * other_data[
i];
1076 data[
i] += other_data[
i] * other_data[
i];
1085 template<
typename Real>
1089 tmp.
Read(is, binary,
false);
1090 if (this->Dim() != tmp.
Dim()) {
1091 KALDI_ERR <<
"VectorBase::Read, size mismatch " << this->Dim()<<
" vs. "<<tmp.
Dim();
1093 this->AddVec(1.0, tmp);
1100 tmp.
Read(is, binary,
false);
1101 if (tmp.
Dim() != Dim())
1102 KALDI_ERR <<
"VectorBase<Real>::Read, size mismatch " 1103 << Dim() <<
" vs. " << tmp.
Dim();
1108 template<
typename Real>
1112 tmp.
Read(is, binary,
false);
1113 if (this->Dim() == 0) this->Resize(tmp.
Dim());
1114 if (this->Dim() != tmp.
Dim()) {
1115 KALDI_ERR <<
"Vector<Real>::Read, adding but dimensions mismatch " 1116 << this->Dim() <<
" vs. " << tmp.
Dim();
1118 this->AddVec(1.0, tmp);
1122 std::ostringstream specific_error;
1126 int peekval =
Peek(is, binary);
1127 const char *my_token = (
sizeof(Real) == 4 ?
"FV" :
"DV");
1128 char other_token_start = (
sizeof(Real) == 4 ?
'D' :
'F');
1129 if (peekval == other_token_start) {
1132 other.
Read(is, binary,
false);
1133 if (this->Dim() != other.
Dim()) this->Resize(other.
Dim());
1134 this->CopyFromVec(other);
1139 if (token != my_token) {
1140 if (token.length() > 20) token = token.substr(0, 17) +
"...";
1141 specific_error <<
": Expected token " << my_token <<
", got " << token;
1146 if ((
MatrixIndexT)size != this->Dim()) this->Resize(size);
1148 is.read(reinterpret_cast<char*>(this->
data_),
sizeof(Real)*size);
1150 specific_error <<
"Error reading vector data (binary mode); truncated " 1151 "stream? (size = " << size <<
")";
1162 if (is.fail()) { specific_error <<
"EOF while trying to read vector.";
goto bad; }
1163 if (s.compare(
"[]") == 0) { Resize(0);
return; }
1164 if (s.compare(
"[")) {
1165 if (s.length() > 20) s = s.substr(0, 17) +
"...";
1166 specific_error <<
"Expected \"[\" but got " << s;
1169 std::vector<Real> data;
1172 if (i ==
'-' || (i >=
'0' && i <=
'9')) {
1175 if (is.fail()) { specific_error <<
"Failed to read number.";
goto bad; }
1176 if (! std::isspace(is.peek()) && is.peek() !=
']') {
1177 specific_error <<
"Expected whitespace after number.";
goto bad;
1182 }
else if (i ==
' ' || i ==
'\t') {
1184 }
else if (i ==
']') {
1186 this->Resize(data.size());
1187 for (
size_t j = 0;
j < data.size();
j++)
1190 if (static_cast<char>(i) ==
'\r') {
1193 }
else if (static_cast<char>(i) ==
'\n') { is.get(); }
1195 KALDI_WARN <<
"After end of vector data, read error.";
1199 }
else if (i == -1) {
1200 specific_error <<
"EOF while reading vector data.";
1202 }
else if (i ==
'\n' || i ==
'\r') {
1203 specific_error <<
"Newline found while reading vector (maybe it's a matrix?)";
1209 data.push_back(std::numeric_limits<Real>::infinity());
1210 KALDI_WARN <<
"Reading infinite value into vector.";
1212 data.push_back(std::numeric_limits<Real>::quiet_NaN());
1213 KALDI_WARN <<
"Reading NaN value into vector.";
1215 if (s.length() > 20) s = s.substr(0, 17) +
"...";
1216 specific_error <<
"Expecting numeric vector data, got " << s;
1224 KALDI_ERR <<
"Failed to read vector from stream. " << specific_error.str()
1225 <<
" File position at start is " 1226 << pos_at_start<<
", currently "<<is.tellg();
1230 template<
typename Real>
1233 KALDI_ERR <<
"Failed to write vector to stream: stream not good";
1236 std::string my_token = (
sizeof(Real) == 4 ?
"FV" :
"DV");
1242 os.write(reinterpret_cast<const char*>(Data()),
sizeof(Real) * size);
1246 os << (*
this)(
i) <<
" ";
1250 KALDI_ERR <<
"Failed to write vector to stream";
1254 template<
typename Real>
1262 template<
typename Real>
1269 if (&v !=
this) CopyFromVec(v);
1271 if (alpha != 1.0) Scale(alpha);
1274 tmp.
MulTp(M, trans);
1275 if (beta != 1.0) Scale(beta);
1280 template<
typename Real>
1296 template<
typename Real>
1303 template<
typename Real>
1311 Real *data = this->
data_;
1312 const Real *mat_data = M.
Data();
1313 for (
MatrixIndexT i = 0;
i < rows;
i++, mat_data += mat_stride, data++)
1314 *data = beta * *data + alpha *
cblas_Xdot(cols,mat_data,1,mat_data,1);
1319 Real *data = this->
data_;
1320 const Real *mat_data = M.
Data();
1322 *data = beta * *data + alpha *
cblas_Xdot(rows, mat_data, mat_stride,
1323 mat_data, mat_stride);
1327 template<
typename Real>
1342 Real *data = this->
data_;
1343 const Real *Mdata = M.
Data(), *Ndata = N.
Data();
1344 for (
MatrixIndexT i = 0;
i < dim;
i++, Mdata += M_row_stride, Ndata += N_col_stride, data++) {
1345 *data = beta * *data + alpha *
cblas_Xdot(M_col_dim, Mdata, M_col_stride, Ndata, N_row_stride);
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
This class provides a way for switching between double and float types.
Packed symetric matrix class.
template double VecMatVec(const VectorBase< double > &v1, const MatrixBase< double > &M, const VectorBase< double > &v2)
void cblas_Xtpmv(MatrixTransposeType trans, const float *Mdata, const int num_rows, float *y, const int y_inc)
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
void cblas_Xtpsv(MatrixTransposeType trans, const float *Mdata, const int num_rows, float *y, const int y_inc)
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
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...
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)
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
float RandGauss(struct RandomState *state=NULL)
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
void MulTp(const TpMatrix< Real > &M, const MatrixTransposeType trans)
Multiplies this vector by lower-triangular matrix: *this <– *this *M.
void cblas_Xcopy(const int N, const float *X, const int incX, float *Y, const int incY)
MatrixIndexT NumRows() const
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
Real Norm(Real p) const
Compute the p-th norm of the vector.
float cblas_Xdot(const int N, const float *const X, const int incX, const float *const Y, const int incY)
MatrixIndexT NumCols() const
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
MatrixIndexT dim_
dimension of vector
Packed matrix: base class for triangular and symmetric matrices.
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
void Swap(Vector< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
#define KALDI_MEMALIGN(align, size, pp_orig)
static const double kMinLogDiffDouble
void RandGauss2(float *a, float *b, RandomState *state)
#define KALDI_MEMALIGN_FREE(x)
Packed symetric matrix class.
template double VecVec(const VectorBase< double > &ra, const VectorBase< float > &rb)
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.
MatrixIndexT Dim() const
Returns the dimension of the vector.
void Scale(Real alpha)
Multiplies all elements by this constant.
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 const float kMinLogDiffFloat
Real * data_
data memory area
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)
#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.
void cblas_Xgemv(MatrixTransposeType trans, MatrixIndexT num_rows, MatrixIndexT num_cols, float alpha, const float *Mdata, MatrixIndexT stride, const float *xdata, MatrixIndexT incX, float beta, float *ydata, MatrixIndexT incY)
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 cblas_Xgbmv(MatrixTransposeType trans, MatrixIndexT num_rows, MatrixIndexT num_cols, MatrixIndexT num_below, MatrixIndexT num_above, float alpha, const float *Mdata, MatrixIndexT stride, const float *xdata, MatrixIndexT incX, float beta, float *ydata, MatrixIndexT incY)
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...
const float kLogZeroFloat
Provides a vector abstraction class.
const double kLogZeroDouble
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) ...
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...
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).