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 296, Mon Oct 4 17:13:29 2004 UTC revision 943, Wed Sep 28 08:52:22 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 <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    SEXP triangularMatrix_validate(SEXP obj);
20    SEXP symmetricMatrix_validate(SEXP obj);
21    
22    /* enum constants from cblas.h and some short forms */
23    enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102};
24    enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113};
25    enum CBLAS_UPLO {CblasUpper=121, CblasLower=122};
26    enum CBLAS_DIAG {CblasNonUnit=131, CblasUnit=132};
27    enum CBLAS_SIDE {CblasLeft=141, CblasRight=142};
28    #define RMJ CblasRowMajor
29    #define CMJ CblasColMajor
30    #define NTR CblasNoTrans
31    #define TRN CblasTrans
32    #define CTR CblasConjTrans
33    #define UPP CblasUpper
34    #define LOW CblasLower
35    #define NUN CblasNonUnit
36    #define UNT CblasUnit
37    #define LFT CblasLeft
38    #define RGT CblasRight
39    
40  char norm_type(char *typstr);  char norm_type(char *typstr);
41  char rcond_type(char *typstr);  char rcond_type(char *typstr);
42  double get_double_by_name(SEXP obj, char *nm);  double get_double_by_name(SEXP obj, char *nm);
43  SEXP set_double_by_name(SEXP obj, double val, char *nm);  SEXP set_double_by_name(SEXP obj, double val, char *nm);
44  SEXP as_det_obj(double val, int log, int sign);  SEXP as_det_obj(double val, int log, int sign);
45  SEXP get_factorization(SEXP obj, char *nm);  SEXP get_factors(SEXP obj, char *nm);
46  SEXP set_factorization(SEXP obj, SEXP val, char *nm);  SEXP set_factors(SEXP obj, SEXP val, char *nm);
47  SEXP cscMatrix_set_Dim(SEXP x, int nrow);  SEXP dgCMatrix_set_Dim(SEXP x, int nrow);
48    char uplo_value(SEXP x);
49    char diag_value(SEXP x);
50    
51  int csc_unsorted_columns(int ncol, const int p[], const int i[]);  int csc_unsorted_columns(int ncol, const int p[], const int i[]);
52  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[]);
53  SEXP triple_as_SEXP(int nrow, int ncol, int nz,  SEXP triple_as_SEXP(int nrow, int ncol, int nz,
54                      const int Ti [], const int Tj [], const double Tx [],                      const int Ti [], const int Tj [], const double Tx [],
55                      char *Rclass);                      char *Rclass);
56  SEXP csc_check_column_sorting(SEXP A);  SEXP csc_check_column_sorting(SEXP A);
57  void csc_components_transpose(int m, int n, int nnz,  void csc_compTr(int m, int n, int nnz,
58                                const int xp[], const int xi[],                  const int xp[], const int xi[], const double xx[],
                               const double xx[],  
59                                int ap[], int ai[], double ax[]);                                int ap[], int ai[], double ax[]);
 void triplet_to_col(int nrow, int ncol, int nz,  
                     const int Ti [], const int Tj [], const double Tx [],  
                     int Ap [], int Ai [], double Ax []);  
60  void ssc_symbolic_permute(int n, int upper, const int perm[],  void ssc_symbolic_permute(int n, int upper, const int perm[],
61                            int Ap[], int Ai[]);                            int Ap[], int Ai[]);
62  double *nlme_symmetrize(double *a, const int nc);  SEXP Matrix_make_named(int TYP, char **names);
63  void nlme_check_Lapack_error(int info, const char *laName);  SEXP check_scalar_string(SEXP sP, char *vals, char *nm);
64  SEXP nlme_replaceSlot(SEXP obj, SEXP names, SEXP value);  double *packed_to_full(double *dest, const double *src, int n,
65  SEXP nlme_weight_matrix_list(SEXP MLin, SEXP wts, SEXP adjst, SEXP MLout);                         enum CBLAS_UPLO uplo);
66    double *full_to_packed(double *dest, const double *src, int n,
67                                  /* stored pointers to symbols */                         enum CBLAS_UPLO uplo, enum CBLAS_DIAG diag);
68                                  /* initialized in R_init_Matrix */  double *packed_getDiag(double *dest, SEXP x);
69  extern SEXP  SEXP Matrix_getElement(SEXP list, char *nm);
70      Matrix_DSym,  
71      Matrix_DIsqrtSym,  
72      Matrix_DimSym,  extern   /* stored pointers to symbols initialized in R_init_Matrix */
73      Matrix_GpSym,  #include "Syms.h"
74      Matrix_LSym,  
75      Matrix_LiSym,  /* zero an array */
76      Matrix_LinvSym,  #define AZERO(x, n) {int _I_, _SZ_ = (n); for(_I_ = 0; _I_ < _SZ_; _I_++) (x)[_I_] = 0;}
77      Matrix_LpSym,  
78      Matrix_LxSym,  /* number of elements in one triangle of a square matrix of order n */
79      Matrix_OmegaSym,  #define PACKED_LENGTH(n)   ((n) * ((n) + 1))/2
80      Matrix_ParentSym,  
81      Matrix_RXXSym,  /* duplicate the slot with name given by sym from src to dest */
82      Matrix_RZXSym,  #define slot_dup(dest, src, sym)  SET_SLOT(dest, sym, duplicate(GET_SLOT(src, sym)))
83      Matrix_XtXSym,  
84      Matrix_ZtXSym,  /**
85      Matrix_ZZxSym,   * Check for valid length of a packed triangular array and return the
86      Matrix_bVarSym,   * corresponding number of columns
87      Matrix_cnamesSym,   *
88      Matrix_devianceSym,   * @param len length of a packed triangular array
89      Matrix_devCompSym,   *
90      Matrix_diagSym,   * @return number of columns
91      Matrix_iSym,   */
92      Matrix_ipermSym,  static R_INLINE
93      Matrix_jSym,  int packed_ncol(int len)
94      Matrix_matSym,  {
95      Matrix_ncSym,      int disc = 8 * len + 1;     /* discriminant */
96      Matrix_pSym,      int sqrtd = (int) sqrt((double) disc);
97      Matrix_permSym,  
98      Matrix_statusSym,      if (len < 0 || disc != sqrtd * sqrtd)
99      Matrix_uploSym,          error(_("invalid 'len' = %d in packed_ncol"));
100      Matrix_xSym,      return (sqrtd - 1)/2;
101      Matrix_zSym;  }
102    
103    /**
104     * Allocate an SEXP of given type and length, assign it as slot nm in
105     * the object, and return the SEXP.  The validity of this function
106     * depends on SET_SLOT not duplicating val when NAMED(val) == 0.  If
107     * this behavior changes then ALLOC_SLOT must use SET_SLOT followed by
108     * GET_SLOT to ensure that the value returned is indeed the SEXP in
109     * the slot.
110     *
111     * @param obj object in which to assign the slot
112     * @param nm name of the slot, as an R name object
113     * @param type type of SEXP to allocate
114     * @param length length of SEXP to allocate
115     *
116     * @return SEXP of given type and length assigned as slot nm in obj
117     */
118    static R_INLINE
119    SEXP ALLOC_SLOT(SEXP obj, SEXP nm, SEXPTYPE type, int length)
120    {
121        SEXP val = allocVector(type, length);
122    
123        SET_SLOT(obj, nm, val);
124        return val;
125    }
126    
127    /**
128     * Expand compressed pointers in the array mp into a full set of indices
129     * in the array mj.
130     *
131     * @param ncol number of columns (or rows)
132     * @param mp column pointer vector of length ncol + 1
133     * @param mj vector of length mp[ncol] to hold the result
134     *
135     * @return mj
136     */
137    static R_INLINE
138    int* expand_cmprPt(int ncol, const int mp[], int mj[])
139    {
140        int j;
141        for (j = 0; j < ncol; j++) {
142            int j2 = mp[j+1], jj;
143            for (jj = mp[j]; jj < j2; jj++) mj[jj] = j;
144        }
145        return mj;
146    }
147    
148    
149    /**
150     * Return the linear index of the (row, col) entry in a csc structure.
151     * If the entry is not found and missing is 0 an error is signaled;
152     * otherwise the missing value is returned.
153     *
154     * @param p vector of column pointers
155     * @param i vector of row indices
156     * @param row row index
157     * @param col column index
158     * @param missing the value to return is the row, col entry does not
159     * exist.  If this is zero and the row, col entry does not exist an
160     * error is signaled.
161     *
162     * @return index of element at (row, col) if it exists, otherwise missing
163     */
164    static R_INLINE int
165    check_csc_index(const int p[], const int i[], int row, int col, int missing)
166    {
167        int k, k2 = p[col + 1];
168                                    /* linear search - perhaps replace by bsearch */
169        for (k = p[col]; k < k2; k++) if (i[k] == row) return k;
170        if (!missing)
171            error("row %d and column %d not defined in rowind and colptr",
172                  row, col);
173        return missing;
174    }
175    
176    SEXP alloc3Darray(SEXPTYPE mode, int nrow, int ncol, int nface);
177    
178    /**
179     * Calculate the zero-based index in a row-wise packed lower triangular matrix.
180     * This is used for the arrays of blocked sparse matrices.
181     *
182     * @param i column number (zero-based)
183     * @param k row number (zero-based)
184     *
185     * @return The index of the (k,i) element of a packed lower triangular matrix
186     */
187    static R_INLINE
188    int Lind(int k, int i)
189    {
190        if (k < i) error("Lind(k = %d, i = %d) must have k >= i", k, i);
191        return (k * (k + 1))/2 + i;
192    }
193    
194    /**
195     * Check for a complete match on matrix dimensions
196     *
197     * @param xd dimensions of first matrix
198     * @param yd dimensions of second matrix
199     *
200     * @return 1 if dimensions match, otherwise 0
201     */
202    static R_INLINE
203    int match_mat_dims(const int xd[], const int yd[])
204    {
205        return xd[0] == yd[0] && xd[1] == yd[1];
206    }
207    
208    double *expand_csc_column(double *dest, int m, int j,
209                              const int Ap[], const int Ai[], const double Ax[]);
210    
211    /**
212     * Apply a permutation to an integer vector
213     *
214     * @param i vector of 0-based indices
215     * @param n length of vector i
216     * @param perm 0-based permutation vector of length max(i) + 1
217     */
218    static R_INLINE void
219    int_permute(int i[], int n, const int perm[])
220    {
221        int j;
222        for (j = 0; j < n; j++) i[j] = perm[i[j]];
223    }
224    
225    /**
226     * Force index pairs to be in the upper triangle of a matrix
227     *
228     * @param i vector of 0-based row indices
229     * @param j vector of 0-based column indices
230     * @param nnz length of index vectors
231     */
232    static R_INLINE void
233    make_upper_triangular(int i[], int j[], int nnz)
234    {
235        int k;
236        for (k = 0; k < nnz; k++) {
237            if (i[k] > j[k]) {
238                int tmp = i[k];
239                i[k] = j[k];
240                j[k] = tmp;
241            }
242        }
243    }
244    
245    void make_array_triangular(double *x, SEXP from);
246    
247    SEXP Matrix_expand_pointers(SEXP pP);
248    
249    
250    /**
251     * Elementwise increment dest by src
252     *
253     * @param dest vector to be incremented
254     * @param src vector to be added to dest
255     * @param n length of vectors
256     *
257     * @return dest
258     */
259    static R_INLINE double*
260    vecIncrement(double dest[], const double src[], int n) {
261        int i;
262        for (i = 0; i < n; i++) dest[i] += src[i];
263        return dest;
264    }
265    
266    /**
267     * Elementwise sum of src1 and src2 into dest
268     *
269     * @param dest vector to be incremented
270     * @param src1 vector to be added
271     * @param src1 second vector to be added
272     * @param n length of vectors
273     *
274     * @return dest
275     */
276    static R_INLINE double*
277    vecSum(double dest[], const double src1[], const double src2[],
278           int n) {
279        int i;
280        for (i = 0; i < n; i++) dest[i] = src1[i] + src2[i];
281        return dest;
282    }
283    
284    #ifdef __cplusplus
285    }
286  #endif  #endif
287    
288    #endif /* MATRIX_MUTILS_H_ */

Legend:
Removed from v.296  
changed lines
  Added in v.943

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