0

I'm using R to try and find the largest possible determinant that a symmetric 3x3 matrix, all of whose entries lie in the set of integers {1,2,3,4,5,6} can have?

Any ideas how I'd go about this?

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • 5
    This question appears to be off-topic because it is about mathematics. – Sven Hohenstein Jan 23 '14 at 21:06
  • ... while you could do it with R **in principle** by some sort of brute-force algorithm, in practice it is probably much better to try to solve the problem by thinking about it ... could we have a little more context please? You could start by generating all 6^6 combinations of entries via `xx <- do.call(expand.grid,replicate(6,1:6,simplify=FALSE))`, put them into a symmetric matrix, and compute the determinant -- it's only about 45000 entries so it **could** be done by brute force. – Ben Bolker Jan 23 '14 at 21:12

2 Answers2

3

There are six degrees of freedom in selecting the elements in a 3x3 symmetric matrix (3 main-diagonal entries and 3 off-diagonal entries). Each of these can take 6 different values, for a grand total of 46,656 possible matrices. We can just brute force check all the determinants:

get.det = function(x) {
    det(matrix(c(x[1], x[2], x[3], x[2], x[4], x[5], x[3], x[5], x[6]), nrow=3))
}

vals = expand.grid(1:6, 1:6, 1:6, 1:6, 1:6, 1:6)
dets = apply(vals, 1, get.det)
vals[which.max(dets),]
#      Var1 Var2 Var3 Var4 Var5 Var6
# 6691    1    6    6    1    6    1

So your matrix is:

[1 6 6
 6 1 6
 6 6 1]

with determinant 325.

josliber
  • 43,891
  • 12
  • 98
  • 133
2

I'm a little slower off the mark than @josilber, reach the same conclusion (my answer is a little bit more general)

d <- 3
n <- d*(d+1)/2
xx <- do.call(expand.grid,replicate(n,1:n,simplify=FALSE))

Construct symmetric matrix:

mm <- matrix(nrow=d,ncol=d)  ## define once, outside loop
m <- function(x) {
   mm[lower.tri(mm,diag=TRUE)] <- unlist(x)
   mm[upper.tri(mm)] <- t(mm)[upper.tri(mm)]
   mm
}

Get list of matrices:

library(plyr)
mList <- alply(xx,1,m,.progress="text")

Get determinants for each matrix:

detvec <- laply(mList,det)

Find the maximum:

max(detvec)  ## 325
mList[[which.max(detvec)]]

Just for fun:

plot(table(detvec))

The mathematical guess/conjecture is whether the max-det matrix is always the matrix with 1 on the diagonal and n on the off-diagonal, or whether this depends on the matrix being odd vs. even ... and how to prove this.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453