function lp_write ( filename, label, A1, b1, A2, b2 )

%*****************************************************************************80
%
%% lp_write() writes linear programming data to an LP file.
%
%  Discussion:
%
%    We have begun to look at rather large problems, and have encountered
%    software packages that put limits on the maximum length of a line
%    in an LP file.  We try to control this by adding line breaks so that,
%    at least when there are no more than 99,999 variables, the lines of the
%    LP file should be no more than 80 characters long.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 June 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string FILENAME, the output filename.
%
%    string LABEL, the problem title.
%
%    integer M, the number of equations
%
%    integer N, the number of variables.
%
%    integer A1(M1,N), B1(M1), the covering coefficients.
%
%    integer A2(M2,N), B2(M2), the counting constraints.
%
  [ m1, n ] = size ( A1 );
  [ m2, n ] = size ( A2 );
%
%  Open the file.
%
  output = fopen ( filename, 'wt' );

  if ( output < 0 ) 
    fprintf ( 1, '\n' );
    fprintf ( 1, 'lp_write(): Error!\n' );
    fprintf ( 1, '  Could not open the output file.\n' );
    error ( 'lp_write(): Error!' );
  end
%
%  Label.
%
  fprintf ( output, '%s\n', label );
  fprintf ( output, '\n' );
%
%  Objective function.
%
  fprintf ( output, 'Maximize\n' );
  fprintf ( output, '\n' );
% fprintf ( output, '  Obj: 0\n' );
%
%  Constraints:
%
  fprintf ( output, 'Subject to\n' );
%
%  Covering constraints:  A1*x=b1
%
  line_length = 0;
  for i = 1 : m1

    first = true;

    for j = 1 : n

      if ( A1(i,j) ~= 0 )

        if ( A1(i,j) < 0 )
          byte_num = fprintf ( output, ' -' );
          line_length = line_length + byte_num;
        elseif ( ~ first )
          byte_num = fprintf ( output, ' +' );
          line_length = line_length + byte_num;
        end

        if ( abs ( A1(i,j) ) == 1 )
          byte_num = fprintf ( output, ' x%d', j );
          line_length = line_length + byte_num;
        else
          byte_num = fprintf ( output, ' %d x%d', abs ( A1(i,j) ), j );
          line_length = line_length + byte_num;
        end

        if ( 70 <= line_length )
          fprintf ( output, '\n' );
          fprintf ( output, '  ' );
          line_length = 2;
        end

        first = false;

      end
    end
    numbytes = fprintf ( output, ' = %d\n', b1(i) );
    line_length = 0;
  end
%
%  Counting constraints:  A2*x=b2
%
  line_length = 0;
  for i = 1 : m2

    first = true;

    for j = 1 : n

      if ( A2(i,j) ~= 0 )

        if ( A2(i,j) < 0 )
          byte_num = fprintf ( output, ' -' );
          line_length = line_length + byte_num;
        elseif ( ~ first )
          byte_num = fprintf ( output, ' +' );
          line_length = line_length + byte_num;
        end

        if ( abs ( A2(i,j) ) == 1 )
          byte_num = fprintf ( output, ' x%d', j );
          line_length = line_length + byte_num;
        else
          byte_num = fprintf ( output, ' %d x%d', abs ( A2(i,j) ), j );
          line_length = line_length + byte_num;
        end

        if ( 70 <= line_length )
          fprintf ( output, '\n' );
          fprintf ( output, '  ' );
          line_length = 2;
        end

        first = false;

      end
    end
    numbytes = fprintf ( output, ' = %d\n', b2(i) );
    line_length = 0;
  end
%
%  All variables are "binary" (taking on values 0 or 1, only.)
%
  fprintf ( output, 'Binary\n' );
  line_length = 0;

  byte_num = fprintf ( output, ' ' );
  line_length = line_length + byte_num;

  for j = 1 : n
    byte_num = fprintf ( output, ' x%d', j );
    line_length = line_length + byte_num;
    if ( 73 <= line_length & j < n )
      fprintf ( output, '\n' );
      fprintf ( output, '  ' );
      line_length = 2;
    end
  end
%
%  End of file.
%
  fprintf ( output, '\n' );
  fprintf ( output, 'End\n' );
%
%  Close the file.
%
  fclose ( output );

  return
end
 
