function A = magic_matrix ( n )

%*****************************************************************************80
%
%% magic_matrix returns a MAGIC matrix.
%
%  Example:
%
%    N = 5
%
%    17    24     1     8    15
%    23     5     7    14    16
%     4     6    13    20    22
%    10    12    19    21     3
%    11    18    25     2     9
%
%  Properties:
%
%    A is not symmetric.
%
%    The row sums and column sums and diagonal sums of A are n*(n^2+1)/2.
%
%    A has an eigenvalue of n*(n^2+1)/2, with left and right eigenvectors
%    of ( 1, 1, 1, ..., 1 ).
%
%    A is not diagonally dominant.
%
%  Modified:
%
%    16 December 2019
%
%  Author:
%
%    The Mathworks, Inc.
%
%  Reference:
%
%    David Collison,
%    Algorithms 117 and 118:
%    Magic square (even order) and Magic square (odd order),
%    Communications of the ACM,
%    Volume 5, Number 8, pages 435-436.
%
%    Maurice Kraitchik,
%    Mathematical Recreations,
%    Norton, 1942, pages 149-152.
%
%  Input:
%
%    integer N, the order of A.
%    N must not be 2.
%
%  Output:
%
%    real A(N,N), the matrix.
%
  if ( n == 2 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'magic_matrix - Fatal error!\n' );
    fprintf ( 1, '  The order N must not be 2.\n' );
    error ( 'magic_matrix - Fatal error!' );
  end

  if ( mod ( n, 2 ) == 1 )
    A = magic_odd ( n );
  elseif ( mod ( n, 4 ) == 0 )
    A = magic_even_doubly ( n );
  else
    A = magic_even_singly ( n );
  end

  return
end

