-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEigenQP.cpp
830 lines (746 loc) · 21.4 KB
/
EigenQP.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/*
Author: Luca Di Gaspero
DIEGM - University of Udine, Italy
http://www.diegm.uniud.it/digaspero/
LICENSE
This file is part of QuadProg++: a C++ library implementing
the algorithm of Goldfarb and Idnani for the solution of a (convex)
Quadratic Programming problem by means of an active-set dual method.
Copyright (C) 2007-2009 Luca Di Gaspero.
Copyright (C) 2009 Eric Moyer.
QuadProg++ is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QuadProg++ 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with QuadProg++. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <limits>
#include <sstream>
#include <stdexcept>
#include "EigenQP.h"
#include <vector>
//#define TRACE_SOLVER
//#include <boost/numeric/ublas/vector.hpp>
//#include <boost/numeric/ublas/matrix.hpp>
using namespace Eigen;
using std::vector;
//namespace ublas = boost::numeric::ublas;
namespace QP {
// Utility functions for updating some data needed by the solution method
void compute_d(VectorXd& d, const MatrixXd& J, const VectorXd& np);
void update_z(VectorXd& z, const MatrixXd& J, const VectorXd& d, int iq);
void update_r(const MatrixXd& R, VectorXd& r, const VectorXd& d, int iq);
bool add_constraint(MatrixXd& R, MatrixXd& J, VectorXd& d, int& iq, double& rnorm);
void delete_constraint(MatrixXd& R, MatrixXd& J, VectorXi& A, VectorXd& u, int n, int p, int& iq, int l);
// Utility functions for computing the Cholesky decomposition and solving
// linear systems
void cholesky_decomposition(MatrixXd& A);
void cholesky_solve(const MatrixXd& L, VectorXd& x, const VectorXd& b);
void forward_elimination(const MatrixXd& L, VectorXd& y, const VectorXd& b);
void backward_elimination(const MatrixXd& U, VectorXd& x, const VectorXd& y);
// Utility functions for computing the scalar product and the euclidean
// distance between two numbers
double scalar_product(const VectorXd& x, const VectorXd& y);
double distance(double a, double b);
// Utility functions for printing ublas::vectors and matrices
void print_matrix(const char* name, const MatrixXd& A, int n = -1, int m = -1);
//template<typename T>
//void print_vector(const char* name, const ublas::vector<T>& v, int n = -1);
// The Solving function, implementing the Goldfarb-Idnani method
double solve_quadprog(MatrixXd& G, VectorXd& g0,
const MatrixXd& CE, const VectorXd& ce0,
const MatrixXd& CI, const VectorXd& ci0,
VectorXd& x)
{
std::ostringstream msg;
{
//Ensure that the dimensions of the matrices and ublas::vectors can be
//safely converted from unsigned int into to int without overflow.
unsigned mx = std::numeric_limits<int>::max();
if(G.cols() >= mx || G.rows() >= mx ||
CE.rows() >= mx || CE.cols() >= mx ||
CI.rows() >= mx || CI.cols() >= mx ||
ci0.size() >= mx || ce0.size() >= mx || g0.size() >= mx){
msg << "The dimensions of one of the input matrices or ublas::vectors were "
<< "too large." << std::endl
<< "The maximum allowable size for inputs to solve_quadprog is:"
<< mx << std::endl;
throw std::logic_error(msg.str());
}
}
int n = G.cols(), p = CE.cols(), m = CI.cols();
if ((int)G.rows() != n)
{
msg << "The ublas::matrix G is not a square ublas::matrix (" << G.rows() << " x "
<< G.cols() << ")";
throw std::logic_error(msg.str());
}
if ((int)CE.rows() != n)
{
msg << "The ublas::matrix CE is incompatible (incorrect number of rows "
<< CE.rows() << " , expecting " << n << ")";
throw std::logic_error(msg.str());
}
if ((int)ce0.size() != p)
{
msg << "The ublas::vector ce0 is incompatible (incorrect dimension "
<< ce0.size() << ", expecting " << p << ")";
throw std::logic_error(msg.str());
}
if ((int)CI.rows() != n)
{
msg << "The ublas::matrix CI is incompatible (incorrect number of rows "
<< CI.rows() << " , expecting " << n << ")";
throw std::logic_error(msg.str());
}
if ((int)ci0.size() != m)
{
msg << "The ublas::vector ci0 is incompatible (incorrect dimension "
<< ci0.size() << ", expecting " << m << ")";
throw std::logic_error(msg.str());
}
x.resize(n);
register int i, j, k, l; /* indices */
int ip; // this is the index of the constraint to be added to the active set
MatrixXd R(n, n), J(n, n);
VectorXd s(m + p), z(n), r(m + p), d(n), np(n), u(m + p), x_old(n), u_old(m + p);
double f_value, psi, c1, c2, sum, ss, R_norm;
double inf;
if (std::numeric_limits<double>::has_infinity)
inf = std::numeric_limits<double>::infinity();
else
inf = 1.0E300;
double t, t1, t2; /* t is the step lenght, which is the minimum of the partial step length t1
* and the full step length t2 */
VectorXi A(m + p), A_old(m + p), iai(m + p);
int q, iq, iter = 0;
vector<bool> iaexcl(m + p);
/* p is the number of equality constraints */
/* m is the number of inequality constraints */
q = 0; /* size of the active set A (containing the indices of the active constraints) */
#ifdef TRACE_SOLVER
std::cout << std::endl << "Starting solve_quadprog" << std::endl;
print_ublas::matrix("G", G);
print_ublas::vector("g0", g0);
print_ublas::matrix("CE", CE);
print_ublas::vector("ce0", ce0);
print_ublas::matrix("CI", CI);
print_ublas::vector("ci0", ci0);
#endif
/*
* Preprocessing phase
*/
/* compute the trace of the original ublas::matrix G */
c1 = 0.0;
for (i = 0; i < n; i++)
{
c1 += G(i, i);
}
/* decompose the ublas::matrix G in the form L^T L */
cholesky_decomposition(G);
#ifdef TRACE_SOLVER
print_matrix("G", G);
#endif
/* initialize the ublas::matrix R */
for (i = 0; i < n; i++)
{
d(i) = 0.0;
for (j = 0; j < n; j++)
R(i, j) = 0.0;
}
R_norm = 1.0; /* this variable will hold the norm of the ublas::matrix R */
/* compute the inverse of the factorized ublas::matrix G^-1, this is the initial value for H */
c2 = 0.0;
for (i = 0; i < n; i++)
{
d(i) = 1.0;
forward_elimination(G, z, d);
for (j = 0; j < n; j++)
J(i, j) = z(j);
c2 += z(i);
d(i) = 0.0;
}
#ifdef TRACE_SOLVER
print_ublas::matrix("J", J);
#endif
/* c1 * c2 is an estimate for cond(G) */
/*
* Find the unconstrained minimizer of the quadratic form 0.5 * x G x + g0 x
* this is a feasible point in the dual space
* x = G^-1 * g0
*/
cholesky_solve(G, x, g0);
for (i = 0; i < n; i++)
x(i) = -x(i);
/* and compute the current solution value */
f_value = 0.5 * scalar_product(g0, x);
#ifdef TRACE_SOLVER
std::cout << "Unconstrained solution: " << f_value << std::endl;
print_ublas::vector("x", x);
#endif
/* Add equality constraints to the working set A */
iq = 0;
for (i = 0; i < p; i++)
{
for (j = 0; j < n; j++)
np(j) = CE(j, i);
compute_d(d, J, np);
update_z(z, J, d, iq);
update_r(R, r, d, iq);
#ifdef TRACE_SOLVER
print_ublas::matrix("R", R, n, iq);
print_ublas::vector("z", z);
print_ublas::vector("r", r, iq);
print_ublas::vector("d", d);
#endif
/* compute full step length t2: i.e., the minimum step in primal space s.t. the contraint
becomes feasible */
t2 = 0.0;
if (fabs(scalar_product(z, z)) > std::numeric_limits<double>::epsilon()) // i.e. z != 0
t2 = (-scalar_product(np, x) - ce0(i)) / scalar_product(z, np);
/* set x = x + t2 * z */
for (k = 0; k < n; k++)
x(k) += t2 * z(k);
/* set u = u+ */
u(iq) = t2;
for (k = 0; k < iq; k++)
u(k) -= t2 * r(k);
/* compute the new solution value */
f_value += 0.5 * (t2 * t2) * scalar_product(z, np);
A(i) = -i - 1;
if (!add_constraint(R, J, d, iq, R_norm))
{
// Equality constraints are linearly dependent
throw std::runtime_error("Constraints are linearly dependent");
return f_value;
}
}
/* set iai = K \ A */
for (i = 0; i < m; i++)
iai(i) = i;
l1: iter++;
#ifdef TRACE_SOLVER
print_ublas::vector("x", x);
#endif
/* step 1: choose a violated constraint */
for (i = p; i < iq; i++)
{
ip = A(i);
iai(ip) = -1;
}
/* compute s(x) = ci^T * x + ci0 for all elements of K \ A */
ss = 0.0;
psi = 0.0; /* this value will contain the sum of all infeasibilities */
ip = 0; /* ip will be the index of the chosen violated constraint */
for (i = 0; i < m; i++)
{
iaexcl[i] = true;
sum = 0.0;
for (j = 0; j < n; j++)
sum += CI(j, i) * x(j);
sum += ci0(i);
s(i) = sum;
psi += std::min(0.0, sum);
}
#ifdef TRACE_SOLVER
print_ublas::vector("s", s, m);
#endif
if (fabs(psi) <= m * std::numeric_limits<double>::epsilon() * c1 * c2* 100.0)
{
/* numerically there are not infeasibilities anymore */
q = iq;
return f_value;
}
/* save old values for u and A */
for (i = 0; i < iq; i++)
{
u_old(i) = u(i);
A_old(i) = A(i);
}
/* and for x */
for (i = 0; i < n; i++)
x_old(i) = x(i);
l2: /* Step 2: check for feasibility and determine a new S-pair */
for (i = 0; i < m; i++)
{
if (s(i) < ss && iai(i) != -1 && iaexcl[i])
{
ss = s(i);
ip = i;
}
}
if (ss >= 0.0)
{
q = iq;
return f_value;
}
/* set np = n(ip) */
for (i = 0; i < n; i++)
np(i) = CI(i, ip);
/* set u = (u 0)^T */
u(iq) = 0.0;
/* add ip to the active set A */
A(iq) = ip;
#ifdef TRACE_SOLVER
std::cout << "Trying with constraint " << ip << std::endl;
print_ublas::vector("np", np);
#endif
l2a:/* Step 2a: determine step direction */
/* compute z = H np: the step direction in the primal space (through J, see the paper) */
compute_d(d, J, np);
update_z(z, J, d, iq);
/* compute N* np (if q > 0): the negative of the step direction in the dual space */
update_r(R, r, d, iq);
#ifdef TRACE_SOLVER
std::cout << "Step direction z" << std::endl;
print_ublas::vector("z", z);
print_ublas::vector("r", r, iq + 1);
print_ublas::vector("u", u, iq + 1);
print_ublas::vector("d", d);
print_ublas::vector("A", A, iq + 1);
#endif
/* Step 2b: compute step length */
l = 0;
/* Compute t1: partial step length (maximum step in dual space without violating dual feasibility */
t1 = inf; /* +inf */
/* find the index l s.t. it reaches the minimum of u+(x) / r */
for (k = p; k < iq; k++)
{
if (r(k) > 0.0)
{
if (u(k) / r(k) < t1)
{
t1 = u(k) / r(k);
l = A(k);
}
}
}
/* Compute t2: full step length (minimum step in primal space such that the constraint ip becomes feasible */
if (fabs(scalar_product(z, z)) > std::numeric_limits<double>::epsilon()) // i.e. z != 0
t2 = -s(ip) / scalar_product(z, np);
else
t2 = inf; /* +inf */
/* the step is chosen as the minimum of t1 and t2 */
t = std::min(t1, t2);
#ifdef TRACE_SOLVER
std::cout << "Step sizes: " << t << " (t1 = " << t1 << ", t2 = " << t2 << ") ";
#endif
/* Step 2c: determine new S-pair and take step: */
/* case (i): no step in primal or dual space */
if (t >= inf)
{
/* QPP is infeasible */
// FIXME: unbounded to raise
q = iq;
return inf;
}
/* case (ii): step in dual space */
if (t2 >= inf)
{
/* set u = u + t * (-r 1) and drop constraint l from the active set A */
for (k = 0; k < iq; k++)
u(k) -= t * r(k);
u(iq) += t;
iai(l) = l;
delete_constraint(R, J, A, u, n, p, iq, l);
#ifdef TRACE_SOLVER
std::cout << " in dual space: "
<< f_value << std::endl;
print_ublas::vector("x", x);
print_ublas::vector("z", z);
print_ublas::vector("A", A, iq + 1);
#endif
goto l2a;
}
/* case (iii): step in primal and dual space */
/* set x = x + t * z */
for (k = 0; k < n; k++)
x(k) += t * z(k);
/* update the solution value */
f_value += t * scalar_product(z, np) * (0.5 * t + u(iq));
/* u = u + t * (-r 1) */
for (k = 0; k < iq; k++)
u(k) -= t * r(k);
u(iq) += t;
#ifdef TRACE_SOLVER
std::cout << " in both spaces: "
<< f_value << std::endl;
print_ublas::vector("x", x);
print_ublas::vector("u", u, iq + 1);
print_ublas::vector("r", r, iq + 1);
print_ublas::vector("A", A, iq + 1);
#endif
if (fabs(t - t2) < std::numeric_limits<double>::epsilon())
{
#ifdef TRACE_SOLVER
std::cout << "Full step has taken " << t << std::endl;
print_ublas::vector("x", x);
#endif
/* full step has taken */
/* add constraint ip to the active set*/
if (!add_constraint(R, J, d, iq, R_norm))
{
iaexcl[ip] = false;
delete_constraint(R, J, A, u, n, p, iq, ip);
#ifdef TRACE_SOLVER
print_ublas::matrix("R", R);
print_ublas::vector("A", A, iq);
print_ublas::vector("iai", iai);
#endif
for (i = 0; i < m; i++)
iai(i) = i;
for (i = p; i < iq; i++)
{
A(i) = A_old(i);
u(i) = u_old(i);
iai(A(i)) = -1;
}
for (i = 0; i < n; i++)
x(i) = x_old(i);
goto l2; /* go to step 2 */
}
else
iai(ip) = -1;
#ifdef TRACE_SOLVER
print_ublas::matrix("R", R);
print_ublas::vector("A", A, iq);
print_ublas::vector("iai", iai);
#endif
goto l1;
}
/* a patial step has taken */
#ifdef TRACE_SOLVER
std::cout << "Partial step has taken " << t << std::endl;
print_ublas::vector("x", x);
#endif
/* drop constraint l */
iai(l) = l;
delete_constraint(R, J, A, u, n, p, iq, l);
#ifdef TRACE_SOLVER
print_ublas::matrix("R", R);
print_ublas::vector("A", A, iq);
#endif
/* update s(ip) = CI * x + ci0 */
sum = 0.0;
for (k = 0; k < n; k++)
sum += CI(k, ip) * x(k);
s(ip) = sum + ci0(ip);
#ifdef TRACE_SOLVER
print_ublas::vector("s", s, m);
#endif
goto l2a;
}
inline void compute_d(VectorXd& d, const MatrixXd& J, const VectorXd& np)
{
register int i, j, n = d.size();
register double sum;
/* compute d = H^T * np */
for (i = 0; i < n; i++)
{
sum = 0.0;
for (j = 0; j < n; j++)
sum += J(j, i) * np(j);
d(i) = sum;
}
}
inline void update_z(VectorXd& z, const MatrixXd& J, const VectorXd& d, int iq)
{
register int i, j, n = z.size();
/* setting of z = H * d */
for (i = 0; i < n; i++)
{
z(i) = 0.0;
for (j = iq; j < n; j++)
z(i) += J(i, j) * d(j);
}
}
inline void update_r(const MatrixXd& R, VectorXd& r, const VectorXd& d, int iq)
{
register int i, j;/*, n = d.size();*/
register double sum;
/* setting of r = R^-1 d */
for (i = iq - 1; i >= 0; i--)
{
sum = 0.0;
for (j = i + 1; j < iq; j++)
sum += R(i, j) * r(j);
r(i) = (d(i) - sum) / R(i, i);
}
}
bool add_constraint(MatrixXd& R, MatrixXd& J, VectorXd& d, int& iq, double& R_norm)
{
int n = d.size();
#ifdef TRACE_SOLVER
std::cout << "Add constraint " << iq << '/';
#endif
register int i, j, k;
double cc, ss, h, t1, t2, xny;
/* we have to find the Givens rotation which will reduce the element
d(j) to zero.
if it is already zero we don't have to do anything, except of
decreasing j */
for (j = n - 1; j >= iq + 1; j--)
{
/* The Givens rotation is done with the ublas::matrix (cc cs, cs -cc).
If cc is one, then element (j) of d is zero compared with element
(j - 1). Hence we don't have to do anything.
If cc is zero, then we just have to switch column (j) and column (j - 1)
of J. Since we only switch columns in J, we have to be careful how we
update d depending on the sign of gs.
Otherwise we have to apply the Givens rotation to these columns.
The i - 1 element of d has to be updated to h. */
cc = d(j - 1);
ss = d(j);
h = distance(cc, ss);
if (fabs(h) < std::numeric_limits<double>::epsilon()) // h == 0
continue;
d(j) = 0.0;
ss = ss / h;
cc = cc / h;
if (cc < 0.0)
{
cc = -cc;
ss = -ss;
d(j - 1) = -h;
}
else
d(j - 1) = h;
xny = ss / (1.0 + cc);
for (k = 0; k < n; k++)
{
t1 = J(k, j - 1);
t2 = J(k, j);
J(k, j - 1) = t1 * cc + t2 * ss;
J(k, j) = xny * (t1 + J(k, j - 1)) - t2;
}
}
/* update the number of constraints added*/
iq++;
/* To update R we have to put the iq components of the d ublas::vector
into column iq - 1 of R
*/
for (i = 0; i < iq; i++)
R(i, iq - 1) = d(i);
#ifdef TRACE_SOLVER
std::cout << iq << std::endl;
print_ublas::matrix("R", R, iq, iq);
print_ublas::matrix("J", J);
print_ublas::vector("d", d, iq);
#endif
if (fabs(d(iq - 1)) <= std::numeric_limits<double>::epsilon() * R_norm)
{
// problem degenerate
return false;
}
R_norm = std::max<double>(R_norm, fabs(d(iq - 1)));
return true;
}
void delete_constraint(MatrixXd& R, MatrixXd& J, VectorXi& A, VectorXd& u, int n, int p, int& iq, int l)
{
#ifdef TRACE_SOLVER
std::cout << "Delete constraint " << l << ' ' << iq;
#endif
register int i, j, k, qq = -1; // just to prevent warnings from smart compilers
double cc, ss, h, xny, t1, t2;
/* Find the index qq for active constraint l to be removed */
for (i = p; i < iq; i++)
if (A(i) == l)
{
qq = i;
break;
}
/* remove the constraint from the active set and the duals */
for (i = qq; i < iq - 1; i++)
{
A(i) = A(i + 1);
u(i) = u(i + 1);
for (j = 0; j < n; j++)
R(j, i) = R(j, i + 1);
}
A(iq - 1) = A(iq);
u(iq - 1) = u(iq);
A(iq) = 0;
u(iq) = 0.0;
for (j = 0; j < iq; j++)
R(j, iq - 1) = 0.0;
/* constraint has been fully removed */
iq--;
#ifdef TRACE_SOLVER
std::cout << '/' << iq << std::endl;
#endif
if (iq == 0)
return;
for (j = qq; j < iq; j++)
{
cc = R(j, j);
ss = R(j + 1, j);
h = distance(cc, ss);
if (fabs(h) < std::numeric_limits<double>::epsilon()) // h == 0
continue;
cc = cc / h;
ss = ss / h;
R(j + 1, j) = 0.0;
if (cc < 0.0)
{
R(j, j) = -h;
cc = -cc;
ss = -ss;
}
else
R(j, j) = h;
xny = ss / (1.0 + cc);
for (k = j + 1; k < iq; k++)
{
t1 = R(j, k);
t2 = R(j + 1, k);
R(j, k) = t1 * cc + t2 * ss;
R(j + 1, k) = xny * (t1 + R(j, k)) - t2;
}
for (k = 0; k < n; k++)
{
t1 = J(k, j);
t2 = J(k, j + 1);
J(k, j) = t1 * cc + t2 * ss;
J(k, j + 1) = xny * (J(k, j) + t1) - t2;
}
}
}
inline double distance(double a, double b)
{
register double a1, b1, t;
a1 = fabs(a);
b1 = fabs(b);
if (a1 > b1)
{
t = (b1 / a1);
return a1 * ::std::sqrt(1.0 + t * t);
}
else
if (b1 > a1)
{
t = (a1 / b1);
return b1 * ::std::sqrt(1.0 + t * t);
}
return a1 * ::std::sqrt(2.0);
}
inline double scalar_product(const VectorXd& x, const VectorXd& y)
{
register int i, n = x.size();
register double sum;
sum = 0.0;
for (i = 0; i < n; i++)
sum += x(i) * y(i);
return sum;
}
void cholesky_decomposition(MatrixXd& A)
{
register int i, j, k, n = A.rows();
register double sum;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
sum = A(i, j);
for (k = i - 1; k >= 0; k--)
sum -= A(i, k)*A(j, k);
if (i == j)
{
if (sum <= 0.0)
{
std::ostringstream os;
// raise error
print_matrix("A", A);
os << "Error in cholesky decomposition, sum: " << sum;
throw std::logic_error(os.str());
exit(-1);
}
A(i, i) = ::std::sqrt(sum);
}
else
A(j, i) = sum / A(i, i);
}
for (k = i + 1; k < n; k++)
A(i, k) = A(k, i);
}
}
void cholesky_solve(const MatrixXd& L, VectorXd& x, const VectorXd& b)
{
int n = L.rows();
VectorXd y(n);
/* Solve L * y = b */
forward_elimination(L, y, b);
/* Solve L^T * x = y */
backward_elimination(L, x, y);
}
inline void forward_elimination(const MatrixXd& L, VectorXd& y, const VectorXd& b)
{
register int i, j, n = L.rows();
y(0) = b(0) / L(0, 0);
for (i = 1; i < n; i++)
{
y(i) = b(i);
for (j = 0; j < i; j++)
y(i) -= L(i, j) * y(j);
y(i) = y(i) / L(i, i);
}
}
inline void backward_elimination(const MatrixXd& U, VectorXd& x, const VectorXd& y)
{
register int i, j, n = U.rows();
x(n - 1) = y(n - 1) / U(n - 1, n - 1);
for (i = n - 2; i >= 0; i--)
{
x(i) = y(i);
for (j = i + 1; j < n; j++)
x(i) -= U(i, j) * x(j);
x(i) = x(i) / U(i, i);
}
}
void print_matrix(const char* name, const MatrixXd& A, int n, int m)
{
std::ostringstream s;
std::string t;
if (n == -1)
n = A.rows();
if (m == -1)
m = A.cols();
s << name << ": " << std::endl;
for (int i = 0; i < n; i++)
{
s << " ";
for (int j = 0; j < m; j++)
s << A(i, j) << ", ";
s << std::endl;
}
t = s.str();
t = t.substr(0, t.size() - 3); // To remove the trailing space, comma and newline
std::cout << t << std::endl;
}
} /*
template<typename T>
void print_vector(const char* name, const ublas::vector<T>& v, int n)
{
std::ostringstream s;
std::string t;
if (n == -1)
n = v.size();
s << name << ": " << std::endl << " ";
for (int i = 0; i < n; i++)
{
s << v(i) << ", ";
}
t = s.str();
t = t.substr(0, t.size() - 2); // To remove the trailing space and comma
std::cout << t << std::endl;
}
}
*/