matfunc.cpp
Go to the documentation of this file.
#include <itpp/base/matfunc.h>
#include <itpp/base/algebra/schur.h>
#include <itpp/base/converters.h>
#include <limits>
namespace itpp
{
// Square root of a square matrix (based on Octave sqrtm implementation)
cmat sqrtm(const mat& A)
{
return sqrtm(to_cmat(A));
}
// Square root of the complex square matrix A
cmat sqrtm(const cmat& A)
{
cmat U, T;
schur(A, U, T);
int n = U.rows();
cmat R(n, n);
R.zeros();
for (int j = 0; j < n; j++)
R(j, j) = std::sqrt(T(j, j));
const double fudge = std::sqrt(std::numeric_limits<double>::min());
for (int p = 0; p < n - 1; p++) {
for (int i = 0; i < n - (p + 1); i++) {
const int j = i + p + 1;
std::complex<double> s = T(i, j);
for (int k = i + 1; k < j; k++)
s -= R(i, k) * R(k, j);
const std::complex<double> d = R(i, i) + R(j, j) + fudge;
const std::complex<double> conj_d = conj(d);
R(i, j) = (s * conj_d) / (d * conj_d);
}
}
return U * R * U.H();
}
bool all(const bvec &testvec)
{
for (int i = 0; i < testvec.length(); i++)
if (!testvec(i)) return false;
return true;
}
bool any(const bvec &testvec)
{
for (int i = 0; i < testvec.length(); i++)
if (testvec(i)) return true;
return false;
}
// ----------------------------------------------------------------------
// Instantiations
// ----------------------------------------------------------------------
template ITPP_EXPORT int length(const vec &v);
template ITPP_EXPORT int length(const cvec &v);
template ITPP_EXPORT int length(const svec &v);
template ITPP_EXPORT int length(const ivec &v);
template ITPP_EXPORT int length(const bvec &v);
template ITPP_EXPORT double sum(const vec &v);
template ITPP_EXPORT std::complex<double> sum(const cvec &v);
template ITPP_EXPORT short sum(const svec &v);
template ITPP_EXPORT int sum(const ivec &v);
template ITPP_EXPORT bin sum(const bvec &v);
template ITPP_EXPORT double sum_sqr(const vec &v);
template ITPP_EXPORT std::complex<double> sum_sqr(const cvec &v);
template ITPP_EXPORT short sum_sqr(const svec &v);
template ITPP_EXPORT int sum_sqr(const ivec &v);
template ITPP_EXPORT bin sum_sqr(const bvec &v);
template ITPP_EXPORT vec cumsum(const vec &v);
template ITPP_EXPORT cvec cumsum(const cvec &v);
template ITPP_EXPORT svec cumsum(const svec &v);
template ITPP_EXPORT ivec cumsum(const ivec &v);
template ITPP_EXPORT bvec cumsum(const bvec &v);
template ITPP_EXPORT double prod(const vec &v);
template ITPP_EXPORT std::complex<double> prod(const cvec &v);
template ITPP_EXPORT short prod(const svec &v);
template ITPP_EXPORT int prod(const ivec &v);
template ITPP_EXPORT bin prod(const bvec &v);
template ITPP_EXPORT vec cross(const vec &v1, const vec &v2);
template ITPP_EXPORT cvec cross(const cvec &v1, const cvec &v2);
template ITPP_EXPORT ivec cross(const ivec &v1, const ivec &v2);
template ITPP_EXPORT svec cross(const svec &v1, const svec &v2);
template ITPP_EXPORT bvec cross(const bvec &v1, const bvec &v2);
template ITPP_EXPORT vec reverse(const vec &in);
template ITPP_EXPORT cvec reverse(const cvec &in);
template ITPP_EXPORT svec reverse(const svec &in);
template ITPP_EXPORT ivec reverse(const ivec &in);
template ITPP_EXPORT bvec reverse(const bvec &in);
template ITPP_EXPORT vec zero_pad(const vec &v, int n);
template ITPP_EXPORT cvec zero_pad(const cvec &v, int n);
template ITPP_EXPORT ivec zero_pad(const ivec &v, int n);
template ITPP_EXPORT svec zero_pad(const svec &v, int n);
template ITPP_EXPORT bvec zero_pad(const bvec &v, int n);
template ITPP_EXPORT vec zero_pad(const vec &v);
template ITPP_EXPORT cvec zero_pad(const cvec &v);
template ITPP_EXPORT ivec zero_pad(const ivec &v);
template ITPP_EXPORT svec zero_pad(const svec &v);
template ITPP_EXPORT bvec zero_pad(const bvec &v);
template ITPP_EXPORT mat zero_pad(const mat &, int, int);
template ITPP_EXPORT cmat zero_pad(const cmat &, int, int);
template ITPP_EXPORT imat zero_pad(const imat &, int, int);
template ITPP_EXPORT smat zero_pad(const smat &, int, int);
template ITPP_EXPORT bmat zero_pad(const bmat &, int, int);
template ITPP_EXPORT vec sum(const mat &m, int dim);
template ITPP_EXPORT cvec sum(const cmat &m, int dim);
template ITPP_EXPORT svec sum(const smat &m, int dim);
template ITPP_EXPORT ivec sum(const imat &m, int dim);
template ITPP_EXPORT bvec sum(const bmat &m, int dim);
template ITPP_EXPORT double sumsum(const mat &X);
template ITPP_EXPORT std::complex<double> sumsum(const cmat &X);
template ITPP_EXPORT short sumsum(const smat &X);
template ITPP_EXPORT int sumsum(const imat &X);
template ITPP_EXPORT bin sumsum(const bmat &X);
template ITPP_EXPORT vec sum_sqr(const mat & m, int dim);
template ITPP_EXPORT cvec sum_sqr(const cmat &m, int dim);
template ITPP_EXPORT svec sum_sqr(const smat &m, int dim);
template ITPP_EXPORT ivec sum_sqr(const imat &m, int dim);
template ITPP_EXPORT bvec sum_sqr(const bmat &m, int dim);
template ITPP_EXPORT mat cumsum(const mat &m, int dim);
template ITPP_EXPORT cmat cumsum(const cmat &m, int dim);
template ITPP_EXPORT smat cumsum(const smat &m, int dim);
template ITPP_EXPORT imat cumsum(const imat &m, int dim);
template ITPP_EXPORT bmat cumsum(const bmat &m, int dim);
template ITPP_EXPORT vec prod(const mat &m, int dim);
template ITPP_EXPORT cvec prod(const cmat &v, int dim);
template ITPP_EXPORT svec prod(const smat &m, int dim);
template ITPP_EXPORT ivec prod(const imat &m, int dim);
template ITPP_EXPORT bvec prod(const bmat &m, int dim);
template ITPP_EXPORT vec diag(const mat &in);
template ITPP_EXPORT cvec diag(const cmat &in);
template ITPP_EXPORT void diag(const vec &in, mat &m);
template ITPP_EXPORT void diag(const cvec &in, cmat &m);
template ITPP_EXPORT mat diag(const vec &v, const int K);
template ITPP_EXPORT cmat diag(const cvec &v, const int K);
template ITPP_EXPORT mat bidiag(const vec &, const vec &);
template ITPP_EXPORT cmat bidiag(const cvec &, const cvec &);
template ITPP_EXPORT void bidiag(const vec &, const vec &, mat &);
template ITPP_EXPORT void bidiag(const cvec &, const cvec &, cmat &);
template ITPP_EXPORT void bidiag(const mat &, vec &, vec &);
template ITPP_EXPORT void bidiag(const cmat &, cvec &, cvec &);
template ITPP_EXPORT mat tridiag(const vec &main, const vec &, const vec &);
template ITPP_EXPORT cmat tridiag(const cvec &main, const cvec &, const cvec &);
template ITPP_EXPORT void tridiag(const vec &main, const vec &, const vec &, mat &);
template ITPP_EXPORT void tridiag(const cvec &main, const cvec &, const cvec &, cmat &);
template ITPP_EXPORT void tridiag(const mat &m, vec &, vec &, vec &);
template ITPP_EXPORT void tridiag(const cmat &m, cvec &, cvec &, cvec &);
template ITPP_EXPORT double trace(const mat &in);
template ITPP_EXPORT std::complex<double> trace(const cmat &in);
template ITPP_EXPORT short trace(const smat &in);
template ITPP_EXPORT int trace(const imat &in);
template ITPP_EXPORT bin trace(const bmat &in);
template ITPP_EXPORT void transpose(const mat &m, mat &out);
template ITPP_EXPORT void transpose(const cmat &m, cmat &out);
template ITPP_EXPORT void transpose(const smat &m, smat &out);
template ITPP_EXPORT void transpose(const imat &m, imat &out);
template ITPP_EXPORT void transpose(const bmat &m, bmat &out);
template ITPP_EXPORT mat transpose(const mat &m);
template ITPP_EXPORT cmat transpose(const cmat &m);
template ITPP_EXPORT smat transpose(const smat &m);
template ITPP_EXPORT imat transpose(const imat &m);
template ITPP_EXPORT bmat transpose(const bmat &m);
template ITPP_EXPORT void hermitian_transpose(const mat &m, mat &out);
template ITPP_EXPORT void hermitian_transpose(const cmat &m, cmat &out);
template ITPP_EXPORT void hermitian_transpose(const smat &m, smat &out);
template ITPP_EXPORT void hermitian_transpose(const imat &m, imat &out);
template ITPP_EXPORT void hermitian_transpose(const bmat &m, bmat &out);
template ITPP_EXPORT mat hermitian_transpose(const mat &m);
template ITPP_EXPORT cmat hermitian_transpose(const cmat &m);
template ITPP_EXPORT smat hermitian_transpose(const smat &m);
template ITPP_EXPORT imat hermitian_transpose(const imat &m);
template ITPP_EXPORT bmat hermitian_transpose(const bmat &m);
template ITPP_EXPORT bool is_hermitian(const mat &X);
template ITPP_EXPORT bool is_hermitian(const cmat &X);
template ITPP_EXPORT bool is_unitary(const mat &X);
template ITPP_EXPORT bool is_unitary(const cmat &X);
template ITPP_EXPORT vec rvectorize(const mat &m);
template ITPP_EXPORT cvec rvectorize(const cmat &m);
template ITPP_EXPORT ivec rvectorize(const imat &m);
template ITPP_EXPORT svec rvectorize(const smat &m);
template ITPP_EXPORT bvec rvectorize(const bmat &m);
template ITPP_EXPORT vec cvectorize(const mat &m);
template ITPP_EXPORT cvec cvectorize(const cmat &m);
template ITPP_EXPORT ivec cvectorize(const imat &m);
template ITPP_EXPORT svec cvectorize(const smat &m);
template ITPP_EXPORT bvec cvectorize(const bmat &m);
template ITPP_EXPORT mat reshape(const mat &m, int rows, int cols);
template ITPP_EXPORT cmat reshape(const cmat &m, int rows, int cols);
template ITPP_EXPORT imat reshape(const imat &m, int rows, int cols);
template ITPP_EXPORT smat reshape(const smat &m, int rows, int cols);
template ITPP_EXPORT bmat reshape(const bmat &m, int rows, int cols);
template ITPP_EXPORT mat reshape(const vec &m, int rows, int cols);
template ITPP_EXPORT cmat reshape(const cvec &m, int rows, int cols);
template ITPP_EXPORT imat reshape(const ivec &m, int rows, int cols);
template ITPP_EXPORT smat reshape(const svec &m, int rows, int cols);
template ITPP_EXPORT bmat reshape(const bvec &m, int rows, int cols);
template ITPP_EXPORT mat kron(const mat &X, const mat &Y);
template ITPP_EXPORT cmat kron(const cmat &X, const cmat &Y);
template ITPP_EXPORT imat kron(const imat &X, const imat &Y);
template ITPP_EXPORT smat kron(const smat &X, const smat &Y);
template ITPP_EXPORT bmat kron(const bmat &X, const bmat &Y);
template ITPP_EXPORT vec repmat(const vec &v, int n);
template ITPP_EXPORT cvec repmat(const cvec &v, int n);
template ITPP_EXPORT ivec repmat(const ivec &v, int n);
template ITPP_EXPORT svec repmat(const svec &v, int n);
template ITPP_EXPORT bvec repmat(const bvec &v, int n);
template ITPP_EXPORT mat repmat(const vec &v, int m, int n, bool transpose);
template ITPP_EXPORT cmat repmat(const cvec &v, int m, int n, bool transpose);
template ITPP_EXPORT imat repmat(const ivec &v, int m, int n, bool transpose);
template ITPP_EXPORT smat repmat(const svec &v, int m, int n, bool transpose);
template ITPP_EXPORT bmat repmat(const bvec &v, int m, int n, bool transpose);
template ITPP_EXPORT mat repmat(const mat &data, int m, int n);
template ITPP_EXPORT cmat repmat(const cmat &data, int m, int n);
template ITPP_EXPORT imat repmat(const imat &data, int m, int n);
template ITPP_EXPORT smat repmat(const smat &data, int m, int n);
template ITPP_EXPORT bmat repmat(const bmat &data, int m, int n);
} // namespace itpp
converters.h
Definitions of converters between different vector and matrix types.
itpp::trace
T trace(const Mat< T > &m)
The trace of the matrix m, i.e. the sum of the diagonal elements.
Definition matfunc.h:762
itpp::tridiag
Mat< T > tridiag(const Vec< T > &main, const Vec< T > &sup, const Vec< T > &sub)
Returns a matrix with the elements of main on the diagonal, the elements of sup on the diagonal row a...
Definition matfunc.h:690
itpp::diag
Mat< T > diag(const Vec< T > &v, const int K=0)
Create a diagonal matrix using vector v as its diagonal.
Definition matfunc.h:557
itpp::bidiag
Mat< T > bidiag(const Vec< T > &main, const Vec< T > &sup)
Returns a matrix with the elements of the input vector main on the diagonal and the elements of the i...
Definition matfunc.h:617
itpp::kron
Mat< Num_T > kron(const Mat< Num_T > &X, const Mat< Num_T > &Y)
Computes the Kronecker product of two matrices.
Definition matfunc.h:443
itpp::sumsum
T sumsum(const Mat< T > &X)
Sum of all elements in the given matrix. Fast version of sum(sum(X))
Definition matfunc.h:101
itpp::sqrtm
ITPP_EXPORT cmat sqrtm(const cmat &A)
Square root of the complex square matrix A.
itpp::is_unitary
bool is_unitary(const Mat< Num_T > &X)
Returns true if matrix X is unitary, false otherwise.
Definition matfunc.h:355
itpp::zero_pad
Vec< T > zero_pad(const Vec< T > &v, int n)
Zero-pad a vector to size n.
Definition matfunc.h:257
itpp::cumsum
Vec< T > cumsum(const Vec< T > &v)
Cumulative sum of all elements in the vector.
Definition matfunc.h:157
itpp::sum
T sum(const Vec< T > &v)
Sum of all elements in the vector.
Definition matfunc.h:59
itpp::is_hermitian
bool is_hermitian(const Mat< Num_T > &X)
Returns true if matrix X is hermitian, false otherwise.
Definition matfunc.h:336
itpp::transpose
void transpose(const Mat< T > &m, Mat< T > &out)
Transposition of the matrix m returning the transposed matrix in out.
Definition matfunc.h:308
itpp::sum_sqr
T sum_sqr(const Vec< T > &v)
Sum of square of the elements in a vector.
Definition matfunc.h:116
itpp::length
int length(const Vec< T > &v)
Length of vector.
Definition matfunc.h:51
itpp::hermitian_transpose
void hermitian_transpose(const Mat< T > &m, Mat< T > &out)
Definition matfunc.h:318
itpp::prod
T prod(const Vec< T > &v)
The product of all elements in the vector.
Definition matfunc.h:195
itpp::cross
Vec< T > cross(const Vec< T > &v1, const Vec< T > &v2)
Vector cross product. Vectors need to be of size 3.
Definition matfunc.h:240
itpp::schur
bool schur(const mat &A, mat &U, mat &T)
Schur decomposition of a real matrix.
Definition schur.cpp:104
itpp::conj
cvec conj(const cvec &x)
Conjugate of complex value.
Definition elem_math.h:226
itpp::rvectorize
Vec< T > rvectorize(const Mat< T > &m)
Row vectorize the matrix [(0,0) (0,1) ... (N-1,N-2) (N-1,N-1)].
Definition matfunc.h:789
itpp::cvectorize
Vec< T > cvectorize(const Mat< T > &m)
Column vectorize the matrix [(0,0) (1,0) ... (N-2,N-1) (N-1,N-1)].
Definition matfunc.h:803
itpp::reverse
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition matfunc.h:777
itpp::reshape
Mat< T > reshape(const Mat< T > &m, int rows, int cols)
Reshape the matrix into an rows*cols matrix.
Definition matfunc.h:822
bmat
Mat< bin > bmat
bin matrix
Definition mat.h:508
matfunc.h
Various functions on vectors and matrices - header file.
itpp
itpp namespace
Definition itmex.h:37
itpp::to_cmat
cmat to_cmat(const Mat< T > &m)
Converts a Mat<T> to cmat.
Definition converters.h:232
itpp::any
ITPP_EXPORT bool any(const bvec &testvec)
Returns true if any element is one and false otherwise.
itpp::all
ITPP_EXPORT bool all(const bvec &testvec)
Returns true if all elements are ones and false otherwise.
schur.h
Definitions of Schur decomposition functions.

Generated on Mon Jun 10 2024 11:49:18 for IT++ by Doxygen 1.9.8