/**
 * Class for performing test on magic squares
 */
public class MagicSquareTest {

    public static final int dimension = 3; // dimension    
    public static int iters = 100000;  // iterations to perform
    public static int iprint = 5;  

    private MagicSquare square;
    private int d;
    private double[][] matrix;
    
    
    /** Creates new MagicSquareTest instance by given MagicSquare instance
     * @param square MagicSquare instance
     */
    public MagicSquareTest(MagicSquare square) {
        this.square = square;
        d = square.getDimension();
    }
    
    public void perform() {
        double[][] m1 = new double[d][d];
        double[][] m2 = new double[d][d];
        double[][] m3 = new double[d][d];
        double[][] buf = null;

        double sum = d*(d*d + 1)/2;
        
        // normalization
        for (int i = 0; i < d; i++) {
            for (int j = 0; j < d; j++) {
                m1[i][j] = m2[i][j] = square.getSquareAsArray()[i][j] / sum;
            }
        }
        
        for (int iter = 0; iter < iters; iter++) {
            for (int i = 0; i < d; i++) {
                for (int j = 0; j < d; j++) {
                    double value = 0;
                    for (int k = 0; k < d; k++) {
                         value += m1[k][j] * m2[i][k];
                    }
                    m3[i][j] = value;
                }
            }
            buf = m2; m2 = m3; m3 = buf;
        }
        
        matrix = m2;
    }
    
    public void printMatrix() {
        if (matrix == null) return;
        for (int i = 0; i < d; i++) {
            for (int j = 0; j < d; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
        
    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) {
        
        for (int i = 0; i < iprint; i++) {
        
            long startTime = System.currentTimeMillis();
            
            MagicSquareTest test = new MagicSquareTest(new MagicSquare(dimension));
            test.perform();
            
            long endTime = System.currentTimeMillis();
            test.printMatrix();
            System.out.println("Execution time: " + (endTime - startTime)*0.001);
        
        }
        
    }

}

============================================


/**
 * Magic Square Class
 */
public class MagicSquare
{
    
    private int d;
    private int square[][];
    
    /**
     * Creates magic square with given dimension.
     * Note: only odd dimentions are supported
     * @param dimension dimension of magic square
     */
    public MagicSquare(int dimension)
    {
        d = dimension;
        square = new int[d][d];
        
        int x = 0,  y = 0;  // coordinates
        int n = 1;          // current number
        
        for (int i = 0; i < d; i++) {
            for (int j = 0; j < d; j++) {
                x = (d - 1)/2 - i + j;
                y = -(d - 1)/2 + i + j;
                square[wrap(x)][wrap(y)] = n++;
            }
        }
    }
    
    /**
     * Returns dimension of magic square.
     * @return dimension
     */
    public int getDimension() {
        return d;
    }
    
    /**
     * Returns magic square as array.
     * @return array
     */
    public int[][] getSquareAsArray() {
        return square;
    }
        
    private int wrap(int x) {
        if (x < 0) {
            return x + d;
        } else if (x >= d) {
            return x - d;
        } else {
            return x;
        }
    }    
}