lilypond-1.3.12
[lilypond.git] / lily / qlp.cc
blobf77534e3e4ca07e688b25020218972ab2cbf2db1
1 /*
2 qlp.cc -- implement Mixed_qp
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1998 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 #include "debug.hh"
10 #include "qlp.hh"
13 void
14 Mixed_qp::add_equality_cons (Vector , double)
16 assert (false);
19 void
20 Mixed_qp::add_fixed_var (int i, Real r)
22 eq_cons.push (i);
23 eq_consrhs.push (r);
27 /**
28 eliminate appropriate variables, until we have a Ineq_constrained_qp
29 then solve that.
31 PRE
32 cons should be ascending
34 Vector
35 Mixed_qp::solve (Vector start) const
37 if (!dim())
38 return Vector (0);
40 print();
41 Ineq_constrained_qp pure (*this);
43 for (int i= eq_cons.size()-1; i>=0; i--)
45 pure.eliminate_var (eq_cons[i], eq_consrhs[i]);
46 start.del (eq_cons[i]);
48 Vector sol = pure.solve (start);
49 for (int i= 0; i < eq_cons.size(); i++)
51 sol.insert (eq_consrhs[i],eq_cons[i]);
53 return sol;
57 void
58 Ineq_constrained_qp::assert_solution (Vector sol) const
60 Array<int> binding;
61 for (int i=0; i < cons_.size(); i++)
63 Real R=cons_[i] * sol- consrhs_[i];
64 assert (R> -EPS);
65 if (R < EPS)
66 binding.push (i);
68 // KKT check...
69 // todo
72 void
73 Ineq_constrained_qp::print() const
75 #ifndef NPRINT
76 DOUT << "Quad " << quad_;
77 DOUT << "lin " << lin_ <<"\n"
78 << "const " << const_term_<<"\n";
79 for (int i=0; i < cons_.size(); i++)
81 DOUT << "constraint["<<i<<"]: " << cons_[i] << " >= " << consrhs_[i];
82 DOUT << "\n";
84 #endif
87 Mixed_qp::Mixed_qp (int n)
88 : Ineq_constrained_qp (n)
92 void
93 Mixed_qp::OK() const
95 #ifndef NDEBUG
96 Ineq_constrained_qp::OK();
97 assert (eq_consrhs.size() == eq_cons.size ());
98 #endif
101 void
102 Mixed_qp::print() const
104 #ifndef NPRINT
105 Ineq_constrained_qp::print();
106 for (int i=0; i < eq_cons.size(); i++)
108 DOUT << "eq cons "<<i<<": x["<<eq_cons[i]<<"] == " << eq_consrhs[i]<<"\n";
110 #endif