Hand Modeling and Simulation Using Stabilized Magnetic Resonance Imaging Eq. 3

An example from Hand Modeling and Simulation Using Stabilized Magnetic Resonance Imaging Eq. 3

The original equation:

placeholder

I❤️LA implementation:

min_(C ∈ ℝ^3) ∑_i ‖x_i + (R_i - I₃)C‖²

where

x_i ∈ ℝ^3
R_i ∈ ℝ^(3×3)

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

"""
min_(C ∈ ℝ^3) ∑_i ‖x_i + (R_i - I₃)C‖²

where

x_i ∈ ℝ^3
R_i ∈ ℝ^(3×3)
"""
import numpy as np
import scipy
import scipy.linalg
from scipy import sparse
from scipy.integrate import quad
from scipy.optimize import minimize


class hand_modeling_3ResultType:
    def __init__( self, ret):
        self.ret = ret


def hand_modeling_3(x, R):
    x = np.asarray(x, dtype=np.float64)
    R = np.asarray(R, dtype=np.float64)

    dim_0 = x.shape[0]
    assert x.shape == (dim_0, 3, )
    assert R.shape == (dim_0, 3, 3)

    def target_0(C):
        sum_0 = 0
        for i in range(1, len(x)+1):
            sum_0 += np.power(np.linalg.norm(x[i-1] + (R[i-1] - np.identity(3)) @ C, 2), 2)
        return sum_0
    ret = minimize(target_0, np.zeros(3)).fun
    return hand_modeling_3ResultType(ret)


def generateRandomData():
    dim_0 = np.random.randint(10)
    x = np.random.randn(dim_0, 3, )
    R = np.random.randn(dim_0, 3, 3)
    return x, R


if __name__ == '__main__':
    x, R = generateRandomData()
    print("x:", x)
    print("R:", R)
    func_value = hand_modeling_3(x, R)
    print("return value: ", func_value.ret)

I❤️LA compiled to MATLAB:

function output = hand_modeling_3(x, R)
% output = hand_modeling_3(x, R)
%
%    min_(C ∈ ℝ^3) ∑_i ‖x_i + (R_i - I₃)C‖²
%    
%    where
%    
%    x_i ∈ ℝ^3
%    R_i ∈ ℝ^(3×3)
    if nargin==0
        warning('generating random input data');
        [x, R] = generateRandomData();
    end
    function [x, R] = generateRandomData()
        dim_0 = randi(10);
        x = randn(dim_0,3);
        R = randn(dim_0,3,3);
    end

    dim_0 = size(x, 1);
    assert( isequal(size(x), [dim_0, 3]) );
    assert( isequal(size(R), [dim_0, 3, 3]) );

    function ret = target_1(C)
        sum_0 = 0;
        for i = 1:size(x, 1)
            sum_0 = sum_0 + norm(x(i,:)' + (squeeze(R(i,:,:)) - speye(3)) * C, 2).^2;
        end
        ret = sum_0;
    end
    [~,optimize_0] = fminunc(@target_1,zeros(3,1));
    ret = optimize_0;
    output.ret = ret;
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*}
 \omit \span \begin{aligned} \min_{\mathit{C} \in \mathbb{R}^{ 3}} \quad & \sum_\mathit{i} \left\|\mathit{x}_{ \mathit{i} } + \left( \mathit{R}_{ \mathit{i} } - I_{ 3 } \right)\mathit{C}\right\|_2^{2} \\
\end{aligned} \\
\intertext{where} 
\mathit{x}_{\mathit{i}} & \in \mathbb{R}^{ 3} \\
\mathit{R}_{\mathit{i}} & \in \mathbb{R}^{ 3 \times 3 } \\
\\
\end{align*}
\end{minipage}
}
\end{center}

\end{document}

I❤️LA LaTeX output: