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

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, getClassDef, getGroupMembers,
           is, isClassDef, kronecker, new, rbind2,
           setAs, setClass, setClassUnion, setGeneric, setMethod,
           setOldClass, show, slot, "slot<-", slotNames, validObject)

importFrom("stats", contr.SAS, contr.helmert, contr.poly, contr.sum,
           contr.treatment, "contrasts<-", cov2cor , diffinv, model.frame,
           rnorm, runif, symnum, terms, toeplitz, update)

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


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

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

export(.M2C,
       .M2R,
       .M2T,
       .M2V,
       .M2diag,
       .M2gen,
       .M2kind,
       .M2m,
       .M2packed,
       .M2sym,
       .M2tri,
       .M2unpacked,
       .M2v,
       .bdiag,
       .dense2sparse,
       .diag.dsC,
       .diag2dense,
       .diag2sparse,
       .diagN2U,
       .diagU2N,
       .ind2dense,
       .ind2sparse,
       .formatSparseSimple,
       .m2V,
       .m2dense,
       .m2sparse,
       .sparse2dense,
       .sparseDiagonal,
       .symDiagonal,
       .trDiagonal,
       .solve.dgC.chol,
       .solve.dgC.lu,
       .solve.dgC.qr,
       .tCRT,
       .updateCHMfactor,
       .validateCsparse,
       Diagonal,
       Hilbert,
       KhatriRao,
       Matrix,
       Matrix.Version,
       MatrixClass,
       T2graph,
       abIseq,
       abIseq1,
       aggregateT,
       anyDuplicatedT,
       asPerm,
       asUniqueT,
       bandSparse,
       bdiag,
       ## 'c' dispatches on first argument only, so allow direct method calls :
       ## c.Matrix, # not yet (see below)
       c.sparseVector,
       colScale,
       condest,
       det,
       diagN2U,
       diagU2N,
       dimScale,
       dmperm,
       drop0,
       fac2Sparse,
       fac2sparse,
       formatSpMatrix,
       formatSparseM,
       graph2T,
       invPerm,
       invertPerm,
       is.null.DN,
       isLDL,
       isPerm,
       isUniqueT,
       mat2triplet,
       nearPD,
       onenormest,
       qr2rankMatrix,
       qrR,
       printSpMatrix,
       printSpMatrix2,
       rankMatrix,
       readHB,
       readMM,
       rep2abI,
       rowScale,
       rsparsematrix,
       signPerm,
       spMatrix,
       sparse.model.matrix,
       sparseMatrix,
       sparseVector)

## 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)

## Redundant now but not yet deprecated ...
export(.CR2RC, .CR2T, .SuiteSparse_version, .T2CR, .dense2g, .dense2kind,
       .dense2m, .dense2v, .sparse2g, .sparse2kind, .sparse2m, .sparse2v,
       .tCR2RC, uniqTsparse)


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

## export() # {probably none ever}


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

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

## Because S4 dispatch is "hard" for c():
## S3method(c, Matrix) # breaks 7 rev. dep. {2023-09-08}
S3method(c, sparseVector)
S3method(c, abIndex)

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


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

export(crossprod, tcrossprod) # *necessary* (once .Primitive in base)
## MJ: why these and not also export(dim, ...) which are also primitive ??

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

## From 'base'  --- several of these are  implicitGeneric
exportMethods("!",
              "%*%",
              "+",
              all.equal,
              as.array,
              as.complex,
              as.integer,
              as.logical,
              as.matrix,
              as.numeric,
              as.vector,
              chol,
              chol2inv,
              colMeans,
              colSums,
              crossprod,
              determinant,
              diag,
              "diag<-",
              diff,
              dim,
              "dim<-",
              dimnames,
              "dimnames<-",
              drop,
              format,
              is.finite,
              is.infinite,
              is.na,
              isSymmetric,
              kronecker,
              length,
              mean,
              norm,
              print,
              qr,
              qr.Q,
              qr.R,
              qr.X,
              qr.coef,
              qr.fitted,
              qr.resid,
              qr.qty,
              qr.qy,
              rcond,
              rep,
              rowMeans,
              rowSums,
              solve,
              summary,
              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 .............................

              generalMatrix,
              symmetricMatrix,
              triangularMatrix,
              diagonalMatrix,

              denseMatrix,
              unpackedMatrix,
              packedMatrix,

              sparseMatrix,
              CsparseMatrix,
              RsparseMatrix,
              TsparseMatrix,

              nMatrix,

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

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

              ndiMatrix,

              lMatrix,

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

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

              ldiMatrix,

              iMatrix,

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

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

              ## idiMatrix,

              dMatrix,

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

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

              ddiMatrix,

              zMatrix,

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

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

              ## zdiMatrix,

              indMatrix,
              pMatrix,

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

              LU,
              denseLU,
              sparseLU,

              QR,
              ## denseQR,
              sparseQR,

              SchurFactorization,
              Schur,

              BunchKaufmanFactorization,
              BunchKaufman,
              pBunchKaufman,

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

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

              ## MJ: aim to deprecate and eventually remove these
              ##     (except perhaps 'index')
              abIndex,
              atomicVector,
              compMatrix,
              index,
              number,
              replValue,
              rleDiff)
