The R Project SVN R-packages

Rev

Rev 1531 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1531 Rev 1544
Line 63... Line 63...
63
 
63
 
64
/** 
64
/** 
65
 * Solve  one  of the matrix equations  op(A)*C = B, or * C*op(A) = B
65
 * Solve  one  of the matrix equations  op(A)*C = B, or * C*op(A) = B
66
 * where A is an lCholCMatrix object and B and C are lgCMatrix 
66
 * where A is an lCholCMatrix object and B and C are lgCMatrix 
67
 * objects.
67
 * objects.
-
 
68
 *
-
 
69
 * An early check is done to see if A is the identity, in which case
-
 
70
 * BIP is returned and bp is unmodified.
68
 * 
71
 * 
69
 * @param side LFT or RGT
72
 * @param side LFT or RGT
70
 * @param transa TRN or NTR
73
 * @param transa TRN or NTR
71
 * @param m number of rows in B and C
74
 * @param m number of rows in B and C
72
 * @param n number of columns in B and C
75
 * @param n number of columns in B and C
73
 * @param Parent Parent array of A
76
 * @param Parent Parent array of A
74
 * @param bi array of row indices of B
-
 
75
 * @param bp array of column pointers of B
-
 
76
 * @param CIP pointer to the row indices for C
77
 * @param BIP pointer to the row indices for B
77
 * @param cp array of column pointers for C
78
 * @param bp array of column pointers for B (may be overwritten)
78
 *
79
 *
79
 * @return Pointer to the updated row indices for C.  The column
80
 * @return Pointer to the updated row indices for C.  The column
80
 * pointers may also be overwritten.
81
 * pointers bp are overwritten with cp.
81
 */
82
 */
82
SEXP
83
SEXP
83
lCholClgCsm(enum CBLAS_SIDE side, enum CBLAS_TRANSPOSE transa, int m,
84
lCholClgCsm(enum CBLAS_SIDE side, enum CBLAS_TRANSPOSE transa, int m,
84
	    int n, const int Parent[], const int bi[], const int bp[],
85
	    int n, const int Parent[], SEXP BIP, int bp[])
85
	    SEXP CIP, int cp[])
-
 
86
{
86
{
87
    int *ci = INTEGER(CIP), cnz, extra, i, j, pari, pos;
87
    int *bi = INTEGER(BIP), bnz, extra, ident, j, pari, pos;
-
 
88
    int nca = (transa == TRN) ? n : m;
-
 
89
 
-
 
90
    ident = 1;
-
 
91
    for (j = 0; j < nca; j++)
-
 
92
	if (Parent[j] >= 0) {
-
 
93
	    ident = 0;
-
 
94
	    break;
-
 
95
	}
-
 
96
    if (ident) return BIP;
88
 
97
 
89
    extra = 0;
98
    extra = 0;
90
    if (side == LFT) {
99
    if (side == LFT) {
91
	if (transa == TRN) {
100
	if (transa == TRN) {
92
	    error(_("code not yet written"));
101
	    error(_("code not yet written"));
Line 94... Line 103...
94
	} else {
103
	} else {
95
	    int *Tci, *Tj, *Ti, ntot;
104
	    int *Tci, *Tj, *Ti, ntot;
96
	    for (j = 0; j < n; j++) {
105
	    for (j = 0; j < n; j++) {
97
		int ii, ii2 = bp[j + 1];
106
		int ii, ii2 = bp[j + 1];
98
		for (ii = bp[j]; ii < ii2; ii++)
107
		for (ii = bp[j]; ii < ii2; ii++)
99
		    for (pari = bi[ii]; pari >= 0; pari = Parent[pari])
108
		    for (pari = Parent[bi[ii]]; pari >= 0;
100
			if (check_csc_index(cp, ci, pari, j, -1) < 0) extra++;
109
			 pari = Parent[pari]) extra++;
101
	    }
110
	    }
102
/* This is not quite right.  C may contain more elements than in the solution. */
-
 
103
	    if (!extra) return CIP;
-
 
104
 
111
 
105
	    cnz = cp[n];
112
	    bnz = bp[n];
106
	    ntot = cnz + extra;
113
	    ntot = bnz + extra;
107
	    Ti = Memcpy(Calloc(ntot, int), ci, cnz);
114
	    Ti = Memcpy(Calloc(ntot, int), bi, bnz);
108
	    Tj = expand_cmprPt(n, cp, Calloc(ntot, int));
115
	    Tj = expand_cmprPt(n, bp, Calloc(ntot, int));
109
	    Tci = Calloc(ntot, int);
116
	    Tci = Calloc(ntot, int);
110
 
117
 
111
	    pos = cnz;
118
	    pos = bnz;
112
	    for (j = 0; j < n; j++) {
119
	    for (j = 0; j < n; j++) {
113
		int ii, ii2 = bp[j + 1];
120
		int ii, ii2 = bp[j + 1];
114
		for (ii = bp[j]; ii < ii2; ii++)
121
		for (ii = bp[j]; ii < ii2; ii++)
115
		    for (pari = bi[ii]; pari >= 0; pari = Parent[pari])
122
		    for (pari = Parent[bi[ii]]; pari >= 0;
116
			if (check_csc_index(cp, ci, pari, j, -1) < 0) {
123
			 pari = Parent[pari]) {
117
			    Ti[pos] = pari;
124
			    Ti[pos] = pari;
118
			    Tj[pos] = j;
125
			    Tj[pos] = j;
119
			    pos++;
126
			    pos++;
120
			}
127
			}
121
	    }
128
	    }
122
	    triplet_to_col(m, n, ntot, Ti, Tj, (double *) NULL,
129
	    triplet_to_col(m, n, ntot, Ti, Tj, (double *) NULL,
123
			   cp, Tci, (double *) NULL);
130
			   bp, Tci, (double *) NULL);
124
 
131
 
125
	    cnz = cp[n];
132
	    bnz = bp[n];
126
	    CIP = PROTECT(allocVector(INTSXP, cnz));
133
	    BIP = PROTECT(allocVector(INTSXP, bnz));
127
	    Memcpy(INTEGER(CIP), Tci, cnz);
134
	    Memcpy(INTEGER(BIP), Tci, bnz);
128
	    
135
	    
129
	    Free(Tci); Free(Ti); Free(Tj);
136
	    Free(Tci); Free(Ti); Free(Tj);
130
	    UNPROTECT(1);
137
	    UNPROTECT(1);
131
	    return CIP;
138
	    return BIP;
132
	}
139
	}
133
    } else {
140
    } else {
134
	if (transa == TRN) {
141
	if (transa == TRN) {
135
	    int *Tci, *Tj, *Ti, ntot;
142
	    int *Tci, *Tj, *Ti, ntot;
136
	    for (j = 0; j < n; j++) {
143
	    for (j = 0; j < n; j++) {
137
		int ii, ii2 = bp[j + 1];
144
		int ii, ii2 = bp[j + 1];
138
		for (pari = j; pari >= 0; pari = Parent[pari]) {
145
		for (pari = Parent[j]; pari >= 0; pari = Parent[pari])
139
		    for (ii = bp[j]; ii < ii2; ii++)
146
		    for (ii = bp[j]; ii < ii2; ii++) extra++;
140
			if (check_csc_index(cp, ci, ii, pari, -1) < 0) extra++;
-
 
141
	    }
147
	    }
142
/* This is not quite right.  C may contain more elements than in the solution. */
-
 
143
	    if (!extra) return CIP;
-
 
144
 
148
 
145
	    cnz = cp[n];
149
	    bnz = bp[n];
146
	    ntot = cnz + extra;
150
	    ntot = bnz + extra;
147
	    Ti = Memcpy(Calloc(ntot, int), ci, cnz);
151
	    Ti = Memcpy(Calloc(ntot, int), bi, bnz);
148
	    Tj = expand_cmprPt(n, cp, Calloc(ntot, int));
152
	    Tj = expand_cmprPt(n, bp, Calloc(ntot, int));
149
	    Tci = Calloc(ntot, int);
153
	    Tci = Calloc(ntot, int);
150
 
154
 
151
	    pos = cnz;
155
	    pos = bnz;
152
	    for (j = 0; j < n; j++) {
156
	    for (j = 0; j < n; j++) {
153
		int ii, ii2 = bp[j + 1];
157
		int ii, ii2 = bp[j + 1];
154
		for (pari = j; pari >= 0; pari = Parent[pari]) {
158
		for (pari = Parent[j]; pari >= 0; pari = Parent[pari]) {
155
		    for (ii = bp[j]; ii < ii2; ii++)
159
		    for (ii = bp[j]; ii < ii2; ii++) {
156
			if (check_csc_index(cp, ci, ii, pari, -1) < 0) {
-
 
157
			    Ti[pos] = ii;
160
			Ti[pos] = bi[ii];
158
			    Tj[pos] = pari;
161
			Tj[pos] = pari;
159
			    pos++;
162
			pos++;
-
 
163
		    }
160
			}
164
		}
161
	    }
165
	    }
162
	    triplet_to_col(m, n, ntot, Ti, Tj, (double *) NULL,
166
	    triplet_to_col(m, n, ntot, Ti, Tj, (double *) NULL,
163
			   cp, Tci, (double *) NULL);
167
			   bp, Tci, (double *) NULL);
164
 
-
 
165
	    cnz = cp[n];
168
	    bnz = bp[n];
166
	    CIP = PROTECT(allocVector(INTSXP, cnz));
169
	    BIP = PROTECT(allocVector(INTSXP, bnz));
167
	    Memcpy(INTEGER(CIP), Tci, cnz);
170
	    Memcpy(INTEGER(BIP), Tci, bnz);
168
	    
171
	    
169
	    Free(Tci); Free(Ti); Free(Tj);
172
	    Free(Tci); Free(Ti); Free(Tj);
170
	    UNPROTECT(1);
173
	    UNPROTECT(1);
171
	    return CIP;
174
	    return BIP;
172
	} else {
175
	} else {
173
	    error(_("code not yet written"));
176
	    error(_("code not yet written"));
174
	    return R_NilValue;
177
	    return R_NilValue;
175
	}
178
	}
176
    }
179
    }
Line 186... Line 189...
186
	error(_("Dimensions of a (%d,%d) and b (%d,%d) do not conform"),
189
	error(_("Dimensions of a (%d,%d) and b (%d,%d) do not conform"),
187
	      n, n, bdims[0], bdims[1]);
190
	      n, n, bdims[0], bdims[1]);
188
    SET_SLOT(ans, Matrix_iSym,
191
    SET_SLOT(ans, Matrix_iSym,
189
	     lCholClgCsm(LFT, NTR, bdims[0], bdims[1],
192
	     lCholClgCsm(LFT, NTR, bdims[0], bdims[1],
190
			 INTEGER(GET_SLOT(a, Matrix_ParentSym)),
193
			 INTEGER(GET_SLOT(a, Matrix_ParentSym)),
191
			 INTEGER(GET_SLOT(b, Matrix_iSym)),
-
 
192
			 INTEGER(GET_SLOT(b, Matrix_pSym)),
-
 
193
			 GET_SLOT(ans, Matrix_iSym),
194
			 GET_SLOT(ans, Matrix_iSym),
194
			 INTEGER(GET_SLOT(ans, Matrix_pSym))));
195
			 INTEGER(GET_SLOT(ans, Matrix_pSym))));
195
    UNPROTECT(1);
196
    UNPROTECT(1);
196
    return ans;
197
    return ans;
197
}
198
}