
I❤ LA: Compilable Markdown for Linear Algebra YONG LI, George Mason University, USA SHOAIB KAMIL, Adobe Research, USA ALEC JACOBSON, University of Toronto and Adobe Research, Canada YOTAM GINGOLD, George Mason University, USA Chalkboard math ❤ A_ij = { 1 if (i,j) ∈ E man 0 otherwise com ual pilat tran D_ii = ∑_j A_i,j ion sla LaTeX output ti L = D ¹( D - A ) on ⁻ where LaTeX LaTeX E ∈ { ℤ × ℤ } MATLAB A ∈ ℝ^(n × n) Python n ∈ ℤ C++/Eigen MATLAB \centering \begin{align*} \resizebox{\textwidth}{!} A_{i, j} & = \begin{cases} { \begin{minipage}[c]{\textwidth} 1 & \text{if} \left( i, j \right) \in E \\ \begin{align*} 0 & \text{otherwise} \end{cases} \\ \textit{A}_{\textit{i}, \textit{j}} & = \begin{cases} 1 & \text{if} \left( \textit{i}, \textit{j} \right) \in \textit{E} \\ 0 & \text{otherwise} \end{cases} \\ \textit{D}_{\textit{i},\textit{i}} & = \sum_\textit{j} \textit{A}_{\textit{i}, \textit{j}} \\ D_{i,i} & = \sum_j A_{i, j} \\ \textit{L} & = \textit{D}^{-1}\left( \textit{D} - \textit{A} \right) \\ L & = D^{-1}\left( D - A \right) \\ Python \intertext{where}function output = laplacian(E, n) \textit{E} & assert(size(E,2)\in \{\mathbb{Z} ==\times 2) \mathbb{Z}\} \\ \intertext{where} \textit{A} & assert(numel(n)\in \mathbb{R}^{ == \textit{n} 1); \times \textit{n} } \\ E & \in \{\mathbb{Z} \times \mathbb{Z}\} \\ \textit{n} & \in \mathbb{Z} \end{align*} Aij_0 = zeros(2,0); A & \in \mathbb{R}^{ n \times n } \\ \end{minipage} Avals_0 = zeros(1,0); } for i = 1:n n & \in \mathbb{Z} for j = 1:n import numpy as np \end{align*} if ismember([i, j],E,'rows') import scipy A = sparse(E(:,1),E(:,2),1,n,n); Aij_0(1:2,end+1) = [i;j]; import scipy.linalg C++/Eigen Avals_0(end+1) = 1; from scipy import sparse D = diag(sum(A,2)); end from scipy.integrate import quad L = D\(D-A); end from scipy.optimize import minimize end import numpy as np sparse_0 = sparse(Aij_0(1,:),Aij_0(2,:),Avals_0,n,n); #include <Eigen/Core> from scipy import sparse as sp A = sparse_0; def laplacian(E, n): #include <Eigen/Dense> Dii_0 = zeros(2,0); assert isinstance(E, list) and len(E) > #include0 <Eigen/Sparse> A = sp.coo_matrix((np.ones(len(E)), np.asarray(E).T), shape=(n,n)) Dvals_0 = zeros(1,0); assert len(E[0]) == 2 #include <iostream> #include <set> D = sp.dia_matrix((A.sum(1).T,0), shape=A.shape) for i = 1:n assert np.ndim(n) == 0 sum_0 = 0; L = sp.dia_matrix((1/D.diagonal(),0),shape=D.shape) @ ( D - A ) for j = 1:size(A,2) _Aij_0 = [] laplacianResultType laplacian( const std::set<std::tuple< int, int > > & E, using namespace Eigen; sum_0 = sum_0 + A(i, j); _Avals_0 = [] end for i in range(1, n+1): const int & n) std::vector<Eigen::Triplet<double>> ijv; Dii_0(1:2,end+1) = [i;i]; for j in range(1, n+1): { Eigen::SparseMatrix<double> A(n, n); for(int e = 0;e<E.rows();e++) { Dvals_0(end+1) = sum_0; if (i, j) in E: end _Aij_0.append((i-1, j-1)) std::vector<Eigen::Triplet<double> > tripletList_A; ijv.emplace_back( E(e,0), E(e,1), 1 ); D = sparse(Dii_0(1,:),Dii_0(2,:),Dvals_0,n,n); _Avals_0.append(1) for( int i=1; i<=n; i++){ L = (D\(D - A)); _sparse_0 = scipy.sparse.coo_matrix((_Avals_0, np.asarray(_Aij_0).T),for( int j=1; j<=n; j++){ shape=(n, n)) } output.A = A; A = _sparse_0 if(E.find(std::tuple< int, int >(i, j)) != E.end()){ SparseMatrix<double> A(n,n); tripletList_A.push_back(Eigen::Triplet<double>(i-1, j-1, 1)); output.D = D; _Dii_0 = [] } A.setFromTriplets(ijv.begin(),ijv.end()); _Dvals_0 = [] } DiagonalMatrix<double,Dynamic> D(A*VectorXd::Ones(A.cols())); output.L = L; for i in range(1, n+1): } end _sum_0 = 0 A.setFromTriplets(tripletList_A.begin(), tripletList_A.end()); DiagonalMatrix<double,Dynamic> Dinv(D.diagonal().array().pow(-1).matrix()); for j in range(1, A.shape[0]+1): Eigen::SparseMatrix<double> D(n, n); SparseMatrix<double> L = Dinv*(SparseMatrix<double>(D) - A); std::vector<Eigen::Triplet<double> > tripletList_D; for( int i=1; i<=n; i++){ double sum_0 = 0; for(int j=1; j<=A.cols(); j++){ sum_0 += A.coeff(i-1, j-1); } tripletList_D.push_back(Eigen::Triplet<double>(i-1, i-1, sum_0)); Fig. 1. Left: Mathematical notation has evolved over centuries to efficiently communicate technical concepts such } as the sparse graph Laplacian construction D.setFromTriplets(tripletList_D.begin(), tripletList_D.end()); in the top left. Meanwhile, programming languages communicate with a machine typically with a reduced character Eigen::SparseQR set<Eigen::SparseMatrix<double>, and syntax Eigen::COLAMDOrdering<int> causing > solver_0; handwritten solver_0.compute(D); Eigen::SparseMatrix<double> L = solver_0.solve((D - A)); translation of mathematics to visually stray far from the “chalkboard math” and from each other. Right: I LA is a novel domain return testResultType(A, specific D, L); language for linear algebra. The I LA code written with rich unicode symbols visually resembles chalkboard math, while still being} a semantically well-defined programming void generateRandomData(std::set<std::tuple< int, int > > & E, int & n) language compilable to various target languages: LaTeX, MATLAB, Python, C++. { n = rand() % 10; const int dim_0 = rand()%10; for(int i=0; i<dim_0; i++){ E.insert(std::make_tuple(rand()%10, rand()%10)); } Communicating linear algebra in written form is challenging: mathemati- programming environments. We outline the} principles of our language de- cians must choose between writing in languages that produce well-formatted sign and highlight design decisions that balanceint main(int argc, char between *argv[]) readability and { srand((int)time(NULL)); but semantically-underdefined representations such as LaTeX; or languages precise semantics, and demonstrate through std::set<std::tuple< case studies int, int the> > E; ability for I LA int n; generateRandomData(E, n); with well-defined semantics but notation unlike conventional math, suchas to bridge the semantic gap between conventionally-written testResultType func_value = test(E, n); linear algebra std::cout<<"return value:\n"<<func_value.L<<std::endl; return 0; C++/Eigen. In both cases, the underlying linear algebra is obfuscated by the and unambiguous interpretation in math programming} environments. requirements of esoteric language syntax (as in LaTeX) or awkward APIs ! due to language semantics (as in C++). The gap between representations CCS Concepts: • Computing methodologies Graphics systems and ! results in communication challenges, including underspecified and irrepro- interfaces;• Software and its engineering Domain specific lan- ! ducible research results, difficulty teaching math concepts underlying com- guages;• Mathematics of computing Mathematical software. plex numerical code, as well as repeated, redundant, and error-prone trans- Additional Key Words and Phrases: linear algebra, mathematical input, domain- lations from communicated linear algebra to executable code. We introduce I LA, a language with syntax designed to closely mimic conventionally- specific language, compiler, scientific computing written linear algebra, while still ensuring an unambiguous, compilable in- terpretation. Inspired by Markdown, a language for writing naturally-structured 1 INTRODUCTION plain text files that translate into valid HTML, I LA allows users to write linear algebra in text form and compile the same source into LaTeX, C++/Eigen, Linear algebra has become the lingua franca of computer graph- Python/NumPy/SciPy, and MATLAB, with easy extension to further math ics and other fields of scientific computing. Matrices and vectors allow researchers to succinctly communicate mathematical expres- sions involving arbitrary amounts of data. Mathematical notation Authors’ addresses: Yong Li, George Mason University, USA, [email protected]; Shoaib using these constructs is a human language that continues to evolve Kamil, Adobe Research, USA, [email protected]; Alec Jacobson, University of Toronto and Adobe Research, Canada, [email protected]; Yotam Gingold, George Ma- over time to be readable yet precise, while eliding details unneces- son University, USA, [email protected]. sary for communication. It allows for communicating expressions Submission ID: 214. 2021-05-24 07:28. Page 1 of 1–14. 0:2 • Yong Li, Shoaib Kamil, Alec Jacobson, and Yotam Gingold and❤ formulae to other researchers, practitioners, educators, and stu- use of I LA through case studies of graphics algorithms that illus- dents, often by publishing scientific papers. trate how the language enables practitioners to write in plain text Communicating expressions and formulae requires writing them and use the same source for generating output suitable for publica- in a language that produces mathematical notation, such as La- tions/communication as well as reference implementations in both TeX, with esoteric syntax that is not readily understandable with- statically and dynamically typed languages. We performed a user out compiling to a print-ready document. On the other hand, imple- study to understand how experienced practitioners learn and per- menting mathematical expressions entails translating them into a ceive I LA. specific programming environment: a language and linear algebra package, with its own specific syntax dissimilar to languages de- 2 RELATED WORK signed for expressing syntax. Thus, succinct expressions must be re- For researchers and practitioners alike to make use of each oth- written multiple times in languages less legible than linear algebra ers’ techniques, they must translate the linear algebra expressions (Fig. 1, left). This has the potential to introduce bugs. Worse, imple- into a specific programming environment. Examples today include: menters, in both academia and industry, favor different program- Python with NumPy/SciPy or TensorFlow or PyTorch; C/C++ with ming environments—and these environments change over time. To Eigen or PETSc or Armadillo or BLAS; MATLAB; Fortran; R; and Ju- participate in research and development, everyone must become lia. Some environments are less than 10 years old (Julia, Armadillo, a highly proficient implementer, which excludes many from con- TensorFlow, PyTorch). Some previously popular environments have tributing.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages14 Page
-
File Size-