27 #ifndef KALDI_MATRIX_KALDI_VECTOR_H_ 28 #define KALDI_MATRIX_KALDI_VECTOR_H_ 1 40 template<
typename Real>
47 bool IsZero(Real cutoff = 1.0e-06)
const;
78 static_cast<UnsignedMatrixIndexT>(
dim_));
85 static_cast<UnsignedMatrixIndexT>(
dim_));
112 template<
typename OtherReal>
116 template<
typename OtherReal>
120 template<
typename OtherReal>
150 this->
Floor(*
this, floor_val, floored_count);
156 this->
Ceiling(*
this, ceil_val, ceiled_count);
180 this->
Pow(*
this, power);
186 void ApplyPowAbs(Real power,
bool include_sign=
false);
189 Real
Norm(Real p)
const;
199 template<
typename OtherReal>
207 template<
typename OtherReal>
240 template<
typename OtherReal>
246 template<
typename OtherReal>
263 void Scale(Real alpha);
277 template<
typename OtherReal>
290 template<
typename OtherReal>
294 template<
typename OtherReal>
298 template<
typename OtherReal>
363 void Read(std::istream &in,
bool binary,
bool add =
false);
366 void Write(std::ostream &Out,
bool binary)
const;
405 template<
typename Real>
415 :
VectorBase<Real>() { Resize(s, resize_type); }
419 template<
typename OtherReal>
435 template<
typename OtherReal>
458 void Read(std::istream &in,
bool binary,
bool add =
false);
500 template<
typename Real>
511 static_cast<UnsignedMatrixIndexT>(length) <=
512 static_cast<UnsignedMatrixIndexT>(t.
Dim()));
513 this->
data_ =
const_cast<Real*
> (t.
Data()+origin);
520 this->
data_ =
const_cast<Real*
> (M.
Data());
535 this->
data_ =
const_cast<Real*
>(data);
557 template<
typename Real>
558 std::ostream & operator << (std::ostream & out, const VectorBase<Real> & v);
562 template<
typename Real>
567 template<
typename Real>
575 template<
typename Real>
581 template<
typename Real>
589 template<
typename Real>
592 template<
typename Real,
typename OtherReal>
598 template<
typename Real>
612 #endif // KALDI_MATRIX_KALDI_VECTOR_H_ void CopyColsFromMat(const MatrixBase< Real > &M)
Performs a column stack of the matrix M.
bool ApproxEqual(const VectorBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).Norm(2.0) <= tol * (*this).Norm(2.0).
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void SetRandUniform()
Sets to numbers uniformly distributed on (0,1)
const Real * Data() const
Returns a pointer to the start of the vector's data (const).
Packed symetric matrix class.
void ApplyExp()
Apply exponential to each value in vector.
Vector(const VectorBase< Real > &v)
Copy-constructor from base-class, needed to copy from SubVector.
void CopyDiagFromTp(const TpMatrix< Real > &M)
Extracts the diagonal of a triangular matrix.
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
void Sigmoid(const VectorBase< Real > &src)
Sets each element of *this to the sigmoid function of the corresponding element of "src"...
bool IsZero(Real cutoff=1.0e-06) const
Returns true if matrix is all zeros.
void CopyFromPacked(const PackedMatrix< OtherReal > &M)
Copy data from a SpMatrix or TpMatrix (must match own size).
void CopyDiagFromMat(const MatrixBase< Real > &M)
Extracts the diagonal of the matrix M.
Vector(const Vector< Real > &v)
Copy constructor. The need for this is controversial.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Base class which provides matrix operations not involving resizing or allocation. ...
void Tanh(const VectorBase< Real > &src)
Sets each element of *this to the tanh of the corresponding element of "src".
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...
void ApplyCeiling(Real ceil_val, MatrixIndexT *ceiled_count=nullptr)
Applies ceiling to all elements.
void ApplyLogAndCopy(const VectorBase< Real > &v)
Apply natural log to another vector and put result in *this.
SubVector(const PackedMatrix< Real > &M)
This constructor initializes the vector to point at the contents of this packed matrix (SpMatrix or T...
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
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 SumLog() const
Returns sum of the logs of the elements.
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
~Vector()
Destructor. Deallocates memory.
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)...
SubVector(const VectorBase< Real > &t, const MatrixIndexT origin, const MatrixIndexT length)
Constructor from a Vector or SubVector.
Real VecMatVec(const VectorBase< Real > &v1, const MatrixBase< Real > &M, const VectorBase< Real > &v2)
Returns .
void MulTp(const TpMatrix< Real > &M, const MatrixTransposeType trans)
Multiplies this vector by lower-triangular matrix: *this <– *this *M.
void ApplyLog()
Apply natural log to all elements.
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.
Real LogSumExp(Real prune=-1.0) const
Returns log(sum(exp())) without exp overflow If prune > 0.0, ignores terms less than the max - prune...
Real ApplySoftMax()
Apply soft-max to vector and return normalizer (log sum of exponentials).
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
MatrixIndexT NumRows() const
void Pow(const VectorBase< Real > &v, Real power)
~VectorBase()
Destructor; does not deallocate memory, this is handled by child classes.
Real Norm(Real p) const
Compute the p-th norm of the vector.
void CopyRowFromMat(const MatrixBase< Real > &M, MatrixIndexT row)
Extracts a row of the matrix M.
void AddVec2(const Real alpha, const VectorBase< Real > &v)
Add vector : *this = *this + alpha * rv^2 [element-wise squaring].
void AddVecVec(Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &r, Real beta)
Add element-by-element product of vectors:
void ApplyFloor(Real floor_val, MatrixIndexT *floored_count=nullptr)
Applies floor to all elements.
VectorBase()
Empty initializer, corresponds to vector of zero size.
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void CopyDiagFromPacked(const PackedMatrix< Real > &M)
Extracts the diagonal of a packed matrix M; works for Sp or Tp.
void Floor(const VectorBase< Real > &v, Real floor_val, MatrixIndexT *floored_count=nullptr)
Applies floor to all elements.
MatrixIndexT dim_
dimension of vector
Packed matrix: base class for triangular and symmetric matrices.
void MulElements(const VectorBase< Real > &v)
Multiply element-by-element by another vector.
void Read(std::istream &in, bool binary, bool add=false)
Reads from C++ stream (option to add to existing contents).
SubVector(const Real *data, MatrixIndexT length)
Constructor from a pointer to memory and a length.
void ApplyPowAbs(Real power, bool include_sign=false)
Take the absolute value of all elements of a vector to a power.
Vector(const VectorBase< OtherReal > &v)
Type conversion constructor.
SubVector(const SubVector &other)
Copy constructor.
KALDI_DISALLOW_COPY_AND_ASSIGN(VectorBase)
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
#define KALDI_PARANOID_ASSERT(cond)
Packed symetric matrix class.
Real * Data()
Returns a pointer to the start of the vector's data.
void CopyFromPtr(const Real *Data, MatrixIndexT sz)
Load data into the vector: sz must match own size.
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.
SubVector(const MatrixBase< Real > &matrix, MatrixIndexT row)
This operation does not preserve const-ness, so be careful.
Real operator()(MatrixIndexT i) const
Indexing operator (const).
Real Sum() const
Returns sum of the elements.
void SetRandn()
Set vector to random normally-distributed noise.
void AddVecDivVec(Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &r, Real beta)
Add element-by-element quotient of two vectors.
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
std::istream & operator>>(std::istream &is, Matrix< Real > &M)
Real * data_
data memory area
Vector()
Constructor that takes no arguments. Initializes to empty.
Matrix for CUDA computing.
void CopyColFromMat(const MatrixBase< OtherReal > &M, MatrixIndexT col)
Extracts a column of the matrix M.
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 InvertElements()
Invert all elements.
#define KALDI_ASSERT(cond)
void Set(Real f)
Set all members of a vector to a specified value.
void ApplyPow(Real power)
Take all elements of vector to a power.
~SubVector()
Destructor (does nothing; no pointers are owned here).
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
void DivElements(const VectorBase< Real > &v)
Divide element-by-element by a vector.
void CopyRowsFromMat(const MatrixBase< Real > &M)
Performs a row stack of the matrix M.
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...
const SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l) const
Returns a const sub-vector of a vector (a range of elements).
Real ApplyLogSoftMax()
Applies log soft-max to vector and returns normalizer (log sum of exponentials).
void ReplaceValue(Real orig, Real changed)
Set each element to y = (x == orig ? changed : x).
void Ceiling(const VectorBase< Real > &v, Real ceil_val, MatrixIndexT *ceiled_count=nullptr)
Applies ceiling to all elements.
Vector(const MatrixIndexT s, MatrixResizeType resize_type=kSetZero)
Constructor with specific size.
MatrixIndexT RandCategorical() const
This function returns a random index into this vector, chosen with probability proportional to the co...
MatrixIndexT SizeInBytes() const
Returns the size in memory of the vector, in bytes.
void ApplyAbs()
Take absolute value of each of the elements.
Provides a vector abstraction class.
void Add(Real c)
Add a constant to each element of a vector.
void AddColSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of columns of M) + beta * *this.
void SetZero()
Set vector to all zeros.
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 CopyDiagFromSp(const SpMatrix< Real > &M)
Extracts the diagonal of a symmetric matrix.
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
void CopyRowFromSp(const SpMatrix< OtherReal > &S, MatrixIndexT row)
Extracts a row of the symmetric matrix S.
Vector for CUDA computing.
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).