function tridiagonal_solver_test ( )

%*****************************************************************************80
%
%% tridiagonal_solver_test() tests tridiagonal_solver().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 December 2019
%
%  Author:
%
%    John Burkardt
%
  addpath ( '../tridiagonal_solver' );

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'tridiagonal_solver_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  tridiagonal_solver() solves a tridiagonal linear system.\n' );

  tridiagonal_solver_test1 ( );
  tridiagonal_solver_test2 ( );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'tridiagonal_solver_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  rmpath ( '../tridiagonal_solver' );

  return
end
function tridiagonal_solver_test1 ( )

%*****************************************************************************80
%
%% tridiagonal_solver_test1() tests tridiagonal_solver on a single system.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 January 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'tridiagonal_solver_test1():\n' );
  fprintf ( 1, '  tridiagonal_solver() solves A*x = b where\n' );
  fprintf ( 1, '  A is a tridiagonal matrix.\n' );

  m = 5;
  g = ( 1 : m-1 )';
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Matrix order M = %d\n', m );
%
%  Set the matrix.
%
  [ a, b, c ] = spline_matrix ( g );
%
%  Set the desired solution.
%
  x1 = ( randperm ( m ) )';
%
%  Compute the corresponding right hand side.
%
  rhs = tridiagonal_mv ( a, b, c, x1 );
%
%  Solve the system.
%
  x2 = tridiagonal_solver ( a, b, c, rhs );
 
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Exact and computed solutions:\n' );
  fprintf ( 1, '\n' );
  for i = 1 : m
    fprintf ( 1, '%d  %g  %g\n', i, x1(i), x2(i) );
  end

  return
end
function [ a, b, c ] = spline_matrix ( g )

%*****************************************************************************80
%
%% spline_matrix() sets the entries of a tridiagonal spline matrix.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  n = length ( g ) + 1;

  a = zeros ( n, 1 );
  b = zeros ( n, 1 );
  c = zeros ( n, 1 );

  a(2:n) = g(1:n-1);

  b(1) = 2.0 * g(1);
  b(2:n-1) = 2.0 * ( g(1:n-2) + g(2:n-1) );
  b(n) = 2.0 * g(n-1);

  c(1:n-1) = g(1:n-1);

  return
end
function tridiagonal_solver_test2 ( )

%*****************************************************************************80
%
%% tridiagonal_solver_test2() tests tridiagonal_solver on 2 systems.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 January 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'tridiagonal_solver_test2()\n' );
  fprintf ( 1, '  tridiagonal_solver() solves A*x = b where\n' );
  fprintf ( 1, '  A is a tridiagonal matrix.\n' );
  fprintf ( 1, '  Solve two linear systems at once.\n' );

  m = 5;
  n = 2;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Matrix order M = %d\n', m );
  fprintf ( 1, '  Number of right hand sides N = %d\n', n );
%
%  Set the matrix.
%
  a = - ones ( m );
  a(1) = 0.0;
  b = 2.0 * ones ( m );
  c = - ones ( m );
  c(m) = 0.0;
%
%  Set the desired solution.
%
  x1 = [];
  for j = 1 : n
    t = ( randperm ( m ) )';
    x1 = [ x1, t ];
  end
%
%  Compute the corresponding right hand side.
%
  rhs = tridiagonal_mv ( a, b, c, x1 );
%
%  Solve the system.
%
  x2 = tridiagonal_solver ( a, b, c, rhs );

  for j = 1 : n
    fprintf ( 1, '\n' );
    fprintf ( 1, '  Solution of linear system %d\n', j );
    fprintf ( 1, '\n' );
    for i = 1 : m
      fprintf ( 1, '%d  %g  %g\n', i, x1(i,j), x2(i,j) );
    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

