The Magic Matrix
In this post, I will display some math magic with numbers. The focus is on the magic matrix. I have also included some (experimental) R code for checks.

The magic matrix is one of those special (square) matrices. With this magic matrix the sum of the numbers in the rows (horizontal), the sum of the numbers in the columns (vertical) and the sum of the numbers in the diagonals are the same.
Here is one example of a 3 by 3 magic matrix.

The sums for each row and each column can be verified. Diagonal sums can be verified as well.
- In the first row, the numbers 2, 7 and 6 sum to 15.
- The numbers in the second row add up to 15 as well (9 + 5 + 1).
- The final third row adds up to 15.
- The first column with numbers 2, 9 and 4 add up to 15.
- One can check that the second column and the final column add up to 15.
- The sum of the main diagonal numbers is equal to 15 from 2, 5 and 8. In linear algebra, this sum is called the trace.
- The other diagonal consists of 4, 5 and 6. These three numbers add up to 15.
Magic Matrices In R [Computing]
In the statistical programming language R, you can easily create magic matrices with the magic package. Use the code install.packages("magic") in order to install the magic package. To load the magic package use the code library(magic).
This second example features a 5 by 5 magic matrix in R.
> library(magic)
>
> A <- magic(5)
>
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 9 2 25 18 11
[2,] 3 21 19 12 10
[3,] 22 20 13 6 4
[4,] 16 14 7 5 23
[5,] 15 8 1 24 17
The row sums and column sums can be easily verified in R. R code for matrices operate in the [i, j] format where i refers to the i-th row and j refers to the j-th column.
> # Check column sums
> sum(A[, 1])
[1] 65
> sum(A[, 2])
[1] 65
> sum(A[, 3])
[1] 65
> sum(A[, 4])
[1] 65
> sum(A[, 5])
[1] 65
>
> # Check row sums:
> sum(A[1, ])
[1] 65
> sum(A[2, ])
[1] 65
> sum(A[3, ])
[1] 65
> sum(A[4, ])
[1] 65
> sum(A[5, ])
[1] 65
Checking the diagonal sums can be tricky. For loops in R are useful here. The diagonal from the top left to the bottom right have the row number and the column number being the same. The second diagonal from the bottom left to the top right starts from the last row and first column and works it way to the first row and the far right column.
> # Check diagonal sum (top left to bottom right)
> diag_sum1 <- 0
>
> for (i in 1:5){
+ diag_sum1 <- diag_sum1 + A[i, i]
+ }
>
> diag_sum1
[1] 65
>
> # Check other diagonal (bottom left to top right)
>
> diag_sum2 <- 0
>
> for (i in 1:5){
+ diag_sum2 <- diag_sum2 + A[6 - i, i]
+ }
>
> diag_sum2
[1] 65
One problem of the check above is that there is a bit too many lines of code. The checking process can be more efficient. I propose a check with a for loop and if statement in R.
The approach is to start with an empty vector to store the row sums, column sums and the diagonal sums. A for loop is used to compute the row and column sums and add these sums to the (storage) vectors. Outside of the for loop, the diagonal sums are computed and added to the vector.
In R, the unique(vect) function outputs the distinct values from the vector. If there is only one then that means that the sums are all the same and we have a magic matrix.
The example of the 10x10 magic square is constructed using 2x2 squares plus the algorithm for 5x5 squares. Nice!
!-=o0o=-!
To follow curated math content follow @math-trail.
If you wish @math-trail to follow you then read this article.
Click here for Mathematics forum on chainBB
Here's more on how to escape matrix and live your 5D life.