Standard form for magic squares
as.standard.Rd
Transforms a magic square or magic hypercube into Frenicle's standard form
Usage
as.standard(a, toroidal = FALSE, one_minus=FALSE)
is.standard(a, toroidal = FALSE, one_minus=FALSE)
Arguments
- a
Magic square or hypercube (array) to be tested or transformed
- toroidal
Boolean, with default
FALSE
meaning to use Frenicle's method, andTRUE
meaning to use additional transformations appropriate to toroidal connectivity- one_minus
Boolean, with
TRUE
meaning to use the transformation \(x\longrightarrow n^2+1-x\) if appropriate, and defaultFALSE
meaning not to use this
Details
For a square, as.standard()
transforms a magic square into
Frenicle's standard form. The four numbers at each of
the four corners are determined. First, the square is rotated so the
smallest of the four is at the upper left. Then, element [1,2]
is compared with element[2,1]
and, if it is larger, the transpose
is taken.
Thus all eight rotated and transposed versions of a magic square have the same standard form.
The square returned by magic()
is in standard form.
For hypercubes, the algorithm is generalized. First, the hypercube is
reflected so that a[1,1,...,1,1]
is the smallest of the \(2^d\)
corner elements (eg a[1,n,1,...,1,1]
).
Next, aperm()
is called so that
a[1,1,...,1,2] < a[1,1,...,2,1] < ... < a[2,1,...,1,1]
.
Note that the inequalities are strict as hypercubes are assumed to be
normal. As of version 1.3-1, as.standard()
will accept arrays of
any dimension (ie arrays a
with minmax(dim(a))==FALSE
will
be handled sensibly).
An array with any dimension of extent zero is in standard form by definition; dimensions of length one are dropped.
If argument toroidal
is TRUE
, then the array a
is
translated using ashift()
so that a[1,1,...,1] == min(a)
.
Such translations preserve the properties of semimagicness and
pandiagonalness (but not magicness or associativity).
It is easier (for me at least) to visualise this by considering
two-dimensional arrays, tiling the plane with copies of a
.
Next, the array is shifted so that a[2,1,1,...,1] <
a[dim(a)[1],1,1,...,1]
and a[1,2,1,..,1] <
a[1,dim(a)[2],1,...,1]
and so on.
Then aperm()
is called as per the non-toroidal case above.
is.standard()
returns TRUE
if the magic square or
hypercube is in standard form. is.standard()
and
as.standard()
check for neither magicness nor normality (use
is.magic
and is.normal
for this).
Note
There does not appear to be a way to make the third letter of “Frenicle” have an acute accent, as it should do.
Examples
is.standard(magic.2np1(4))
#> [1] FALSE
as.standard(magic.4n(3))
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#> [1,] 1 132 120 37 49 84 72 85 97 36 24 133
#> [2,] 143 14 26 107 95 62 74 59 47 110 122 11
#> [3,] 142 15 27 106 94 63 75 58 46 111 123 10
#> [4,] 4 129 117 40 52 81 69 88 100 33 21 136
#> [5,] 5 128 116 41 53 80 68 89 101 32 20 137
#> [6,] 139 18 30 103 91 66 78 55 43 114 126 7
#> [7,] 138 19 31 102 90 67 79 54 42 115 127 6
#> [8,] 8 125 113 44 56 77 65 92 104 29 17 140
#> [9,] 9 124 112 45 57 76 64 93 105 28 16 141
#> [10,] 135 22 34 99 87 70 82 51 39 118 130 3
#> [11,] 134 23 35 98 86 71 83 50 38 119 131 2
#> [12,] 12 121 109 48 60 73 61 96 108 25 13 144
as.standard(magichypercube.4n(1,5))
#> , , 1, 1, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 1 1020 1016 13
#> [2,] 1023 6 10 1011
#> [3,] 1022 7 11 1010
#> [4,] 4 1017 1013 16
#>
#> , , 2, 1, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 1008 21 25 996
#> [2,] 18 1003 999 30
#> [3,] 19 1002 998 31
#> [4,] 1005 24 28 993
#>
#> , , 3, 1, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 992 37 41 980
#> [2,] 34 987 983 46
#> [3,] 35 986 982 47
#> [4,] 989 40 44 977
#>
#> , , 4, 1, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 49 972 968 61
#> [2,] 975 54 58 963
#> [3,] 974 55 59 962
#> [4,] 52 969 965 64
#>
#> , , 1, 2, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 960 69 73 948
#> [2,] 66 955 951 78
#> [3,] 67 954 950 79
#> [4,] 957 72 76 945
#>
#> , , 2, 2, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 81 940 936 93
#> [2,] 943 86 90 931
#> [3,] 942 87 91 930
#> [4,] 84 937 933 96
#>
#> , , 3, 2, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 97 924 920 109
#> [2,] 927 102 106 915
#> [3,] 926 103 107 914
#> [4,] 100 921 917 112
#>
#> , , 4, 2, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 912 117 121 900
#> [2,] 114 907 903 126
#> [3,] 115 906 902 127
#> [4,] 909 120 124 897
#>
#> , , 1, 3, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 896 133 137 884
#> [2,] 130 891 887 142
#> [3,] 131 890 886 143
#> [4,] 893 136 140 881
#>
#> , , 2, 3, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 145 876 872 157
#> [2,] 879 150 154 867
#> [3,] 878 151 155 866
#> [4,] 148 873 869 160
#>
#> , , 3, 3, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 161 860 856 173
#> [2,] 863 166 170 851
#> [3,] 862 167 171 850
#> [4,] 164 857 853 176
#>
#> , , 4, 3, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 848 181 185 836
#> [2,] 178 843 839 190
#> [3,] 179 842 838 191
#> [4,] 845 184 188 833
#>
#> , , 1, 4, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 193 828 824 205
#> [2,] 831 198 202 819
#> [3,] 830 199 203 818
#> [4,] 196 825 821 208
#>
#> , , 2, 4, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 816 213 217 804
#> [2,] 210 811 807 222
#> [3,] 211 810 806 223
#> [4,] 813 216 220 801
#>
#> , , 3, 4, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 800 229 233 788
#> [2,] 226 795 791 238
#> [3,] 227 794 790 239
#> [4,] 797 232 236 785
#>
#> , , 4, 4, 1
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 241 780 776 253
#> [2,] 783 246 250 771
#> [3,] 782 247 251 770
#> [4,] 244 777 773 256
#>
#> , , 1, 1, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 768 261 265 756
#> [2,] 258 763 759 270
#> [3,] 259 762 758 271
#> [4,] 765 264 268 753
#>
#> , , 2, 1, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 273 748 744 285
#> [2,] 751 278 282 739
#> [3,] 750 279 283 738
#> [4,] 276 745 741 288
#>
#> , , 3, 1, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 289 732 728 301
#> [2,] 735 294 298 723
#> [3,] 734 295 299 722
#> [4,] 292 729 725 304
#>
#> , , 4, 1, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 720 309 313 708
#> [2,] 306 715 711 318
#> [3,] 307 714 710 319
#> [4,] 717 312 316 705
#>
#> , , 1, 2, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 321 700 696 333
#> [2,] 703 326 330 691
#> [3,] 702 327 331 690
#> [4,] 324 697 693 336
#>
#> , , 2, 2, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 688 341 345 676
#> [2,] 338 683 679 350
#> [3,] 339 682 678 351
#> [4,] 685 344 348 673
#>
#> , , 3, 2, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 672 357 361 660
#> [2,] 354 667 663 366
#> [3,] 355 666 662 367
#> [4,] 669 360 364 657
#>
#> , , 4, 2, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 369 652 648 381
#> [2,] 655 374 378 643
#> [3,] 654 375 379 642
#> [4,] 372 649 645 384
#>
#> , , 1, 3, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 385 636 632 397
#> [2,] 639 390 394 627
#> [3,] 638 391 395 626
#> [4,] 388 633 629 400
#>
#> , , 2, 3, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 624 405 409 612
#> [2,] 402 619 615 414
#> [3,] 403 618 614 415
#> [4,] 621 408 412 609
#>
#> , , 3, 3, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 608 421 425 596
#> [2,] 418 603 599 430
#> [3,] 419 602 598 431
#> [4,] 605 424 428 593
#>
#> , , 4, 3, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 433 588 584 445
#> [2,] 591 438 442 579
#> [3,] 590 439 443 578
#> [4,] 436 585 581 448
#>
#> , , 1, 4, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 576 453 457 564
#> [2,] 450 571 567 462
#> [3,] 451 570 566 463
#> [4,] 573 456 460 561
#>
#> , , 2, 4, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 465 556 552 477
#> [2,] 559 470 474 547
#> [3,] 558 471 475 546
#> [4,] 468 553 549 480
#>
#> , , 3, 4, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 481 540 536 493
#> [2,] 543 486 490 531
#> [3,] 542 487 491 530
#> [4,] 484 537 533 496
#>
#> , , 4, 4, 2
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 528 501 505 516
#> [2,] 498 523 519 510
#> [3,] 499 522 518 511
#> [4,] 525 504 508 513
#>
#> , , 1, 1, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 512 517 521 500
#> [2,] 514 507 503 526
#> [3,] 515 506 502 527
#> [4,] 509 520 524 497
#>
#> , , 2, 1, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 529 492 488 541
#> [2,] 495 534 538 483
#> [3,] 494 535 539 482
#> [4,] 532 489 485 544
#>
#> , , 3, 1, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 545 476 472 557
#> [2,] 479 550 554 467
#> [3,] 478 551 555 466
#> [4,] 548 473 469 560
#>
#> , , 4, 1, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 464 565 569 452
#> [2,] 562 459 455 574
#> [3,] 563 458 454 575
#> [4,] 461 568 572 449
#>
#> , , 1, 2, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 577 444 440 589
#> [2,] 447 582 586 435
#> [3,] 446 583 587 434
#> [4,] 580 441 437 592
#>
#> , , 2, 2, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 432 597 601 420
#> [2,] 594 427 423 606
#> [3,] 595 426 422 607
#> [4,] 429 600 604 417
#>
#> , , 3, 2, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 416 613 617 404
#> [2,] 610 411 407 622
#> [3,] 611 410 406 623
#> [4,] 413 616 620 401
#>
#> , , 4, 2, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 625 396 392 637
#> [2,] 399 630 634 387
#> [3,] 398 631 635 386
#> [4,] 628 393 389 640
#>
#> , , 1, 3, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 641 380 376 653
#> [2,] 383 646 650 371
#> [3,] 382 647 651 370
#> [4,] 644 377 373 656
#>
#> , , 2, 3, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 368 661 665 356
#> [2,] 658 363 359 670
#> [3,] 659 362 358 671
#> [4,] 365 664 668 353
#>
#> , , 3, 3, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 352 677 681 340
#> [2,] 674 347 343 686
#> [3,] 675 346 342 687
#> [4,] 349 680 684 337
#>
#> , , 4, 3, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 689 332 328 701
#> [2,] 335 694 698 323
#> [3,] 334 695 699 322
#> [4,] 692 329 325 704
#>
#> , , 1, 4, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 320 709 713 308
#> [2,] 706 315 311 718
#> [3,] 707 314 310 719
#> [4,] 317 712 716 305
#>
#> , , 2, 4, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 721 300 296 733
#> [2,] 303 726 730 291
#> [3,] 302 727 731 290
#> [4,] 724 297 293 736
#>
#> , , 3, 4, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 737 284 280 749
#> [2,] 287 742 746 275
#> [3,] 286 743 747 274
#> [4,] 740 281 277 752
#>
#> , , 4, 4, 3
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 272 757 761 260
#> [2,] 754 267 263 766
#> [3,] 755 266 262 767
#> [4,] 269 760 764 257
#>
#> , , 1, 1, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 769 252 248 781
#> [2,] 255 774 778 243
#> [3,] 254 775 779 242
#> [4,] 772 249 245 784
#>
#> , , 2, 1, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 240 789 793 228
#> [2,] 786 235 231 798
#> [3,] 787 234 230 799
#> [4,] 237 792 796 225
#>
#> , , 3, 1, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 224 805 809 212
#> [2,] 802 219 215 814
#> [3,] 803 218 214 815
#> [4,] 221 808 812 209
#>
#> , , 4, 1, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 817 204 200 829
#> [2,] 207 822 826 195
#> [3,] 206 823 827 194
#> [4,] 820 201 197 832
#>
#> , , 1, 2, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 192 837 841 180
#> [2,] 834 187 183 846
#> [3,] 835 186 182 847
#> [4,] 189 840 844 177
#>
#> , , 2, 2, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 849 172 168 861
#> [2,] 175 854 858 163
#> [3,] 174 855 859 162
#> [4,] 852 169 165 864
#>
#> , , 3, 2, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 865 156 152 877
#> [2,] 159 870 874 147
#> [3,] 158 871 875 146
#> [4,] 868 153 149 880
#>
#> , , 4, 2, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 144 885 889 132
#> [2,] 882 139 135 894
#> [3,] 883 138 134 895
#> [4,] 141 888 892 129
#>
#> , , 1, 3, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 128 901 905 116
#> [2,] 898 123 119 910
#> [3,] 899 122 118 911
#> [4,] 125 904 908 113
#>
#> , , 2, 3, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 913 108 104 925
#> [2,] 111 918 922 99
#> [3,] 110 919 923 98
#> [4,] 916 105 101 928
#>
#> , , 3, 3, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 929 92 88 941
#> [2,] 95 934 938 83
#> [3,] 94 935 939 82
#> [4,] 932 89 85 944
#>
#> , , 4, 3, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 80 949 953 68
#> [2,] 946 75 71 958
#> [3,] 947 74 70 959
#> [4,] 77 952 956 65
#>
#> , , 1, 4, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 961 60 56 973
#> [2,] 63 966 970 51
#> [3,] 62 967 971 50
#> [4,] 964 57 53 976
#>
#> , , 2, 4, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 48 981 985 36
#> [2,] 978 43 39 990
#> [3,] 979 42 38 991
#> [4,] 45 984 988 33
#>
#> , , 3, 4, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 32 997 1001 20
#> [2,] 994 27 23 1006
#> [3,] 995 26 22 1007
#> [4,] 29 1000 1004 17
#>
#> , , 4, 4, 4
#>
#> [,1] [,2] [,3] [,4]
#> [1,] 1009 12 8 1021
#> [2,] 15 1014 1018 3
#> [3,] 14 1015 1019 2
#> [4,] 1012 9 5 1024
#>
##non-square arrays:
as.standard(magic(7)[1:3,])
#> [,1] [,2] [,3]
#> [1,] 4 12 20
#> [2,] 44 3 11
#> [3,] 42 43 2
#> [4,] 33 41 49
#> [5,] 24 32 40
#> [6,] 15 23 31
#> [7,] 13 21 22
## Toroidal transforms preserve pandiagonalness:
is.pandiagonal(as.standard(hudson(11)))
#> [1] TRUE
## but not magicness:
is.magic(as.standard(magic(10),TRUE))
#> [1] FALSE