function [ axis, angle ] = rotation_mat2axis ( a )

%*****************************************************************************80
%
%% rotation_mat2axis() converts a rotation from matrix to axis format in 3D.
%
%  Discussion:
%
%    The computation is based on the fact that a rotation matrix must
%    have an eigenvector corresponding to the eigenvalue of 1, hence:
%
%      ( A - I ) * v = 0.
%
%    The eigenvector V is the axis of rotation.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    12 January 2021
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Jack Kuipers,
%    Quaternions and Rotation Sequences,
%    Princeton, 1998.
%
%  Input:
%
%    real A(3,3), the rotation matrix.
%
%  Output:
%
%    real AXIS(3), the axis vector which remains
%    unchanged by the rotation.
%
%    real ANGLE, the angular measurement of the
%    rotation about the axis, in radians.
%

%
%  Compute the normalized axis of rotation.
%
  axis = zeros ( 3, 1 );

  axis(1) = a(3,2) - a(2,3);
  axis(2) = a(1,3) - a(3,1);
  axis(3) = a(2,1) - a(1,2);

  axis_norm = norm ( axis );

  if ( axis_norm == 0.0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'ROTATION_MAT2AXIS_3D - Fatal error!\n' );
    fprintf ( 1, '  A is not a rotation matrix,\n' );
    fprintf ( 1, '  or there are multiple axes of rotation.\n' );
    error ( 'ROTATION_MAT2AXIS_3D - Fatal error!' );
  end

  axis(1:3) = axis(1:3) / axis_norm;
%
%  Find the angle.
%
  angle = acos ( 0.5 * ( a(1,1) + a(2,2) + a(3,3) - 1.0 ) );

  return
end
