/
lsConstrain.c
307 lines (231 loc) · 6.45 KB
/
lsConstrain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* $Author: sinnwell $ */
/* $Date: 2006/11/28 21:21:00 $ */
/* $Header: /people/biostat3/sinnwell/Projects/IBDReg/Make/RCS/lsConstrain.c,v 1.2 2006/11/28 21:21:00 sinnwell Exp $ */
/* $Locker: $ */
/* SUBROUTINE LSTSQ
ALGORITHM AS225 APPL. STATIST. (1987) VOL. 36, NO. 2
COMPUTES THE LEAST-SQUARES PROJECTION OF X ONTO
THE INTERSECTION OF K SIMULTANEOUS AFFINE CONSTRAINTS,
USING THE MAHALANOBIS DISTANCE DETERMINED BY S.
THE ITH CONSTRAINT IS OF THE FORM
SUM OVER J OF A(I,J)*X(J) (.LE.,.EQ.) B(I)
--.LE. IF IFLAG(I) .EQ. 0
--.EQ. IF IFLAG(I) .EQ. 1
Translated from Fortran to C by Dan Schaid 9/9/2005
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <R.h>
#include <R_ext/Error.h>
#define ulong unsigned
static double **double_vec_to_mat(double *Yvec, ulong nrow, ulong ncol);
static double **double_matrix(ulong nrow, ulong ncol);
static void errmsg(char *string);
/*************** Global vars ******************************************************/
/* s and a are global, so we can later return to S, and then free
them from memory. We do this because there are multiple ways to
return from the lsConstrain function, and so we want to avoid
redundant free of memory */
/**********************************************************************************/
void lsConstrain(
/* SUBROUTINE LSTSQ(X, S, A, B, IFLAG, N, K, NWORK, ITMAX, EPS, EPS2,
* W, XHAT, XKT, ITER, SUPDIF, IFAULT)
*/
double *x, /* vector of length N */
int *n_in,
int *k_in,
int *nwork_in,
int *itmax,
double *eps,
double *eps2,
double *svec, /* to be an N x N matrix */
double *avec, /* A(K, N) */
double *b, /* B(K) */
int *iflag, /* vec length K */
double *xhat, /* vec length N */
double *xkt, /* vec length K */
int *iter,
double *supdif,
int *ifault,
double *w)
{
double zero = 0.0;
double two = 2.0;
double **s, **a;
int k = *k_in;
int n = *n_in;
int nk = n * k;
int nk2 = nk + nk;
int nk2k = nk2 + k;
int index;
int i,j,l;
int ind1, ind2;
double abdif;
double sum, temp;
/* need to have a function to later call (from S) to free mem */
s = double_vec_to_mat(svec, n, n);
a = double_vec_to_mat(avec, k, n);
*ifault = 0;
for(i=1; i<=k; i++){
for(j=1; j<=n; j++){
index = nk + (j - 1) * k + i;
w[index-1] = zero;
for(l=1; l<=n; l++){
w[index-1] += a[i-1][l-1] * s[l-1][j-1];
}
}
}
for(i=1; i<=k; i++){
index = nk2 + i;
w[index-1] = zero;
for(j=1; j<=n; j++){
ind2 = nk + (j - 1) * k + i;
w[index-1] += a[i-1][j-1] * w[ind2-1];
}
if(fabs(w[index-1]) <= (*eps2)){
*ifault = 3;
/* free memory */
for(i=0; i< n; i++){
Free(s[i]);
}
Free(s);
s = NULL;
for(i=0; i< k; i++){
Free(a[i]);
}
Free(a);
a = NULL;
return;
}
}
for(j=1; j<=n; j++){
index = nk2k + j;
w[index-1] = x[j-1];
xhat[j-1] = x[j-1];
for(i=1; i<=k; i++){
index = (i - 1) * n + j;
w[index-1] = zero;
}
}
*iter = 0;
while( (*iter) < (*itmax)){
(*iter) ++;
(*supdif) = zero;
for(i=1; i<=k; i++){
for(j=1; j<=n; j++){
index = (i - 1) * n + j;
xhat[j-1] += w[index-1];
}
sum = zero;
for(j=1; j<=n; j++){
sum += a[i-1][j-1] * xhat[j-1];
}
sum = sum - b[i-1];
if(iflag[i-1] == 0 && sum <= zero)
{
for(j=1; j<=n; j++){
index = (i - 1) * n + j;
w[index-1] = zero;
}
}
else
{
index = nk2 + i;
temp = sum / w[index-1];
for(j=1; j<=n; j++){
ind1 = (i - 1) * n + j;
ind2 = nk + (j - 1) * k + i;
w[ind1-1] = w[ind2-1] * temp;
xhat[j-1] = xhat[j-1] - w[ind1-1];
}
}
/* FIND LARGEST CHANGE, AND CHECK FOR CONVERGENCE */
for(j=1; j<=n; j++){
index = nk2k + j;
abdif = fabs(xhat[j-1] - w[index-1]);
if( (*supdif) < abdif) (*supdif) = abdif;
}
}
if( (*supdif) <= (*eps)){
for(i=1; i<=k; i++){
/* FIND A NON-ZERO DENOMINATOR:*/
for(j=1; j<=n; j++){
index = nk + (j-1) * k + i;
if(fabs(w[index-1]) > (*eps2)) break;
}
ind2 = (i-1) * n + j;
xkt[i-1] = two * w[ind2-1] / w[index-1];
}
/* free memory */
for(i=0; i< n; i++){
Free(s[i]);
}
Free(s);
s = NULL;
for(i=0; i< k; i++){
Free(a[i]);
}
Free(a);
a = NULL;
return;
}
for(j=1; j<=n; j++){
index = nk2k + j;
w[index-1] = xhat[j-1];
}
} /* end while loop */
*ifault = 1;
/* free memory */
for(i=0; i< n; i++){
Free(s[i]);
}
Free(s);
s = NULL;
for(i=0; i< k; i++){
Free(a[i]);
}
Free(a);
a = NULL;
return;
}
/***********************************************************************************/
static double **double_vec_to_mat(double *Yvec, ulong nrow, ulong ncol){
ulong i,j,k;
double **Y;
Y=double_matrix(nrow,ncol);
k=0;
for (j=0;j<ncol;j++){
for (i=0;i<nrow;i++){
Y[i][j]=Yvec[k];
k++;
}
}
return Y;
}
/***********************************************************************************/
static double **double_matrix(ulong nrow, ulong ncol){
/* allocate double matrix with subscript range m[0 ..(nrow-1)][0..(ncol-1)] */
ulong i;
double **m;
/* allocate pointers to rows */
m=(double **) Calloc(nrow, double *);
if (!m) errmsg("mem alloc failure 1 in double_matrix");
/* allocate vec of memory for each row */
for(i=0;i<nrow;i++) {
m[i]=(double *) Calloc(ncol, double);
if(!m[i]) errmsg("mem alloc failure 2 in double_matrix");
}
/* return pointer to array of pointers to rows */
return m;
}
/***********************************************************************************/
static void errmsg(char *string){
/* Function to emulate "stop" of S+ - see page 134, S Programing, by
Venables and Ripley */
/* PROBLEM "%s", string RECOVER(NULL_ENTRY);
For R 4.1.x and later, use Rf_error from R_ext/Error.h
*/
Rf_error(string);
}