function [F, signoX, signoXF] = formaproblema(F, signoX, signoXF, problema)
%formaproblema
%   Fn = formaproblema(F, signoX, signoXF, problema)
%
%   Convierte unas recetas a la forma estándar, X >= 0 y X F(k) = 0, o a la
%   forma canónica, X >=0 y X F(k) >= 0.
%      F           recetas originales en tiempo discreto
%      signoX      signo de las intensidades; por defecto X >= 0
%      signoXF     signo de los balances materiales; por defecto X F(k) = 0
%      problema    [] para la forma estándar y 1 para la forma canónica
%      Fn          recetas transformadas en tiempo discreto

if nargin < 2, signoX = []; end
if nargin < 3, signoXF = []; end
if nargin < 4, problema = []; end

[filas, columnas, ancho] = size(F);
if isempty(signoX), signoX = 1; end
if isempty(signoXF), signoXF = 0; end
if size(signoX, 2) == 1, signoX = signoX.*ones(1, filas); end
if size(signoXF, 2) == 1, signoXF = signoXF.*ones(1, columnas); end

if isempty(problema)
    F = formaestandar(F, signoX, signoXF, filas, columnas, ancho);
elseif problema == 1
    F = formacanonica(F, signoX, signoXF, filas, columnas, ancho);
end




function [Fn] = formaestandar(F, signoX, signoXF, filas, columnas, ancho)
if and(sum(signoX==1) == filas, sum(signoXF==0) == columnas)
    Fn = F;
    return
end
filasn = sum(signoX == 1) + sum(signoX == 2) + 2*sum(signoX == 3) + sum(signoXF == 1) + sum(signoXF == 2) + 2*sum(signoXF == 3);
Fn = zeros(filasn, columnas, ancho);
if sum(signoX == 1) == filas
    Fn(1:filas,:,:) = F(1:filas,:,:);
    c2 = filas + 1;
else
    c2 = 1;
    for c1 = 1:filas
        switch signoX(c1)
            case 0
            case 1, Fn(c2, :, :) = F(c1, :, :); c2 = c2 + 1;
            case 2, Fn(c2, :, :) = -F(c1, :, :); c2 = c2 + 1;
            case 3, Fn(c2, :, :) = F(c1, :, :); Fn(c2+1, :, :) = -F(c1, :, :); c2 = c2 + 2;
        end
    end
end
if sum(signoXF == 0) ~= columnas
    for c1 = 1:columnas
        switch signoXF(c1)
            case 0
            case 1, Fn(c2, c1, 1) = -1; c2 = c2 + 1;
            case 2, Fn(c2, c1, 1) = 1; c2 = c2 + 1;
            case 3, Fn(c2, c1, 1) = -1; Fn(c2+1, c1, 1) = 1; c2 = c2 + 2;
        end
    end
end





function Fm = formacanonica(F, signoX, signoXF, filas, columnas, ancho)
if and(sum(signoX==1) == filas, sum(signoXF==1) == columnas)
    Fm = F;
    return
end
filasn = sum(signoX == 1) + sum(signoX == 2) + 2*sum(signoX == 3);
columnasn = 2*sum(signoXF == 0) + sum(signoXF == 1) + sum(signoXF == 2);
if sum(signoX == 1) == filas
    Fn = F;
else
    Fn = zeros(filasn, columnas, ancho);
    c2 = 1;
    for c1 = 1:filas
        switch signoX(c1)
            case 0
            case 1, Fn(c2, :, :) = F(c1, :, :); c2 = c2 + 1;
            case 2, Fn(c2, :, :) = -F(c1, :, :); c2 = c2 + 1;
            case 3, Fn(c2, :, :) = F(c1, :, :); Fn(c2+1, :, :) = -F(c1, :, :); c2 = c2 + 2;
        end
    end
end
if sum(signoXF == 1) == columnas
    Fm = Fn;
else
    Fm = zeros(filasn, columnasn, ancho);
    c2 = 1;
    for c1 = 1:columnas
        switch signoXF(c1)
            case 0, Fm(:, c2, :) = Fn(:, c1, :); Fm(:, c2+1, :) = -Fn(:, c1, :); c2 = c2 + 2;
            case 1, Fm(:, c2, :) = Fn(:, c1, :); c2 = c2 + 1;
            case 2, Fm(:, c2, :) = -Fn(:, c1, :); c2 = c2 + 1;
            case 3
        end
    end
end
