<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>魔方矩阵 on 格物堂</title><link>https://gewutang.cn/tags/%E9%AD%94%E6%96%B9%E7%9F%A9%E9%98%B5/</link><description>Recent content in 魔方矩阵 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Mon, 29 Aug 2011 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E9%AD%94%E6%96%B9%E7%9F%A9%E9%98%B5/index.xml" rel="self" type="application/rss+xml"/><item><title>R中的魔方矩阵</title><link>https://gewutang.cn/2011/08/29/magic/</link><pubDate>Mon, 29 Aug 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/08/29/magic/</guid><description>&lt;p&gt;某个朋友去证券公司笔试，题目中竟然有一道要求用C语言完成魔方矩阵的输出。如果之前没有做过相关练习，骤然间写出代码可是不容易的事情。&lt;/p&gt;&#10;&lt;p&gt;今天下午查了一下R中的魔方矩阵，发现里面已经有一个magic包可以完成魔方矩阵的输出。R真有点无所不包的气势。&lt;/p&gt;&#10;&lt;h2 id="魔方矩阵原理应用与可视化实现"&gt;&lt;a href="#%e9%ad%94%e6%96%b9%e7%9f%a9%e9%98%b5%e5%8e%9f%e7%90%86%e5%ba%94%e7%94%a8%e4%b8%8e%e5%8f%af%e8%a7%86%e5%8c%96%e5%ae%9e%e7%8e%b0" class="header-anchor"&gt;&lt;/a&gt;魔方矩阵：原理、应用与可视化实现&#10;&lt;/h2&gt;&lt;h3 id="一数学原理"&gt;&lt;a href="#%e4%b8%80%e6%95%b0%e5%ad%a6%e5%8e%9f%e7%90%86" class="header-anchor"&gt;&lt;/a&gt;一、数学原理&#10;&lt;/h3&gt;&lt;p&gt;魔方矩阵（Magic Square）又称幻方，是$n \times n$的方阵，满足以下特性：&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;strong&gt;等和定律&lt;/strong&gt;：每行、每列及两条主对角线的元素和相等，称为魔方常数$M =\frac{n(n^2+1)}{2}$；&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;元素排列&lt;/strong&gt;：包含$1$到$n^2$的连续自然数；&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;构造规律&lt;/strong&gt;：奇数阶常用Siamese方法（楼梯法），偶数阶采用LUX或边界填充策略。&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;以3阶魔方为例：&lt;/p&gt;&#10;&lt;p&gt;$$&#10;&lt;code&gt;\begin{bmatrix} 8 &amp;amp; 1 &amp;amp; 6 \\ 3 &amp;amp; 5 &amp;amp; 7 \\ 4 &amp;amp; 9 &amp;amp; 2 \\ \end{bmatrix}&lt;/code&gt;&#10;$$&lt;/p&gt;&#10;&lt;p&gt;其行、列、对角线之和均为$15$。&lt;/p&gt;&#10;&lt;h3 id="二核心价值"&gt;&lt;a href="#%e4%ba%8c%e6%a0%b8%e5%bf%83%e4%bb%b7%e5%80%bc" class="header-anchor"&gt;&lt;/a&gt;二、核心价值&#10;&lt;/h3&gt;&lt;h4 id="1-科学研究维度"&gt;&lt;a href="#1-%e7%a7%91%e5%ad%a6%e7%a0%94%e7%a9%b6%e7%bb%b4%e5%ba%a6" class="header-anchor"&gt;&lt;/a&gt;1. 科学研究维度&#10;&lt;/h4&gt;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;数论验证&lt;/strong&gt;：魔方常数与矩阵阶数的数学关系为代数理论提供验证案例&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;组合优化&lt;/strong&gt;：启发旅行商问题(TSP)、排班系统等组合优化算法设计&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h4 id="2-文化应用场景"&gt;&lt;a href="#2-%e6%96%87%e5%8c%96%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af" class="header-anchor"&gt;&lt;/a&gt;2. 文化应用场景&#10;&lt;/h4&gt;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;密码原型&lt;/strong&gt;：古代战争曾用魔方矩阵加密军事情报&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;艺术设计&lt;/strong&gt;：印度泰姬陵地砖、敦煌壁画均包含幻方图案&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="三r语言实现"&gt;&lt;a href="#%e4%b8%89r%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0" class="header-anchor"&gt;&lt;/a&gt;三、R语言实现&#10;&lt;/h3&gt;&lt;h4 id="1-构造魔方矩阵"&gt;&lt;a href="#1-%e6%9e%84%e9%80%a0%e9%ad%94%e6%96%b9%e7%9f%a9%e9%98%b5" class="header-anchor"&gt;&lt;/a&gt;1. 构造魔方矩阵&#10;&lt;/h4&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 安装必要包&#10;if (!require(&amp;quot;magic&amp;quot;)) install.packages(&amp;quot;magic&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：magic&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：abind&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(magic)&#10;&#10;# 生成5阶魔方矩阵&#10;magic_matrix &amp;lt;- magic(5)&#10;print(magic_matrix)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [,1] [,2] [,3] [,4] [,5]&#10;## [1,] 9 2 25 18 11&#10;## [2,] 3 21 19 12 10&#10;## [3,] 22 20 13 6 4&#10;## [4,] 16 14 7 5 23&#10;## [5,] 15 8 1 24 17&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h4 id="2-可视化"&gt;&lt;a href="#2-%e5%8f%af%e8%a7%86%e5%8c%96" class="header-anchor"&gt;&lt;/a&gt;2. 可视化&#10;&lt;/h4&gt;&lt;pre&gt;&lt;code class="language-r"&gt;library(ggplot2)&#10;library(reshape2)&#10;&#10;# 数据重塑&#10;melted_matrix &amp;lt;- melt(magic_matrix)&#10;colnames(melted_matrix) &amp;lt;- c(&amp;quot;Row&amp;quot;, &amp;quot;Column&amp;quot;, &amp;quot;Value&amp;quot;)&#10;&#10;# 绘制热力图&#10;ggplot(melted_matrix, aes(x = Column, y = Row, fill = Value)) +&#10; geom_tile(color = &amp;quot;white&amp;quot;) +&#10; scale_fill_gradient(low = &amp;quot;#FFD700&amp;quot;, high = &amp;quot;#FF4500&amp;quot;) +&#10; geom_text(aes(label = Value), color = &amp;quot;black&amp;quot;, size = 4) +&#10; theme_minimal(base_family=&amp;quot;PingFang SC&amp;quot;) +&#10; labs(title = &amp;quot;5阶魔方矩阵可视化&amp;quot;,&#10; subtitle = &amp;quot;行、列、对角线之和均为65&amp;quot;) +&#10; coord_fixed()&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2011/08/29/magic/index_files/figure-html/unnamed-chunk-2-1.png" width="672" /&gt;&#10;&lt;p&gt;还可以写一段更简单的代码：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 安装包，如果为安装&#10;# install.packages(&amp;quot;magic&amp;quot;)&#10;library(magic)&#10;# 定义魔方矩阵函数magic.image()&#10;magic.image &amp;lt;- function(n,...){&#10; # 生成n阶魔方矩阵&#10; m&amp;lt;- magic(n)&#10; # 生成魔方矩阵热图&#10; image(m)&#10; # 添加数字标签&#10; for(j in 1:n){&#10; for(i in 1:n){&#10; text(1/(n-1)*i-1/(n-1),1+1/(n-1)-1/(n-1)*j,m[j,i])&#10; }&#10; }&#10;}&#10;# 使用函数&#10;magic.image(5)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2011/08/29/magic/index_files/figure-html/unnamed-chunk-3-1.png" width="672" /&gt;&#10;</description></item></channel></rss>