is.magic {magic}R Documentation

Various tests for the magicness of a square

Description

Returns TRUE if the square is magic, semimagic, panmagic, associative, normal. If argument give.answers is TRUE, also returns additional information about the sums.

Usage

is.magic(m, give.answers = FALSE, func=sum, boolean=FALSE) 
is.panmagic(m, give.answers = FALSE, func=sum, boolean=FALSE) 
is.pandiagonal(m, give.answers = FALSE, func=sum, boolean=FALSE) 
is.semimagic(m, give.answers = FALSE, func=sum, boolean=FALSE) 
is.semimagic.default(m)
is.associative(m)
is.normal(m)
is.sparse(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)
is.antimagic(m, give.answers = FALSE, func=sum) 
is.totally.antimagic(m, give.answers = FALSE, func=sum)
is.heterosquare(m, func=sum) 
is.totally.heterosquare(m, func=sum)
is.sam(m)
is.stam(m)

Arguments

m

The square to be tested

give.answers

Boolean, with TRUE meaning return additional information about the sums (see details)

func

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 func evaluate to TRUE. If boolean is FALSE, square m is magic etc if all applications of func are identical

Details

Value

Returns TRUE if the square is semimagic, etc. and FALSE if not.

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 2\times 2 submatrix (tbt.sums); for other size windows use subsums() directly. Function is.mostperfect() returns both of these.

Function is.semimagic.default() returns TRUE if the argument is semimagic [with respect to sum()] using a faster method than is.semimagic().

Note

Functions that take a func argument apply that function to each row, column, and diagonal as necessary. If func takes its default value of sum(), the sum will be returned; if prod(), the product will be returned, and so on. There are many choices for this argument that produce interesting tests; consider func=max, for example. With this, a “magic” square is one whose row, sum and (unbroken) diagonals have identical maxima. Thus diag(5) is magic with respect to max(), but diag(6) isn't.

Argument boolean is designed for use with non-default values for the func argument; for example, a latin square is semimagic with respect to func=function(x){all(diff(sort(x))==1)}.

Function is.magic() is vectorized; if a list is supplied, the defaults are assumed.

Author(s)

Robin K. S. Hankin

References

https://mathworld.wolfram.com/MagicSquare.html

See Also

minmax,is.perfect,is.semimagichypercube,sam

Examples

is.magic(magic(4))

is.magic(diag(7),func=max)  # TRUE
is.magic(diag(8),func=max)  # FALSE

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))

[Package magic version 1.6-1 Index]