What is the maximum value of a 3 × 3 matrix consisting of 123456789?

Source: Internet
Author: User
123456789 how is the calculation equal to 1? -The abccsss answer assumes that each number can only appear once. 123456789 how is the calculation equal to 1? -Abccsss answer
It is assumed that each number can only appear once. Reply: Mathematica code

Relatively simple

Det/@N@Range@9~Permutations~{9}~ArrayReshape~{9!,3,3}//Max


The above uses Matlab brute-force cracking (enumeration cases). No other cases with the same determinant have been output yet, which seems to be generated in seconds.

max_det = 0;init_perm = reshape(1:9, [3, 3]);all_perms = perms(1:9);for i = 1:size(all_perms, 1)    matrix = all_perms(i, :);    matrix = reshape(matrix, [3, 3]);    det_value = det(matrix);        if det_value > max_det        max_det = det_value;        init_perm = matrix;    end    end
List = Permutations [Range [9], {9}]; List = Permutations [Range [9], {9}];
Matrix = Partition [#, 3] & // @ list;
Answer = Det/@ matrix;
M = Max [answer];
Pos = Flatten [Position [answer, m];
Matrix [[#] & // @ pos post a python version with no technical content or brute force max...

import itertoolsimport timedef max_matrix():begin = time.time()elements = [1, 2, 3, 4, 5, 6, 7, 8, 9]maxdet = 0maxmat = []for i in itertools.permutations(elements, 9):det = i[0] * i[4] * i[8] + i[1] * i[5] * i[6] + i[2] * i[3] * i[7] - i[2] * i[4] * i[6] - i[1] * i[3] * i[8] - i[0] * i[5] * i[7]if(det > maxdet):maxdet = detmaxmat = []for j in range(0, 9):maxmat.append(i[j])print "|" + str(maxmat[0]) + " " + str(maxmat[1]) + " " + str(maxmat[2]) + "|"print "|" + str(maxmat[3]) + " " + str(maxmat[4]) + " " + str(maxmat[5]) + "| = " + str(maxdet)print "|" + str(maxmat[6]) + " " + str(maxmat[7]) + " " + str(maxmat[8]) + "|"end = time.time()print str(end - begin) + 's used.'if __name__ == '__main__':max_matrix()
The question should be changed to 1 2 3... n ^ 2 to form the maximum value of the n-order Determinant. It is interesting to find the time complexity of the optimal solution. C ++:

#include 
  
   #include using namespace std;int ans, a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};int main() {  do    ans = max(ans, a[0] * (a[4] * a[8] - a[5] * a[7]) +                   a[1] * (a[5] * a[6] - a[3] * a[8]) +                   a[2] * (a[3] * a[7] - a[4] * a[6]));  while (next_permutation(a, a + 9));  printf("%d\n", ans);}
  
Arrange the answers of yellow to get them.
9 4 2
3 8 6
5 1 7

It's easy to see the idea.
1. All numbers are arranged on the diagonal line with the slope of-1. (That is, 987 is in a diagonal line, 654 is in a line, and 321 is in a line) it is easy to see that this is the method that maximizes the positive value.
2. For the diagonal lines in the reverse direction, it is excluded that the sum of any two numbers except the primary diagonal lines is equal. The larger the product, the smaller the primary diagonal line element. (That is, the maximum value of the three products is minimized, and the maximum result matches the minimum number)

However, the above method is limited to 1 ~ The 3x3 matrix of 9 is not necessarily applicable to other matrices.
Obviously, this method requires only the diagonal lines (or parallel to the diagonal lines) for both the positive and negative sides, but the 4x4 determinant begins to bend...

Then, I feel that there are three other vulnerabilities. First, the greedy method does not necessarily guarantee the maximum positive value, nor does it necessarily guarantee the minimum reverse value, or the maximum difference between positive and negative values. (Not all are vulnerabilities, and some may be established by hengcheng)
However, I feel that for a non-negative matrix of 3x3, greedy can obtain the maximum value in most cases.

PS: I tried a lot of groups, but this solution was used. Then I tried another group [1 2 3 4 5 6 7 8 100]. Obviously, the answer has changed, because 100 of the weights are much greater than 8 and 7, 2 and 1 are directly given to 100 in negative cases. This proves that the greedy method sometimes does not get the maximum value. I already have python, c, and MMA code. Let me get a math.

p=perms(1:9);[n,~]=size(p);z=zeros(n,1);for i=1:n    z(i)=det(reshape(p(i,:),3,3));endmax(z)id=find(z==max(z));for i=1:length(id)    disp(reshape(p(id(i),:),3,3));end
For the third-level poor lifting, it is easier to use the det function:

p = reshape(perms(1:9),'',3,3);M = max(sum(prod(p,2),3)-sum(prod(p,3),2));
There are still a few Mathematica languages for the topic. Come on.
Direct 9! Results are saved as positive, with 0 Optimizations

Det[Partition[#, 3]] & /@ Permutations[Range[9]] // Max412

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.