Nothing
#' Construct doubly even order magic square (Yang-Hui · Dürer · Ramanujan inspired)
#'
#' @param n Positive integer, order = 4n
#' @param params Parameter setting. Can be a list containing a11, d1, d2, d12, d21 (d22 is optional, automatically set to d12+d21).
#' Predefined strings:
#' - "natural": natural square parameters (a11=1, d1=1, d2=4n, d12=2n, d21=2n*4n)
#' - "type1" : second row of Table 3 (a11=1, d1=1, d2=2n, d12=(2n)^2, d21=2*(2n)^2)
#' - "type2" : third row of Table 3 (a11=1, d1=4n, d2=1, d12=?, d21=?, d22=?) [values incomplete, for illustration only]
#' @param L1 Sets of rows/columns to flip (1‑based), default automatically generated symmetric sets
#' @param L2 Sets of rows/columns to flip (1‑based), default automatically generated symmetric sets
#'
#' @return 4n x 4n magic square matrix
#' @export
#'
#' @examples
#' # Example 1: Ramanujan's 8th order magic square (matrix 4 in the paper)
#' # Using natural square parameters, L1 = L2 = {2,3,6,7}
#' cat("\n===== Ramanujan's 8th order magic square =====\n")
#' ramanujan8 <- YangRamanujan(n = 2,params = "natural",L1 = c(2, 3, 6, 7),L2 = c(2, 3, 6, 7))
#' print(ramanujan8)
#' cat("Is it a magic square: ", is_magic_square(ramanujan8), "\n")
#' # Example 2: One of Yang-Hui's 4th order magic squares (matrix 2 in the paper)
#' # For n=1, use natural square, L1 = L2 = {2,3} (rows/columns 1..4 for order 4)
#' cat("\n===== Yang-Hui 4th order magic square (matrix 2) =====\n")
#' yanghui4 <- YangRamanujan(n = 1, params = "natural",L1 = c(2, 3),L2 = c(2, 3))
#' print(yanghui4)
#' cat("Is it a magic square: ", is_magic_square(yanghui4), "\n")
#' # Example 3: Dürer's 4th order magic square (matrix 3 in the paper)
#' # According to Table 1, use specific parameters (n=1) and L1 = L2 = {2,3}
#' # Parameters from row (22) in Table 1: a11=1, d1=-1, d2=-8, d12=-2, d21=-4, d22=-6
#' cat("\n===== Dürer's 4th order magic square (matrix 3) =====\n")
#' duerer_params <- list(a11 = 1, d1 = 1, d2 = 8, d12 = 2, d21 = 4)
#' duerer4 <- YangRamanujan(n = 1,params = duerer_params,L1 = c(2, 3),L2 = c(2, 3))
#' print(duerer4)
#' cat("Is it a magic square: ", is_magic_square(duerer4), "\n")
#' # Example 4: Custom 12th order magic square (n=3), using natural square, default symmetric L
#' cat("\n===== Custom 12th order magic square (natural square, default L) =====\n")
#' magic12 <- YangRamanujan(n = 3, params = "natural")
#' # Print only first 6 rows to save space
#' print(magic12)
#' cat("Is it a magic square: ", is_magic_square(magic12), "\n")
YangRamanujan <- function(n,
params = "natural",
L1 = NULL,
L2 = NULL) {
# Parse parameters
if (is.character(params)) {
params <- switch(
params,
natural = list(
a11 = 1,
d1 = 1,
d2 = 4 * n,
d12 = 2 * n,
d21 = 2 * n * (4 * n)
),
type1 = list(
a11 = 1,
d1 = 1,
d2 = 2 * n,
d12 = (2 * n)^2,
d21 = 2 * (2 * n)^2
),
type2 = list(
a11 = 1,
d1 = 4 * n,
d2 = 1,
d12 = 2, # Values for type2 are not clearly printed; here just for demonstration
d21 = 3 # Please refer to Table 3 in the paper for correct values
),
stop("Unknown params type. Please use 'natural', 'type1' or provide a custom list")
)
}
# Ensure d22 exists and satisfies d12 + d21 = d22
if (is.null(params$d22)) {
params$d22 <- params$d12 + params$d21
} else {
if (params$d12 + params$d21 != params$d22) {
warning("d12 + d21 does not equal d22, a magic square may not be obtained")
}
}
# Construct initial matrix M
M0 <- construct_M(n,
params$a11, params$d1, params$d2,
params$d12, params$d21, params$d22)
# Default L1, L2
if (is.null(L1)) L1 <- generate_symmetric_L(n)
if (is.null(L2)) L2 <- L1
# Apply transformation
M_final <- transform_M(M0, L1, L2)
# Optional verification
if (!is_magic_square(M_final)) {
warning("The generated matrix may not be a valid magic square. Please check the parameters")
}
M_final
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.