// -*- C++ -*-
// ---------------------------------------------------------------------------
//
// This file is a part of the CLHEP - a Class Library for High Energy Physics.
//
// 
// Copyright Cornell University 1993, 1996, All Rights Reserved.
// 
// This software written by Nobu Katayama and Mike Smyth, Cornell University.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice and author attribution, this list of conditions and the
//    following disclaimer. 
// 2. Redistributions in binary form must reproduce the above copyright
//    notice and author attribution, this list of conditions and the
//    following disclaimer in the documentation and/or other materials
//    provided with the distribution.
// 3. Neither the name of the University nor the names of its contributors
//    may be used to endorse or promote products derived from this software
//    without specific prior written permission.
// 
// Creation of derivative forms of this software for commercial
// utilization may be subject to restriction; written permission may be
// obtained from Cornell University.
// 
// CORNELL MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  By way
// of example, but not limitation, CORNELL MAKES NO REPRESENTATIONS OR
// WARRANTIES OF MERCANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT
// THE USE OF THIS SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS,
// COPYRIGHTS, TRADEMARKS, OR OTHER RIGHTS.  Cornell University shall not be
// held liable for any liability with respect to any claim by the user or any
// other party arising from use of the program.
//
// This is the definitions of the inline member functions of the
// HepMatrix class
//

namespace CLHEP {

inline HepMatrix::HepMatrix()
  : m(0), nrow(0), ncol(0), size(0) {}

inline HepMatrix::HepMatrix_row HepMatrix::operator[] (int r)
#ifdef HEP_GNU_OPTIMIZED_RETURN
  return b(*this,r);
{
#else
{
  HepMatrix_row b(*this,r);
#endif
  return b;
}

inline const HepMatrix::HepMatrix_row_const HepMatrix::operator[] (int r) const
#ifdef HEP_GNU_OPTIMIZED_RETURN
  return b(*this,r);
{
#else
{
  HepMatrix_row_const b(*this,r);
#endif
  return b;
}

inline double &HepMatrix::HepMatrix_row::operator[](int c) {
#ifdef MATRIX_BOUND_CHECK
  if (_r<0 || _r>=_a.num_row() || c<0 || c>=_a.num_col())
    HepGenMatrix::error("Range error in HepMatrix::operator[][]");
#endif
  return *(_a.m.begin()+_r*_a.ncol+c);
}

inline const double &HepMatrix::HepMatrix_row_const::operator[](int c) const
{
#ifdef MATRIX_BOUND_CHECK
  if (_r<0 || _r>=_a.num_row() || c<0 || c>=_a.num_col())
    HepGenMatrix::error("Range error in HepMatrix::operator[][]");
#endif
  return *(_a.m.begin()+_r*_a.ncol+c);
}

inline HepMatrix::HepMatrix_row::HepMatrix_row(HepMatrix&a,int r) 
: _a(a) {
  _r = r;
}

inline HepMatrix::HepMatrix_row_const::HepMatrix_row_const 
(const HepMatrix&a, int r) 
   : _a(a) 
{
  _r = r;
}

// This function swaps two Matrices without doing a full copy.
inline void swap(HepMatrix &m1,HepMatrix &m2) {
  HepGenMatrix::swap(m1.m,m2.m);
/*** commented
  HepGenMatrix::swap(m1.nrow,m2.nrow);
  HepGenMatrix::swap(m1.ncol,m2.ncol);
  HepGenMatrix::swap(m1.size,m2.size);
*/
}

  /*-ap inline */ HepMatrix HepMatrix::inverse(int &ierr) const
#ifdef HEP_GNU_OPTIMIZED_RETURN
  return mTmp(*this);
{
#else
{
  HepMatrix mTmp(*this);
#endif
  mTmp.invert(ierr);
  return mTmp;
}

}  // namespace CLHEP

