magic_matrix, a Python code which computes a magic matrix, for any odd order n, such that all rows and columns have the same sum.

A magic matrix is an n by n array, whose entries include each positive integer from 1 to n^2, in such a way that each row and column has a common sum. Sometimes the diagonals also form this sum.

When n is odd, there is a simple algorithm for producing a corresponding magic square. When n is even, (and greater than 2!) there is a different, more complicated, algorithm. This code only implements the algorithm for the case when n is odd.

A magic matrix can be created as a list of lists, or as a numpy array. This code demonstrates both approaches, and suggests why the list of lists is a particularly awkward way of dealing with matrices.


The computer code and data files made available on this web page are distributed under the MIT license


magic_matrix is available in a MATLAB version and an Octave version and a Python version.

Related Data and Programs:

jordan_matrix, a Python code which returns a random matrix in Jordan canonical form.

levenshtein_matrix, a Python code which returns the Levenshtein distance matrix defined by two strings.

monopoly_matrix, a Python code which computes the adjacency and transition matrices for the game of Monopoly.

plasma_matrix, a Python code which sets up a matrix associated with a problem in plasma physics.

risk_matrix, a Python code which computes the transition and adjacency matrix for the game of risk.

snakes_matrix, a Python code which computes the transition matrix for the game of Snakes and Ladders.

tennis_matrix, a Python code which computes the transition matrix for a game of tennis, which has 17 distinct states.

test_matrix, a Python code which defines test matrices for which the condition number, determinant, eigenvalues, eigenvectors, inverse, null vectors, P*L*U factorization or linear system solution are known. Examples include the Fibonacci, Hilbert, Redheffer, Vandermonde, Wathen and Wilkinson matrices.

usa_matrix, a Python code which defines the adjacency matrix for US states, using a variety of matrix formats.

wathen_matrix, a Python code which compares storage schemes (full, banded, sparse triplet, sparse) and solution strategies (A\x, linpack, conjugate gradient (CG)) for linear systems involving the Wathen matrix, which can arise when solving a problem using the finite element method (FEM).

web_matrix, a Python code which stores sample matrices describing a web page network. These matrices are typically very sparse, and the examples here are stored using the sparse triplet (ST) format. They can be used to demonstrate pagerank and other graph algorithms.

wishart_matrix, a Python code which produces sample matrices from the Wishart or Bartlett distributions, useful for sampling random covariance matrices.


  1. Christian Hill,
    Example E6.2,
    Learning Scientific Programming with Python,
    Cambridge University Press,
    Second Edition, 2020,
    ISBN: 978-1108745918

Source Code:

Last modified on 22 January 2023.