#
# Computes the Schur norm of +1/-1 matrices.
# This version does not try to eleminate matrices by equivalence
# The code was written by Jean-Pierre Schoch, adapted from a
# MATLAB code written by Nathaniel Johnston. (nathaniel@njohnston.ca)
# last updated May 4, 2022
using Convex, SCS
using LinearAlgebra
using Combinatorics
function schurnorm(T)
n=size(T,1)
Y0=Semidefinite(n)
Y1=Semidefinite(n)
problem = minimize(maximum(diag(Y0))/2 + maximum(diag(Y1))/2)
problem.constraints += ([Y0 T; T' Y1] ⪰ 0)
#solver = SCS.Optimizer()
solve!(problem,SCS.Optimizer;silent_solver=true)
problem.optval # the optimal value is returned to the calling function
end
function dec_to_bin(dec)
bin = Int32[] # creates an empty vector of integer type
while dec >= 1
push!(bin,mod(dec,2)) # adds 0 or 1 to bin
dec = floor(Int32,dec/2)
end
return bin
end
function largeschurnorm(n,thresh)
# n = dimension of +1/-1 matrix
# matrices whose Schur norm < thresh are discarded
#
LOG = Matrix{Int32}[] # collection of matrices who's schur norm we have computed
large = Matrix{Int32}[]
largeA = zeros(Int32,n,n) # matrix corresponding to the largest Schur norm
#fname = string("Schurnorms_",n,".txt")
#f = open(fname,"a") # append new results to file f
# our square matrices have +1 in the first row and column,hence we need the (n-1)x(n-1)
# submatrix
nm1=(n-1)*(n-1)
nm = 2^nm1 # total permutations of the +1/-1 (n-1)x(n-1) submatrix
maxsn = 0
iter = 0
for i = 1:nm
v = dec_to_bin(i-1)
x=zeros(Int32,nm1-length(v))
b=[v;x] # this is now a vector of length (n-1)*(n-1)
B = reshape(b,n-1,n-1)
# Do some rudimentary checking to make sure we do not waste time
# computing Schur norms of matrices that are equivalent to other
# matrices that we are computing Schur norms of.
sv = (2 .^collect(0:n-2))'*B
if (sv[1,:] == sort(sv[1,:]))
A = Int32[1 ones(1,n-1);ones(n-1) (2*B .-1)]
iter += 1 # count the number of matrices we compute the Schur norm for
sn = opnorm(A) #schurnorm(A)
if (sn> thresh)
#println(f,"sn ",sn," ",A)
push!(large,A)
if (sn > maxsn)
maxsn = sn
#largeA=copy(A)
end
end
end
end
#println(f,"# matrices checked ",iter," largest Schur norm ",maxsn," corresponding Matrix ",largeA)
#close(f)
return iter ,maxsn,large
end