function ccs_to_crs_test ( )

%*****************************************************************************80
%
%% ccs_to_crs_test() tests ccs_to_crs() converts a CCS matrix to CRS format.
%
%  Discussion:
%
%    Consider the following small matrix.
%
%          2  3  0  0  0
%          3  0  4  0  6
%      A = 0 -1 -3  2  0
%          0  0  1  0  0
%          0  4  2  0  1
%
%    In CCS format, the matrix would be:
%
%      #  ICC  CCC  ACC
%     --  ---  ---  ---
%      1    1    1    2
%      2    2         3
%
%      3    1    3    3
%      4    3        -1
%      5    5         4
%
%      6    2    6    4
%      7    3        -3
%      8    4         1
%      9    5         2
%
%     10    3   10    2
%
%     11    2   11    6
%     12    5         1
%
%     13    *   13
%
%    In CRS format, this would be:
%
%      #  ROW  COL  VAL
%     --  ---  ---  ---
%      1    1    1    2
%      2         2    3
%
%      3    3    1    3
%      4         3    4
%      5         5    6
%
%      6    6    2   -1
%      7         3   -3
%      8         4    2
%
%      9    9    3    1
% 
%     10   10    2    4
%     11         3    2
%     12         5    1
%
%     13   13    *
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    12 June 2022
%
%  Author:
%
%    John Burkardt
%
  addpath ( '../ccs_to_crs' );

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ccs_to_crs_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s.\n', version ( ) );
  fprintf ( 1, '  Test ccs_to_crs().\n' );

  ccs_to_crs_test01 ( );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ccs_to_crs_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  rmpath ( '../ccs_to_crs' );

  return
end
function ccs_to_crs_test01 ( )

%*****************************************************************************80
%
%% ccs_to_crs_test01() tests ccs_to_crs() using a 1-based CCS matrix.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    12 May 2026
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ccs_to_crs_test01()\n' );
  fprintf ( 1, '  Convert a 1-based CCS matrix to CRS format.\n' );

  m = 5;
  n = 5;
  ncc = 12;

  Acc = [ ...
    2.0,  3.0, ...
    3.0, -1.0,  4.0, ...
    4.0, -3.0,  1.0, 2.0, ...
    2.0, ...
    6.0, 1.0 ];
  ccc = [ ...
    1, 3, 6, 10, 11, 13 ];
  icc = [ ...
    1, 2, ...
    1, 3, 5, ...
    2, 3, 4, 5, ...
    3, ...
    2, 5 ];
%
%  Print the CCS matrix.
%
  ccs_print ( m, n, ncc, icc, ccc, Acc, '  The CCS matrix:' );
%
%  Convert CCS to CRS.
%
  [ m, n, ncrs, icrs, ccrs, Acrs ] = ccs_to_crs (  m, n, ncc, icc, ccc, Acc );
%
%  Print the CRS matrix.
%
  crs_print ( m, n, ncrs, icrs, ccrs, Acrs, '  The CRS matrix:' );

  return
end
function ccs_print ( m, n, ncc, icc, ccc, acc, title )

%*****************************************************************************80
%
%% ccs_print() prints a sparse matrix in CCS format.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    23 July 2014
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, the number of rows in the matrix.
%
%    integer N, the number of columns in the matrix.
%
%    integer NCC, the number of elements.
%
%    integer ICC(NCC), the rows.
%
%    integer CCC(N+1), the compressed columns.
%
%    real ACC(NCC), the values.
%
%    character TITLE, a title.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '%s\n', title );
  fprintf ( 1, '     #     I     J       A\n' );
  fprintf ( 1, '  ----  ----  ----  --------------\n' );
  fprintf ( 1, '\n' );

  if ( ccc(1) == 0 )

    j = 0;
    for k = 1 : ncc
      i = icc(k);
      while ( ccc(j+2) <= k - 1 )
        j = j + 1;
      end
      fprintf ( 1, '  %4d  %4d  %4d  %16.8g\n', k - 1, i, j, acc(k) );
    end

  else

    j = 1;
    for k = 1 : ncc
      i = icc(k);
      while ( ccc(j+1) <= k )
        j = j + 1;
      end
      fprintf ( 1, '  %4d  %4d  %4d  %16.8g\n', k, i, j, acc(k) );
    end

  end

  return
end
function crs_print ( m, n, ncrs, rcrs, ccrs, Acrs, title )

%*****************************************************************************80
%
%% crs_print() prints a sparse matrix in CRS format.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    23 July 2014
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, the number of rows in the matrix.
%
%    integer N, the number of columns in the matrix.
%
%    integer NCRS, the number of elements.
%
%    integer RCRS(NCRS), the rows.
%
%    integer CCRS(N+1), the compressed columns.
%
%    real ACRS(NCC), the values.
%
%    character ( len = * ) TITLE, a title.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '%s\n', title );
  fprintf ( 1, '     #     I     J       A\n' );
  fprintf ( 1, '  ----  ----  ----  --------------\n' );
  fprintf ( 1, '\n' );

  if ( rcrs(1) == 0 )

    i = 1;
    k = 0;
    while ( i <= m )
      for c = rcrs(i) + 1 : rcrs(i+1)
        j = ccrs(c) + 1;
        k = k + 1;
        fprintf ( 1, '  %4d  %4d  %4d  %16.8g\n', k - 1, i - 1, j - 1, Acrs(k) );
      end
      i = i + 1;
    end

  else

    i = 1;
    k = 0;
    while ( i <= m )
      for c = rcrs(i) : rcrs(i+1) - 1
        j = ccrs(c);
        k = k + 1;
        fprintf ( 1, '  %4d  %4d  %4d  %16.8g\n', k, i, j, Acrs(k) );
      end
      i = i + 1;
    end

  end

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end

