function [x_o,JJ]=quadratic(B,A,d,b,x_s,J,P,ro)
% kvadraticke programovanie: 1/2 x'*B*x -d'*x, A*x<=b ;ro- pocet rovnic v
% obmedzeniach;ak ro=0 obmedzenia su len nerovnice; x_s startovaci bod pripustny;
%ro <= dlzka J
% P=0 - quadratic spustame na zaciatku; P=1 spustame ho po redukcii
% aktivnych ohraniceni
%  ak P=0 a J=[0] (ro=0 ) je x_s vnutorny, alebo na hranici
%pri P=0 hladame pripustny bod na hranici
%inak  J =[cisla riadkov s rovnicami] 
q=length(J);J0=J;
[r,s]=size(A);
if P==0
x_0=B\d;V=A*x_0-b;
% ak ohranicenia su rovnice a nerovnice, 
if ro>0
for i=1:ro
    VV(i)=V(J(i));
end
Vn=norm(VV);
if V<= 0 & abs(Vn)< 10^(-5)
  'optimal solution'
  x_0
  return
end

p_0=x_0-x_s;
v=-A*x_s+b; vv=A*p_0;
ss=10^7;jj=0;
for i=ro+1:r
if vv(i)>0
    if ss>v(i)/vv(i)
ss=min(ss,v(i)/vv(i));
jj=i;
    end
end
end
if jj>0
J1(1)=[J jj];ss=min(1,ss);xx=x_s+ss*p_0;
else
    xx=x_s;J1=J;
end
J2=J1;
[p,mu]=quadratic_i(xx,B,A,J2,d);
Q=1/2*xx'*B*xx-d'*xx
 pn=norm(p);
end
% ak ohranicenia su vsetky nerovnosti
if ro==0 & V<= 10^(-6) 
  'optimal solution'
  x_0
  return
end
if ro==0
p_0=x_0-x_s;
v=-A*x_s+b; vv=A*p_0;
ss=10^7;jj=0;
for i=1:r
if vv(i)>0
    if ss>v(i)/vv(i)
ss=min(ss,v(i)/vv(i));
jj=i;
    end
end
end

xx=x_s+ss*p_0;
 J=[jj];
  [p,mu]=quadratic_i(xx,B,A,J,d);
     pn=norm(p);
end
end
% ak sa cely quadratc spusta nanovo (po redukcii aktivnych rovnic)
 if P==1
     xx=x_s;
     [p,mu]=quadratic_i(xx,B,A,J,d);
     pn=norm(p);
 end
 %iteracie
 P=1;
for k=1:s
    k
if abs(pn)> 10^(-6)
  q=length(J);
v=-A*xx+b; vv=A*p;
  ss=10^7;jj=0;
for i=1:r
if vv(i)>10^(-6)
    if ss>v(i)/vv(i)
ss=min(ss,v(i)/vv(i));jj=i;
    end
end
end  
ss=min(1,ss);ss
xx=xx+ss*p; dd=0;
for i=1:q
    if J(i)==jj
    dd=i;
    break;
    end
end

if dd == 0& jj>0
  J(1,q+1)=jj;q=q+1; 
end
    
    k,jj,xx, A*xx,  ss, J
Q=1/2*xx'*B*xx-d'*xx
[p,mu]=quadratic_i(xx,B,A,J,d)
 pn=norm(p);id=length(J);
end
end
x_o=xx, A*x_o
Q=1/2 *x_o'*B*x_o-d'*x_o
mi=1;
for i=ro+1:id
    mi=min(mi,mu(i));
end 
if mi>-10^(-7)
    'x_o is optimal'
    return
end
qq=0;jj=0;
for i=ro+1:q
    if mu(J(i))< qq
qq=min(qq,mu(J(i)));jj=i;
    end
end
JJ=zeros(q,1); JJ([1:jj-1],1)=J([1:jj-1],1);JJ([jj:q-1],1)=J([jj+1:q],1);

'spust program so zmenenymi datami'
J=JJ, x_s=x_o , P=1


