useDynLib(Matrix, .registration = TRUE)


## ==== IMPORTS ========================================================

## Try to import all of the functions that we need
## (including generic functions for which we define methods),
## but not more ...

## Some of the imports are necessary for when 'Matrix'
## is loaded but not attached:
## $ R --vanilla -e 'require(methods); (M <- Matrix::Matrix(0:1, 3L, 3L)); as(M, "sparseMatrix")'

importFrom("grDevices", colorRampPalette, grey)

importFrom("graphics", image, par)

importFrom("grid", convertHeight, convertWidth, current.viewport,
           gpar, grid.rect)

importFrom("lattice", levelplot, panel.levelplot.raster)

importFrom("methods", .hasSlot, .selectSuperClasses, .slotNames,
           Arith, Compare, Complex, Logic, Math, Math2, Ops, Summary,
           as, callGeneric, callNextMethod, canCoerce, cbind2, coerce,
           extends, getClass, getClassDef, getGroupMembers,
           implicitGeneric, is, isClassDef, kronecker, new,
           packageSlot, prototype, rbind2, representation,
           setAs, setClass, setClassUnion, setGeneric, setMethod,
           setOldClass, setReplaceMethod, setValidity,
           signature, show, slot, "slot<-", slotNames, validObject)

importFrom("stats", "contrasts<-", cov2cor , diffinv, model.frame,
           rnorm, runif, symnum, terms, toeplitz, update, setNames)

importFrom("utils", capture.output, head, head.matrix, str, tail, tail.matrix)


## ==== EXPORTS ========================================================

## ---- Non-generic functions ------------------------------------------

export(.CR2RC,
       .CR2T,
       .M2diag,
       .M2sym,
       .M2tri,
       .SuiteSparse_version,
       .T2CR,
       .bdiag,
       .dense2g,
       .dense2kind,
       .dense2m,
       .dense2sparse,
       .dense2v,
       .diag.dsC,
       .diag2dense,
       .diag2sparse,
       .diagN2U,
       .diagU2N,
       .formatSparseSimple,
       .m2dense,
       .m2sparse,
       .sparse2dense,
       .sparse2g,
       .sparse2kind,
       .sparse2m,
       .sparse2v,
       .sparseDiagonal,
       .symDiagonal,
       .trDiagonal,
       .solve.dgC.chol,
       .solve.dgC.lu,
       .solve.dgC.qr,
       .tCR2RC,
       .updateCHMfactor,
       .validateCsparse,
       Diagonal,
       Hilbert,
       KhatriRao,
       Matrix,
       MatrixClass,
       T2graph,
       abIseq,
       abIseq1,
       anyDuplicatedT,
       bandSparse,
       bdiag,
       c.sparseVector, # c() dispatches on first argument only => allow method to be called explicitly if that argument may not be a sparseVector
       colScale,
       condest,
       det,
       diagN2U,
       diagU2N,
       dimScale,
       dmperm,
       drop0,
       fac2Sparse,
       fac2sparse,
       formatSpMatrix,
       formatSparseM,
       graph2T,
       invPerm,
       is.null.DN,
       isLDL,
       mat2triplet,
       nearPD,
       onenormest,
       qr2rankMatrix,
       qrR,
       printSpMatrix,
       printSpMatrix2,
       rankMatrix,
       readHB,
       readMM,
       rep2abI,
       rowScale,
       rsparsematrix,
       spMatrix,
       sparse.model.matrix,
       sparseMatrix,
       sparseVector,
       uniqTsparse)

## Deprecated since Matrix 1.5-4 {Apr 2023}
export(..2dge, .C2nC, .T2Cmat, .asmatrix, .dense2sy,
       .diag2mat, .diag2sT, .diag2tT, .dsy2dsp, .dsy2mat, .dxC2mat,
       .m2dgC, .m2lgC, .m2ngC, .m2ngCn, .m2ngTn, .n2dgT, .nC2d, .nC2l)

## Defunct since Matrix 1.3-3 {May 2021}
export(cBind, rBind)


## ---- S3 generic functions -------------------------------------------

## export() # {probably none ever}


## ---- S3 methods -----------------------------------------------------

## So that dispatch also happens inside of 'base' functions:
S3method(as.array, Matrix)
S3method(as.array, sparseVector)
S3method(as.matrix, Matrix)
S3method(as.matrix, sparseVector)
S3method(as.vector, Matrix)
S3method(as.vector, sparseVector)

## Because S4 dispatch is "hard" for c():
S3method(c, abIndex)
S3method(c, sparseVector)

## For printing return values of our summary() methods:
S3method(print, sparseSummary)
S3method(print, diagSummary)


## ---- S4 generic functions, methods ----------------------------------

## From 'Matrix' {no need to also export(); see WRE}
exportMethods("%&%",
              BunchKaufman,
              Cholesky,
              Schur,
              band,
              expand,
              expm,
              facmul,
              forceSymmetric,
              isDiagonal,
              isTriangular,
              lu,
              nnzero,
              pack,
              skewpart,
              symmpart,
              tril,
              triu,
              unpack,
              updown,
              writeMM)

## From 'base'
exportMethods("!",
              "%*%",
              "+",
	      all,
              any,
              all.equal,
              as.array,
	      as.integer,
	      as.logical,
	      as.matrix,
	      as.numeric,
	      as.vector,
	      chol,
	      chol2inv,
	      colMeans,
	      colSums,
	      crossprod,
              determinant,
              diag,
              "diag<-",
	      diff,
	      dim,
	      "dim<-",
	      dimnames,
	      "dimnames<-",
              drop,
              ## eigen, # if(.Matrix.avoiding.as.matrix)
              format,
	      is.finite,
              is.infinite,
	      is.na,
	      isSymmetric,
              kronecker,
              length,
	      mean,
	      norm,
              print,
              qr,
	      qr.R,
              qr.Q,
	      qr.coef,
	      qr.fitted,
	      qr.resid,
	      qr.qty,
	      qr.qy,
              rcond,
              rep,
              rowMeans,
	      rowSums,
              solve,
              summary,
              ## svd, # if(.Matrix.avoiding.as.matrix)
              t,
	      tcrossprod,
              unname,
	      which,
	      zapsmall)

## From 'graphics'
exportMethods(image)

## From 'methods'
exportMethods(Arith, Compare, Logic, Math, Math2, Ops, Summary,
              cbind2, coerce, rbind2, show)

## From 'stats'
exportMethods(cov2cor, toeplitz, update)

## From 'utils'
exportMethods(head, tail)


## ---- S4 CLASSES -----------------------------------------------------

exportClasses(Matrix, # and its subclasses .............................

              compMatrix,
              generalMatrix,
              triangularMatrix,
              symmetricMatrix,
              diagonalMatrix,

              denseMatrix,
              unpackedMatrix,
              packedMatrix,

              sparseMatrix,
              CsparseMatrix,
              RsparseMatrix,
              TsparseMatrix,

              nMatrix,

              ndenseMatrix,
              ngeMatrix,
              ntrMatrix,
              ntpMatrix,
              nsyMatrix,
              nspMatrix,

              nsparseMatrix,
              ngCMatrix,
              ngRMatrix,
              ngTMatrix,
              ntCMatrix,
              ntRMatrix,
              ntTMatrix,
              nsCMatrix,
              nsRMatrix,
              nsTMatrix,

              ## ndiMatrix,

              lMatrix,

              ldenseMatrix,
              lgeMatrix,
              ltrMatrix,
              ltpMatrix,
              lsyMatrix,
              lspMatrix,

              lsparseMatrix,
              lgCMatrix,
              lgRMatrix,
              lgTMatrix,
              ltCMatrix,
              ltRMatrix,
              ltTMatrix,
              lsCMatrix,
              lsRMatrix,
              lsTMatrix,

              ldiMatrix,

              iMatrix,

              ## idenseMatrix,
              ## igeMatrix,
              ## itrMatrix,
              ## itpMatrix,
              ## isyMatrix,
              ## ispMatrix,

              ## isparseMatrix,
              ## igCMatrix,
              ## igRMatrix,
              ## igTMatrix,
              ## itCMatrix,
              ## itRMatrix,
              ## itTMatrix,
              ## isCMatrix,
              ## isRMatrix,
              ## isTMatrix,
              ## idiMatrix,

              dMatrix,

              ddenseMatrix,
              dgeMatrix,
              dtrMatrix,
              dtpMatrix,
              dsyMatrix,
              dspMatrix,
              dpoMatrix,
	      dppMatrix,
              corMatrix,
              ## copMatrix,

              dsparseMatrix,
              dgCMatrix,
              dgRMatrix,
              dgTMatrix,
              dtCMatrix,
              dtRMatrix,
              dtTMatrix,
              dsCMatrix,
              dsRMatrix,
              dsTMatrix,

              ddiMatrix,

              zMatrix,

              ## zdenseMatrix,
              ## zgeMatrix,
              ## ztrMatrix,
              ## ztpMatrix,
              ## zsyMatrix,
              ## zspMatrix,

              ## zsparseMatrix,
              ## zgCMatrix,
              ## zgRMatrix,
              ## zgTMatrix,
              ## ztCMatrix,
              ## ztRMatrix,
              ## ztTMatrix,
              ## zsCMatrix,
              ## zsRMatrix,
              ## zsTMatrix,

              ## zdiMatrix,

	      indMatrix,
              pMatrix,

              MatrixFactorization, # and its subclasses ................

              LU,
              denseLU,
	      sparseLU,

              ## QR,
              ## denseQR,
              sparseQR,

              Schur,

              BunchKaufman,
	      pBunchKaufman,

              CholeskyFactorization,
              Cholesky,
	      pCholesky,
	      CHMfactor,
              CHMsuper,
              dCHMsuper,
	      nCHMsuper, # unused
	      CHMsimpl,
              dCHMsimpl,
              nCHMsimpl, # unused

              sparseVector, # and its subclasses .......................
              nsparseVector,
	      lsparseVector,
	      isparseVector,
	      dsparseVector,
	      zsparseVector,

              ## "Other" virtual {for now just unions}:
              atomicVector,
              index,
              number,
              replValue, # dispatch fails when not exported
              xsparseVector,

              ## "Other" non-virtual {for now just experimental index classes}:
              abIndex,
              rleDiff)
