首页
学习
活动
专区
圈层
工具
发布

Maple幻方
EN

Stack Overflow用户
提问于 2018-03-15 10:52:47
回答 1查看 276关注 0票数 0

好了,我真的需要你们的帮助,伙计们,我真的迷路了,这看起来很简单,但我搞不懂。注意:这是用于3x3幻方的

下面是魔方的条件: 1.一行的元素之和=k 2.一列的元素之和=k 3.对角线的元素之和=k

问题是:我必须将上面的3个条件转换成一个线性系统Bx=0,其中x=(a,b,c,d,e,f,g,h,i)表示矩阵3x3的未知数,B是7x9矩阵。

下面是我所做的:

所以,是的,我们的目标是编写一个具有Bx=0形式的同构系统,然后确定它的解决方案。但是在这一点上我有点迷失了,我觉得自己很愚蠢,因为这似乎很容易哈哈,如果有人能帮助我会非常感谢你!

EN

回答 1

Stack Overflow用户

发布于 2018-03-16 00:14:10

我建议用较少的自由变量来求解系统,然后使用1,...,9的排列来检查解决方案。请尝试以下操作,它使用k=15并返回单个解决方案(在移除旋转和反射之后):

代码语言:javascript
复制
restart;

# Number of variables.
m := 9;

# Variables.
X := [ seq( x[i], i=1..m ) ];

# Indices.
N := [ seq( i, i=1..m ) ];

# Equations.
EQ := [ x[1] + x[2] + x[3] = 15,
        x[4] + x[5] + x[6] = 15,
        x[7] + x[8] + x[9] = 15,
        x[1] + x[4] + x[7] = 15,
        x[2] + x[5] + x[8] = 15,
        x[3] + x[6] + x[9] = 15,
        x[1] + x[5] + x[9] = 15,
        x[3] + x[5] + x[7] = 15
];

# Constraints to remove equivalent solutions.
INEQ := [ x[1] < x[3], x[1] < x[7], x[1] < x[9], x[3] < x[7] ];

# Solve in terms of free parameters.
A, B := LinearAlgebra:-GenerateMatrix( EQ, X );
S := convert( LinearAlgebra:-LinearSolve( A, B, ':-free'=x ), 'list' );

# Free parameters.
Q := convert( indets( S, 'name' ), 'list' );
n := numelems( Q );

# Table to store solutions.
H := table();

# Cycle through all possible values for Q, first by combination, and then by permutation,
# and record any solutions found.
for i from 1 to combinat:-numbcomb( m, n ) do

    if i = 1 then
        C := convert( combinat:-firstcomb( m, n ), 'list' ):
    else
        C := convert( combinat:-nextcomb( C, m ), 'list' ):
    end if:

    for j from 1 to n! do

        if j = 1 then
            P := C:
        else
            P := combinat:-nextperm( P ):
        end if:

        T := eval( S, Q =~ P ):

        # Check if it is a solution satisfying all the constraints.
        if andmap( is, [ sort( T ) = N, op( eval( EQ, X =~ T ) ), op( eval( INEQ, X =~ T ) ) ] ) then
                H[T] := NULL:
        end if:

    end do:

end do:

# Solutions.
map( op, [ indices( H ) ] );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49290806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档