lilypond-0.1.16
[lilypond.git] / lily / qlpsolve.cc
blobb863a63f67069a991dabff491f912f7b72cd7362
1 /*
2 qlpsolve.cc -- implement Active_constraints, Inactive_iter
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997 Han-Wen Nienhuys <hanwen@stack.nl>
8 TODO:
9 try fixed point arithmetic, to speed up lily.
12 #include "ineq-constrained-qp.hh"
13 #include "qlpsolve.hh"
14 #include "debug.hh"
15 #include "choleski.hh"
17 const Real TOL=1e-1; // roughly 1/30 mm
19 String
20 Active_constraints::status() const
22 String s ("Active|Inactive [");
23 for (int i=0; i< active.size(); i++)
25 s += String (active[i]) + " ";
28 s+="| ";
29 for (int i=0; i< inactive.size(); i++)
31 s += String (inactive[i]) + " ";
33 s+="]";
35 return s;
38 void
39 Active_constraints::OK()
41 #ifndef NDEBUG
42 H.OK();
43 A.OK();
44 assert (active.size() +inactive.size () == opt->cons.size ());
45 assert (H.dim() == opt->dim ());
46 assert (active.size() == A.rows ());
47 Array<int> allcons;
49 for (int i=0; i < opt->cons.size(); i++)
50 allcons.push (0);
51 for (int i=0; i < active.size(); i++)
53 int j = active[i];
54 allcons[j]++;
56 for (int i=0; i < inactive.size(); i++)
58 int j = inactive[i];
59 allcons[j]++;
61 for (int i=0; i < allcons.size(); i++)
62 assert (allcons[i] == 1);
63 #endif
66 Vector
67 Active_constraints::get_lagrange (Vector gradient)
69 return (A*gradient);
72 void
73 Active_constraints::add (int k)
75 // add indices
76 int cidx=inactive[k];
77 active.push (cidx);
79 inactive.swap (k,inactive.size()-1);
80 inactive.pop();
82 Vector a (opt->cons[cidx]);
83 // update of matrices
84 Vector Ha = H*a;
85 Real aHa = a*Ha;
86 Vector addrow (Ha.dim());
87 if (abs (aHa) > EPS)
90 a != 0, so if Ha = O(EPS), then
91 Ha * aH / aHa = O(EPS^2/EPS)
93 if H*a == 0, the constraints are dependent.
95 H -= Matrix (Ha/aHa , Ha);
99 sorry, don't know how to justify this. ..
101 addrow=Ha;
102 addrow/= aHa;
103 A -= Matrix (A*a, addrow);
104 A.insert_row (addrow,A.rows());
105 }else
106 WARN << "degenerate constraints";
109 void
110 Active_constraints::drop (int k)
112 int q=active.size()-1;
114 // drop indices
115 inactive.push (active[k]);
116 active.swap (k,q);
117 A.swap_rows (k,q);
118 active.pop();
120 Vector a (A.row (q));
121 if (a.norm() > EPS)
126 Real q = a*opt->quad*a;
127 Matrix aaq (a,a/q);
128 H += aaq;
129 A -= A*opt->quad*aaq;
130 }else
131 WARN << "degenerate constraints";
132 #ifndef NDEBUG
133 Vector rem_row (A.row (q));
134 assert (rem_row.norm() < EPS);
135 #endif
137 A.delete_row (q);
141 Active_constraints::Active_constraints (Ineq_constrained_qp const *op)
142 : A(0,op->dim()),
143 H(op->dim()),
144 opt (op)
146 for (int i=0; i < op->cons.size(); i++)
147 inactive.push (i);
148 Choleski_decomposition chol (op->quad);
151 ugh.
153 H=chol.inverse();
154 OK();
157 /** Find the optimum which is in the planes generated by the active
158 constraints.
160 Vector
161 Active_constraints::find_active_optimum (Vector g)
163 return H*g;