From 596a513a78d31d635af69e8dc1f84dda26436639 Mon Sep 17 00:00:00 2001 From: "Per.Andreas.Brodtkorb" Date: Mon, 23 Apr 2012 13:10:11 +0000 Subject: [PATCH] Added magic square --- pywafo/src/wafo/demos.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pywafo/src/wafo/demos.py b/pywafo/src/wafo/demos.py index 38d50ab..b9469ee 100644 --- a/pywafo/src/wafo/demos.py +++ b/pywafo/src/wafo/demos.py @@ -6,8 +6,23 @@ Created on 20. jan. 2011 import numpy as np from numpy import exp from wafo.misc import meshgrid -__all__ = ['peaks', 'humps'] +__all__ = ['peaks', 'humps', 'magic'] +def magic(n): + ''' + Return magic square for n of odd orders. + + A magic square has the property that the sum of every row and column, + as well as both diagonals, is the same number. + ''' + if np.mod(n,1)==1: # odd order + ix = np.arange(n)+1 + J, I = np.meshgrid(ix,ix) + A = np.mod(I+J-(n+3)/2,n) + B = np.mod(I+2*J-2,n) + M = n*A + B + 1 + + return M def peaks(x=None, y=None, n=51): '''