// -*- 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.
//

namespace CLHEP {

inline HepDiagMatrix::HepDiagMatrix() 
   : m(0), nrow(0)
{}

inline int HepDiagMatrix::num_row() const { return nrow;}
inline int HepDiagMatrix::num_col() const  { return nrow;}
inline int HepDiagMatrix::num_size() const  { return nrow;}

inline double & HepDiagMatrix::fast(int row,int col)
{
#ifdef MATRIX_BOUND_CHECK
  if (row<1 || row>nrow || col<1 || col>nrow)
    error("Range error in HepDiagMatrix::fast()");
#endif
  if (row != col)
    error("Index error in HepDiagMatrix::fast(i,j): i != j");

  return *(m.begin()+(col-1));
}

inline const double & HepDiagMatrix::fast(int row,int col) const
{
#ifdef MATRIX_BOUND_CHECK
  if (row<1 || row>nrow || col<1 || col>nrow)
    error("Range error in HepDiagMatrix::fast()");
#endif
  if (row == col) {
     return *(m.begin()+(col-1));
  } else {
#if defined(__sun) || !defined(__GNUG__)
//
// Sun CC 4.0.1 has this bug.
//
    zero = 0;
#endif
    return zero;
  }
}

inline double & HepDiagMatrix::operator()(int row, int col)
{
   return fast(col,row);
}

inline const double & HepDiagMatrix::operator()(int row, int col) const 
{ 
   return fast(col,row);
}

inline void HepDiagMatrix::assign(const HepDiagMatrix &m2) {(*this)=m2;}

inline HepDiagMatrix HepDiagMatrix::T() const {return HepDiagMatrix(*this);}

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

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

inline double &HepDiagMatrix::HepDiagMatrix_row::operator[](int c) 
{
   return _a.fast(_r+1, c+1);
}

inline const double&
HepDiagMatrix::HepDiagMatrix_row_const::operator[](int c) const 
{
   return _a.fast(_r+1,c+1);
}

inline HepDiagMatrix::HepDiagMatrix_row::HepDiagMatrix_row
(HepDiagMatrix& a, int r) 
   : _a(a), _r(r)
{}

inline HepDiagMatrix::HepDiagMatrix_row_const::HepDiagMatrix_row_const
(const HepDiagMatrix& a, int r) 
   : _a(a), _r(r)
{}

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

}  // namespace CLHEP
