32 template<
typename Real>
34 size_t nr = num_rows_,
35 sz = (nr * (nr + 1)) / 2;
39 template<
typename Real>
42 size_t nr = num_rows_,
43 sz = (nr * (nr + 1)) / 2;
47 template<
typename Real>
50 size_t dim = num_rows_, size = ((dim*(dim+1))/2);
51 for (
size_t i = 0;
i < size;
i++)
55 template<
typename Real>
62 size_t size = ((
static_cast<size_t>(r) * static_cast<size_t>(r + 1)) / 2);
64 if (static_cast<size_t>(static_cast<MatrixIndexT>(size)) != size) {
65 KALDI_WARN <<
"Allocating packed matrix whose full dimension does not fit " 66 <<
"in MatrixIndexT: not all code is tested for this case.";
72 if ((data =
KALDI_MEMALIGN(16, size *
sizeof(Real), &temp)) != NULL) {
73 this->
data_ =
static_cast<Real *
> (data);
76 throw std::bad_alloc();
80 template<
typename Real>
86 template<
typename Real>
93 template<
typename Real>
99 else if (this->num_rows_ == r) {
return; }
103 size_t r_min = std::min(r, num_rows_);
104 size_t mem_size_min =
sizeof(Real) * (r_min*(r_min+1))/2,
105 mem_size_full =
sizeof(Real) * (r*(r+1))/2;
108 char *ptr =
static_cast<char*
>(
static_cast<void*
>(tmp.
data_));
110 memset(static_cast<void*>(ptr + mem_size_min), 0, mem_size_full-mem_size_min);
115 if (
data_ != NULL) Destroy();
117 if (resize_type ==
kSetZero) SetZero();
122 template<
typename Real>
131 template<
typename Real>
140 template<
typename Real>
151 template<
typename Real>
152 template<
typename OtherReal>
156 memcpy(
data_, orig.
Data(), SizeInBytes());
160 size_t nr = NumRows(),
161 size = (nr * (nr + 1)) / 2;
162 for (
size_t i = 0;
i < size;
i++, dst++, src++)
179 template<
typename Real>
180 template<
typename OtherReal>
185 memcpy(
data_, vec.
Data(), size *
sizeof(Real));
206 template<
typename Real>
208 memset(
data_, 0, SizeInBytes());
211 template<
typename Real>
213 memset(
data_, 0, SizeInBytes());
215 (*
this)(row, row) = 1.0;
218 template<
typename Real>
222 ans += (*
this)(row, row);
226 template<
typename Real>
235 template<
typename Real>
238 KALDI_ERR <<
"Failed to write vector to stream: stream not good";
241 int32 size = this->NumRows();
246 std::string my_token = (
sizeof(Real) == 4 ?
"FP" :
"DP");
251 os.write((
const char*)
data_,
sizeof(Real) * num_elems);
260 for (
int32 k = 0; k <
j + 1; k++) {
263 os << ( (j==size-1)?
"]\n" :
"\n");
269 KALDI_ERR <<
"Failed to write packed matrix to stream";
297 template<
typename Real>
301 tmp.
Read(is, binary,
false);
302 if (this->NumRows() == 0) this->Resize(tmp.
NumRows());
304 if (this->NumRows() != tmp.
NumRows()) {
305 if (tmp.
NumRows() == 0)
return;
306 else KALDI_ERR <<
"PackedMatrix::Read, size mismatch " << this->NumRows()
310 this->AddPacked(1.0, tmp);
314 std::ostringstream specific_error;
316 int peekval =
Peek(is, binary);
317 const char *my_token = (
sizeof(Real) == 4 ?
"FP" :
"DP");
318 const char *new_format_token =
"[";
319 bool is_new_format =
false;
320 char other_token_start = (
sizeof(Real) == 4 ?
'D' :
'F');
324 if (peekval == other_token_start) {
327 other.
Read(is, binary,
false);
329 this->CopyFromPacked(other);
334 if (token != my_token) {
335 if(token != new_format_token) {
336 specific_error <<
": Expected token " << my_token <<
", got " << token;
340 is_new_format =
true;
355 is.read(reinterpret_cast<char*>(
data_),
sizeof(Real)*num_elems);
357 if (is.fail())
goto bad;
361 std::vector<Real> data;
365 if (i == -1) { specific_error <<
"Got EOF while reading matrix data";
goto bad; }
366 else if (static_cast<char>(i) ==
']') {
369 if (static_cast<char>(i) ==
'\r') {
373 else if (static_cast<char>(i) ==
'\n') { is.get(); }
376 KALDI_WARN <<
"After end of matrix data, read error.";
380 num_lines =
int32(sqrt(data.size()*2));
384 this->Resize(num_lines);
388 for(
int32 i = 0; i < data.size(); i++) {
394 else if ( (i >=
'0' && i <=
'9') || i ==
'-' ) {
398 specific_error <<
"Stream failure/EOF while reading matrix data.";
403 else if (isspace(i)) {
410 data.push_back(std::numeric_limits<Real>::infinity());
411 KALDI_WARN <<
"Reading infinite value into matrix.";
413 data.push_back(std::numeric_limits<Real>::quiet_NaN());
414 KALDI_WARN <<
"Reading NaN value into matrix.";
416 specific_error <<
"Expecting numeric matrix data, got " << str;
423 KALDI_ERR <<
"Failed to read packed matrix from stream. " << specific_error.str()
424 <<
" File position at start is " 425 << pos_at_start <<
", currently " << is.tellg();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void CopyFromVec(const SubVector< OtherReal > &orig)
CopyFromVec just interprets the vector as having the same layout as the packed matrix.
This class provides a way for switching between double and float types.
Real Trace() const
< Set to random values of a normal distribution
void Read(std::istream &in, bool binary, bool add=false)
void Write(std::ostream &out, bool binary) 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...
Real * data_
data memory area
void AddPacked(const Real alpha, const PackedMatrix< Real > &M)
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).
A class for storing matrices.
void ScaleDiag(const Real alpha)
void SetUnit()
< Set to zero
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...
void SetRandn()
< Set to unit matrix.
void AddToDiag(const Real r)
Packed matrix: base class for triangular and symmetric matrices.
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
MatrixIndexT num_rows_
< Number of columns
#define KALDI_MEMALIGN(align, size, pp_orig)
void Swap(PackedMatrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
void SetDiag(const Real alpha)
#define KALDI_MEMALIGN_FREE(x)
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 Init(MatrixIndexT dim)
Init assumes the current contents of the class are is invalid (i.e.
void CopyFromPacked(const PackedMatrix< OtherReal > &orig)
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
#define KALDI_ASSERT(cond)
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...
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).