lilypond-0.1.35
[lilypond.git] / lily / qlp.cc
blob7a99cc639ff18693f767014595fc1ffc59d72d74
1 /*
2 qlp.cc -- implement Mixed_qp
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 #include "debug.hh"
10 #include "qlp.hh"
12 void
13 Mixed_qp::add_equality_cons (Vector , double)
15 assert (false);
18 void
19 Mixed_qp::add_fixed_var (int i, Real r)
21 eq_cons.push (i);
22 eq_consrhs.push (r);
26 /**
27 eliminate appropriate variables, until we have a Ineq_constrained_qp
28 then solve that.
30 PRE
31 cons should be ascending
33 Vector
34 Mixed_qp::solve (Vector start) const
36 if (!dim())
37 return Vector (0);
39 print();
40 Ineq_constrained_qp pure (*this);
42 for (int i= eq_cons.size()-1; i>=0; i--)
44 pure.eliminate_var (eq_cons[i], eq_consrhs[i]);
45 start.del (eq_cons[i]);
47 Vector sol = pure.solve (start);
48 for (int i= 0; i < eq_cons.size(); i++)
50 sol.insert (eq_consrhs[i],eq_cons[i]);
52 return sol;
56 void
57 Ineq_constrained_qp::assert_solution (Vector sol) const
59 Array<int> binding;
60 for (int i=0; i < cons.size(); i++)
62 Real R=cons[i] * sol- consrhs[i];
63 assert (R> -EPS);
64 if (R < EPS)
65 binding.push (i);
67 // KKT check...
68 // todo
71 void
72 Ineq_constrained_qp::print() const
74 #ifndef NPRINT
75 DOUT << "Quad " << quad;
76 DOUT << "lin " << lin <<"\n"
77 << "const " << const_term<<"\n";
78 for (int i=0; i < cons.size(); i++)
80 DOUT << "constraint["<<i<<"]: " << cons[i] << " >= " << consrhs[i];
81 DOUT << "\n";
83 #endif
86 Mixed_qp::Mixed_qp (int n)
87 : Ineq_constrained_qp (n)
91 void
92 Mixed_qp::OK() const
94 #ifndef NDEBUG
95 Ineq_constrained_qp::OK();
96 assert (eq_consrhs.size() == eq_cons.size ());
97 #endif
100 void
101 Mixed_qp::print() const
103 #ifndef NPRINT
104 Ineq_constrained_qp::print();
105 for (int i=0; i < eq_cons.size(); i++)
107 DOUT << "eq cons "<<i<<": x["<<eq_cons[i]<<"] == " << eq_consrhs[i]<<"\n";
109 #endif