# Path: nrgmap/tridiagonalize.py
# def construct_tridmat(data,offset):
#     '''
#     Construct tridiagonal matrix.
# 
#     Parameters:
#         :data: The datas of lower, middle, upper tridiagonal part.
#         :offset: The offsets indicating the position of datas.
# 
#     Return:
#         2D sparse matrix, use res.toarray() to get a dense array.
#     '''
#     n=len(data[1])
#     if ndim(data[1])==1:
#         p=1
#     else:
#         p=len(data[1][0])
#     N=n*p
#     B=ndarray([n,n],dtype='O')
#     #fill datas
#     for i in range(n):
#         for j in range(n):
#             for k in range(3):
#                 if j-i==offset[k]:
#                     B[i,j]=complex128(data[offset[k]+1][min(i,j)])
#     B=sps.bmat(B)
#     return B


from numpy import *
from scipy import sparse as sps
from .tridiagonalize import construct_tridmat
import warnings
            e.g. for multi-band, it's (# of sites, degree of freedom, degree of freedom).
                 for single-band, it's (# of sites).
        2. <file_prefix>.el.dat, an array of on-site energies, in the format(2-band,2-site as an example)
            | E0[0,0].real E0[0,0].imag
            | E0[0,1].real E0[0,1].imag
            | E0[1,0].real E0[1,0].imag
            | E0[0,1].real E0[0,1].imag
            | E1[0,0].real E1[0,0].imag
            |          ... ...
        3. <file_prefix>.tl.dat, an array of hopping terms with the first element t0 the coupling strength with impurity.
            Its data format is similar to `el.dat`.
        '''
        savetxt(file_prefix+'.info.dat',array(self.elist.shape))
        savetxt(file_prefix+'.el.dat',self.elist.ravel().view('float64'))
        savetxt(file_prefix+'.tl.dat',self.tlist.ravel().view('float64'))

    def get_H0(self,e0):
        '''
        Hamiltonian without interaction terms.

        Return:
            sparse matrix, the hamiltonian.
        '''
        tlist=self.tlist
        elist=self.elist
        N=self.nsite+1
        B=ndarray([N,N],dtype='O')
        elist=concatenate([e0[newaxis,...],elist],axis=0)
        tlistH=swapaxes(tlist,1,2).conj()
        offset=[-1,0,1]

# Path: nrgmap/tridiagonalize.py
# def construct_tridmat(data,offset):
#     '''
#     Construct tridiagonal matrix.
# 
#     Parameters:
#         :data: The datas of lower, middle, upper tridiagonal part.
#         :offset: The offsets indicating the position of datas.
# 
#     Return:
#         2D sparse matrix, use res.toarray() to get a dense array.
#     '''
#     n=len(data[1])
#     if ndim(data[1])==1:
#         p=1
#     else:
#         p=len(data[1][0])
#     N=n*p
#     B=ndarray([n,n],dtype='O')
#     #fill datas
#     for i in range(n):
#         for j in range(n):
#             for k in range(3):
#                 if j-i==offset[k]:
#                     B[i,j]=complex128(data[offset[k]+1][min(i,j)])
#     B=sps.bmat(B)
#     return B


# Path: nrgmap/chain.py
from numpy import *
from scipy import sparse as sps
from .tridiagonalize import construct_tridmat
import warnings

            e.g. for multi-band, it's (# of sites, degree of freedom, degree of freedom).
                 for single-band, it's (# of sites).
        2. <file_prefix>.el.dat, an array of on-site energies, in the format(2-band,2-site as an example)
            | E0[0,0].real E0[0,0].imag
            | E0[0,1].real E0[0,1].imag
            | E0[1,0].real E0[1,0].imag
            | E0[0,1].real E0[0,1].imag
            | E1[0,0].real E1[0,0].imag
            |          ... ...
        3. <file_prefix>.tl.dat, an array of hopping terms with the first element t0 the coupling strength with impurity.
            Its data format is similar to `el.dat`.
        '''
        savetxt(file_prefix+'.info.dat',array(self.elist.shape))
        savetxt(file_prefix+'.el.dat',self.elist.ravel().view('float64'))
        savetxt(file_prefix+'.tl.dat',self.tlist.ravel().view('float64'))

    def get_H0(self,e0):
        '''
        Hamiltonian without interaction terms.

        Return:
            sparse matrix, the hamiltonian.
        '''
        tlist=self.tlist
        elist=self.elist
        N=self.nsite+1
        B=ndarray([N,N],dtype='O')
        elist=concatenate([e0[newaxis,...],elist],axis=0)
        tlistH=swapaxes(tlist,1,2).conj()
        offset=[-1,0,1]

        return construct_tridmat([tlist,elist,tlistH],offset).toarray()