An example from Analytic Eigensystems for Isotropic Distortion Energies Eq. 13
The original equation:
I❤️LA implementation:
`T₁` = 1/√2 U[0 0 0
0 0 -1
0 1 0]Vᵀ
where
U ∈ ℝ^(3×3)
V ∈ ℝ^(3×3)
I❤️LA compiled to C++/Eigen:
/*
`T₁` = 1/√2 U[0 0 0
0 0 -1
0 1 0]Vᵀ
where
U ∈ ℝ^(3×3)
V ∈ ℝ^(3×3)
*/
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <iostream>
#include <set>
struct analytic_eigensystems_13ResultType {
Eigen::Matrix<double, 3, 3> T₁;
analytic_eigensystems_13ResultType(const Eigen::Matrix<double, 3, 3> & T₁)
: T₁(T₁)
{}
};
analytic_eigensystems_13ResultType analytic_eigensystems_13(
const Eigen::Matrix<double, 3, 3> & U,
const Eigen::Matrix<double, 3, 3> & V)
{
Eigen::Matrix<double, 3, 3> T₁_0;
T₁_0 << 0, 0, 0,
0, 0, -1,
0, 1, 0;
Eigen::Matrix<double, 3, 3> T₁ = 1 / double(sqrt(2)) * U * T₁_0 * V.transpose();
return analytic_eigensystems_13ResultType(T₁);
}
void generateRandomData(Eigen::Matrix<double, 3, 3> & U,
Eigen::Matrix<double, 3, 3> & V)
{
U = Eigen::MatrixXd::Random(3, 3);
V = Eigen::MatrixXd::Random(3, 3);
}
int main(int argc, char *argv[])
{
srand((int)time(NULL));
Eigen::Matrix<double, 3, 3> U;
Eigen::Matrix<double, 3, 3> V;
generateRandomData(U, V);
analytic_eigensystems_13ResultType func_value = analytic_eigensystems_13(U, V);
std::cout<<"return value:\n"<<func_value.T₁<<std::endl;
return 0;
}
I❤️LA compiled to Python/NumPy/SciPy:
"""
`T₁` = 1/√2 U[0 0 0
0 0 -1
0 1 0]Vᵀ
where
U ∈ ℝ^(3×3)
V ∈ ℝ^(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 analytic_eigensystems_13ResultType:
def __init__( self, T1):
self.T1 = T1
def analytic_eigensystems_13(U, V):
U = np.asarray(U, dtype=np.float64)
V = np.asarray(V, dtype=np.float64)
assert U.shape == (3, 3)
assert V.shape == (3, 3)
T1_0 = np.zeros((3, 3))
T1_0[0] = [0, 0, 0]
T1_0[1] = [0, 0, -1]
T1_0[2] = [0, 1, 0]
T1 = 1 / np.sqrt(2) * U @ T1_0 @ V.T
return analytic_eigensystems_13ResultType(T1)
def generateRandomData():
U = np.random.randn(3, 3)
V = np.random.randn(3, 3)
return U, V
if __name__ == '__main__':
U, V = generateRandomData()
print("U:", U)
print("V:", V)
func_value = analytic_eigensystems_13(U, V)
print("return value: ", func_value.T1)
I❤️LA compiled to MATLAB:
function output = analytic_eigensystems_13(U, V)
% output = analytic_eigensystems_13(U, V)
%
% `T₁` = 1/√2 U[0 0 0
% 0 0 -1
% 0 1 0]Vᵀ
%
% where
%
% U ∈ ℝ^(3×3)
% V ∈ ℝ^(3×3)
if nargin==0
warning('generating random input data');
[U, V] = generateRandomData();
end
function [U, V] = generateRandomData()
U = randn(3, 3);
V = randn(3, 3);
end
assert( isequal(size(U), [3, 3]) );
assert( isequal(size(V), [3, 3]) );
T1_0 = zeros(3, 3);
T1_0(1,:) = [0, 0, 0];
T1_0(2,:) = [0, 0, -1];
T1_0(3,:) = [0, 1, 0];
T1 = 1 / sqrt(2) * U * T1_0 * V';
output.T1 = T1;
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{T₁} & = \frac{1}{\sqrt{2}}\mathit{U}\begin{bmatrix}
0 & 0 & 0\\
0 & 0 & -1\\
0 & 1 & 0\\
\end{bmatrix}{\mathit{V}}^T \\
\intertext{where}
\mathit{U} & \in \mathbb{R}^{ 3 \times 3 } \\
\mathit{V} & \in \mathbb{R}^{ 3 \times 3 } \\
\\
\end{align*}
\end{minipage}
}
\end{center}
\end{document}
I❤️LA LaTeX output: