Learn R Programming

Manipulation of high-dimensional arrays in R with the magic package

Overview

The magic package implements functionality for manipulating high-dimensional arrays using efficient vectorised methods. The original application was high-dimensional magic hypercubes. This README shows some of the more useful functions in the package.

Installation

You can install the released version of magic from CRAN with:

# install.packages("magic")  # uncomment this to install the package
library("magic")

Package highlights

  • Function adiag() binds arbitrarily-dimensioned arrays corner-to-corner
  • Function apad() pads arbitrarily-dimensioned arrays
  • Function apldrop() is a replacement for APL’s drop
  • Function aplus() superimposes two arrays of different dimensions and returns the sum of overlapping elements
  • Function arev() is a multidimensional generalization of rev()
  • Function arot() is a generalization of matlab’s rotdim
  • Function fnsd() returns the first nonsingleton dimension of an arbitrary dimensioned array
  • Function ashift() shifts the origin of arbitrary dimensioned arrays

Much of the package functionality is vectorised in array dimension.

Further information

For more detail, see the package vignette

vignette("magic")

Copy Link

Version

Install

install.packages('magic')

Monthly Downloads

27,304

Version

1.6-1-1

License

GPL-2

Issues

Pull Requests

Stars

Forks

Maintainer

Robin K S Hankin

Last Published

September 25th, 2026

Functions in magic (1.6-1-1)

as.standard

Standard form for magic squares
arow

Generalized row and col
is.magichypercube

magic hypercubes
is.ok

Does a vector have the sum required to be a row or column of a magic square?
latin

Random latin squares
is.square.palindromic

Is a square matrix square palindromic?
magichypercube.4n

Magic hypercubes of order 4n
lozenge

Conway's lozenge algorithm for magic squares
magiccube.2np1

Magic cubes of order 2n+1
magicplot

Joins consecutive numbers of a magic square.
eq

Comparison of two magic squares
magic-package

tools:::Rd_package_title("magic")
magic.prime

Magic squares prime order
magic.product

Product of two magic squares
magiccubes

Magic cubes of order 3
panmagic.4

Panmagic squares of order 4
nqueens

N queens problem
perfectcube6

A perfect cube of order 6
perfectcube5

A perfect magic cube of order 5
hendricks

A perfect magic cube due to Hendricks
strachey

Strachey's algorithm for magic squares
magic.constant

Magic constant of a magic square or hypercube
hadamard

Hadamard matrices
panmagic.6npm1

Panmagic squares of order 4n, 6n+1 and 6n-1
magic

Creates magic squares
magic.4np2

Magic squares of order 4n+2
hudson

Pandiagonal magic squares due to Hudson
notmagic.2n

An unmagic square
is.magic

Various tests for the magicness of a square
minmax

are all elements of a vector identical?
transf

Frenicle's equivalent magic squares
magic.2np1

Magic squares of odd order
magic.4n

Magic squares of order 4n
magic.8

Regular magic squares of order 8
recurse

Recursively apply a permutation
panmagic.8

Panmagic squares of order 8
process

Force index arrays into range
sam

Sparse antimagic squares
shift

Shift origin of arrays and vectors
subsums

Sums of submatrices
apad

Pad arrays
cilleruelo

A class of multiplicative magic squares due to Cilleruelo and Luca
arot

Rotates an array about two specified dimensions
Frankenstein

A perfect magic cube due to Frankenstein
do.index

Apply a function to array element indices
Ollerenshaw

A most perfect square due to Ollerenshaw
fnsd

First non-singleton dimension
diag.off

Extracts broken diagonals
force.integer

Integerize array elements
apl

Replacements for APL functions take and drop
aplus

Generalized array addition
cube2

A pantriagonal magic cube
adiag

Binds arrays corner-to-corner
allsubhypercubes

Subhypercubes of magic hypercubes
allsums

Row, column, and two diagonal sums of arrays
arev

Reverses some dimensions; a generalization of rev
circulant

Circulant matrices of any order