An example from Handheld Multi-Frame Super-Resolution Eq. 4
The original equation:
I❤️LA implementation:
Ω = [`e₁` `e₂`][`k₁` 0
0 `k₂`] [`e₁`ᵀ
`e₂`ᵀ]
where
`k₁` ∈ ℝ
`k₂` ∈ ℝ
`e₁` ∈ ℝ²
`e₂` ∈ ℝ²
I❤️LA compiled to C++/Eigen:
/*
Ω = [`e₁` `e₂`][`k₁` 0
0 `k₂`] [`e₁`ᵀ
`e₂`ᵀ]
where
`k₁` ∈ ℝ
`k₂` ∈ ℝ
`e₁` ∈ ℝ²
`e₂` ∈ ℝ²
*/
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <iostream>
#include <set>
struct multi_frame_4ResultType {
Eigen::Matrix<double, 2, 2> Ω;
multi_frame_4ResultType(const Eigen::Matrix<double, 2, 2> & Ω)
: Ω(Ω)
{}
};
multi_frame_4ResultType multi_frame_4(
const double & k₁,
const double & k₂,
const Eigen::Matrix<double, 2, 1> & e₁,
const Eigen::Matrix<double, 2, 1> & e₂)
{
Eigen::Matrix<double, 2, 2> Ω_0;
Ω_0 << e₁, e₂;
Eigen::Matrix<double, 2, 2> Ω_1;
Ω_1 << k₁, 0,
0, k₂;
Eigen::Matrix<double, 2, 2> Ω_2;
Ω_2 << e₁.transpose(),
e₂.transpose();
Eigen::Matrix<double, 2, 2> Ω = Ω_0 * Ω_1 * Ω_2;
return multi_frame_4ResultType(Ω);
}
void generateRandomData(double & k₁,
double & k₂,
Eigen::Matrix<double, 2, 1> & e₁,
Eigen::Matrix<double, 2, 1> & e₂)
{
k₁ = rand() % 10;
k₂ = rand() % 10;
e₁ = Eigen::VectorXd::Random(2);
e₂ = Eigen::VectorXd::Random(2);
}
int main(int argc, char *argv[])
{
srand((int)time(NULL));
double k₁;
double k₂;
Eigen::Matrix<double, 2, 1> e₁;
Eigen::Matrix<double, 2, 1> e₂;
generateRandomData(k₁, k₂, e₁, e₂);
multi_frame_4ResultType func_value = multi_frame_4(k₁, k₂, e₁, e₂);
std::cout<<"return value:\n"<<func_value.Ω<<std::endl;
return 0;
}
I❤️LA compiled to Python/NumPy/SciPy:
"""
Ω = [`e₁` `e₂`][`k₁` 0
0 `k₂`] [`e₁`ᵀ
`e₂`ᵀ]
where
`k₁` ∈ ℝ
`k₂` ∈ ℝ
`e₁` ∈ ℝ²
`e₂` ∈ ℝ²
"""
import numpy as np
import scipy
import scipy.linalg
from scipy import sparse
from scipy.integrate import quad
from scipy.optimize import minimize
class multi_frame_4ResultType:
def __init__( self, Ω):
self.Ω = Ω
def multi_frame_4(k1, k2, e1, e2):
e1 = np.asarray(e1, dtype=np.float64)
e2 = np.asarray(e2, dtype=np.float64)
assert np.ndim(k1) == 0
assert np.ndim(k2) == 0
assert e1.shape == (2,)
assert e2.shape == (2,)
Ω_0 = np.hstack(((e1).reshape(2, 1), (e2).reshape(2, 1)))
Ω_1 = np.zeros((2, 2))
Ω_1[0] = [k1, 0]
Ω_1[1] = [0, k2]
Ω_2 = np.vstack((e1.T.reshape(1, 2), e2.T.reshape(1, 2)))
Ω = Ω_0 @ Ω_1 @ Ω_2
return multi_frame_4ResultType(Ω)
def generateRandomData():
k1 = np.random.randn()
k2 = np.random.randn()
e1 = np.random.randn(2)
e2 = np.random.randn(2)
return k1, k2, e1, e2
if __name__ == '__main__':
k1, k2, e1, e2 = generateRandomData()
print("k1:", k1)
print("k2:", k2)
print("e1:", e1)
print("e2:", e2)
func_value = multi_frame_4(k1, k2, e1, e2)
print("return value: ", func_value.Ω)
I❤️LA compiled to MATLAB:
function output = multi_frame_4(k1, k2, e1, e2)
% output = multi_frame_4(`k₁`, `k₂`, `e₁`, `e₂`)
%
% Ω = [`e₁` `e₂`][`k₁` 0
% 0 `k₂`] [`e₁`ᵀ
% `e₂`ᵀ]
%
% where
% `k₁` ∈ ℝ
% `k₂` ∈ ℝ
% `e₁` ∈ ℝ²
% `e₂` ∈ ℝ²
if nargin==0
warning('generating random input data');
[k1, k2, e1, e2] = generateRandomData();
end
function [k1, k2, e1, e2] = generateRandomData()
k1 = randn();
k2 = randn();
e1 = randn(2,1);
e2 = randn(2,1);
end
e1 = reshape(e1,[],1);
e2 = reshape(e2,[],1);
assert(numel(k1) == 1);
assert(numel(k2) == 1);
assert( numel(e1) == 2 );
assert( numel(e2) == 2 );
Omega_0 = [[reshape(e1, [2, 1]), reshape(e2, [2, 1])]];
Omega_1 = zeros(2, 2);
Omega_1(1,:) = [k1, 0];
Omega_1(2,:) = [0, k2];
Omega_2 = [[e1']; [e2']];
Omega = Omega_0 * Omega_1 * Omega_2;
output.Omega = Omega;
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*}
\mathit{Ω} & = \begin{bmatrix}
\textit{e₁} & \textit{e₂}\\
\end{bmatrix}\begin{bmatrix}
\textit{k₁} & 0\\
0 & \textit{k₂}\\
\end{bmatrix}\begin{bmatrix}
{\textit{e₁}}^T\\
{\textit{e₂}}^T\\
\end{bmatrix} \\
\intertext{where}
\textit{k₁} & \in \mathbb{R} \\
\textit{k₂} & \in \mathbb{R} \\
\textit{e₁} & \in \mathbb{R}^{ 2} \\
\textit{e₂} & \in \mathbb{R}^{ 2} \\
\\
\end{align*}
\end{minipage}
}
\end{center}
\end{document}
I❤️LA LaTeX output: