Matrizer

Matrizer is an optimizing compiler for matrix expressions. Given approximate sizes of the matrices involved, it searches for mathematically equivalent expressions requiring fewer FLOPs to compute. The result is output as Python/numpy or MATLAB code.

Beware: this is still highly unpolished alpha-level software. Many features are missing, and there is no guarantee that the expressions output are optimal or even correct. It's best to think of the results as a source of inspiration rather than something to be blindly trusted.

TODO: write some documentation.

Try an example:
# log of Gaussian probability density function # variable declarations K: n x n posdef y: n x 1 d: 1 x 1 # dimension estimates n ~ 100 # expression to be optimized tmp pi = 3.14159; r = 0.5*(y' K^-1 y) + 0.5*log(det(K)) + 0.5*d*log(2*pi)
# normal equations for linear regression # variable declarations X: n x d y: n x 1 # dimension estimates n ~ 100 d ~ 5 # expression to be optimized w = (X'X)^-1 X' y
# Sherman–Morrison–Woodbury formula # http://en.wikipedia.org/wiki/Woodbury_matrix_identity # variable declarations A : n x n C : m x m U : n x m V : m x n # dimension estimates n ~ 1000 m ~ 10 # expression to be optimized D = (A^-1 + U * C * V)^-1
# Matrix calculus example (linear regression with squared loss) # variable declarations X: n x m y: n x 1 w: m x 1 # dimension estimates n ~ 100 m ~ 2 # expression to be optimized r = deriv( (X w - y)'(X w - y), w);
# Matrix calculus example (Gaussian density) # variable declarations K: n x n posdef y: n x 1 # dimension estimates n ~ 100 # expression to be optimized r = deriv( log(det(K)) + y' K^-1 y, K);
Iterations: Beam size: Rewrite depth:

Parsing expression...

Naive AST:


Naive computation requires FLOPs.

Optimizing, please wait...

Transformed to concrete expression requiring FLOPs:


Result requires FLOPs.

Optimized AST:


Python/numpy code:


MATLAB code:


Optimization path: