1 #ifndef ALGO_BLAST_GUMBEL_PARAMS__INCLUDED_NJN_MATRIX
2 #define ALGO_BLAST_GUMBEL_PARAMS__INCLUDED_NJN_MATRIX
60 static inline Matrix <T> *
matrix (
size_t k_,
size_t m_,
size_t n_,
T a_ =
static_cast <T> (0));
72 inline Matrix (
size_t m_,
size_t n_,
const T *vector_);
73 inline Matrix (
size_t m_,
size_t n_,
T a_ =
static_cast <T> (0));
77 virtual inline void *
create (
bool isCopy_ =
false)
const;
78 virtual inline void copy (
size_t m_,
size_t n_,
const T *
const *matrix_);
79 virtual inline void copy (
size_t m_,
size_t n_,
const T *vector_);
80 virtual inline void copy (
size_t m_,
size_t n_,
T a_ =
static_cast <T> (0));
82 virtual inline operator bool ()
const;
84 virtual inline std::ostream &
out (std::ostream &ostr_)
const;
85 virtual inline std::istream &
in (std::istream &istr_);
94 virtual inline size_t getM ()
const {
return d_m;}
95 virtual inline size_t getN ()
const {
return d_n;}
106 virtual inline void init (
size_t m_,
size_t n_);
107 virtual inline void free ();
128 template <
typename T>
129 bool operator== (
const Njn::Matrix <T> &matrix_,
const Njn::Matrix <T> &matrix0_);
131 template <
typename S,
typename T>
132 void copy (Njn::Matrix <S> *matrix_,
const Njn::Matrix <T> &matrix0_);
134 template <
typename T>
135 std::ostream &
operator<< (std::ostream &ostr_,
const Njn::Matrix <T> &matrix_);
137 template <
typename T>
138 std::istream &
operator>> (std::istream &istr_, Njn::Matrix <T> &matrix_);
149 template <
typename T>
157 for (
size_t i = 0;
i < k_;
i++)
158 matrix [
i].
copy (m_, n_, a_);
163 template <
typename T>
166 assert (x_.getM () == y_.getM ());
167 assert (x_.getN () == y_.getN ());
169 for (
size_t i = 0;
i < x_.getM ();
i++) {
170 for (
size_t j = 0; j < x_.getN (); j++) {
178 template <
typename T>
181 assert (x_.getM () == y_.getM ());
182 assert (x_.getN () == y_.getN ());
184 for (
size_t i = 0;
i < x_.getM ();
i++) {
185 for (
size_t j = 0; j < x_.getN (); j++) {
193 template <
typename T>
196 assert (x_.getM () == y_.getM ());
197 assert (x_.getN () == y_.getN ());
199 for (
size_t i = 0;
i < x_.getM ();
i++) {
200 for (
size_t j = 0; j < x_.getN (); j++) {
208 template <
typename T>
211 if (x_.getM () != x_.getN ())
return false;
213 for (
size_t i = 0;
i < x_.getM ();
i++) {
214 for (
size_t j = 0; j <
i; j++) {
215 if (x_ [
i][j] != x_ [j][
i])
return false;
222 template <
typename T>
224 : d_m (0), d_n (0), d_matrix_p (0), d_value (0)
227 template <
typename T>
229 : d_m (0), d_n (0), d_matrix_p (0), d_value (0)
234 template <
typename T>
239 : d_m (0), d_n (0), d_matrix_p (0), d_value (0)
241 copy (m_, n_, vector_);
244 template <
typename T>
249 : d_m (0), d_n (0), d_matrix_p (0), d_value (0)
254 template <
typename T>
257 template <
typename T>
260 if (
this != &matrix_)
262 copy (matrix_.getM (), matrix_.getN (), matrix_.getMatrix ());
263 this->setValue () = matrix_.getValue ();
268 template <
typename T>
272 if (isCopy_) matrix->operator= (*this);
276 template <
typename T>
279 template <
typename T>
296 for (
i = 0;
i < getM ();
i++) {
298 if (
i != 0) ostr_ << endl;
300 for (
size_t j = 0; j < getN (); j++) {
302 if (j != 0) ostr_ <<
'\t';
303 ostr_ << getMatrix () [
i][j];
312 ostr_ << getM () <<
"\t! row dimension of matrix\n";
313 ostr_ << getN () <<
"\t! column dimension of matrix\n";
315 for (
i = 0;
i < getM ();
i++) {
317 if (
i != 0) ostr_ <<
"\t! matrix elements\n";
319 for (j = 0; j < getN (); j++) {
321 if (j != 0) ostr_ <<
'\t';
322 ostr_ << getMatrix () [
i][j];
326 ostr_ <<
"\t! matrix elements";
333 if (! isSymmetric (*
this))
IoUtil::abort (
"Matrix::out : matrix is not symmetric");
335 for (
i = 0;
i < getM ();
i++)
337 if (
i != 0) ostr_ << endl;
339 for (
size_t j =
i; j < getN (); j++)
341 if (j !=
i) ostr_ <<
'\t';
342 ostr_ << getMatrix () [
i][j];
346 else IoUtil::abort (
"Matrix::out : impossible MatrixIO::getFormat ()");
354 template <
typename T>
369 stringstream sstream;
372 T value = this->getValue ();
386 if (! sstream.eof ())
IoUtil::abort (
"Njn::Matrix::in : bad value for the MACHINE");
389 if (
n == 0)
IoUtil::abort (
"Njn::Matrix::in : bad n for the MACHINE");
392 if (! sstream.eof ())
IoUtil::abort (
"Njn::Matrix::in : bad value for the MACHINE");
395 if (m *
n != v.size ())
IoUtil::abort (
"Njn::Matrix::in : rows for the MACHINE have different lengths.");
397 if (m != this->getM () ||
n != this->getN ()) {
403 for (
i = 0;
i < m;
i++) {
405 for (j = 0; j <
n; j++) d_matrix_p [
i][j] = v [k++];
414 stringstream sstream;
421 if (sstream.fail ())
IoUtil::abort (
"Njn::Matrix::in : bad m");
428 if (sstream.fail ())
IoUtil::abort (
"Njn::Matrix::in : bad n");
430 if (m != this->getM () ||
n != this->getN ()) {
436 for (
i = 0;
i < this->getM ();
i++) {
443 for (j = 0; j < this->getN (); j++) {
445 sstream >> d_matrix_p [
i][j];
447 if (sstream.fail ()) {
449 ostringstream sistream;
451 ostringstream sjstream;
453 IoUtil::abort (
"Njn::Matrix::in : bad d_matrix_p [" + sistream.str () +
"][" + sjstream.str () +
"]");
467 while (sstream >>
value)
471 if (! sstream.eof ())
IoUtil::abort (
"Njn::Matrix::in : bad value for the MatrixIO::SYMMETRIC");
474 if (
n == 0)
IoUtil::abort (
"Njn::Matrix::in : bad n for the MatrixIO::SYMMETRIC");
477 if (! istr_.eof ())
IoUtil::abort (
"Njn::Matrix::in : bad value for the MatrixIO::SYMMETRIC");
480 if (m * (m + 1) / 2 != v.size ())
IoUtil::abort (
"Njn::Matrix::in : rows for the MatrixIO::SYMMETRIC have incorrect lengths.");
482 if (m != this->getM () ||
n != this->getN ()) {
488 for (
i = 0;
i < m;
i++)
490 for (j = 0; j <
i; j++) d_matrix_p [
i][j] = d_matrix_p [j][
i];
491 for (j =
i; j <
n; j++) d_matrix_p [
i][j] = v [k++];
494 else IoUtil::abort (
"Matrix::in : impossible MatrixIO::getFormat ()");
502 template <
typename T>
505 copy (this->getM (), this->getN (), a_);
509 template <
typename T>
512 return d_matrix_p [i_];
515 template <
typename T>
518 return d_matrix_p [i_];
521 template <
typename T>
525 const T *
const *matrix_)
527 if (m_ != this->getM () || n_ != this->getN ()) {
532 for (
size_t i = 0;
i < this->getM ();
i++)
534 for (
size_t j = 0; j < this->getN (); j++)
536 d_matrix_p [
i][j] = matrix_ [
i][j];
541 template <
typename T>
547 if (m_ != this->getM () || n_ != this->getN ()) {
553 for (
size_t i = 0;
i < this->getM ();
i++)
555 for (
size_t j = 0; j < this->getN (); j++)
557 d_matrix_p [
i][j] = vector_ [k++];
562 template <
typename T>
568 if (m_ != this->getM () || n_ != this->getN ())
574 for (
size_t i = 0;
i < this->getM ();
i++)
576 for (
size_t j = 0; j < this->getN (); j++)
578 d_matrix_p [
i][j] = a_;
583 template <
typename T>
586 if (m_ > 0) d_matrix_p =
new T* [m_];
588 for (
size_t i = 0;
i < m_;
i++)
590 d_matrix_p [
i] =
new T [n_];
597 template <
typename T>
600 for (
size_t i = 0;
i < this->getM ();
i++) {
601 delete [] d_matrix_p [
i]; d_matrix_p [
i] = 0;
603 if (this->getM () > 0)
delete [] d_matrix_p;
612 template <
typename S,
typename T>
613 void copy (Njn::Matrix <S> *matrix_,
const Njn::Matrix <T> &matrix0_)
615 matrix_->copy (matrix0_.getM (), matrix0_.getN ());
617 for (
size_t i = 0;
i < matrix0_.getM ();
i++) {
618 for (
size_t j = 0; j < matrix0_.getN (); j++) {
619 (*matrix_) [
i][j] =
static_cast <S> (matrix0_ [
i][j]);
625 template <
typename T>
626 bool operator== (
const Njn::Matrix <T> &matrix_,
const Njn::Matrix <T> &matrix0_)
628 if (matrix_.getM () != matrix0_.getM ())
return false;
629 if (matrix_.getN () != matrix0_.getN ())
return false;
630 for (
size_t i = 0;
i < matrix_.getM ();
i++) {
631 for (
size_t j = 0; j < matrix_.getN (); j++) {
632 if (matrix_.getMatrix () [
i][j] != matrix0_.getMatrix () [
i][j])
return false;
639 template <
typename T>
640 std::ostream &
operator<< (std::ostream &ostr_,
const Njn::Matrix <T> &matrix_)
641 {
return matrix_.out (ostr_);}
643 template <
typename T>
644 std::istream &
operator>> (std::istream &istr_, Njn::Matrix <T> &matrix_)
645 {
return matrix_.in (istr_);}
static bool absRelApprox(const Matrix< T > &x_, const Matrix< T > &y_, T tol_, T rtol_)
virtual void * create(bool isCopy_=false) const
virtual T * operator[](size_t i_)
virtual const T getValue() const
virtual size_t getN() const
virtual const T *const * getMatrix() const
static bool approx(const Matrix< T > &x_, const Matrix< T > &y_, T eps_)
static bool relApprox(const Matrix< T > &x_, const Matrix< T > &y_, T eps_)
virtual std::istream & in(std::istream &istr_)
virtual std::ostream & out(std::ostream &ostr_) const
virtual void copy(size_t m_, size_t n_, const T *const *matrix_)
static bool isSymmetric(const Matrix< T > &x_)
Matrix & operator=(const Matrix &matrix_)
static Matrix< T > * matrix(size_t k_, size_t m_, size_t n_, T a_=static_cast< T >(0))
virtual void init(size_t m_, size_t n_)
virtual size_t getM() const
static void DLIST_NAME() init(DLIST_LIST_TYPE *list)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define USING_SCOPE(ns)
Use the specified namespace.
#define END_SCOPE(ns)
End the previously defined scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
#define BEGIN_SCOPE(ns)
Define a new scope.
const GenericPointer< typename T::ValueType > T2 value
Defines Limits for the types used in NCBI C/C++ toolkit.
bool relApprox(T x_, T y_, T eps_)
bool approx(T x_, T y_, T eps_)
bool absRelApprox(T x_, T y_, T tol_, T rtol_)
std::istream & getLine(std::istream &in_, std::string &str_, const char t_=getTerminator())
void copy(Njn::Matrix< S > *matrix_, const Njn::Matrix< T > &matrix0_)
bool operator==(const Njn::Matrix< T > &matrix_, const Njn::Matrix< T > &matrix0_)
std::istream & operator>>(std::istream &istr_, Njn::Matrix< T > &matrix_)
void setFormat(Format format_)
std::ostream & operator<<(std::ostream &ostr_, const Njn::Matrix< T > &matrix_)