Matrix $Rev: 2718 $ at $LastChangedDate: 2011-10-06 11:45:17 +0200 (Thu, 06 Oct 2011) $
Mutils.c File Reference
#include <limits.h>
#include "Mutils.h"
#include <R_ext/Lapack.h>
Include dependency graph for Mutils.c:

Go to the source code of this file.

Defines

#define ddense_CLASSES
 Duplicate a [dln]denseMatrix or a numeric matrix or vector as a [dln]geMatrix.
#define do_ii_FILL(_i_, _j_)
#define DUP_MMATRIX_ddense_CASES
#define DUP_MMATRIX_NON_CLASS
#define DUP_MMATRIX_SET_1
#define END_packed_getDiag
#define FULL_TO_PACKED(TYPE)
#define ldense_CLASSES
#define MAKE_SYMMETRIC_BODY(_TO_, _FROM_)
#define MAKE_TRIANGULAR_BODY(_TO_, _FROM_, _ZERO_, _ONE_)
#define Matrix_Error_Bufsiz   4096
#define ndense_CLASSES
#define PACKED_TO_FULL(TYPE)
#define SPRINTF   buf = Alloca(Matrix_Error_Bufsiz, char); R_CheckStack(); sprintf

Functions

SEXP as_det_obj (double val, int log, int sign)
SEXP check_scalar_string (SEXP sP, char *vals, char *nm)
 Check validity of 1-letter string from a set of possible values (typically used in S4 validity method)
void d_packed_getDiag (double *dest, SEXP x, int n)
 Copy the diagonal elements of the packed denseMatrix x to dest.
SEXP dense_nonpacked_validate (SEXP obj)
SEXP dup_mMatrix_as_dgeMatrix (SEXP A)
SEXP dup_mMatrix_as_geMatrix (SEXP A)
Rboolean equal_string_vectors (SEXP s1, SEXP s2)
double get_double_by_name (SEXP obj, char *nm)
SEXP get_factors (SEXP obj, char *nm)
static double * install_diagonal (double *dest, SEXP A)
 Zero a square matrix of size nc then copy a vector to the diagonal.
static int * install_diagonal_int (int *dest, SEXP A)
void l_packed_getDiag (int *dest, SEXP x, int n)
char La_norm_type (const char *typstr)
char La_rcond_type (const char *typstr)
SEXP m_encodeInd (SEXP ij, SEXP di, SEXP chk_bnds)
 Encode Matrix index (i,j) |--> i + j * nrow {i,j : 0-origin}.
SEXP m_encodeInd2 (SEXP i, SEXP j, SEXP di, SEXP chk_bnds)
 Encode Matrix index (i,j) |--> i + j * nrow {i,j : 0-origin}.
void make_d_matrix_symmetric (double *to, SEXP from) void make_i_matrix_symmetric(int *to
void make_d_matrix_triangular (double *to, SEXP from) void make_i_matrix_triangular(int *to
int Matrix_check_class_etc (SEXP x, const char **valid)
 Return the 0-based index of an is() match in a vector of class-name strings terminated by an empty string.
SEXP Matrix_expand_pointers (SEXP pP)
SEXP Matrix_getElement (SEXP list, char *nm)
 Return the element of a given name from a named list.
SEXP new_dgeMatrix (int nrow, int ncol)
SEXP set_double_by_name (SEXP obj, double val, char *nm)
SEXP set_factors (SEXP obj, SEXP val, char *nm)
void tr_d_packed_getDiag (double *dest, SEXP x)
void tr_l_packed_getDiag (int *dest, SEXP x)

Define Documentation

#define ddense_CLASSES
Value:
"dgeMatrix", "dtrMatrix",                               \
                    "dsyMatrix", "dpoMatrix", "ddiMatrix",              \
                    "dtpMatrix", "dspMatrix", "dppMatrix",              \
                    /* sub classes of those above:*/                    \
                    /* dtr */ "Cholesky", "LDL", "BunchKaufman",        \
                    /* dtp */ "pCholesky", "pBunchKaufman",             \
                    /* dpo */ "corMatrix"

Duplicate a [dln]denseMatrix or a numeric matrix or vector as a [dln]geMatrix.

This is for the many *_matrix_{prod,crossprod,tcrossprod,etc.} functions that work with both classed and unclassed matrices.

Parameters:
Aeither a denseMatrix object or a matrix object

Definition at line 499 of file Mutils.c.

Referenced by dup_mMatrix_as_dgeMatrix(), and dup_mMatrix_as_geMatrix().

#define do_ii_FILL (   _i_,
  _j_ 
)
Value:
int i;                                                          \
        if(check_bounds) {                                              \
            for(i=0; i < n; i++) {                                      \
                if(_i_[i] == NA_INTEGER || _j_[i] == NA_INTEGER)        \
                    ii[i] = NA_INTEGER;                                 \
                else {                                                  \
                    if(_i_[i] < 0 || _i_[i] >= Di[0])                   \
                        error(_("subscript 'i' out of bounds in M[ij]")); \
                    if(_j_[i] < 0 || _j_[i] >= Di[1])                   \
                        error(_("subscript 'j' out of bounds in M[ij]")); \
                    ii[i] = _i_[i] + _j_[i] * nr;                       \
                }                                                       \
            }                                                           \
        } else {                                                        \
            for(i=0; i < n; i++)                                        \
                ii[i] = (_i_[i] == NA_INTEGER || _j_[i] == NA_INTEGER)  \
                    ? NA_INTEGER : _i_[i] + _j_[i] * nr;                \
        }

Referenced by m_encodeInd(), and m_encodeInd2().

#define DUP_MMATRIX_ddense_CASES
#define DUP_MMATRIX_NON_CLASS
Value:
if (isMatrix(A)) {      /* "matrix" */                          \
            ad = getAttrib(A, R_DimSymbol);                             \
            an = getAttrib(A, R_DimNamesSymbol);                        \
        } else {/* maybe "numeric" (incl integer,logical) --> (n x 1) */\
            int* dd = INTEGER(ad = PROTECT(allocVector(INTSXP, 2)));    \
            nprot++;                                                    \
            dd[0] = LENGTH(A);                                          \
            dd[1] = 1;                                                  \
            an = R_NilValue;                                            \
        }                                                               \
        ctype = 0

Referenced by dup_mMatrix_as_dgeMatrix(), and dup_mMatrix_as_geMatrix().

#define DUP_MMATRIX_SET_1
Value:
SET_SLOT(ans, Matrix_DimSym, duplicate(ad));            \
    SET_SLOT(ans, Matrix_DimNamesSym, (LENGTH(an) == 2) ?       \
             duplicate(an): allocVector(VECSXP, 2));            \
    sz = INTEGER(ad)[0] * INTEGER(ad)[1]

Referenced by dup_mMatrix_as_dgeMatrix(), and dup_mMatrix_as_geMatrix().

#define END_packed_getDiag
Value:
int j, pos = 0;                                                 \
                                                                        \
    if (*uplo_P(x) == 'U') {                                            \
        for(pos= 0, j=0; j < n; pos += 1+(++j))  dest[j] = xx[pos];     \
    } else {                                                            \
        for(pos= 0, j=0; j < n; pos += (n - j), j++) dest[j] = xx[pos]; \
    }                                                                   \
    return

Referenced by d_packed_getDiag(), and l_packed_getDiag().

#define FULL_TO_PACKED (   TYPE)
Value:
TYPE *full_to_packed_ ## TYPE(TYPE *dest, const TYPE *src, int n,       \
                      enum CBLAS_UPLO uplo, enum CBLAS_DIAG diag)       \
{                                                                       \
    int i, j, pos = 0;                                                  \
                                                                        \
    for (j = 0; j < n; j++) {                                           \
        switch(uplo) {                                                  \
        case UPP:                                                       \
            for (i = 0; i <= j; i++)                                    \
                dest[pos++] = (i == j && diag== UNT) ? 1 : src[i + j*n]; \
            break;                                                      \
        case LOW:                                                       \
            for (i = j; i < n; i++)                                     \
                dest[pos++] = (i == j && diag== UNT) ? 1 : src[i + j*n]; \
            break;                                                      \
        default:                                                        \
            error(_("'uplo' must be UPP or LOW"));                      \
        }                                                               \
    }                                                                   \
    return dest;                                                        \
}

Definition at line 326 of file Mutils.c.

#define ldense_CLASSES
Value:
"lgeMatrix", "ltrMatrix",               \
                    "lsyMatrix", "ldiMatrix",           \
                    "ltpMatrix", "lspMatrix"

Definition at line 508 of file Mutils.c.

Referenced by dup_mMatrix_as_geMatrix().

#define MAKE_SYMMETRIC_BODY (   _TO_,
  _FROM_ 
)
Value:
{                                                               \
    int i, j, n = INTEGER(GET_SLOT(_FROM_, Matrix_DimSym))[0];  \
                                                                \
    if (*uplo_P(_FROM_) == 'U') {                               \
        for (j = 0; j < n; j++)                                 \
            for (i = j+1; i < n; i++)                           \
                _TO_[i + j*n] = _TO_[j + i*n];                  \
    } else {                                                    \
        for (j = 1; j < n; j++)                                 \
            for (i = 0; i < j && i < n; i++)                    \
                _TO_[i + j*n] = _TO_[j + i*n];                  \
    }                                                           \
}

Definition at line 202 of file Mutils.c.

#define MAKE_TRIANGULAR_BODY (   _TO_,
  _FROM_,
  _ZERO_,
  _ONE_ 
)
Value:
{                                                               \
    int i, j, *dims = INTEGER(GET_SLOT(_FROM_, Matrix_DimSym)); \
    int n = dims[0], m = dims[1];                               \
                                                                \
    if (*uplo_P(_FROM_) == 'U') {                               \
        for (j = 0; j < n; j++)                                 \
            for (i = j+1; i < m; i++)                           \
                _TO_[i + j*m] = _ZERO_;                         \
    } else {                                                    \
        for (j = 1; j < n; j++)                                 \
            for (i = 0; i < j && i < m; i++)                    \
                _TO_[i + j*m] = _ZERO_;                         \
    }                                                           \
    if (*diag_P(_FROM_) == 'U') {                               \
        j = (n < m) ? n : m;                                    \
        for (i = 0; i < j; i++)                                 \
            _TO_[i * (m + 1)] = _ONE_;                          \
    }                                                           \
}

Definition at line 174 of file Mutils.c.

#define Matrix_Error_Bufsiz   4096

Definition at line 224 of file Mutils.c.

#define ndense_CLASSES
Value:
"ngeMatrix", "ntrMatrix",               \
                    "nsyMatrix",                        \
                    "ntpMatrix", "nspMatrix"

Definition at line 513 of file Mutils.c.

Referenced by dup_mMatrix_as_geMatrix().

#define PACKED_TO_FULL (   TYPE)
Value:
TYPE *packed_to_full_ ## TYPE(TYPE *dest, const TYPE *src,              \
                        int n, enum CBLAS_UPLO uplo)                    \
{                                                                       \
    int i, j, pos = 0;                                                  \
                                                                        \
    AZERO(dest, n*n);                                                   \
    for (j = 0; j < n; j++) {                                           \
        switch(uplo) {                                                  \
        case UPP:                                                       \
            for (i = 0; i <= j; i++) dest[i + j * n] = src[pos++];      \
            break;                                                      \
        case LOW:                                                       \
            for (i = j; i < n; i++) dest[i + j * n] = src[pos++];       \
            break;                                                      \
        default:                                                        \
            error(_("'uplo' must be UPP or LOW"));                      \
        }                                                               \
    }                                                                   \
    return dest;                                                        \
}

Definition at line 301 of file Mutils.c.

#define SPRINTF   buf = Alloca(Matrix_Error_Bufsiz, char); R_CheckStack(); sprintf

Referenced by check_scalar_string().


Function Documentation

SEXP as_det_obj ( double  val,
int  log,
int  sign 
)

Definition at line 92 of file Mutils.c.

Referenced by dgeMatrix_determinant().

Here is the caller graph for this function:

SEXP check_scalar_string ( SEXP  sP,
char *  vals,
char *  nm 
)

Check validity of 1-letter string from a set of possible values (typically used in S4 validity method)

Parameters:
sP
valsa string containing the possible valid letters
nmthe name of the slot being checked
Returns:
a SEXP, either NULL (= success) or an error message

Definition at line 236 of file Mutils.c.

References _, and SPRINTF.

Referenced by symmetricMatrix_validate(), and triangularMatrix_validate().

Here is the caller graph for this function:

void d_packed_getDiag ( double *  dest,
SEXP  x,
int  n 
)

Copy the diagonal elements of the packed denseMatrix x to dest.

Parameters:
destvector of length ncol(x)
xpointer to an object representing a packed array
nnumber of columns in the matrix represented by x
Returns:
dest

Definition at line 363 of file Mutils.c.

References END_packed_getDiag, and Matrix_xSym.

Referenced by dtpMatrix_getDiag(), and tr_d_packed_getDiag().

Here is the caller graph for this function:

SEXP dense_nonpacked_validate ( SEXP  obj)

Definition at line 292 of file Mutils.c.

References _, Matrix_DimSym, and Matrix_xSym.

Referenced by dgeMatrix_validate(), dsyMatrix_validate(), and dtrMatrix_validate().

Here is the caller graph for this function:

SEXP dup_mMatrix_as_geMatrix ( SEXP  A)
Rboolean equal_string_vectors ( SEXP  s1,
SEXP  s2 
)

Definition at line 268 of file Mutils.c.

References _.

Referenced by ddense_skewpart(), ddense_symmpart(), and dense_to_symmetric().

Here is the caller graph for this function:

double get_double_by_name ( SEXP  obj,
char *  nm 
)

Definition at line 47 of file Mutils.c.

References _.

SEXP get_factors ( SEXP  obj,
char *  nm 
)

Definition at line 109 of file Mutils.c.

References _, and Matrix_factorSym.

Referenced by dgCMatrix_LU(), dgCMatrix_matrix_solve(), dgeMatrix_LU_(), dpoMatrix_chol(), dppMatrix_chol(), dspMatrix_trf(), and dsyMatrix_trf().

Here is the caller graph for this function:

static double* install_diagonal ( double *  dest,
SEXP  A 
) [static]

Zero a square matrix of size nc then copy a vector to the diagonal.

Parameters:
destdestination array of length nc * nc
Apointer to a square Matrix object
Returns:
dest

Definition at line 461 of file Mutils.c.

References AZERO, diag_P, Matrix_DimSym, and Matrix_xSym.

static int* install_diagonal_int ( int *  dest,
SEXP  A 
) [static]

Definition at line 474 of file Mutils.c.

References AZERO, diag_P, Matrix_DimSym, and Matrix_xSym.

Referenced by dup_mMatrix_as_geMatrix().

Here is the caller graph for this function:

void l_packed_getDiag ( int *  dest,
SEXP  x,
int  n 
)

Definition at line 380 of file Mutils.c.

References END_packed_getDiag, and Matrix_xSym.

Referenced by ltpMatrix_getDiag(), and tr_l_packed_getDiag().

Here is the caller graph for this function:

char La_norm_type ( const char *  typstr)

Definition at line 10 of file Mutils.c.

References _.

Referenced by get_norm(), get_norm_sp(), and get_norm_sy().

Here is the caller graph for this function:

char La_rcond_type ( const char *  typstr)

Definition at line 29 of file Mutils.c.

References _.

Referenced by dgeMatrix_rcond(), dtpMatrix_rcond(), and dtrMatrix_rcond().

Here is the caller graph for this function:

SEXP m_encodeInd ( SEXP  ij,
SEXP  di,
SEXP  chk_bnds 
)

Encode Matrix index (i,j) |--> i + j * nrow {i,j : 0-origin}.

Parameters:
ij,:2-column integer matrix
di,:dim(.), i.e. length 2 integer vector
chk_bnds,:logical indicating 0 <= ij[,k] < di[k] need to be checked.
Returns:
encoded index; integer if prod(dim) is small; double otherwise

Definition at line 738 of file Mutils.c.

References _, and do_ii_FILL.

SEXP m_encodeInd2 ( SEXP  i,
SEXP  j,
SEXP  di,
SEXP  chk_bnds 
)

Encode Matrix index (i,j) |--> i + j * nrow {i,j : 0-origin}.

Parameters:
i,:integer vector
j,:integer vector of same length as 'i'
di,:dim(.), i.e. length 2 integer vector
chk_bnds,:logical indicating 0 <= ij[,k] < di[k] need to be checked.
Returns:
encoded index; integer if prod(dim) is small; double otherwise

Definition at line 796 of file Mutils.c.

References _, and do_ii_FILL.

void make_d_matrix_symmetric ( double *  to,
SEXP  from 
)

Referenced by dsyMatrix_as_matrix().

Here is the caller graph for this function:

void make_d_matrix_triangular ( double *  to,
SEXP  from 
)

Referenced by dtrMatrix_as_matrix(), dtrMatrix_dtrMatrix_mm(), and LU_expand().

Here is the caller graph for this function:

int Matrix_check_class_etc ( SEXP  x,
const char **  valid 
)

Return the 0-based index of an is() match in a vector of class-name strings terminated by an empty string.

These are the ones users should use -- is() versions, also looking at super classes:

Returns -1 for no match. Strives to find the correct environment() for is().

Parameters:
xan R object, about which we want is(x, .) information.
validvector of possible matches terminated by an empty string.
Returns:
index of match or -1 for no match

Definition at line 889 of file Mutils.c.

References _, and Matrix_check_class_and_super.

Referenced by as_cholmod_factor(), as_cholmod_sparse(), as_cholmod_triplet(), compressed_to_TMatrix(), Csparse_subassign(), dup_mMatrix_as_dgeMatrix(), dup_mMatrix_as_geMatrix(), Matrix_as_cs(), R_init_Matrix(), R_to_CMatrix(), and Tsparse_diagU2N().

Here is the caller graph for this function:

SEXP Matrix_expand_pointers ( SEXP  pP)

Definition at line 421 of file Mutils.c.

References expand_cmprPt().

Here is the call graph for this function:

SEXP Matrix_getElement ( SEXP  list,
char *  nm 
)

Return the element of a given name from a named list.

Parameters:
list
nmname of desired element
Returns:
element of list with name nm

Definition at line 442 of file Mutils.c.

SEXP new_dgeMatrix ( int  nrow,
int  ncol 
)

Definition at line 714 of file Mutils.c.

References ALLOC_SLOT(), Matrix_DimNamesSym, Matrix_DimSym, and Matrix_xSym.

Here is the call graph for this function:

SEXP set_double_by_name ( SEXP  obj,
double  val,
char *  nm 
)

Definition at line 63 of file Mutils.c.

References _.

SEXP set_factors ( SEXP  obj,
SEXP  val,
char *  nm 
)

Definition at line 129 of file Mutils.c.

References _, and Matrix_factorSym.

Referenced by dgeMatrix_LU_(), dpoMatrix_chol(), dppMatrix_chol(), dspMatrix_trf(), dsyMatrix_trf(), install_lu(), and internal_chm_factor().

Here is the caller graph for this function:

void tr_d_packed_getDiag ( double *  dest,
SEXP  x 
)

Definition at line 389 of file Mutils.c.

References d_packed_getDiag(), diag_P, and Matrix_DimSym.

Here is the call graph for this function:

void tr_l_packed_getDiag ( int *  dest,
SEXP  x 
)

Definition at line 405 of file Mutils.c.

References diag_P, l_packed_getDiag(), and Matrix_DimSym.

Here is the call graph for this function: