The R Project SVN R-packages

Rev

Rev 780 | Rev 2172 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 780 Rev 1190
Line 23... Line 23...
23
{
23
{
24
    double *lastcol;
24
    double *lastcol;
25
    int i, jj;
25
    int i, jj;
26
 
26
 
27
    if (j >= k)
27
    if (j >= k)
28
	error("incorrect left cyclic shift, j (%d) >= k (%d)", j, k);
28
	error(_("incorrect left cyclic shift, j (%d) >= k (%d)"), j, k);
29
    if (j < 0)
29
    if (j < 0)
30
	error("incorrect left cyclic shift, j (%d) < 0", j, k);
30
	error(_("incorrect left cyclic shift, j (%d) < 0"), j, k);
31
    if (ldx < k)
31
    if (ldx < k)
32
	error("incorrect left cyclic shift, k (%d) > ldx (%d)", k, ldx);
32
	error(_("incorrect left cyclic shift, k (%d) > ldx (%d)"), k, ldx);
33
    lastcol = (double*) R_alloc(k+1, sizeof(double));
33
    lastcol = (double*) R_alloc(k+1, sizeof(double));
34
				/* keep a copy of column j */
34
				/* keep a copy of column j */
35
    for(i = 0; i <= j; i++) lastcol[i] = x[i + j*ldx];
35
    for(i = 0; i <= j; i++) lastcol[i] = x[i + j*ldx];
36
				/* For safety, zero the rest */
36
				/* For safety, zero the rest */
37
    for(i = j+1; i <= k; i++) lastcol[i] = 0.;
37
    for(i = j+1; i <= k; i++) lastcol[i] = 0.;
Line 72... Line 72...
72
    SET_STRING_ELT(nms, 0, mkChar("jmin"));
72
    SET_STRING_ELT(nms, 0, mkChar("jmin"));
73
    SET_STRING_ELT(nms, 1, mkChar("rank"));
73
    SET_STRING_ELT(nms, 1, mkChar("rank"));
74
    SET_STRING_ELT(nms, 2, mkChar("cosines"));
74
    SET_STRING_ELT(nms, 2, mkChar("cosines"));
75
    SET_STRING_ELT(nms, 3, mkChar("sines"));
75
    SET_STRING_ELT(nms, 3, mkChar("sines"));
76
    if (left_cyclic(x, ldx, jmin, rank - 1, REAL(cosines), REAL(sines)))
76
    if (left_cyclic(x, ldx, jmin, rank - 1, REAL(cosines), REAL(sines)))
77
	error("Unknown error in getGivens");
77
	error(_("Unknown error in getGivens"));
78
    UNPROTECT(1);
78
    UNPROTECT(1);
79
    return ans;
79
    return ans;
80
}
80
}
81
 
81
 
82
SEXP checkGivens(SEXP X, SEXP jmin, SEXP rank)
82
SEXP checkGivens(SEXP X, SEXP jmin, SEXP rank)
Line 84... Line 84...
84
    SEXP ans = PROTECT(allocVector(VECSXP, 2)),
84
    SEXP ans = PROTECT(allocVector(VECSXP, 2)),
85
	Xcp = PROTECT(duplicate(X));
85
	Xcp = PROTECT(duplicate(X));
86
    int  *Xdims;
86
    int  *Xdims;
87
 
87
 
88
    if (!(isReal(X) & isMatrix(X)))
88
    if (!(isReal(X) & isMatrix(X)))
89
	error("X must be a numeric (double precision) matrix");
89
	error(_("X must be a numeric (double precision) matrix"));
90
    Xdims = INTEGER(coerceVector(getAttrib(X, R_DimSymbol), INTSXP));
90
    Xdims = INTEGER(coerceVector(getAttrib(X, R_DimSymbol), INTSXP));
91
    SET_VECTOR_ELT(ans, 1, getGivens(REAL(Xcp), Xdims[0],
91
    SET_VECTOR_ELT(ans, 1, getGivens(REAL(Xcp), Xdims[0],
92
				     asInteger(jmin), asInteger(rank)));
92
				     asInteger(jmin), asInteger(rank)));
93
    SET_VECTOR_ELT(ans, 0, Xcp);
93
    SET_VECTOR_ELT(ans, 0, Xcp);
94
    UNPROTECT(2);
94
    UNPROTECT(2);
Line 100... Line 100...
100
    SEXP ans;
100
    SEXP ans;
101
    int info, n, p, k, *Xdims, *ydims;
101
    int info, n, p, k, *Xdims, *ydims;
102
    double *xpx, d_one = 1., d_zero = 0.;
102
    double *xpx, d_one = 1., d_zero = 0.;
103
 
103
 
104
    if (!(isReal(X) & isMatrix(X)))
104
    if (!(isReal(X) & isMatrix(X)))
105
	error("X must be a numeric (double precision) matrix");
105
	error(_("X must be a numeric (double precision) matrix"));
106
    Xdims = INTEGER(coerceVector(getAttrib(X, R_DimSymbol), INTSXP));
106
    Xdims = INTEGER(coerceVector(getAttrib(X, R_DimSymbol), INTSXP));
107
    n = Xdims[0];
107
    n = Xdims[0];
108
    p = Xdims[1];
108
    p = Xdims[1];
109
    if (!(isReal(y) & isMatrix(y)))
109
    if (!(isReal(y) & isMatrix(y)))
110
	error("y must be a numeric (double precision) matrix");
110
	error(_("y must be a numeric (double precision) matrix"));
111
    ydims = INTEGER(coerceVector(getAttrib(y, R_DimSymbol), INTSXP));
111
    ydims = INTEGER(coerceVector(getAttrib(y, R_DimSymbol), INTSXP));
112
    if (ydims[0] != n)
112
    if (ydims[0] != n)
113
	error(
113
	error(_(
114
	    "number of rows in y (%d) does not match number of rows in X (%d)",
114
	    "number of rows in y (%d) does not match number of rows in X (%d)"),
115
	    ydims[0], n);
115
	    ydims[0], n);
116
    k = ydims[1];
116
    k = ydims[1];
117
    if (k < 1 || p < 1) return allocMatrix(REALSXP, p, k);
117
    if (k < 1 || p < 1) return allocMatrix(REALSXP, p, k);
118
    ans = PROTECT(allocMatrix(REALSXP, p, k));
118
    ans = PROTECT(allocMatrix(REALSXP, p, k));
119
    F77_CALL(dgemm)("T", "N", &p, &k, &n, &d_one, REAL(X), &n, REAL(y), &n,
119
    F77_CALL(dgemm)("T", "N", &p, &k, &n, &d_one, REAL(X), &n, REAL(y), &n,
120
		    &d_zero, REAL(ans), &p);
120
		    &d_zero, REAL(ans), &p);
121
    xpx = (double *) R_alloc(p * p, sizeof(double));
121
    xpx = (double *) R_alloc(p * p, sizeof(double));
122
    F77_CALL(dsyrk)("U", "T", &p, &n, &d_one, REAL(X), &n, &d_zero,
122
    F77_CALL(dsyrk)("U", "T", &p, &n, &d_one, REAL(X), &n, &d_zero,
123
		    xpx, &p);
123
		    xpx, &p);
124
    F77_CALL(dposv)("U", &p, &k, xpx, &p, REAL(ans), &p, &info);
124
    F77_CALL(dposv)("U", &p, &k, xpx, &p, REAL(ans), &p, &info);
125
    if (info) error("Lapack routine dposv returned error code %d", info);
125
    if (info) error(_("Lapack routine dposv returned error code %d"), info);
126
    UNPROTECT(1);
126
    UNPROTECT(1);
127
    return ans;
127
    return ans;
128
}
128
}
129
 
129
 
130
    
130
    
Line 133... Line 133...
133
    SEXP ans;
133
    SEXP ans;
134
    int info, n, p, k, *Xdims, *ydims, lwork;
134
    int info, n, p, k, *Xdims, *ydims, lwork;
135
    double *work, tmp, *xvals;
135
    double *work, tmp, *xvals;
136
 
136
 
137
    if (!(isReal(X) & isMatrix(X)))
137
    if (!(isReal(X) & isMatrix(X)))
138
	error("X must be a numeric (double precision) matrix");
138
	error(_("X must be a numeric (double precision) matrix"));
139
    Xdims = INTEGER(coerceVector(getAttrib(X, R_DimSymbol), INTSXP));
139
    Xdims = INTEGER(coerceVector(getAttrib(X, R_DimSymbol), INTSXP));
140
    n = Xdims[0];
140
    n = Xdims[0];
141
    p = Xdims[1];
141
    p = Xdims[1];
142
    if (!(isReal(y) & isMatrix(y)))
142
    if (!(isReal(y) & isMatrix(y)))
143
	error("y must be a numeric (double precision) matrix");
143
	error(_("y must be a numeric (double precision) matrix"));
144
    ydims = INTEGER(coerceVector(getAttrib(y, R_DimSymbol), INTSXP));
144
    ydims = INTEGER(coerceVector(getAttrib(y, R_DimSymbol), INTSXP));
145
    if (ydims[0] != n)
145
    if (ydims[0] != n)
146
	error(
146
	error(_(
147
	    "number of rows in y (%d) does not match number of rows in X (%d)",
147
	    "number of rows in y (%d) does not match number of rows in X (%d)"),
148
	    ydims[0], n);
148
	    ydims[0], n);
149
    k = ydims[1];
149
    k = ydims[1];
150
    if (k < 1 || p < 1) return allocMatrix(REALSXP, p, k);
150
    if (k < 1 || p < 1) return allocMatrix(REALSXP, p, k);
151
    xvals = (double *) R_alloc(n * p, sizeof(double));
151
    xvals = (double *) R_alloc(n * p, sizeof(double));
152
    Memcpy(xvals, REAL(X), n * p);
152
    Memcpy(xvals, REAL(X), n * p);
153
    ans = PROTECT(duplicate(y));
153
    ans = PROTECT(duplicate(y));
154
    lwork = -1;
154
    lwork = -1;
155
    F77_CALL(dgels)("N", &n, &p, &k, xvals, &n, REAL(ans), &n,
155
    F77_CALL(dgels)("N", &n, &p, &k, xvals, &n, REAL(ans), &n,
156
		    &tmp, &lwork, &info);
156
		    &tmp, &lwork, &info);
157
    if (info)
157
    if (info)
158
	error("First call to Lapack routine dgels returned error code %d",
158
	error(_("First call to Lapack routine dgels returned error code %d"),
159
	      info);
159
	      info);
160
    lwork = (int) tmp;
160
    lwork = (int) tmp;
161
    work = (double *) R_alloc(lwork, sizeof(double));
161
    work = (double *) R_alloc(lwork, sizeof(double));
162
    F77_CALL(dgels)("N", &n, &p, &k, xvals, &n, REAL(ans), &n,
162
    F77_CALL(dgels)("N", &n, &p, &k, xvals, &n, REAL(ans), &n,
163
		    work, &lwork, &info);
163
		    work, &lwork, &info);
164
    if (info)
164
    if (info)
165
	error("Second call to Lapack routine dgels returned error code %d",
165
	error(_("Second call to Lapack routine dgels returned error code %d"),
166
	      info);
166
	      info);
167
    UNPROTECT(1);
167
    UNPROTECT(1);
168
    return ans;
168
    return ans;
169
}
169
}
170
 
170
 
Line 173... Line 173...
173
    SEXP ans, Givens, Gcpy, nms, pivot, qraux, X;
173
    SEXP ans, Givens, Gcpy, nms, pivot, qraux, X;
174
    int i, n, nGivens = 0, p, trsz, *Xdims, rank;
174
    int i, n, nGivens = 0, p, trsz, *Xdims, rank;
175
    double rcond = 0., tol = asReal(tl), *work;
175
    double rcond = 0., tol = asReal(tl), *work;
176
 
176
 
177
    if (!(isReal(Xin) & isMatrix(Xin)))
177
    if (!(isReal(Xin) & isMatrix(Xin)))
178
	error("X must be a real (numeric) matrix");
178
	error(_("X must be a real (numeric) matrix"));
179
    if (tol < 0.) error("tol, given as %g, must be non-negative", tol);
179
    if (tol < 0.) error(_("tol, given as %g, must be non-negative"), tol);
180
    if (tol > 1.) error("tol, given as %g, must be <= 1", tol);
180
    if (tol > 1.) error(_("tol, given as %g, must be <= 1"), tol);
181
    ans = PROTECT(allocVector(VECSXP,5));
181
    ans = PROTECT(allocVector(VECSXP,5));
182
    SET_VECTOR_ELT(ans, 0, X = duplicate(Xin));
182
    SET_VECTOR_ELT(ans, 0, X = duplicate(Xin));
183
    Xdims = INTEGER(coerceVector(getAttrib(X, R_DimSymbol), INTSXP));
183
    Xdims = INTEGER(coerceVector(getAttrib(X, R_DimSymbol), INTSXP));
184
    n = Xdims[0]; p = Xdims[1];
184
    n = Xdims[0]; p = Xdims[1];
185
    SET_VECTOR_ELT(ans, 2, qraux = allocVector(REALSXP, (n < p) ? n : p));
185
    SET_VECTOR_ELT(ans, 2, qraux = allocVector(REALSXP, (n < p) ? n : p));
Line 199... Line 199...
199
	double *xpt = REAL(X), tmp;
199
	double *xpt = REAL(X), tmp;
200
 
200
 
201
	lwork = -1;
201
	lwork = -1;
202
	F77_CALL(dgeqrf)(&n, &p, xpt, &n, REAL(qraux), &tmp, &lwork, &info);
202
	F77_CALL(dgeqrf)(&n, &p, xpt, &n, REAL(qraux), &tmp, &lwork, &info);
203
	if (info)
203
	if (info)
204
	    error("First call to dgeqrf returned error code %d", info);
204
	    error(_("First call to dgeqrf returned error code %d"), info);
205
	lwork = (int) tmp;
205
	lwork = (int) tmp;
206
	work = (double *) R_alloc((lwork < 3*trsz) ? 3*trsz : lwork,
206
	work = (double *) R_alloc((lwork < 3*trsz) ? 3*trsz : lwork,
207
				  sizeof(double));
207
				  sizeof(double));
208
	F77_CALL(dgeqrf)(&n, &p, xpt, &n, REAL(qraux), work, &lwork, &info);
208
	F77_CALL(dgeqrf)(&n, &p, xpt, &n, REAL(qraux), work, &lwork, &info);
209
	if (info)
209
	if (info)
210
	    error("Second call to dgeqrf returned error code %d", info);
210
	    error(_("Second call to dgeqrf returned error code %d"), info);
211
	iwork = (int *) R_alloc(trsz, sizeof(int));
211
	iwork = (int *) R_alloc(trsz, sizeof(int));
212
	F77_CALL(dtrcon)("1", "U", "N", &rank, xpt, &n, &rcond,
212
	F77_CALL(dtrcon)("1", "U", "N", &rank, xpt, &n, &rcond,
213
			 work, iwork, &info);
213
			 work, iwork, &info);
214
	if (info)
214
	if (info)
215
	    error("Lapack routine dtrcon returned error code %d", info);
215
	    error(_("Lapack routine dtrcon returned error code %d"), info);
216
	while (rcond < tol) {	/* check diagonal elements */
216
	while (rcond < tol) {	/* check diagonal elements */
217
	    double minabs = (xpt[0] < 0.) ? -xpt[0]: xpt[0];
217
	    double minabs = (xpt[0] < 0.) ? -xpt[0]: xpt[0];
218
	    int jmin = 0;
218
	    int jmin = 0;
219
	    for (i = 1; i < rank; i++) {
219
	    for (i = 1; i < rank; i++) {
220
		double el = xpt[i*(n+1)];
220
		double el = xpt[i*(n+1)];
Line 230... Line 230...
230
	    }		
230
	    }		
231
	    rank--;
231
	    rank--;
232
	    F77_CALL(dtrcon)("1", "U", "N", &rank, xpt, &n, &rcond,
232
	    F77_CALL(dtrcon)("1", "U", "N", &rank, xpt, &n, &rcond,
233
			     work, iwork, &info);
233
			     work, iwork, &info);
234
	    if (info)
234
	    if (info)
235
		error("Lapack routine dtrcon returned error code %d", info);
235
		error(_("Lapack routine dtrcon returned error code %d"), info);
236
	}
236
	}
237
    }
237
    }
238
    SET_VECTOR_ELT(ans, 4, Gcpy = allocVector(VECSXP, nGivens));
238
    SET_VECTOR_ELT(ans, 4, Gcpy = allocVector(VECSXP, nGivens));
239
    for (i = 0; i < nGivens; i++)
239
    for (i = 0; i < nGivens; i++)
240
	SET_VECTOR_ELT(Gcpy, i, VECTOR_ELT(Givens, i));
240
	SET_VECTOR_ELT(Gcpy, i, VECTOR_ELT(Givens, i));