# This is sad! Some of the tests in this directory use magic # squares. Our new magic squares code yields a different order than # the old code, which breaks these tests. So, I'm just including the # old code here. It works fine, but it's much slower than the new # stuff. # What programming language would be complete without magic squares? # This function generates magic squares. A magic square is a square # matrix of order N whose elements consist of the consecutive whole # numbers from 1 to N^2, arranged so that the sums of each row, each # column, and each diagonal are all equal. # Note that magic(2) is undefined and returns NULL. # I hacked this up one night when I should have been working on the # house. (That's my excuse for this mess.) # Copyright (C) 1994-96 K. Scott Hunziker. magic = function (n) { local (x; i; j; r; c; v); n = scalar (integer (n)); if (n == 1) { return [1]; } if (n < 3) { return NULL; } if (n % 2) { x = fill (n,n; 0); for (i in 1:n) { v = (i-1)*n + 1 : i*n; r = 2*i-1:2*i-n; r -= n*(r>n); r += n*(r<1); c = (n+3)/2-i:(n+3)/2-i+n-1; c -= n*(c>n); c+= n*(c<1); for (j in 1:n) { x[r[j];c[j]] = v[j]; } } elseif (n % 4) x = self (n/2); x = [ x, x+n^2/2; x+n^2*3/4, x+n^2/4 ]; for (i in 1:(n/2-1)/2) { c=x[1:n/2;i]; x[1:n/2;i] = x[n/2+1:n;i]; x[n/2+1:n;i] = c; } if (n > 6) { for (i in 1:(n/2-1)/2-1) { j = n-i+1; c = x[1:n/2;j]; x[1:n/2;j] = x[n/2+1:n;j]; x[n/2+1:n;j] = c; } } j = (n+2)/4; c = x[j;1]; x[j;1] = x[j+n/2;1]; x[j+n/2;1] = c; c = x[j;j]; x[j;j] = x[j+n/2;j]; x[j+n/2;j] = c; else x = ( self.block (n; 1) [2,1;n:1] + self.block (n; n^2-n+1) [;n:1] ); for (i in 2:n/2) { if (i%4 > 1) { r = (self.block (n; (i-1)*n+1) + self.block (n; n^2-i*n+1) [2,1;]); else r = (self.block (n; (i-1)*n+1) [2,1;n:1] + self.block( n; n^2-i*n+1) [;n:1]); } x = [ r[1;]; x; r[2;] ]; } } return x; }; magic.block = function (n; s) { local (i; j; x) x = fill (2,n; 0); x[2;1] = s; s += 1; i = 1; for (j in n-1:2:2) { if (i == 1) { x[1;j] = s; x[1;j-1] = s+1; i=2; else x[2;j-1] = s; x[2;j] = s+1; i=1; } s += 2; } x[2;n] = s; return x; }; provide ("OLDmagic");