Convex Optimization page 220

An example from Convex Optimization page 220

The original equation:

placeholder

I❤️LA implementation:

`L(x,ν)` = xᵀWx + ∑_i ν_i(x_i²-1)

where

x ∈ ℝ^n
W ∈ ℝ^(n×n)
ν ∈ ℝ^n

I❤️LA compiled to C++/Eigen:

/*
`L(x,ν)` = xᵀWx + ∑_i ν_i(x_i²-1)

where

x ∈ ℝ^n
W ∈ ℝ^(n×n)
ν ∈ ℝ^n
*/
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <iostream>
#include <set>

struct convex_optimization_220ResultType {
    double L_left_parenthesis_x_comma_ν_right_parenthesis;
    convex_optimization_220ResultType(const double & L_left_parenthesis_x_comma_ν_right_parenthesis)
    : L_left_parenthesis_x_comma_ν_right_parenthesis(L_left_parenthesis_x_comma_ν_right_parenthesis)
    {}
};

convex_optimization_220ResultType convex_optimization_220(
    const Eigen::VectorXd & x,
    const Eigen::MatrixXd & W,
    const Eigen::VectorXd & ν)
{
    const long n = x.size();
    assert( W.rows() == n );
    assert( W.cols() == n );
    assert( ν.size() == n );

    double sum_0 = 0;
    for(int i=1; i<=x.size(); i++){
        sum_0 += ν[i-1] * (pow(x[i-1], 2) - 1);
    }
    double L_left_parenthesis_x_comma_ν_right_parenthesis = (double)(x.transpose() * W * x) + sum_0;

    return convex_optimization_220ResultType(L_left_parenthesis_x_comma_ν_right_parenthesis);
}


void generateRandomData(Eigen::VectorXd & x,
    Eigen::MatrixXd & W,
    Eigen::VectorXd & ν)
{
    const int n = rand()%10;
    x = Eigen::VectorXd::Random(n);
    W = Eigen::MatrixXd::Random(n, n);
    ν = Eigen::VectorXd::Random(n);
}


int main(int argc, char *argv[])
{
    srand((int)time(NULL));
    Eigen::VectorXd x;
    Eigen::MatrixXd W;
    Eigen::VectorXd ν;
    generateRandomData(x, W, ν);
    convex_optimization_220ResultType func_value = convex_optimization_220(x, W, ν);
    std::cout<<"return value:\n"<<func_value.L_left_parenthesis_x_comma_ν_right_parenthesis<<std::endl;
    return 0;
}

I❤️LA compiled to Python/NumPy/SciPy:

"""
`L(x,ν)` = xᵀWx + ∑_i ν_i(x_i²-1)

where

x ∈ ℝ^n
W ∈ ℝ^(n×n)
ν ∈ ℝ^n
"""
import numpy as np
import scipy
import scipy.linalg
from scipy import sparse
from scipy.integrate import quad
from scipy.optimize import minimize


class convex_optimization_220ResultType:
    def __init__( self, L_left_parenthesis_x_comma_ν_right_parenthesis):
        self.L_left_parenthesis_x_comma_ν_right_parenthesis = L_left_parenthesis_x_comma_ν_right_parenthesis


def convex_optimization_220(x, W, ν):
    x = np.asarray(x, dtype=np.float64)
    W = np.asarray(W, dtype=np.float64)
    ν = np.asarray(ν, dtype=np.float64)

    n = x.shape[0]
    assert x.shape == (n,)
    assert W.shape == (n, n)
    assert ν.shape == (n,)

    sum_0 = 0
    for i in range(1, len(x)+1):
        sum_0 += ν[i-1] * (np.power(x[i-1], 2) - 1)
    L_left_parenthesis_x_comma_ν_right_parenthesis = (x.T.reshape(1, n) @ W @ x).item() + sum_0
    return convex_optimization_220ResultType(L_left_parenthesis_x_comma_ν_right_parenthesis)


def generateRandomData():
    n = np.random.randint(10)
    x = np.random.randn(n)
    W = np.random.randn(n, n)
    ν = np.random.randn(n)
    return x, W, ν


if __name__ == '__main__':
    x, W, ν = generateRandomData()
    print("x:", x)
    print("W:", W)
    print("ν:", ν)
    func_value = convex_optimization_220(x, W, ν)
    print("return value: ", func_value.L_left_parenthesis_x_comma_ν_right_parenthesis)

I❤️LA compiled to MATLAB:

function output = convex_optimization_220(x, W, nu)
% output = convex_optimization_220(x, W, ν)
%
%    `L(x,ν)` = xᵀWx + ∑_i ν_i(x_i²-1)
%    
%    where
%    
%    x ∈ ℝ^n
%    W ∈ ℝ^(n×n)
%    ν ∈ ℝ^n
    if nargin==0
        warning('generating random input data');
        [x, W, nu] = generateRandomData();
    end
    function [x, W, nu] = generateRandomData()
        n = randi(10);
        x = randn(n,1);
        W = randn(n, n);
        nu = randn(n,1);
    end

    x = reshape(x,[],1);
    nu = reshape(nu,[],1);

    n = size(x, 1);
    assert( numel(x) == n );
    assert( isequal(size(W), [n, n]) );
    assert( numel(nu) == n );

    sum_0 = 0;
    for i = 1:size(x,1)
        sum_0 = sum_0 + nu(i) * (x(i).^2 - 1);
    end
    L_x_nu = x' * W * x + sum_0;
    output.L_x_nu = L_x_nu;
end

I❤️LA compiled to LaTeX:

\documentclass[12pt]{article}
\usepackage{mathdots}
\usepackage[bb=boondox]{mathalfa}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{libertine}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\usepackage[paperheight=8in,paperwidth=4in,margin=.3in,heightrounded]{geometry}
\let\originalleft\left
\let\originalright\right
\renewcommand{\left}{\mathopen{}\mathclose\bgroup\originalleft}
\renewcommand{\right}{\aftergroup\egroup\originalright}
\begin{document}

\begin{center}
\resizebox{\textwidth}{!} 
{
\begin{minipage}[c]{\textwidth}
\begin{align*}
\textit{L(x,ν)} & = {\mathit{x}}^T\mathit{W}\mathit{x} + \sum_\mathit{i} \mathit{ν}_{ \mathit{i} }\left( {\mathit{x}_{ \mathit{i} }}^{2} - 1 \right) \\
\intertext{where} 
\mathit{x} & \in \mathbb{R}^{ \mathit{n}} \\
\mathit{W} & \in \mathbb{R}^{ \mathit{n} \times \mathit{n} } \\
\mathit{ν} & \in \mathbb{R}^{ \mathit{n}} \\
\\
\end{align*}
\end{minipage}
}
\end{center}

\end{document}

I❤️LA LaTeX output: