// matrix/kaldi-matrix.h
// Copyright 2009-2011 Ondrej Glembek; Microsoft Corporation; Lukas Burget;
// Saarland University; Petr Schwarz; Yanmin Qian;
// Karel Vesely; Go Vivace Inc.; Haihua Xu
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#ifndef KALDI_MATRIX_KALDI_MATRIX_H_
#define KALDI_MATRIX_KALDI_MATRIX_H_ 1
#include "matrix/matrix-common.h"
namespace kaldi {
/// @{ \addtogroup matrix_funcs_scalar
/// We need to declare this here as it will be a friend function.
/// tr(A B), or tr(A B^T).
template<typename Real>
Real TraceMatMat(const MatrixBase<Real> &A, const MatrixBase<Real> &B,
MatrixTransposeType trans = kNoTrans);
/// @}
/// \addtogroup matrix_group
/// @{
/// Base class which provides matrix operations not involving resizing
/// or allocation. Classes Matrix and SubMatrix inherit from it and take care
/// of allocation and resizing.
template<typename Real>
class MatrixBase {
public:
// so this child can access protected members of other instances.
friend class Matrix<Real>;
// friend declarations for CUDA matrices (see ../cudamatrix/)
friend class CuMatrixBase<Real>;
friend class CuMatrix<Real>;
friend class CuSubMatrix<Real>;
friend class CuPackedMatrix<Real>;
friend class PackedMatrix<Real>;
/// Returns number of rows (or zero for emtpy matrix).
inline MatrixIndexT NumRows() const { return num_rows_; }
/// Returns number of columns (or zero for emtpy matrix).
inline MatrixIndexT NumCols() const { return num_cols_; }
/// Stride (distance in memory between each row). Will be >= NumCols.
inline MatrixIndexT Stride() const { return stride_; }
/// Returns size in bytes of the data held by the matrix.
size_t SizeInBytes() const {
return static_cast<size_t>(num_rows_) * static_cast<size_t>(stride_) *
sizeof(Real);
}
/// Gives pointer to raw data (const).
inline const Real* Data() const {
return data_;
}
/// Gives pointer to raw data (non-const).
inline Real* Data() { return data_; }
/// Returns pointer to data for one row (non-const)
inline Real* RowData(MatrixIndexT i) {
KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
static_cast<UnsignedMatrixIndexT>(num_rows_));
return data_ + i * stride_;
}
/// Returns pointer to data for one row (const)
inline const Real* RowData(MatrixIndexT i) const {
KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
static_cast<UnsignedMatrixIndexT>(num_rows_));
return data_ + i * stride_;
}
/// Indexing operator, non-const
/// (only checks sizes if compiled with -DKALDI_PARANOID)
inline Real& operator() (MatrixIndexT r, MatrixIndexT c) {
KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
static_cast<UnsignedMatrixIndexT>(num_rows_) &&
static_cast<UnsignedMatrixIndexT>(c) <
static_cast<UnsignedMatrixIndexT>(num_cols_));
return *(data_ + r * stride_ + c);
}
/// Indexing operator, provided for ease of debugging (gdb doesn't work
/// with parenthesis operator).
Real &Index (MatrixIndexT r, MatrixIndexT c) { return (*this)(r, c); }
/// Indexing operator, const
/// (only checks sizes if compiled with -DKALDI_PARANOID)
inline const Real operator() (MatrixIndexT r, MatrixIndexT c) const {
KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
static_cast<UnsignedMatrixIndexT>(num_rows_) &&
static_cast<UnsignedMatrixIndexT>(c) <
static_cast<UnsignedMatrixIndexT>(num_cols_));
return *(data_ + r * stride_ + c);
}
/* Basic setting-to-special values functions. */
/// Sets matrix to zero.
void SetZero();
/// Sets all elements to a specific value.
void Set(Real);
/// Sets to zero, except ones along diagonal [for non-square matrices too]
void SetUnit();
/// Sets to random values of a normal distribution
void SetRandn();
/// Sets to numbers uniformly distributed on (0, 1)
void SetRandUniform();
/* Copying functions. These do not resize the matrix! */
/// Copy given matrix. (no resize is done).
template<typename OtherReal>
void CopyFromMat(const MatrixBase<OtherReal> & M,
MatrixTransposeType trans = kNoTrans);
/// Copy from compressed matrix.
void CopyFromMat(const CompressedMatrix &M);
/// Copy given spmatrix. (no resize is done).
template<typename OtherReal>
void CopyFromSp(const SpMatrix<OtherReal> &M);
/// Copy given tpmatrix. (no resize is done).
template<typename OtherReal>
void CopyFromTp(const TpMatrix<OtherReal> &M,
MatrixTransposeType trans = kNoTrans);
/// Copy from CUDA matrix. Implemented in ../cudamatrix/cu-matrix.h