0

Ok I really need your help guys I'm really lost this looks so simple but I can't figure it out. Note : this is for 3x3 magic square

So here's the condition for a magic square : 1. the sum of the elements of a row = k 2.the sum of the elements of a column = k 3. the sum of the elements of a diagonal = k

The question is : I have to translate the 3 conditions up there into a linear systeme Bx=0 where x=(a,b,c,d,e,f,g,h,i) which represents the unknown of the matrix 3x3 and B is a 7x9 matrix.

so here's what I've done :

enter image description here

So yea the goal is to write a homogeneous system with the form Bx=0 to then determine its solution. But I'm kinda lost at this point, I feel stupid cause it seems easy haha could someone help would be greatly appreciated thank you !

Shashwat
  • 2,342
  • 1
  • 17
  • 33
  • I see a big potential issue with this plan: typically magic square requires that 1) all values are integers and 2) all values are distinct. Both of those conditions seem to be significantly non-linear. – SergGr Mar 15 '18 at 10:42

1 Answers1

1

I suggest solving the system in terms of a smaller number of free variables, and then using permutations of [1,...,9] to check for solutions. Please try the following, which uses k=15 and returns the single solution (after removing rotations and reflections):

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 ) ] );
  • But my goal is to create with the informations i have an homogeneous system of the form Bx = 0 so i'm not sure if this work mmm – Dany Pépin Mar 15 '18 at 19:35
  • ok turns out i forgot to add the second diagonal in my equations so eq8:= a+e+i=k: should be there , my teacher mentionned that since k = a+b+c=k we can replace this value of k in each other equations so we end up we 7 equations. – Dany Pépin Mar 15 '18 at 20:10