function [X, Y, exit] = programalinealaux(F, C, D, signoX, signoXF, tipo)
%programalinealaux
%
%   Función auxiliar de programalineal.m. Lanza los problemas en Octave y
%   en las versiones superiores de Matlab. 

switch tipo
case 1
	[X, Y, exit] = programalinealoctave(F, C, D, signoX, signoXF);
case 2
	[X, Y, exit] = programalinealmatlab(F, C, D, signoX, signoXF);
end



function [X, Y, exit] = programalinealoctave(F, C, D, signoX, signoXF)
[filas, columnas] = size(F);
filasn = filas - sum(signoX == 0);
LB = zeros(1, filasn); UB = zeros(1, filasn); Fn = zeros(filasn, columnas); Cn = zeros(filasn, 1);
c2 = 1;
for c1 = 1:filas
    switch signoX(c1)
        case 0,
        case 1, LB(c2) = 0   ; UB(c2) = Inf; Fn(c2,:) = F(c1,:); Cn(c2) = C(c1); c2 = c2 + 1;
        case 2, LB(c2) = -Inf; UB(c2) = 0  ; Fn(c2,:) = F(c1,:); Cn(c2) = C(c1); c2 = c2 + 1;
        case 3, LB(c2) = -Inf; UB(c2) = Inf; Fn(c2,:) = F(c1,:); Cn(c2) = C(c1); c2 = c2 + 1;
    end
end
CTYPE = '';
for c1 = 1:columnas
    switch signoXF(c1)
        case 0, CTYPE = [CTYPE, 'S'];
        case 1, CTYPE = [CTYPE, 'L'];
        case 2, CTYPE = [CTYPE, 'U'];
        case 3, CTYPE = [CTYPE, 'F'];
    end
end
[Xn, FVAL, STATUS, EXTRA] = glpk(-Cn', Fn', -D', LB, UB, CTYPE);
X = zeros(1, filas);
c0 = 1;
for c1 = 1:filas
    switch signoX(c1)
        case 0,
        case 1, X(c1) = Xn(c0); c0 = c0 + 1;
        case 2, X(c1) = Xn(c0); c0 = c0 + 1;
        case 3, X(c1) = Xn(c0); c0 = c0 + 1;
    end
end
Y = EXTRA.lambda;
switch STATUS
    case 180, exit = 1;
    case 182, exit = -2;
    case 183, exit = -2;
    case 184, exit = -3;
    case 151, exit = 1;
    otherwise, exit = 0;
end








function [X, Y, exit] = programalinealmatlab(F, C, D, signoX, signoXF)
[filas, columnas] = size(F);
filasn = filas - sum(signoX == 0);
LB = zeros(1, filasn); UB = zeros(1, filasn); Fn = zeros(filasn, columnas); Cn = zeros(filasn, 1);
c2 = 1;
for c1 = 1:filas
    switch signoX(c1)
        case 0,
        case 1, LB(c2) = 0   ; UB(c2) = Inf; Fn(c2,:) = F(c1,:); Cn(c2) = C(c1); c2 = c2 + 1;
        case 2, LB(c2) = -Inf; UB(c2) = 0  ; Fn(c2,:) = F(c1,:); Cn(c2) = C(c1); c2 = c2 + 1;
        case 3, LB(c2) = -Inf; UB(c2) = Inf; Fn(c2,:) = F(c1,:); Cn(c2) = C(c1); c2 = c2 + 1;
    end
end
numeq = sum(signoXF == 0);
numin = columnas - numeq;
Aeq = zeros(filasn, numeq); Deq = zeros(1, numeq);
Ain = zeros(filasn, numin); Din = zeros(1, numin);
ceq = 1; cin = 1;
for c1 = 1:columnas
    switch signoXF(c1)
        case 0, Aeq(:, ceq) = Fn(:,c1); Deq(ceq) = D(c1); ceq = ceq + 1;
        case 1, Ain(:, cin) = Fn(:,c1); Din(cin) = D(c1); cin = cin + 1;
        case 2, Ain(:, cin) = -Fn(:,c1); Din(cin) = -D(c1); cin = cin + 1;
        case 3
    end
end
options = optimset('Display','off', 'TolFun', 1e-12, 'TolX', 1e-12);
[Xn,FVAL,EXITFLAG,OUTPUT,LAMBDA] = linprog(-Cn', -Ain', Din', -Aeq', Deq', LB, UB, [], options);
X = zeros(1, filas);
c0 = 1;
for c1 = 1:filas
    switch signoX(c1)
        case 0,
        case 1, X(c1) = Xn(c0); c0 = c0 + 1;
        case 2, X(c1) = Xn(c0); c0 = c0 + 1;
        case 3, X(c1) = Xn(c0); c0 = c0 + 1;
    end
end
Y = zeros(columnas, 1);
ci0 = 1; ce0 = 1;
for c1 = 1:columnas
    switch signoXF(c1)
        case 0, Y(c1) = LAMBDA.eqlin(ce0); ce0 = ce0 + 1;
        case 1, Y(c1) = LAMBDA.ineqlin(ci0); ci0 = ci0 + 1;
        case 2, Y(c1) = -LAMBDA.ineqlin(ci0); ci0 = ci0 + 1;
        case 3
    end
end
exit = EXITFLAG;
