-
Notifications
You must be signed in to change notification settings - Fork 6
/
sdpa_jordan.cpp
411 lines (352 loc) · 10.5 KB
/
sdpa_jordan.cpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* -------------------------------------------------------------
This file is a component of SDPA
Copyright (C) 2004 SDPA Project
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
------------------------------------------------------------- */
#include <sdpa_jordan.h>
#include <sdpa_dataset.h>
namespace sdpa {
mpf_class Jal::trace(DenseLinearSpace& aMat)
{
mpf_class ret = 0.0;
for (int l=0; l<aMat.SDP_nBlock; ++l) {
mpf_class* target = aMat.SDP_block[l].de_ele;
int size = aMat.SDP_block[l].nRow;
for (int j=0; j<size; ++j) {
ret += target[j*size+j];
}
}
for (int l=0; l<aMat.SOCP_nBlock; ++l) {
rError("dataset:: current version do not support SOCP");
}
for (int l=0; l<aMat.LP_nBlock; ++l) {
ret += aMat.LP_block[l];
}
return ret;
}
// calculate the minimum eigen value of lMat*xMat*(lMat^T)
// lMat is lower triangular¡¢xMat is symmetric
// block size > 20 : Lanczos method
// block size <= 20 : QR method
// QR method: workVec is temporary space and needs
// 3*xMat.nRow-1 length memory.
mpf_class Jal::getMinEigen(DenseLinearSpace& lMat,
DenseLinearSpace& xMat,
WorkVariables& work)
{
mpf_class min = 1.0E50;
mpf_class value;
// for SDP
for (int l=0; l<xMat.SDP_nBlock; ++l) {
if (xMat.SDP_block[l].nRow > 20){ // use Lanczos method
value = Lal::getMinEigen(lMat.SDP_block[l],
xMat.SDP_block[l],
work.DLS1.SDP_block[l],
work.SDP_BV1.ele[l],
work.SDP_BV2.ele[l],
work.SDP_BV3.ele[l],
work.SDP_BV4.ele[l],
work.SDP_BV5.ele[l],
work.SDP_BV6.ele[l],
work.SDP_BV7.ele[l],
work.SDP_BV8.ele[l],
work.SDP_BV9.ele[l],
work.SDP2_BV1.ele[l]);
} else { // use QR method
Lal::let(work.DLS2.SDP_block[l],'=',xMat.SDP_block[l],'T',lMat.SDP_block[l]);
Lal::let(work.DLS1.SDP_block[l],'=',lMat.SDP_block[l],'*',work.DLS2.SDP_block[l]);
Lal::getMinEigenValue(work.DLS1.SDP_block[l],work.SDP_BV1.ele[l],work.SDP2_BV1.ele[l]);
value = work.SDP_BV1.ele[l].ele[0];
}
if (value < min) {
min = value;
}
} // end of 'for (int l)'
// for SOCP
for (int l=0; l<xMat.SOCP_nBlock; ++l) {
rError("getMinEigen:: current version does not support SOCP");
}
// for LP
for (int l=0; l<xMat.LP_nBlock; ++l) {
value = xMat.LP_block[l] * lMat.LP_block[l] * lMat.LP_block[l];
if (value < min) {
min = value;
}
} // end of 'for (int l)'
return min;
};
// calculate the minimum eigen value of xMat by QR method.
mpf_class Jal::getMinEigen(DenseLinearSpace& xMat,
WorkVariables& work)
{
mpf_class min = 1.0E50;
mpf_class value;
work.DLS1.copyFrom(xMat);
// for SDP
for (int l=0; l<xMat.SDP_nBlock; ++l) {
Lal::getMinEigenValue(work.DLS1.SDP_block[l],work.SDP_BV1.ele[l],work.SDP2_BV1.ele[l]);
value = work.SDP_BV1.ele[l].ele[0];
if (value < min) {
min = value;
}
} // end of 'for (int l)'
// for SOCP
for (int l=0; l<xMat.SOCP_nBlock; ++l) {
rError("getMinEigen:: current version does not support SOCP");
}
// for LP
for (int l=0; l<xMat.LP_nBlock; ++l) {
value = xMat.LP_block[l];
if (value < min) {
min = value;
}
} // end of 'for (int l)'
return min;
};
bool Jal::getInvChol(DenseLinearSpace& invCholMat,
DenseLinearSpace& aMat,
DenseLinearSpace& workMat)
{
// for SDP
if (workMat.SDP_nBlock!=aMat.SDP_nBlock
|| invCholMat.SDP_nBlock!=aMat.SDP_nBlock) {
rError("getInvChol:: different memory size");
}
for (int l=0; l<aMat.SDP_nBlock; ++l) {
if (Lal::getCholesky(workMat.SDP_block[l],aMat.SDP_block[l]) == false) {
return false;
}
Lal::getInvLowTriangularMatrix(invCholMat.SDP_block[l],
workMat.SDP_block[l]);
}
// for SOCP
for (int l=0; l<aMat.SOCP_nBlock; ++l) {
rError("no support for SOCP");
}
// fo LP
if (invCholMat.LP_nBlock!=aMat.LP_nBlock) {
rError("getInvChol:: different memory size");
}
for (int l=0; l<aMat.LP_nBlock; ++l) {
if (aMat.LP_block[l] < 0.0){
return false;
}
invCholMat.LP_block[l] = 1.0 / sqrt(aMat.LP_block[l]);
}
return _SUCCESS;
}
bool Jal::getInvCholAndInv(DenseLinearSpace& invCholMat,
DenseLinearSpace& inverseMat,
DenseLinearSpace& aMat,
DenseLinearSpace& workMat)
{
mpf_class MONE = 1.0;
if (getInvChol(invCholMat, aMat, workMat)== false) {
return FAILURE;
}
for (int l=0; l<aMat.SDP_nBlock; ++l) {
inverseMat.SDP_block[l].copyFrom(invCholMat.SDP_block[l]);
Rtrmm ("Left","Lower","Transpose","NonUnitDiag",
invCholMat.SDP_block[l].nRow,
invCholMat.SDP_block[l].nCol,
MONE,
invCholMat.SDP_block[l].de_ele,
invCholMat.SDP_block[l].nRow,
inverseMat.SDP_block[l].de_ele,
inverseMat.SDP_block[l].nRow);
}
for (int l=0; l<aMat.SOCP_nBlock; ++l) {
rError("rNewton:: we don't make this ruoutin");
}
for (int l=0; l<aMat.LP_nBlock; ++l) {
inverseMat.LP_block[l] = 1.0 / aMat.LP_block[l];
}
return _SUCCESS;
}
bool Jal::multiply(DenseLinearSpace& retMat,
DenseLinearSpace& aMat,
DenseLinearSpace& bMat,
mpf_class* scalar)
{
bool total_judge = _SUCCESS;
// for SDP
if (retMat.SDP_nBlock!=aMat.SDP_nBlock
|| retMat.SDP_nBlock!=bMat.SDP_nBlock) {
rError("multiply:: different nBlock size");
}
for (int l=0; l<retMat.SDP_nBlock; ++l) {
bool judge = Lal::multiply(retMat.SDP_block[l],aMat.SDP_block[l],
bMat.SDP_block[l],scalar);
if (judge == FAILURE) {
total_judge = FAILURE;
}
}
// for SOCP
#if 0
if (retMat.SOCP_nBlock!=aMat.SOCP_nBlock
|| retMat.SOCP_nBlock!=bMat.SOCP_nBlock) {
rError("multiply:: different nBlock size");
}
for (int l=0; l<retMat.SOCP_nBlock; ++l) {
bool judge = Lal::multiply(retMat.SOCP_block[l],aMat.SOCP_block[l],
bMat.SOCP_block[l],scalar);
if (judge == FAILURE) {
total_judge = FAILURE;
}
}
#endif
// for LP
if (retMat.LP_nBlock!=aMat.LP_nBlock
|| retMat.LP_nBlock!=bMat.LP_nBlock) {
rError("multiply:: different nBlock size");
}
for (int l=0; l<retMat.LP_nBlock; ++l) {
if (scalar == NULL) {
retMat.LP_block[l] = aMat.LP_block[l] * bMat.LP_block[l];
} else {
retMat.LP_block[l] = aMat.LP_block[l] * bMat.LP_block[l] * (*scalar);
}
}
return total_judge;
}
#if 0
// CAUTION!!! We don't initialize retMat to zero matrix for efficiently.
bool Jal::multiply(DenseLinearSpace& retMat,
SparseLinearSpace& aMat,
DenseLinearSpace& bMat,
mpf_class* scalar)
{
bool total_judge = _SUCCESS;
// for SDP
for (int l=0; l<aMat.SDP_sp_nBlock; ++l) {
int index = aMat.SDP_sp_index[l];
bool judge = Lal::multiply(retMat.SDP_block[index],aMat.SDP_sp_block[l],
bMat.SDP_block[index],scalar);
if (judge == FAILURE) {
total_judge = FAILURE;
}
}
// for SOCP
for (int l=0; l<aMat.SOCP_sp_nBlock; ++l) {
int index = aMat.SOCP_sp_index[l];
bool judge = Lal::multiply(retMat.SOCP_block[index],aMat.SOCP_sp_block[l],
bMat.SOCP_block[index],scalar);
if (judge == FAILURE) {
total_judge = FAILURE;
}
}
// for LP
for (int l=0; l<aMat.LP_sp_nBlock; ++l) {
int index = aMat.LP_sp_index[l];
if (scalar == NULL) {
retMat.LP_block[index] =
aMat.LP_sp_block[l] * bMat.LP_block[index];
} else {
retMat.LP_block[index] =
aMat.LP_sp_block[l] * bMat.LP_block[index] * (*scalar);
}
}
return total_judge;
}
// CAUTION!!! We don't initialize retMat to zero matrix for efficiently.
bool Jal::multiply(DenseLinearSpace& retMat,
DenseLinearSpace& aMat,
SparseLinearSpace& bMat,
mpf_class* scalar )
{
bool total_judge = _SUCCESS;
// for SDP
for (int l=0; l<bMat.SDP_sp_nBlock; ++l) {
int index = bMat.SDP_sp_index[l];
bool judge = Lal::multiply(retMat.SDP_block[index],aMat.SDP_block[index],
bMat.SDP_sp_block[l],scalar);
if (judge == FAILURE) {
total_judge = FAILURE;
}
}
// for SOCP
#if 0
for (int l=0; l<bMat.SOCP_sp_nBlock; ++l) {
int index = bMat.SOCP_sp_index[l];
bool judge = Lal::multiply(retMat.SOCP_block[index],aMat.SOCP_block[index],
bMat.SOCP_sp_block[l],scalar);
if (judge == FAILURE) {
total_judge = FAILURE;
}
}
#endif
// for LP
for (int l=0; l<bMat.LP_sp_nBlock; ++l) {
int index = bMat.LP_sp_index[l];
if (scalar == NULL) {
retMat.LP_block[index] =
aMat.LP_block[index] * bMat.LP_sp_block[l];
} else {
retMat.LP_block[index] =
aMat.LP_block[index] * bMat.LP_sp_block[l] * (*scalar);
}
}
return total_judge;
}
#endif
// retMat = (A * B + B * A)/2
bool Jal::jordan_product(DenseLinearSpace& retMat,
DenseLinearSpace& aMat,
DenseLinearSpace& bMat)
{
multiply(retMat,aMat,bMat);
Lal::getSymmetrize(retMat);
return _SUCCESS;
}
// retMat = A * B
bool Jal::ns_jordan_product(DenseLinearSpace& retMat,
DenseLinearSpace& aMat,
DenseLinearSpace& bMat)
{
multiply(retMat,aMat,bMat);
return _SUCCESS;
}
// retMat = A * B * A
bool Jal::jordan_quadratic_product(DenseLinearSpace& retMat,
DenseLinearSpace& aMat,
DenseLinearSpace& bMat,
DenseLinearSpace& workMat)
{
multiply(workMat,aMat,bMat);
multiply(retMat,workMat,aMat);
return _SUCCESS;
}
// retMat = (A * B * C + C * B * A)/2
bool Jal::jordan_triple_product(DenseLinearSpace& retMat,
DenseLinearSpace& aMat,
DenseLinearSpace& bMat,
DenseLinearSpace& cMat,
DenseLinearSpace& workMat)
{
multiply(workMat,aMat,bMat);
multiply(retMat,workMat,cMat);
Lal::getSymmetrize(retMat);
return _SUCCESS;
}
// retMat = A * B * C
bool Jal::ns_jordan_triple_product(DenseLinearSpace& retMat,
DenseLinearSpace& aMat,
DenseLinearSpace& bMat,
DenseLinearSpace& cMat,
DenseLinearSpace& workMat)
{
multiply(workMat,aMat,bMat);
multiply(retMat,workMat,cMat);
return _SUCCESS;
}
}