sum, transpose and diag

You're probably already aware that the special properties of a magic square have to do with the various ways of summing its elements. If you take the sum along any row or column, or along either of the two main diagonals, you will always get the same number. Let's verify that using SCILAB. 

The first statement to try is,

sum(S,'c')

SCILAB replies with,

ans =

! 34. !
! 34. !
! 34. !
! 34. ! 

When you don't specify an output variable, SCILAB uses the variable ans, short for answer, to store the results of a calculation. You have computed a row vector containing the sums of the columns of S. Sure enough, each of the columns has the same sum, the magic sum, 34.

The next statement is also similar to the previous one.

sum(S,'r')

SCILAB displays

        ans =

        ! 34. 34. 34. 34. !

The sum of the elements on the main diagonal is easily obtained with the help of the diag function, which picks off that diagonal.

diag(S)

produces

        ans =

        ! 16. !
        ! 10. !
        ! 7.   !
        ! 1.   !

You have verified that the matrix in Dürer's engraving is indeed a magic square and, in the process, have sampled a few SCILAB matrix operations. The following sections continue to use this matrix to illustrate additional SCILAB capabilities.