math

class sptk::Matrix

Matrix.

Public Functions

explicit Matrix(int num_row = 0, int num_column = 0)
Parameters
  • num_row[in] Number of rows.

  • num_column[in] Number of columns.

Matrix(int num_row, int num_column, const std::vector<double> &vector)
Parameters
  • num_row[in] Number of rows.

  • num_column[in] Number of columns.

  • vector[in] Diagonal elements.

Matrix(const Matrix &matrix)
Parameters

matrix[in] Matrix.

Matrix &operator=(const Matrix &matrix)
Parameters

matrix[in] Matrix.

inline int GetNumRow() const
Returns

Number of rows.

inline int GetNumColumn() const
Returns

Number of columns.

void Resize(int num_row, int num_column)

Resize matrix.

Parameters
  • num_row[in] Number of rows.

  • num_column[in] Number of columns.

inline double *operator[](int row)
Parameters

row[in] Row.

inline const double *operator[](int row) const
Parameters

row[in] Row.

double &At(int row, int column)

Get element.

Parameters
  • row[in] i.

  • column[in] j.

Returns

(i, j)-th element.

const double &At(int row, int column) const

Get element.

Parameters
  • row[in] i.

  • column[in] j.

Returns

(i, j)-th element.

Matrix &operator+=(const Matrix &matrix)
Parameters

matrix[in] Addend.

Returns

Sum.

Matrix &operator-=(const Matrix &matrix)
Parameters

matrix[in] Subtrahend.

Returns

Difference.

Matrix &operator*=(const Matrix &matrix)
Parameters

matrix[in] Multiplicand.

Returns

Product.

Matrix operator+(const Matrix &matrix) const
Parameters

matrix[in] Addend.

Returns

Sum.

Matrix operator-(const Matrix &matrix) const
Parameters

matrix[in] Subtrahend.

Returns

Difference.

Matrix operator*(const Matrix &matrix) const
Parameters

matrix[in] Multiplicand.

Returns

Product.

Matrix operator-() const

Negate.

Returns

Negated matrix.

void Fill(double value)

Overwrite all elements with a value.

Parameters

value[in] Value.

void FillDiagonal(double value)

Overwrite diagonal elements with a value.

Parameters

value[in] Diagonal value.

void Negate()

Negate all elements of matrix.

bool Transpose(Matrix *transposed_matrix) const

Transpose matrix.

Parameters

transposed_matrix[out] Transposed matrix.

Returns

True on success, false on failure.

bool GetSubmatrix(int row_offset, int num_row_of_submatrix, int column_offset, int num_column_of_submatrix, Matrix *submatrix) const

Get submatrix.

Parameters
  • row_offset[in] Offset of row.

  • num_row_of_submatrix[in] Number of rows of submatrix.

  • column_offset[in] Offset of column.

  • num_column_of_submatrix[in] Number of columns of submatrix.

  • submatrix[out] Submatrix.

Returns

True on success, false on failure.

bool GetDeterminant(double *determinant) const

Compute determinant.

Parameters

determinant[out] Determinant.

Returns

True on success, false on failure.

class sptk::Matrix2D

2D matrix.

Public Functions

Matrix2D()

Make 2D matrix.

Matrix2D(const Matrix2D &matrix)
Parameters

matrix[in] 2D matrix.

Matrix2D &operator=(const Matrix2D &matrix)
Parameters

matrix[in] 2D matrix.

inline double *operator[](int row)
Parameters

row[in] Row.

inline const double *operator[](int row) const
Parameters

row[in] Row.

double &At(int row, int column)

Get element.

Parameters
  • row[in] i.

  • column[in] j.

Returns

(i, j)-th element.

const double &At(int row, int column) const

Get element.

Parameters
  • row[in] i.

  • column[in] j.

Returns

(i, j)-th element.

void Fill(double value)

Overwrite all elements with a value.

Parameters

value[in] Value.

void FillDiagonal(double value)

Overwrite diagonal elements with a value.

Parameters

value[in] Diagonal value.

void Negate()

Negate all elements of matrix.

void Negate(const Matrix2D &matrix)

Negate all elements of matrix.

bool CrossTranspose(Matrix2D *transposed_matrix) const

Compute cross-transpose matrix.

Parameters

transposed_matrix[out] Cross-transposed matrix.

Returns

True on success, false on failure.

bool Invert(Matrix2D *inverse_matrix) const

Compute inverse matrix.

Parameters

inverse_matrix[out] Inverse matrix.

Returns

True on success, false on failure.

Public Static Functions

static bool Add(const Matrix2D &matrix, Matrix2D *output)

Compute sum.

Parameters
  • matrix[in] Addend.

  • output[inout] Result.

Returns

True on success, false on failure.

static bool Add(const Matrix2D &first_matrix, const Matrix2D &second_matrix, Matrix2D *output)

Compute sum.

Parameters
  • first_matrix[in] Augend.

  • second_matrix[in] Addend.

  • output[out] Result.

Returns

True on success, false on failure.

static bool Subtract(const Matrix2D &matrix, Matrix2D *output)

Compute difference.

Parameters
  • matrix[in] Subtrahend.

  • output[inout] Result.

Returns

True on success, false on failure.

static bool Subtract(const Matrix2D &first_matrix, const Matrix2D &second_matrix, Matrix2D *output)

Compute difference.

Parameters
  • first_matrix[in] Minuend.

  • second_matrix[in] Subtrahend.

  • output[out] Result.

Returns

True on success, false on failure.

static bool Multiply(const Matrix2D &matrix, const std::vector<double> &column_vector, std::vector<double> *output)

Compute Ax.

Parameters
  • matrix[in] A.

  • column_vector[in] x.

  • output[out] Result.

Returns

True on success, false on failure.

static bool Multiply(const Matrix2D &first_matrix, const Matrix2D &second_matrix, Matrix2D *output)

Compute AB.

Parameters
  • first_matrix[in] A.

  • second_matrix[in] B.

  • output[out] Result.

Returns

True on success, false on failure.

class sptk::SymmetricMatrix

Symmetric matrix.

Public Functions

explicit SymmetricMatrix(int num_dimension = 0)
Parameters

num_dimension[in] Size of matrix.

SymmetricMatrix(const SymmetricMatrix &matrix)
Parameters

matrix[in] Symmetric matrix.

SymmetricMatrix &operator=(const SymmetricMatrix &matrix)
Parameters

matrix[in] Symmetric matrix.

inline int GetNumDimension() const
Returns

Number of dimensions.

void Resize(int num_dimension)

Resize matrix.

Parameters

num_dimension[in] Number of dimensions.

inline Row operator[](int row)
Parameters

row[in] Row.

inline const Row operator[](int row) const
Parameters

row[in] Row.

double &At(int row, int column)

Get element.

Parameters
  • row[in] i.

  • column[in] j.

Returns

(i, j)-th element.

const double &At(int row, int column) const

Get element.

Parameters
  • row[in] i.

  • column[in] j.

Returns

(i, j)-th element.

void Fill(double value)

Overwrite all elements with a value.

Parameters

value[in] Value.

bool GetDiagonal(std::vector<double> *diagonal_elements) const

Get diagonal elements.

Parameters

diagonal_elements[out] Diagonal elements.

Returns

True on success, false on failure.

bool SetDiagonal(const std::vector<double> &diagonal_elements) const

Set diagonal elements.

Parameters

diagonal_elements[in] Diagonal elements.

Returns

True on success, false on failure.

bool CholeskyDecomposition(SymmetricMatrix *lower_triangular_matrix, std::vector<double> *diagonal_elements) const

Perform Cholesky decomposition.

Parameters
  • lower_triangular_matrix[out] Lower triangular matrix.

  • diagonal_elements[out] Diagonal elements.

Returns

True on success, false on failure.

bool Invert(SymmetricMatrix *inverse_matrix) const

Compute inverse matrix.

Parameters

inverse_matrix[out] Inverse matrix.

Returns

True on success, false on failure.

class Row

A row of symmetric matrix.a

Public Functions

inline Row(const SymmetricMatrix &matrix, int row)
Parameters
  • matrix[in] Symmetric matrix.

  • row[in] Row index.

double &operator[](int column)
Parameters

column[in] Column index.

Returns

Element.

const double &operator[](int column) const
Parameters

column[in] Column index.

Returns

Element.

class sptk::SymmetricSystemSolver

Solve the following symmetric system:

\[ \boldsymbol{A} \boldsymbol{x} = \boldsymbol{b}, \]
where \(\boldsymbol{A}\) is a symmetric matrix.

The inputs are \(\boldsymbol{A}\) and \(M\)-th order constant vector:

\[ \begin{array}{cccc} b(0), & b(1), & \ldots, & b(M). \end{array} \]
The outputs are the unknown coefficients
\[ \begin{array}{cccc} x(0), & x(1), & \ldots, & x(M). \end{array} \]

Public Functions

explicit SymmetricSystemSolver(int num_order)
Parameters

num_order[in] Order of vector, \(M\).

inline int GetNumOrder() const
Returns

Order of vector.

inline bool IsValid() const
Returns

True if this object is valid.

bool Run(const SymmetricMatrix &coefficient_matrix, const std::vector<double> &constant_vector, std::vector<double> *solution_vector, SymmetricSystemSolver::Buffer *buffer) const
Parameters
  • coefficient_matrix[in] \((M+1, M+1)\) matrix \(\boldsymbol{A}\).

  • constant_vector[in] \(M\)-th order vector \(\boldsymbol{b}\).

  • solution_vector[out] \(M\)-th order vector \(\boldsymbol{x}\).

  • buffer[out] Buffer.

Returns

True on success, false on failure.

class Buffer

Buffer for SymmetricSystemSolver class.

class sptk::ToeplitzPlusHankelSystemSolver

Solve the following Toeplitz-plus-Hankel system:

\[ (\boldsymbol{T} + \boldsymbol{H}) \boldsymbol{x} = \boldsymbol{b}, \]
where
\[\begin{split}\begin{eqnarray} \boldsymbol{T} &=& \left[ \begin{array}{ccccc} t(0) & t(-1) & t(-2) & \cdots & t(-M) \\ t(1) & t(0) & t(-1) & \cdots & t(-M+1) \\ t(2) & t(1) & t(0) & \ddots & \vdots \\ \vdots & \vdots & \ddots & \ddots & t(-1) \\ t(M) & t(M-1) & \cdots & t(1) & t(0) \end{array} \right], \\ \boldsymbol{H} &=& \left[ \begin{array}{ccccc} h(0) & h(1) & h(2) & \cdots & h(M) \\ h(1) & h(2) & h(3) & \cdots & h(M+1) \\ h(2) & t(3) & h(4) & \ddots & \vdots \\ \vdots & \vdots & \ddots & \ddots & h(2M-1) \\ h(M) & h(M+1) & \cdots & h(2M-1)& h(2M) \end{array} \right], \\ \boldsymbol{b} &=& \left[ \begin{array}{cccc} b(0) & b(1) & \cdots & b(M) \end{array} \right]^\mathsf{T}. \\ \end{eqnarray}\end{split}\]

The inputs are \((2M+1)\) Toeplitz coefficient vector:

\[ \begin{array}{cccc} t(-M), & t(-M+1), & \ldots, & t(M), \end{array} \]
\((2M+1)\) Hankel coefficient vector:
\[ \begin{array}{cccc} h(0), & h(1), & \ldots, & h(2M), \end{array} \]
and \((M+1)\) constant vector:
\[ \begin{array}{cccc} b(0), & b(1), & \ldots, & b(M). \end{array} \]
The outputs are the unknown coefficients
\[ \begin{array}{cccc} x(0), & x(1), & \ldots, & x(M). \end{array} \]

[1] G. Merchant and T. Parks, “Efficient solution of a Toeplitz-plus-Hankel coefficient matrix system of equations,” IEEE Transactions on Acoustics, Speech, and Signal Processing, vol. 30, no. 1, pp. 40-44, 1982.

Public Functions

ToeplitzPlusHankelSystemSolver(int num_order, bool coefficients_modification = true)
Parameters
  • num_order[in] Order of vector, \(M\).

  • coefficients_modification[in] If true, perform coefficients modification.

inline int GetNumOrder() const
Returns

Order of vector.

inline bool GetCoefficientsModificationFlag()
Returns

Ture if coefficients modification is enabled.

inline bool IsValid() const
Returns

True if this object is valid.

bool Run(const std::vector<double> &toeplitz_coefficient_vector, const std::vector<double> &hankel_coefficient_vector, const std::vector<double> &constant_vector, std::vector<double> *solution_vector, ToeplitzPlusHankelSystemSolver::Buffer *buffer) const
Parameters
  • toeplitz_coefficient_vector[in] \(2M\)-th order coefficient vector of Toeplitz matrix \(\boldsymbol{T}\).

  • hankel_coefficient_vector[in] \(2M\)-th order coefficient vector of Hankel matrix \(\boldsymbol{H}\).

  • constant_vector[in] \(M\)-th order constant vector \(\boldsymbol{b}\).

  • solution_vector[out] \(M\)-th order solution vector \(\boldsymbol{x}\).

  • buffer[out] Buffer.

Returns

True on success, false on failure.

class Buffer

Buffer for ToeplitzPlusHankelSystemSolver class.

class sptk::VandermondeSystemSolver

Solve the following Vandermonde system:

\[\begin{split} \left[ \begin{array}{cccc} 1 & 1 & \cdots & 1 \\ x(0) & x(1) & \cdots & x(M) \\ x^2(0) & x^2(1) & \cdots & x^2(M) \\ \vdots & \vdots & \ddots & \vdots \\ x^M(0) & x^M(1) & \cdots & x^M(M) \end{array} \right] \left[ \begin{array}{c} w(0) \\ w(1) \\ w(2) \\ \vdots \\ w(M) \end{array} \right] = \left[ \begin{array}{c} q(0) \\ q(1) \\ q(2) \\ \vdots \\ q(M) \end{array} \right]. \end{split}\]

The inputs are

\[ \begin{array}{cccc} x(0), & x(1), & \ldots, & x(M), \end{array} \]
and
\[ \begin{array}{cccc} q(0), & q(1), & \ldots, & q(M). \end{array} \]
The outputs are the unknown coefficients
\[ \begin{array}{cccc} w(0), & w(1), & \ldots, & w(M). \end{array} \]

[1] W. H. Press, et al., “Numerical recipes in C: The art of scientific computing,” Cambridge University Press, pp. 90-92, 1992.

Public Functions

explicit VandermondeSystemSolver(int num_order)
Parameters

num_order[in] Order of vector, \(M\).

inline int GetNumOrder() const
Returns

Order of vector.

inline bool IsValid() const
Returns

True if this object is valid.

bool Run(const std::vector<double> &coefficient_vector, const std::vector<double> &constant_vector, std::vector<double> *solution_vector, VandermondeSystemSolver::Buffer *buffer) const
Parameters
  • coefficient_vector[in] \(M\)-th order vector \(\boldsymbol{x}\).

  • constant_vector[in] \(M\)-th order vector \(\boldsymbol{q}\).

  • solution_vector[out] \(M\)-th order vector \(\boldsymbol{w}\).

  • buffer[out] Buffer.

Returns

True on success, false on failure.

class Buffer

Buffer for VandermondeSystemSolver class.