% This script finds a Toeplitz Hermitian PSD matrix that is closest to a
% given Hermitian matrix, as measured by the Frobenius norm. That is, for
% a given matrix P, it solves:
%    minimize   || Z - P ||_F
%    subject to Z >= 0
%
% Adapted from an example provided in the SeDuMi documentation. Notice
% the use of SDP mode to simplify the semidefinite constraint.

% The data. P is Hermitian, but is neither Toeplitz nor PSD.
P = [ 4,     1+2*j,     3-j       ; ...
      1-2*j, 3.5,       0.8+2.3*j ; ...
      3+j,   0.8-2.3*j, 4         ];

% Construct and solve the model
n = size( P, 1 );
cvx_begin sdp
    variable Z(n,n) hermitian toeplitz
    dual variable Q
    minimize( norm( Z - P, 'fro' ) )
    Z >= 0 : Q;
cvx_end

% Display resuls
disp( 'The original matrix, P: ' );
disp( P )
disp( 'The optimal point, Z:' );
disp( Z )
disp( 'The optimal dual variable, Q:' );
disp( Q )
disp( 'min( eig( Z ) ), min( eig( Q ) ) (both should be nonnegative, or close):' );
disp( sprintf( '   %g   %g\n', min( eig( Z ) ), min( eig( Q ) ) ) );
disp( 'The optimal value, || Z - P ||_F:' );
disp( norm( Z - P, 'fro' ) );
disp( 'Complementary slackness: Z * Q, should be near zero:' );
disp( Z * Q )
 
Calling Mosek 9.1.9: 20 variables, 6 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            : 6               
  Cones                  : 1               
  Scalar variables       : 11              
  Matrix variables       : 1               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 1                 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            : 6               
  Cones                  : 1               
  Scalar variables       : 11              
  Matrix variables       : 1               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 5
Optimizer  - Cones                  : 1
Optimizer  - Scalar variables       : 7                 conic                  : 7               
Optimizer  - Semi-definite variables: 1                 scalarized             : 21              
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 : 15                after factor           : 15              
Factor     - dense dim.             : 0                 flops                  : 1.71e+03        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   3.0e+00  4.2e+00  2.3e+01  0.00e+00   2.100000000e+01   -1.000000000e+00  1.0e+00  0.00  
1   5.0e-01  7.1e-01  5.7e+00  -6.75e-01  5.553938940e+00   -4.067426847e+00  1.7e-01  0.01  
2   1.2e-01  1.7e-01  9.4e-01  3.88e-01   1.234217382e-01   -2.772964901e+00  4.1e-02  0.01  
3   1.3e-02  1.8e-02  1.9e-02  1.05e+00   -1.236225345e+00  -1.580956459e+00  4.3e-03  0.01  
4   4.3e-04  6.1e-04  1.1e-04  1.01e+00   -1.444457521e+00  -1.455975261e+00  1.4e-04  0.01  
5   4.0e-05  5.6e-05  3.0e-06  1.01e+00   -1.450262016e+00  -1.451315401e+00  1.3e-05  0.01  
6   1.2e-06  1.7e-06  1.6e-08  1.00e+00   -1.450787698e+00  -1.450819401e+00  4.0e-07  0.01  
7   1.2e-08  1.7e-08  1.6e-11  1.00e+00   -1.450803301e+00  -1.450803622e+00  4.1e-09  0.01  
8   2.2e-09  3.1e-09  1.3e-12  1.00e+00   -1.450803469e+00  -1.450803527e+00  7.4e-10  0.01  
9   4.0e-11  1.1e-09  4.2e-15  1.00e+00   -1.450803513e+00  -1.450803514e+00  1.3e-11  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -1.4508035135e+00   nrm: 1e+00    Viol.  con: 3e-10    var: 0e+00    barvar: 0e+00    cones: 0e+00  
  Dual.    obj: -1.4508035144e+00   nrm: 4e+00    Viol.  con: 0e+00    var: 2e-11    barvar: 4e-09    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 9         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): +1.4508
 
The original matrix, P: 
   4.0000 + 0.0000i   1.0000 + 2.0000i   3.0000 - 1.0000i
   1.0000 - 2.0000i   3.5000 + 0.0000i   0.8000 + 2.3000i
   3.0000 + 1.0000i   0.8000 - 2.3000i   4.0000 + 0.0000i

The optimal point, Z:
   4.2827 + 0.0000i   0.8079 + 1.7342i   2.5574 - 0.7938i
   0.8079 - 1.7342i   4.2827 + 0.0000i   0.8079 + 1.7342i
   2.5574 + 0.7938i   0.8079 - 1.7342i   4.2827 + 0.0000i

The optimal dual variable, Q:
   0.3366 + 0.0000i  -0.0635 - 0.2866i  -0.3051 + 0.1422i
  -0.0635 + 0.2866i   0.2561 + 0.0000i  -0.0635 - 0.2866i
  -0.3051 - 0.1422i  -0.0635 + 0.2866i   0.3366 + 0.0000i

min( eig( Z ) ), min( eig( Q ) ) (both should be nonnegative, or close):
   3.58847e-10   5.12669e-11

The optimal value, || Z - P ||_F:
    1.4508

Complementary slackness: Z * Q, should be near zero:
   1.0e-05 *

   0.1280 - 0.0034i  -0.0270 - 0.1083i  -0.1146 + 0.0571i
   0.0635 - 0.2864i  -0.2559 - 0.0000i   0.0635 + 0.2864i
  -0.1146 - 0.0571i  -0.0270 + 0.1083i   0.1280 + 0.0034i