SCM

SCM Repository

[matrix] Diff of /pkg/src/Mutils.h
ViewVC logotype

Diff of /pkg/src/Mutils.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 476, Wed Feb 2 11:51:24 2005 UTC revision 582, Mon Feb 28 18:15:21 2005 UTC
# Line 1  Line 1 
1  #ifndef MATRIX_MUTILS_H  #ifndef MATRIX_MUTILS_H
2  #define MATRIX_MUTILS_H  #define MATRIX_MUTILS_H
3    
4    #ifdef __cplusplus
5    extern "C" {
6    #endif
7    
8  #include <Rdefines.h>  #include <Rdefines.h>
9  #include <Rconfig.h>  #include <Rconfig.h>
10  #include "cblas.h"  #include <R.h>  /* to include Rconfig.h */
11    
12    #ifdef ENABLE_NLS
13    #include <libintl.h>
14    #define _(String) dgettext ("Matrix", String)
15    #else
16    #define _(String) (String)
17    #endif
18    
19  /* short forms of some enum constants from cblas.h */  /* enum constants from cblas.h and some short forms */
20    enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102};
21    enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113};
22    enum CBLAS_UPLO {CblasUpper=121, CblasLower=122};
23    enum CBLAS_DIAG {CblasNonUnit=131, CblasUnit=132};
24    enum CBLAS_SIDE {CblasLeft=141, CblasRight=142};
25  #define RMJ CblasRowMajor  #define RMJ CblasRowMajor
26  #define CMJ CblasColMajor  #define CMJ CblasColMajor
27  #define NTR CblasNoTrans  #define NTR CblasNoTrans
# Line 25  Line 41 
41  SEXP as_det_obj(double val, int log, int sign);  SEXP as_det_obj(double val, int log, int sign);
42  SEXP get_factors(SEXP obj, char *nm);  SEXP get_factors(SEXP obj, char *nm);
43  SEXP set_factors(SEXP obj, SEXP val, char *nm);  SEXP set_factors(SEXP obj, SEXP val, char *nm);
44  SEXP cscMatrix_set_Dim(SEXP x, int nrow);  SEXP dgCMatrix_set_Dim(SEXP x, int nrow);
45  int csc_unsorted_columns(int ncol, const int p[], const int i[]);  int csc_unsorted_columns(int ncol, const int p[], const int i[]);
46  void csc_sort_columns(int ncol, const int p[], int i[], double x[]);  void csc_sort_columns(int ncol, const int p[], int i[], double x[]);
47  SEXP triple_as_SEXP(int nrow, int ncol, int nz,  SEXP triple_as_SEXP(int nrow, int ncol, int nz,
# Line 42  Line 58 
58  void nlme_check_Lapack_error(int info, const char *laName);  void nlme_check_Lapack_error(int info, const char *laName);
59  SEXP nlme_replaceSlot(SEXP obj, SEXP names, SEXP value);  SEXP nlme_replaceSlot(SEXP obj, SEXP names, SEXP value);
60  SEXP nlme_weight_matrix_list(SEXP MLin, SEXP wts, SEXP adjst, SEXP MLout);  SEXP nlme_weight_matrix_list(SEXP MLin, SEXP wts, SEXP adjst, SEXP MLout);
61    SEXP Matrix_make_named(int TYP, char **names);
62                                  /* stored pointers to symbols */                                  /* stored pointers to symbols */
63                                  /* initialized in R_init_Matrix */                                  /* initialized in R_init_Matrix */
64  extern  extern
# Line 51  Line 67 
67  /* zero an array */  /* zero an array */
68  #define AZERO(x, n) {int _I_, _SZ_ = (n); for(_I_ = 0; _I_ < _SZ_; _I_++) (x)[_I_] = 0;}  #define AZERO(x, n) {int _I_, _SZ_ = (n); for(_I_ = 0; _I_ < _SZ_; _I_++) (x)[_I_] = 0;}
69    
70    /**
71     * Allocate an SEXP of given type and length, assign it as slot nm in
72     * the object, and return the SEXP.  The validity of this function
73     * depends on SET_SLOT not duplicating val when NAMED(val) == 0.  If
74     * this behavior changes then ALLOC_SLOT must use SET_SLOT followed by
75     * GET_SLOT to ensure that the value returned is indeed the SEXP in
76     * the slot.
77     *
78     * @param obj object in which to assign the slot
79     * @param nm name of the slot, as an R name object
80     * @param type type of SEXP to allocate
81     * @param length length of SEXP to allocate
82     *
83     * @return SEXP of given type and length assigned as slot nm in obj
84     */
85    static R_INLINE
86    SEXP ALLOC_SLOT(SEXP obj, SEXP nm, SEXPTYPE type, int length)
87    {
88        SEXP val = allocVector(type, length);
89    
90        SET_SLOT(obj, nm, val);
91        return val;
92    }
93    
94    /**
95     * Expand the column pointers in the array mp into a full set of column indices
96     * in the array mj.
97     *
98     * @param ncol number of columns
99     * @param mp column pointer vector of length ncol + 1
100     * @param mj vector of length mp[ncol] - 1 to hold the result
101     *
102     * @return mj
103     */
104    static R_INLINE
105    int* expand_column_pointers(int ncol, const int mp[], int mj[])
106    {
107        int j;
108        for (j = 0; j < ncol; j++) {
109            int j2 = mp[j+1], jj;
110            for (jj = mp[j]; jj < j2; jj++) mj[jj] = j;
111        }
112        return mj;
113    }
114    
115    
116    /**
117     * Return the linear index of the (row, col) entry in a csc structure.
118     * If the entry is not found and missing is 0 an error is signaled;
119     * otherwise the missing value is returned.
120     *
121     * @param p vector of column pointers
122     * @param i vector of row indices
123     * @param row row index
124     * @param col column index
125     * @param missing the value to return is the row, col entry does not
126     * exist.  If this is zero and the row, col entry does not exist an
127     * error is signaled.
128     *
129     * @return index of element at (row, col) if it exists, otherwise missing
130     */
131    static R_INLINE int
132    check_csc_index(const int p[], const int i[], int row, int col, int missing)
133    {
134        int k, k2 = p[col + 1];
135                                    /* linear search - perhaps replace by bsearch */
136        for (k = p[col]; k < k2; k++) if (i[k] == row) return k;
137        if (!missing)
138            error("row %d and column %d not defined in rowind and colptr",
139                  row, col);
140        return missing;
141    }
142    
143    SEXP alloc3Darray(SEXPTYPE mode, int nrow, int ncol, int nface);
144    
145    /**
146     * Calculate the zero-based index in a row-wise packed lower triangular matrix.
147     * This is used for the arrays of blocked sparse matrices.
148     *
149     * @param i column number (zero-based)
150     * @param k row number (zero-based)
151     *
152     * @return The index of the (k,i) element of a packed lower triangular matrix
153     */
154    static R_INLINE
155    int Lind(int k, int i)
156    {
157        if (k < i) error("Lind(k = %d, i = %d) must have k >= i", k, i);
158        return (k * (k + 1))/2 + i;
159    }
160    
161    /**
162     * Check for a complete match on matrix dimensions
163     *
164     * @param xd dimensions of first matrix
165     * @param yd dimensions of second matrix
166     *
167     * @return 1 if dimensions match, otherwise 0
168     */
169    static R_INLINE
170    int match_mat_dims(const int xd[], const int yd[])
171    {
172        return xd[0] == yd[0] && xd[1] == yd[1];
173    }
174    
175    double *expand_csc_column(double *dest, int m, int j,
176                              const int Ap[], const int Ai[], const double Ax[]);
177    
178    /**
179     * Apply a permutation to an integer vector
180     *
181     * @param i vector of 0-based indices
182     * @param n length of vector i
183     * @param perm 0-based permutation vector of length max(i) + 1
184     */
185    static R_INLINE void
186    int_permute(int i[], int n, const int perm[])
187    {
188        int j;
189        for (j = 0; j < n; j++) i[j] = perm[i[j]];
190    }
191    
192    /**
193     * Force index pairs to be in the upper triangle of a matrix
194     *
195     * @param i vector of 0-based row indices
196     * @param j vector of 0-based column indices
197     * @param nnz length of index vectors
198     */
199    static R_INLINE void
200    make_upper_triangular(int i[], int j[], int nnz)
201    {
202        int k;
203        for (k = 0; k < nnz; k++) {
204            if (i[k] > j[k]) {
205                int tmp = i[k];
206                i[k] = j[k];
207                j[k] = tmp;
208            }
209        }
210    }
211    
212    void make_array_triangular(double *x, SEXP from);
213    
214    #ifdef __cplusplus
215    }
216  #endif  #endif
217    
218    #endif /* MATRIX_MUTILS_H_ */

Legend:
Removed from v.476  
changed lines
  Added in v.582

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge