is.magic {magic} | R Documentation |
Returns TRUE
if the square is magic, semimagic, panmagic, associative,
normal. If argument give.answers
is TRUE
, also returns
additional information about the sums.
is.magic(m, give.answers = FALSE, FUN=sum, boolean=FALSE) is.panmagic(m, give.answers = FALSE, FUN=sum, boolean=FALSE) is.semimagic(m, give.answers = FALSE, FUN=sum, boolean=FALSE) is.associative(m) is.normal(m) is.mostperfect(m,give.answers=FALSE) is.2x2.correct(m,give.answers=FALSE) is.bree.correct(m,give.answers=FALSE) is.latin(m,give.answers=FALSE)
m |
The square to be tested |
give.answers |
Boolean, with TRUE meaning return additional
information about the sums (see details). |
FUN |
A function that is evaluated for each row, column, and unbroken diagonal. |
boolean |
Boolean, with TRUE meaning that the square is
deemed magic, semimagic, etc, if all applications of FUN
evaluate to TRUE . If boolean is FALSE , square
m is magic etc if all applications of FUN are identical. |
A semimagic square is one all of whose row sums equal all its columnwise sums (ie the magic constant).
A magic square is a semimagic square with the sum of both unbroken diagonals equal to the magic constant.
A panmagic square is a magic square all of whose broken diagonals sum to the magic constant. Ollerenshaw calls this a ``pandiagonal'' square.
A most-perfect square has all 2-by-2 arrays anywhere within the
square summing to 2S where S=n^2+1; and all pairs
of integers n/2 distant along the same major (NW-SE) diagonal
sum to S (note that the S used here differs from
Ollerenshaw's because her squares are numbered starting at zero). The
first condition is tested by is.2x2.correct
and the second
by is.bree.correct
.
All most-perfect squares are panmagic.
A normal square is one that contains n^2 consecutive integers (typically starting at 0 or 1).
An associative square is a magic square in
which
a[i,j]+a[n+1-i,n+1-j]=n^2+1.
Note that an associative semimagic square is magic; see also
is.square.palindromic()
. The definition extends to magic
hypercubes: a hypercube a
is associative if a+arev(a)
is
constant.
A latin square of size n-by-n is one in which
each column and each row comprises the integers 1 to n (not
necessarily in that order). Function is.latin()
is a wrapper
for is.latinhypercube()
because there is no natural way to
present the extra information given when give.answers
is
TRUE
in a manner consistent with the other functions documented
here.
Returns TRUE
if the square is semimagic, etc.
If give.answers
is taken as an argument and is TRUE
, return a
list of at least five elements. The first element of the list is the
answer: it is TRUE
if the square is (semimagic, magic,
panmagic) and FALSE
otherwise.
Elements 2-5 give the result of a call to allsums()
, viz: rowwise
and columnwise sums; and broken major (ie NW-SE) and minor (ie NE-SW)
diagonal sums.
Function is.bree.correct()
also returns the sums of
elements distant n/2 along a major diagonal
(diag.sums
); and function is.2x2.correct()
returns the
sum of each 2x2 submatrix (tbt.sums
); for
other size windows use subsums()
directly.
Function is.mostperfect()
returns both of these.
Function is.magic()
is vectorized; if a list is supplied, the
defaults are assumed.
Robin K. S. Hankin
http://mathworld.wolfram.com/MagicSquare.html
minmax
,is.perfect
,is.semimagichypercube
is.magic(magic(4)) is.magic(diag(9),FUN=max) #should be TRUE stopifnot(is.magic(magic(3:8))) is.panmagic(panmagic.4()) is.panmagic(panmagic.8()) data(Ollerenshaw) is.mostperfect(Ollerenshaw) proper.magic <- function(m){is.magic(m) & is.normal(m)} proper.magic(magic(20))