% Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 01/29/06
% Updated to use GP mode by Almir Mutapcic 02/08/06
%
% Given a square matrix M, the goal is to find a vector (with dii > 0)
% such that ||DMD^{-1}||_F is minimized, where D = diag(d).
% The problem can be cast as an unconstrained geometric program:
%           minimize sqrt( sum_{i,j=1}^{n} Mij^2*di^2/dj^2 )
%

rs = randn( 'state' );
randn( 'state', 0 );

% matrix size (M is an n-by-n matrix)
n = 4;
M = randn(n,n);

% formulating the problem as a GP
cvx_begin gp
  variable d(n)
  minimize( sqrt( sum( sum( diag(d.^2)*(M.^2)*diag(d.^-2) ) ) ) )
  % Alternate formulation: norm( diag(d)*abs(M)*diag(1./d), 'fro' )
cvx_end

% displaying results
D = diag(d);
disp('The matrix D that minimizes ||DMD^{-1}||_F is: ');
disp(D);
disp('The minimium Frobenius norm achieved is: ');
disp(norm(D*M*inv(D),'fro'));
disp('while the Frobunius norm of the original matrix M is: ');
disp(norm(M,'fro'));
 
Calling Mosek 9.1.9: 82 variables, 32 equality constraints
------------------------------------------------------------

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            : 32              
  Cones                  : 16              
  Scalar variables       : 82              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 3
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            : 32              
  Cones                  : 16              
  Scalar variables       : 82              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 13
Optimizer  - Cones                  : 16
Optimizer  - Scalar variables       : 48                conic                  : 48              
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
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 : 91                after factor           : 91              
Factor     - dense dim.             : 0                 flops                  : 1.58e+03        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   2.0e+01  1.3e+00  1.4e+01  0.00e+00   4.139191995e-01   -1.288163203e+01  1.0e+00  0.00  
1   4.3e+00  2.8e-01  3.3e+00  -4.38e-01  1.395618862e+00   -3.815128541e+00  2.2e-01  0.01  
2   5.6e-01  3.7e-02  1.9e-01  5.20e-01   2.274828194e+00   1.474450895e+00   2.8e-02  0.01  
3   9.2e-02  6.0e-03  6.3e-03  1.29e+00   2.269999438e+00   2.144594407e+00   4.7e-03  0.01  
4   2.5e-02  1.6e-03  8.5e-04  1.33e+00   2.072029381e+00   2.042509915e+00   1.3e-03  0.01  
5   6.1e-03  4.0e-04  1.1e-04  1.11e+00   2.031446660e+00   2.024626030e+00   3.1e-04  0.01  
6   6.6e-04  4.4e-05  4.0e-06  1.02e+00   2.018278526e+00   2.017546513e+00   3.4e-05  0.01  
7   7.2e-05  4.7e-06  1.4e-07  1.00e+00   2.017465148e+00   2.017386371e+00   3.6e-06  0.01  
8   6.5e-06  4.3e-07  3.9e-09  1.00e+00   2.017394509e+00   2.017387325e+00   3.3e-07  0.01  
9   6.6e-07  4.4e-08  1.3e-10  1.00e+00   2.017388720e+00   2.017387990e+00   3.4e-08  0.01  
10  9.5e-08  6.2e-09  6.9e-12  1.00e+00   2.017388192e+00   2.017388088e+00   4.8e-09  0.01  
11  1.2e-08  7.7e-10  3.0e-13  1.00e+00   2.017388117e+00   2.017388104e+00   5.9e-10  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 2.0173881172e+00    nrm: 8e+00    Viol.  con: 2e-08    var: 0e+00    cones: 5e-12  
  Dual.    obj: 2.0173881043e+00    nrm: 1e+00    Viol.  con: 0e+00    var: 8e-10    cones: 0e+00  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 11        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.25231
 
The matrix D that minimizes ||DMD^{-1}||_F is: 
    1.1912         0         0         0
         0    1.0000         0         0
         0         0    1.0674         0
         0         0         0    1.7831

The minimium Frobenius norm achieved is: 
    3.2523

while the Frobunius norm of the original matrix M is: 
    3.6126