% Boyd & Vandenberghe "Convex Optimization"
% Almir Mutapcic - Jan 2006
%
% Given a symmetric matrix A in R^(n-by-n) with some entries unspecified
% we find its completion such that A is positive semidefinite and
% it has a maximum determinant out of all possible completions.
% This problem can be formulated as a log det (and det_rootn) problem.
%
% This is a numerical instance of the specified book exercise.

% problem size
n = 4;

% create and solve the problem
cvx_begin sdp
  % A is a PSD symmetric matrix (n-by-n)
  variable A(n,n) symmetric;
  A >= 0;

  % constrained matrix entries.
  A(1,1) == 3;
  A(2,2) == 2;
  A(3,3) == 1;
  A(4,4) == 5;
  % Note that because A is symmetric, these off-diagonal
  % constraints affect the corresponding element on the
  % opposite side of the diagonal.
  A(1,2) == .5;
  A(1,4) == .25;
  A(2,3) == .75;

  % find the solution to the problem
  maximize( log_det( A ) )
  % maximize( det_rootn( A ) )
cvx_end

% display solution
disp(['Matrix A with maximum determinant (' num2str(det(A)) ') is:'])
A
disp(['Its eigenvalues are:'])
eigs = eig(A)
 
Calling Mosek 9.1.9: 59 variables, 18 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 18              
  Cones                  : 4               
  Scalar variables       : 13              
  Matrix variables       : 2               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 1
Eliminator terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 2                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 18              
  Cones                  : 4               
  Scalar variables       : 13              
  Matrix variables       : 2               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 16
Optimizer  - Cones                  : 4
Optimizer  - Scalar variables       : 12                conic                  : 12              
Optimizer  - Semi-definite variables: 2                 scalarized             : 46              
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 101               after factor           : 103             
Factor     - dense dim.             : 0                 flops                  : 2.67e+03        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   3.2e+00  4.0e+00  2.7e+01  0.00e+00   2.282783840e+01   -3.220408006e+00  1.0e+00  0.00  
1   6.4e-01  8.0e-01  6.3e+00  -4.82e-01  1.131513779e+01   5.009346393e-01   2.0e-01  0.01  
2   1.7e-01  2.2e-01  1.0e+00  5.08e-01   4.413294072e+00   8.163553142e-01   5.5e-02  0.01  
3   4.8e-02  6.0e-02  1.6e-01  7.45e-01   3.272925849e+00   2.171307260e+00   1.5e-02  0.01  
4   7.8e-03  9.8e-03  1.1e-02  8.47e-01   3.099056911e+00   2.905643702e+00   2.5e-03  0.01  
5   4.8e-04  6.0e-04  1.7e-04  9.92e-01   3.028576122e+00   3.016657110e+00   1.5e-04  0.01  
6   9.7e-06  1.2e-05  4.8e-07  1.00e+00   3.024305294e+00   3.024063282e+00   3.0e-06  0.01  
7   4.7e-08  5.8e-08  1.6e-10  1.00e+00   3.024221238e+00   3.024220077e+00   1.5e-08  0.01  
8   2.6e-09  2.4e-09  1.1e-12  1.00e+00   3.024220730e+00   3.024220687e+00   5.6e-10  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 3.0242207305e+00    nrm: 5e+00    Viol.  con: 5e-09    var: 0e+00    barvar: 0e+00    cones: 0e+00  
  Dual.    obj: 3.0242206875e+00    nrm: 7e+00    Viol.  con: 0e+00    var: 8e-12    barvar: 8e-09    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 8         time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +3.02422
 
Matrix A with maximum determinant (20.578) is:

A =

    3.0000    0.5000    0.1875    0.2500
    0.5000    2.0000    0.7500    0.0417
    0.1875    0.7500    1.0000    0.0156
    0.2500    0.0417    0.0156    5.0000

Its eigenvalues are:

eigs =

    0.5964
    2.0908
    3.2773
    5.0355