Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

allsums

Row, column, and two diagonal sums of arrays


Description

Returns all rowsums, all columnsums, and all (broken) diagonal sums of a putative magic square.

Usage

allsums(m,func=NULL, ...)

Arguments

m

The square to be tested

func

Function, with default NULL interpreted assum(), to be applied to the square rowwise, columnwise, and diagonalwise

...

Further arguments passed to func()

Value

Returns a list of four elements. In the following, “sums” means “the result of applying func()”.

rowsums

All n row sums

colsums

All n column sums

majors

All n broken major diagonals (northwest-southeast). First element is the long (unbroken) major diagonal, tested by is.magic()

minors

All n broken minor diagonals (northeast-southwest). First element is the long (unbroken) minor diagonal.

Note

If func() returns a vector, then the allsums() returns a list whose columns are the result of applying func(). See third and fourth examples below.

Used by is.magic() et seq.

The major and minor diagonals would benefit from being recoded in C.

Author(s)

Robin K. S. Hankin

See Also

Examples

allsums(magic(7))
allsums(magic(7),func=max)

allsums(magic(7),func=range)
allsums(magic(7),func=function(x){x[1:2]})


allsums(magic(7),sort)
  # beware! compare apply(magic(7),1,sort) and apply(magic(7),2,sort)

magic

Create and Investigate Magic Squares

v1.5-9
GPL-2
Authors
Robin K. S. Hankin
Initial release
2018-09-14

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.