/*---------------------------------------------------------------------------* * IT++ * *---------------------------------------------------------------------------* * Copyright (c) 1995-2004 by Tony Ottosson, Thomas Eriksson, Pål Frenger, * * Tobias Ringström, and Jonas Samuelsson. * * * * Permission to use, copy, modify, and distribute this software and its * * documentation under the terms of the GNU General Public License is hereby * * granted. No representations are made about the suitability of this * * software for any purpose. It is provided "as is" without expressed or * * implied warranty. See the GNU General Public License for more details. * *---------------------------------------------------------------------------*/ /*! \file \brief Implementation of special vectors and matrices. \author Tony Ottosson, Tobias Ringström and Pål Frenger 1.12 2004/09/30 11:53:30 */ #include "base/specmat.h" #include "base/elmatfunc.h" #include "base/matfunc.h" #include "base/binary.h" #include "base/stat.h" namespace itpp { ivec find(const bvec &invector) { assert(invector.size()>0); ivec temp(invector.size()); int pos=0; for (int i=0;i(1.0)) CREATE_SET_FUNS( vec, mat, zeros, 0.0) CREATE_SET_FUNS( bvec, bmat, zeros_b, bin(0)) CREATE_SET_FUNS( ivec, imat, zeros_i, 0) CREATE_SET_FUNS( cvec, cmat, zeros_c, complex(0.0)) CREATE_EYE_FUN(mat, eye, 0.0, 1.0) CREATE_EYE_FUN(bmat, eye_b, bin(0), bin(1)) CREATE_EYE_FUN(imat, eye_i, 0, 1) CREATE_EYE_FUN(cmat, eye_c, complex(0.0), complex(1.0)) template void eye(int size, Mat &m) { m.set_size(size, size, false); m = T(0); for (int i=size-1; i>=0; i--) m(i,i) = T(1); } #endif //DOXYGEN_SHOULD_SKIP_THIS vec impulse(int size) { vec t(size); t.clear(); t[0]=1.0; return t; } vec linspace(double from, double to, int points) { if (points<2){ // This is the "Matlab definition" of linspace vec output(1); output(0)=to; return output; } else{ vec output(points); double step = (to - from) / double(points-1); int i; for (i=0; i cmat toeplitz(const cvec &c, const cvec &r) { int size = c.size(); it_assert(size == r.size(), "toeplitz(): Incorrect sizes of input vectors."); cmat output(size, size); cvec c_conj = conj(c); c_conj[0] = conj(c_conj[0]); for (int i = 0; i < size; i++) { cmat tmp = reshape(c_conj(0, size - 1 - i), size - i, 1); output.set_submatrix(i, size - 1, i, i, tmp); } for (int i = 0; i < size - 1; i++) { cmat tmp = reshape(r(1, size - 1 - i), 1, size - 1 - i); output.set_submatrix(i, i, i + 1, size - 1, tmp); } return output; } // Adam Piatyszek cmat toeplitz(const cvec &c) { int size = c.size(); cmat output(size, size); cvec c_conj = conj(c); c_conj[0] = conj(c_conj[0]); for (int i = 0; i < size; i++) { cmat tmp = reshape(c_conj(0, size - 1 - i), size - i, 1); output.set_submatrix(i, size - 1, i, i, tmp); } for (int i = 0; i < size - 1; i++) { cmat tmp = reshape(c(1, size - 1 - i), 1, size - 1 - i); output.set_submatrix(i, i, i + 1, size - 1, tmp); } return output; } mat rotation_matrix(int dim, int plane1, int plane2, double angle) { mat m; double c = std::cos(angle), s = std::sin(angle); it_assert(plane1>=0 && plane2>=0 && plane1 fabs(a)) { t = -a/b; s = -1.0 / std::sqrt(1 + t*t); c = s * t; } else { t = -b/a; c = 1.0 / std::sqrt(1 + t*t); s = c * t; } } } void givens(double a, double b, mat &m) { double t, c, s; m.set_size(2,2); if (b == 0) { m(0,0) = 1.0; m(1,1) = 1.0; m(1,0) = 0.0; m(0,1) = 0.0; } else { if (fabs(b) > fabs(a)) { t = -a/b; s = -1.0 / std::sqrt(1 + t*t); c = s * t; } else { t = -b/a; c = 1.0 / std::sqrt(1 + t*t); s = c * t; } m(0,0) = c; m(1,1) = c; m(0,1) = s; m(1,0) = -s; } } mat givens(double a, double b) { mat m(2,2); givens(a, b, m); return m; } void givens_t(double a, double b, mat &m) { double t, c, s; m.set_size(2,2); if (b == 0) { m(0,0) = 1.0; m(1,1) = 1.0; m(1,0) = 0.0; m(0,1) = 0.0; } else { if (fabs(b) > fabs(a)) { t = -a/b; s = -1.0 / std::sqrt(1 + t*t); c = s * t; } else { t = -b/a; c = 1.0 / std::sqrt(1 + t*t); s = c * t; } m(0,0) = c; m(1,1) = c; m(0,1) = -s; m(1,0) = s; } } mat givens_t(double a, double b) { mat m(2,2); givens_t(a, b, m); return m; } //! Template instantiation of eye template void eye(int, mat &); //! Template instantiation of eye template void eye(int, bmat &); //! Template instantiation of eye template void eye(int, imat &); //! Template instantiation of eye template void eye(int, cmat &); } //namespace itpp