Creative Visualisation of Magic Squares

Magic squares are n*n square grids in which each row, column, and diagonal add to the same sum. The ‘magic line’, drawn by connecting each integer in numerical sequence, creates a pattern that can differ or be shared between instances. The dual purpose of this work is: 1. to produce a variety of artworks—printed, digital and animated that use the magic line as a departure point; and 2. to investigate mathematical processes (matrix transformations) that produce identical magic line patterns, with the aim of identifying all unique such patterns, and simplifying the classification of magic squares, using visual and programmatic methods. The ongoing creative process has led us to produce a web-based visualisation tool (squares.cubelife.org), with which we have explored different types of mathematical curve to visualise the magic line, and to identify certain useful groups. This can already produce an extensive variety of interesting aesthetic results that differ from existing ‘straight line’ visualisations of magic squares. As well as exploring the creative visual permutations, our methods also enable the measurement of certain properties, such as the length of the magic line and the results of certain transformations.


INTRODUCTION
"If we see nature is beautiful, then we are, in essence, seeing it in patterns. Pattern is the crystallisation of beauty. To understand beauty and to understand pattern are aspects of the same thing." (Yanagi 2013) This work combines research and practice in process-based art, computer science and mathematics, the challenge being to understand each context sufficiently and identify overlaps between them, while considering multiple audiences and interest groups. The aims are to: • identify common magic line patterns; • simplify the classification of magic squares; • explore different vector-based approaches to draw the magic line creatively; • create online software as a vehicle for this visual and mathematical research.
In a modest way, the aim is to advance knowledge within these fields, as well as produce artworks that merge and illustrate the creative and combinatorial research.
Magic squares are matrices of n 2 numbers arranged in square n*n grids (where n-the 'order'-is a number greater than 2), in which each row, column, and diagonal add to the same sum: the 'magic constant'. The 'magic line' is drawn by connecting each integer in numerical sequence. Variations of magic squares produce sets of magic line patterns that appear either ordered/symmetrical, or visually disordered/asymmetrical. The attraction of both visually ordered and disordered patterns resulting from a common set of rules feeds into the creative intuition for balance between order and disorder. This balance has a parallel in complexity science in the 'sweet spot'-where the interesting stuff happens between similar extremes (Everitt & Robertson 2007, Everitt 2011. Until the advent of the computer, magic squares were considered mainly as mathematical puzzles and-before that for many centuries-used in India and China since around 600 CE for 'magical' and ritual properties in talismans and other charms, a surviving practice in some cultures (Neel 2012).
The order 4 magic square in Albrecht Dürer's engraving 'Melencolia I' (1514) is well known. Cornelius Agrippa (1533) associated seven specific magic squares with the known planets of the time which popularised their usage in European culture and filtered into occult and now New Age practice. "It is affirmed by magicians that there are certain tables of numbers distributed to the seven planets" (ibid.) Although parallel mathematical investigations of magic squares ran alongside this occult use, computing has now advanced knowledge in the field by making the necessary calculations and analysis less laborious. Yet Agrippa's 'planetary' magic squares still feature in some more recent creative work (see Maxwell-Davies, later).
The analysis of magic squares we are undertaking falls into the branch of mathematics known as combinatorics, or the solution of problems that choose and arrange the elements of certain (usually finite) sets according to prescribed rules. Magic squares themselves contain many subgroups, some of which we use in this work.
There are three basic types: odd, odd-even and even, each having several construction techniques. These three groups comprise magic squares where the value of n is as in the following sequences: • odd: 3, 5, 7, 9, 11… • odd-even: 6, 10, 14, 18, 22… • even: 4, 8, 12, 16, 20… For increasing values of n across the three groups, the permutations of unique magic squares grow exponentially. While there is a single unique order 3 magic square and 880 order 4s, calculations give 275305224 of order 5; for order 6 this rises to an almost inconceivable estimate of around 1.775392(12)·10 19 variations (Trump 2018).
There are further classification groups within each set of order n magic squares, named variously by researchers as associative, pandiagonal, ultramagic, most-perfect and other often overlapping categories.
Although current work is focussed mainly on order 4s, the software we have developed for analysis and visual display accepts higher-numbered orders

THE SPECIFIC FOCUS OF THIS WORK
Given these cross-disciplinary elements, the focus of this research is to: • produce a variety of artworks-printed, digital and animated-from the order 4 magic square data set, manipulated programmatically in a web-based interface with various display options that generate SVGs (Scalable Vector Graphics) for resolution-independent PDFs and finequality prints. The software accepts any magic square or whole series, drawing and/or animating the resulting magic lines in various styles; • investigate the matrix transformations or other mathematical processes that produce identical magic line patterns, and work these into formulae that describe and group the transformations into sets.
One of the central research questions is to check whether some of the magic squares in the accepted 'unique' set of 880 have identical shared magic line patterns. Visual inspection makes it apparent that many do. The next step is to analyse these duplications, group them and categorise whether the identical magic line patterns were also rotated or reflected. The final analysis is to determine which numerical matrix transformations produce these identical patterns, the artistic aim being to create a new set of 'thoroughly unique' magic squares in which each magic line pattern is different.

CONTEXT
Other artists have used magic squares as data for generative or procedural artworks, the most notable being Vera Molnar (Baby & Banz n.d.)-whose work is the subject of various re-creations or 'hommagesʼ (Lange 2013)-but also others e.g. Paul Heimbach (n.d.) and Margaret Kepner (n.d., 2017), who referred to her work with magic squares as a "visual expression of systems" (Happersett 2014). Sir Peter Maxwell-Davies also used Agrippa's order 9 magic square of the Moon for his setting of the choral plainsong 'Ave Maris Stella ' (1975), the order 8 of Mercury for 'A Mirror of Whitening Light' (1977( , Roberts 2016) and other magic squares for compositions, which he saw as "dance patterns, whose steps pass through 'mazes' and consequently as note patterns, memorable without reference to numbers" (McGregor 2004). He elaborates on this in his writings: "I would claim, with only the authority by instinct, and some listeners' experiences of the work that Ave Maris has assumed some of the healing qualities also associated with this square" (Maxwell-Davies 2017).

Inspired by Maxwell-Davies, composer Oliver
Thurley has a brief but interesting blog post on his own more programmatic use of magic squares for musical composition (2012). Although these uses are innovative in the creative sense, they do not engage deeply with mathematical properties or explore some possible alternative methods of rendering the magic line visually.
It is taken as incidental that this work also draws on the approach of established figures in the wider field of systems, algorithmic or generative art, such as Paul Brown, Ernest Edmonds, Roman Verotsko, Peter Beyls, etc.
It is worth noting that magic matrices also exist in 3-dimensional (see figure 3) or even higher spaces-4D, 5D… Previous work from one of the authors used magic cubes (3D magic matrices) to generate visuals for the interactive heartbeat-driven work 'cubeLife'see a still-shot in figure 4 (Everitt & Turner 1999, Edmonds et. al. 2004. As well as a basic magic cube visualiser shown in figure 3 on the right (Everitt & Daglish n.d.), this artwork was preceded by a simple desktop application designed to collect and visualise the magic line-the translation of which into modern web-based code (using JavaScript) was the starting-point for the current more fully-featured web application.

THE METHOD
For the numerical base data, we initially used the 880 order 4 magic squares collated by Mutsumi Suzuki (collated by Rivera 2019) based on the original set published in 1693 by Frénicle de Bessey (cited in Heinz 2010), widely accepted to be complete in that each magic square is numerically unique.
Henry Dudeney proposed 12 groups (1917), later named after Greek deities by Jim Moran (1982). The patterns shown in figure 5 illustrate how each complementary pair of numbers (i.e. 1-16, 2-15, 3-14…) relate to each other within the matrix; but note that these are not transformation diagramsthey simply show the positional relationships between each number. It remains to be seen if they are useful for our process. Rotations and reflections of the magic square matrix are considered trivial and excluded from any set of unique magic squares of any order. In mathematics these are called the octic group or dihedral group of 4, abbreviated to 'D4' (Weisstein 2020). We refer to them as: identity (ID), rotate 90 (R1), rotate 180° (R2), rotate -90° (R3), mirror vertically (MV) and horizontally (MH); mirror along diagonal 1 (MD1) and diagonal 2 (MD2).
To simplify magic square types by pattern analysis, the sequence of investigation has been to: • scan the magic line patterns visually; • see if the various SVG line renderings retain pattern similarity and symmetry; • measure line lengths to select possible similar patterns; • attempt tests with fully automated image comparison algorithms; • determine how matrix transformations account for some identical patterns.
The generative artistic aims have been to create an extensively customisable work that outputs: • synchronous/asynchronous animations, from single squares to whole sets; • curated partial renderings and still shots; • complete renderings with optional settings; • style projected, printed, or online versions; • magic lines of any order to explore complexity and shared or unique patterns; • different data sets with search capabilities; • renderings from manual user input.

CODE
The public-facing project is built with the browserbased web technologies HTML, CSS, with clientside JavaScript to generate Scalable Vector Graphics (SVGs) (https://squares.cubelife.org). The source code-under ongoing development-is in a public code repository on GitHub (Raczinski 2020).  'straight', 'quadvertex', 'quadline', 'arc', and 'altarc'. In addition, using the Haskell language (better than JavaScript for large datasets) we generated all possible order 4 magic squares programmatically for completeness (total 7040) using an algorithm that includes the 8 D4 transformations. Excluding these reduced numbers to the expected 880, following a simple numerical order that differs from that used by Suzuki (Rivera 2019).
The software contains various sets order 4 squares: the Suzuki set (ibid.); our own set (7040/880); plus smaller incomplete collections of larger order magic squares. These are processed to build detailed data structures containing indices for various computational processes (to accelerate drawing and animation). Specifically, the process of generating a visualisation is as follows.
• Each unique number sequence is converted to an array of coordinates ready for drawing the magic line as an SVG. • For the 'straight' style (the usual magic line diagram, see figure 12) the points on these coordinates are connected in order and then back to the start to form a continuous zig-zag line. The software also allows for pre-formatted printed output that fits neatly at certain sizes and can scale to any desired resolution.
Overall, writing this interface created methods of displaying varying visual representations, with the option of showing the numerical grids that generate them (singly or in filtered groups and sets), as well as building a data library of coded sequences; in effect, a comprehensive catalogue of mathematical magic square data and visual patterns.

FINDINGS
Our first finding within the unique numerical arrangements of the 880 unique magic squares was that identical magic line patterns appear multiple times, shared between 2 or more magic squares, including several groups otherwise held to be numerically unique. For the creative work, this duplication needed to be eliminated for the desired visual output, but uncovering any underlying mathematical commonalities then became a major part of the algorithmic process. It was immediately clear that certain pairs of magic squares-known as 'complementaries', in which the order of the numbers is simply reversed (i.e. 16=1, 15=2, 14=3…) obviously produce identical magic lines. The reversal of the number sequence is graphically visible when the magic line is animated (start and end points are reversed), but the static magic line pattern is unchanged. It is still under investigation why some complementaries are contained in Suzuki's set of 880 (but not our own).
On further investigation, some numerically distinct magic squares also produce identical, rotated or reflected versions of a single magic line pattern, but in these cases the matrix transformations and resulting transposition of numbers require more investigation to assess how 'trivial' or not these cases might be considered mathematically or, if not, whether the shared magic line pattern can be considered as a distinct kind of 'trivial' within certain recognised magic square sub-groups. Calculation of the length of the magic line serves as a preliminary step towards identifying these shared patterns programmatically (and is required for the animation process). The total length of differing magic lines varies and, while not a guarantee of homology, this starting-point identified a total of 267 different possible line lengths from the set of 880 (based on the quadvertex style), 35 of which have unique lengths, meaning the other 232 possible lengths are shared. This indicates that these groups contain potential duplicates of some kind, although differing patterns can also have a common length by coincidence.
Careful manual analysis of groups with shared lengths identified 383 unique magic line patterns if visual D4 rotations and reflections are excluded from the original set of 880 (as distinct from D4 transformations on the number sequences).
Francis Gaspalou argued that the 7040 order 4 magic squares can be grouped into 32 different categories producing a set of 220 essentially different squares (n.d.). We want to confirm this against our visual representation, so we are in the process of investigating and programming functions to represent and document this and other groups more distinctly, with the aim of moving towards the formulation of some general rules.
Basically, we have identified a disconnect between the established theory of magic square numbers and magic lines. The known transformations on the number grid can produce duplicates of the original magic line, but these do not represent the same number transformations. Yet because the integers in the grid are the primary focus in the existing literature, similarities in magic line patterns is an area not yet adequately covered.
For example, if we apply the 8 D4 transformations to the numbers of Suzuki's magic square #9, we get the eight magic lines shown in figure 9. It is apparent that the last seven represent what we would consider visually trivial equivalents of the first magic line pattern and they should not be included in the set of 880 unique magic squares. However, two of these magic lines appear in the set (in this case Suzuki #9 and #13, both of which have a line length of 1987). At this point there are three approaches to identify and remove visual duplicates. We can compare magic lines by running image comparison algorithms; and (from the calculation of the SVG magic line length for creative animations) identify groups that share the same magic line length so may possibly-but not definitely-be equivalent. We can also separately identify the mathematical transformations that generate equivalent magic lines. Investigating this third mathematical approach shows that there are transformations outside the D4 set that can nevertheless produce equivalent magic lines. For example, reverse engineering the matrix transformation between #9 and #13 and two further examples produces interesting matrix transformations-see figure 10. To verify whether these transformations work universally or not we have applied all three to Suzuki #9 in figure 11. Figure 11: ID, XMV1, XMV2, XMV3 applied to Suzuki #9 The first transformation (XMV1) clearly yields a vertically reflected version of the original not created by one of the D4 transformations. So, for this particular square, these transformations yield just one equivalent magic line.
We suspect that these transformations relate in some way to the 12 classification groups mentioned earlier, although these are not actual transforms but illustrate relationships between complementary numbers.
The investigation into these numerical matrix transformations has only just started (we have only codified three to date), so we may have more concrete results in due course.
Another numerical transformation guarantees an identical magic line-the addition of n + 8 mod 16 to each number reorders the matrix and creates a valid numerical transposition with a different start and end point; the magic line trajectory through the matrix from 1-16 remains identical. This has only been tested on a small sample, and although the results are consistent so far, this is no guarantee for the whole set.

CONCLUSION
Contributions to knowledge, reflecting the overall aims outlined in the bullet points from the introduction are as follows: • successfully identify common magic line patterns, and make progress towards identifying the underlying reasons for these; • propose at least one simplification for the classification of magic squares; • thoroughly explore a variety of creative vector-based methods to draw the magic line and output images; • complete the fully-functional online software as a mathematical and creative research vehicle.
Regarding the first two points: we propose that complimentary squares (at the very least) should be removed from any set of visually unique squares. Other cases in which the magic line represents shared relationships between differing magic square number grids requires more investigation.
The last two points are fully satisfied, yet there is still plenty of room for experimentation. Some visual outputs of the full set of 880 magic squares in three styles from the scalable PDF files have already been exhibited (Everitt & Raczinski 2019) and although these were from earlier in the process, they were received well.
Colour and magic line choice via the web interface, offers a vast variety of static results, including animated versions that can be 'grabbed' part-way through to generate complex interwoven patterns. The next step will be to exhibit live animations of the various processes; speed (from meditative to frantic) and other parameters can already be controlled, and animations run indefinitely and appear non-repetitive, as each magic line length cycles in its own time, yielding pleasing large-scale complex dynamic patterns. Finally, following the creative use of other kinds of data in previous public works (where solar weather was used to filter keywords from a corpus of diary entries) there is the opportunity to connect the exhibited and online displays to further data from sources yet to be selected, as well as to encourage user input e.g. 'name a pattern', 'choose your colours', 'select a group to display', etc.
For the mathematical work, the priorities are: • a 'magic carpet' visualiser (where groups of magic squares merge into a numerically connected matrix); • user interaction and public access to all facets of the work; • confirmation of groups of identical patterns from image comparison.
Although we are keen to address both creative and mathematical audiences in future work, it is important to combine the output so that the artistic output illustrates the underlying mathematical theory and, while this work is interdisciplinary, we also intend to identify specialist outlets to publish selected findings.
One further line of inquiry has emerged from audience feedback: as soon as we added curved lines, people began to find meaning by declaring what the images suggest to them (for example, in figure 7). The role of symmetry and asymmetry in this perception is as yet unexplored.