Module magic_square
[hide private]
[frames] | no frames]

Module magic_square

source code

Simple operations with magic squares.

Prerequisites:
Functions:
Examples:
>>> from magic_square import *
>>> print magic(3)
[[8 1 6]
 [3 5 7]
 [4 9 2]]
>>> magic_constant()
15
>>> ismagic(magic(4))
True
>>> magic_constant()
34
>>> magic_constant([1, 1, 1, 1])
2
>>> ismagic([[1, 2], [3, 4]])
False
Notes:

(1) Function magic(N) produces the same magic squares as Matlab and Octave command magic(N). The speed of calculations for N close to 1000 is about 100--200 times faster than in Octave.

(2) Integer arithmetic in NumPy is done modulo 2**32. That can give a false positive in ismagic(A) for integer arrays with overflowing in row, column, or diagonal sums. To avoid that and to avoid wrong answers in magic_constant(A), in such cases the array's dtype should be changed to 'int64', or if 'int64' also overflows, to 'object'.

Screenshots:

That's how it looks in SAGE:

SAGE

And that's how it looks in IDLE:

IDLE

Author:
Alec Mihailovs <alec@mihailovs.com>
Last Updated:
February 22, 2007.


Functions [hide private]
  ismagic(A)
Test whether the given array is a magic square.
  magic_constant(A=None)
Magic constant of the magic square.
  magic(N)
Create an N by N magic square.

Variables [hide private]
  __version__ = '0.2'
Development Status :: 3 - Alpha
  __author__ = 'Alec Mihailovs <alec@mihailovs.com>'
Alec Mihailovs <alec@mihailovs.com>
  _constant = None
Last calculated magic constant.

Function Details [hide private]

ismagic(A)

source code 

Test whether the given array is a magic square.

Input:
A -- 2D array, or a sequence that can be interpreted as such.
Output:
bool or NotImplementedType -- True if A is a magic square, NotImplemented if the number of dimensions of A is not 2 or 1, or the size is not a perfect square in the 1D case, and False otherwise.
Examples:
>>> from magic_square import *
>>> ismagic(magic(3))
True
>>> ismagic([1, 1, 1, 1])
True
>>> ismagic([[8, 1, 6], [3, 5, 7], [4, 9, 2]])
True
>>> ismagic(1)            # 0 dimensions
NotImplemented
>>> ismagic('[[1]]')      # a string gives 0 dimensions
NotImplemented
>>> ismagic([[[1]]])      # 3 dimensions
NotImplemented
>>> ismagic(array([[1, 2], [3, 4]]))
False
Notes:

Integer arithmetic in NumPy is done modulo 2**32 as in the following example:

>>> from numpy import array
>>> array([2**16])
array([65536])
>>> _*_
array([0])

That can give a false positive in ismagic(A) for integer arrays with overflowing in row, column, or diagonal sums. To avoid that, in such cases the array's dtype should be changed to either 'int64', or 'object', see magic_constant(A) Notes.

magic_constant(A=None)

source code 

Magic constant of the magic square.

Input:
A -- 2D array, or a sequence that can be interpreted as such. If not entered, the last constructed magic(n) or last array A tested in ismagic(A) is used.
Output:
dtype of the array, or Python long, or NoneType -- the magic constant if the array is a magic square, or None otherwise. Python long can occur if A is None and the magic constant is calculated for the last constructed magic(n) with large n.
Examples:
>>> from magic_square import *
>>> magic_constant([1, 1, 1, 1])
2
>>> print magic_constant([1, 2, 3, 4])
None
>>> ismagic(magic(6))
True
>>> magic_constant()
111
>>> a = magic(5000)
>>> magic_constant()
62500002500L
Notes:

Integer arithmetic in NumPy is done modulo 2**32. That makes magic_constant(A) to return wrong answers for integer arrays with overflowing in row, column, or diagonal sums. For example,

>>> magic_constant(magic(5000))
-1924506940
>>> ismagic(magic(5000))
True
>>> magic_constant()
-1924506940

Note that

>>> 62500002500L % 2**32 == -1924506940 % 2**32
True

To avoid such wrong answers, the array's dtype can be changed to 'int64', or if 'int64' also overflows, to 'object' (that one significantly slows down the calculations.) In this example,

>>> from numpy import array
>>> magic_constant(array(magic(5000), dtype='int64'))
62500002500
>>> magic_constant(array(magic(5000), dtype='object'))  # long
62500002500L

magic(N)

source code 

Create an N by N magic square.

Input:
N -- an integer in some form, may be float or quotted.
Output:
an 'int32' N by N array -- the same magic square as in Matlab and Octave magic(N) commands. In particular, the Siamese method is used for odd N (but with a different implementation.)
Examples:
>>> from magic_square import *
>>> magic(4)
array([[16,  2,  3, 13],
       [ 5, 11, 10,  8],
       [ 9,  7,  6, 12],
       [ 4, 14, 15,  1]])
>>> magic_constant()
34
>>> magic(5.0)                     # can be float
array([[17, 24,  1,  8, 15],
       [23,  5,  7, 14, 16],
       [ 4,  6, 13, 20, 22],
       [10, 12, 19, 21,  3],
       [11, 18, 25,  2,  9]])
>>> print magic('6')               # can be quotted
[[35  1  6 26 19 24]
 [ 3 32  7 21 23 25]
 [31  9  2 22 27 20]
 [ 8 28 33 17 10 15]
 [30  5 34 12 14 16]
 [ 4 36 29 13 18 11]]
>>> magic(2)                       # consistent with Octave
Traceback (most recent call last):
TypeError: No such magic squares exist.
>>> magic(0)
array([], shape=(0, 0), dtype=int32)
>>> magic_constant()               # the empty sum is 0
0
Notes:
The calculations for n close to 1000 are about 100--200 times faster than in Octave.

Variables Details [hide private]

__version__

Development Status :: 3 - Alpha
Value:
'0.2'                                                                  
      

__author__

Alec Mihailovs <alec@mihailovs.com>

Value:
'Alec Mihailovs <alec@mihailovs.com>'                                  
      

_constant

Last calculated magic constant.
Value:
None