function[] = makeproblem(l,u,n,m,ipos,spa,savefile)

% function[] = makeproblem(l,u,n,m,ipos,spa,savefile)
%
% Generate random data for maximum of quadratics -type of 
% problems. The data is saved into the file savefile
% in the form (in this example n=3)
%
%   A_11 A_12 A_13
%   A_12 A_14 A_15
%   A_13 A_15 A_16
%   b_11 b_12 b_13
%   c_1
%   A_21 A_22 A_23
%   A_22 A_24 A_25
%   A_23 A_25 A_26
%   b_21 b_22 b_23
%   c_2
%    ...
%   A_m1 A_m2 A_m3
%   A_m2 A_m4 A_m5
%   A_m3 A_m5 A_m6
%   b_m1 b_m2 b_m3
%   c_m
%   xinit_1 xinit_2 xinit_3
%   n
%   m
%   ipos
%   spa
%
%
% Input parameters:
%
%   l         lower limit for random numbers.
%   u         upper limit for random numbers.
%   n         dimension of the matrix and vectors.
%   m         number of quadratic functions.
%   ipos      parameter for matrix type:  
%               ipos = 1: positive definite matrix required
%               ipos = 0: negative definite matrix required
%   spa       density parameter:  
%               spa = 0: diagonal matrix is required
%               spa = 1: dense matrix is required
%               0 < spa < 1: sparse matrix with approximately 
%                        spa*n*n + n nonzeros (the diagonal 
%                        elements are always nonzero)
%
%
% Napsu Karmitsa, Last modified 06.03.2009
%


% iteration number

k = 1

l
u


for i = 1:m

  if spa == 1

% Dense symmetric matrix

    X = l + (u-l) * rand(n)
    A = (X + X')/2

  elseif spa == 0

% Diagonal matrix

    A = diag(l+(u-l)*rand(1,n))
    
  else

    if (spa < 0 || spa > 1)
      error('Parameter spa should only get values in interval [0,1].');
    else 

% Sparse symmetric matrix

      X = (u-l) * sprand(n,n,spa/2)
      A = (X + X')/2
      A = A + diag((u-l)*rand(1,n))
      X = spones(A)
      A = l*X + A
      A = full(A)
    end
  end


  lambda = eig(A)
  lmin = min(lambda)


  if ipos == 1
    ipos

% Correction to the matrix (do this always)

    X = eye(n)*(abs(lmin)+1.0)
    A = A + X


  elseif ipos==0
    ipos

    if k == 1

% Correction to the matrix (the first matrix is enforced to be
% positive definite)

      X = eye(n)*(abs(lmin)+1.0)
      A = A + X

    else

% Create a new matrix, if the smallest eigenvalue is positive.

      while lmin > 0

        if spa == 1

% Dense symmetric matrix

          X = l + (u-l) * rand(n)
          A = (X + X')/2

        elseif spa == 0

% Diagonal matrix

          A = diag(l+(u-l)*rand(1,n))
    
        else

% Sparse symmetric matrix

          X = (u-l) * sprand(n,n,spa/2)
          A = (X + X')/2
          A = A + diag((u-l)*rand(1,n))
          X = spones(A)
          A = l*X + A
          A = full(A)
        end

        lambda = eig(A)
        lmin = min(lambda)

      end
    end

  else
    error('Parameter ipos should only get values 0 or 1.');

  end



% random vector

  b = l + (u-l) * rand(1,n)


% random scalar

  if k == 1
    if ipos == 0
      c = l
    else
      c = l + (u-l) * rand
    end
  
  else
    c = l + (u-l) * rand

  end

  lmin


% save the data

  if k == 1
    save(savefile, 'A', 'b', 'c','-ASCII', '-double')
    k = 2
  else
    save(savefile, 'A', 'b', 'c','-ASCII', '-double', '-append')
  end

% for silmukka kiinni?
end


% starting point

xinit = l + (u-l) * rand(1,n)


save(savefile, 'xinit', '-ASCII', '-double', '-append')
save(savefile, 'n', '-ASCII', '-append')
save(savefile, 'm', '-ASCII', '-append')
save(savefile, 'ipos', '-ASCII', '-append')
save(savefile, 'spa', '-ASCII', '-append')
