/*
    DFT++ is a density functional package developed by the research group
    of Professor Tomas Arias

    Copyright 1996-2003 Sohrab Ismail-Beigi

    This file is part of DFT++.

    DFT++ is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DFT++ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DFT++; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Please see the file CREDITS for a list of authors.

    For academic users, we request that publications using results obtained with
    this software reference

    "New algebraic formulation of density functional calculation," by Sohrab Ismail-Beigi
    and T.A. Arias, Computer Physics Communications 128:1-2, 1-45 (June 2000).

    and, if using the wavelet basis, further reference

    "Multiresolution analysis of electronic structure: semicardinal and wavelet bases,"
    T.A. Arias, Reviews of Modern Physics 71:1, 267-311 (January 1999).

    and 

    "Robust ab initio calculation of condensed matter: transparent convergence through
    semicardinal multiresolution analysis,'' I.P. Daykov, T.A. Arias, and
    Torkel D. Engeness, Physical Review Letters, 90:21, 216402 (May 2003).

    For your convenience, preprints of the above articles may be obtained from
    http://arXiv.org/abs/cond-mat/9909130, 9805262, and 0204411, respectively.
*/

/*--------------------------  ComplexMatrix  ----------------------------*
 *                                                                       *
 * class: ComplexMatrix whose elements are scalars (ComplexMatrix.c)     *
 *                                                                       *
 *-----------------------------------------------------------------------*/
#ifndef DFT_COMPLEXMATRIX_H
#define DFT_COMPLEXMATRIX_H

class ComplexMatrix
{
  // data
public:
  int nr,nc;    /* Number of rows and columns */
  complex *c;    /* Holds the data */
  int hermetian;

  /* Constructors and destructor */
  ComplexMatrix(int nrows=0,int ncols=0);
  ComplexMatrix(const ComplexMatrix &m1); /* copy constructor */
  ~ComplexMatrix();
  
  /* Operators */
  void operator=(const ComplexMatrix &m1);    /* Nonstandard: returns void */
  inline scalar &operator()(int i,int j) const
    { return c[nc*i+j]; }
  friend ComplexMatrix operator+(const ComplexMatrix &m1,const ComplexMatrix &m2);
  friend ComplexMatrix operator-(const ComplexMatrix &m1,const ComplexMatrix &m2);
  friend ComplexMatrix operator*(const ComplexMatrix &m1,const ComplexMatrix &m2);
  friend ComplexMatrix operator*(complex s,const ComplexMatrix &m);
  friend ComplexMatrix operator*(const ComplexMatrix &m, complex s);
  friend ComplexMatrix operator*(real s,const ComplexMatrix &m);
  friend ComplexMatrix operator*(const ComplexMatrix &m, real s);
  friend ComplexMatrix operator*(const diag_matrix &d, const ComplexMatrix &m);
  friend ComplexMatrix operator*(const ComplexMatrix &m,const diag_matrix &d);
  void operator*=(scalar s);
  void operator+=(const ComplexMatrix &m);
  void operator-=(const ComplexMatrix &m);
  
  /* member function: */
  /* Does the memory allocations of the constructor */
  void init(int nrows,int ncols);
  /* Free up memory */
  void freemem(void);
  /* Binary read/write ComplexMatrix to/from fname */
  // to fully comply with MPI standard, read and  write also need to be
  // parallelized.
  void write(char *fname);
  void write(FILE *fp);
  void read(char *fname);
  void read(FILE *fp);
  /* zero out all the entries */
  void zero_out(void);
  void print();
  void printe(); // print in exp format
};

/* Allocate/free an array of matrices */
ComplexMatrix **alloc_Matrix_array(int nmats,int nrows,int ncols);
void free_Matrix_array(int nmats,ComplexMatrix **M);

/* Read/write an array of matrices from/to a file */
void read_ComplexMatrix_array(char *fname,int nmatrices,ComplexMatrix *M);
void write_ComplexMatrix_array(char *fname,int nmatrices,ComplexMatrix *M);

/* Trace and diagonal of ComplexMatrix */
scalar trace(const ComplexMatrix &m);
diag_matrix diag(const ComplexMatrix &m);

/* Diagonalization routines, etc. */
void diagonalize_herm(real *eigs,ComplexMatrix &evecs,ComplexMatrix &a,int n);
ComplexMatrix herm_adjoint(ComplexMatrix &a);
ComplexMatrix Uminusonehalf(ComplexMatrix &U,ComplexMatrix &W,real *u);
ComplexMatrix Q(const ComplexMatrix &G,ComplexMatrix &W,real *mu);
ComplexMatrix R(const ComplexMatrix &A,ComplexMatrix &Z,real *beta);
void scale_accumulate(scalar s,ComplexMatrix &min,ComplexMatrix &mout);

void scale_accumulate(int nmat,scalar s,ComplexMatrix **min,ComplexMatrix **mout);
void scaled_sum(scalar s1,ComplexMatrix &m1,scalar s2,ComplexMatrix &m2,ComplexMatrix &mout);
void scaled_sum(int nmat,
		scalar s1,ComplexMatrix **m1,scalar s2,
                ComplexMatrix **m2,ComplexMatrix **mout);
real abs2(const ComplexMatrix &m);
real abs2(int nmat,ComplexMatrix **m);
scalar dot(ComplexMatrix &A,ComplexMatrix &B);
scalar dot(int nmat,ComplexMatrix **A,ComplexMatrix **B);

#endif // DFT_COMPLEXMATRIX_H
