DiffMatic.jl: Symbolic differentiation of vector/matrix/tensor expressions

DiffMatic.jl is a package for symbolic differentiation of matrix expressions. Input an expression involving matrices and vectors and obtain the derivative also in terms of matrices and vectors.

In case you have used the excellent matrixcalculus.org - DiffMatic.jl is a Julia implementation based on the same idea.

Short example

using DiffMatic

@matrix A B C
@vector x

expr = ((A .* B) * C * x)' * x

g = to_std(gradient(expr, x); format = JuliaFunc())

# output

quote
    #= ... =#
    function (A, B, C, x)
        #= ... =#
        #= ... =#
        return (A .* B) * (C * x) + (transpose(C) * (transpose(A) .* transpose(B))) * x
    end
end

evaluate the generated function:

grad = eval(g)

A = rand(10, 10)
B = rand(10, 10)
C = rand(10, 10)
x = rand(10)

grad(A, B, C, x)

Why use DiffMatic.jl

Many linear algebra libraries build on top of BLAS for achieving high-performance matrix-vector or matrix-matrix products. Having the derivative in terms of these high-level operations allows one to leverage the expertise and care that has been invested into these libraries.

Why not use DiffMatic.jl

DiffMatic.jl cannot differentiate control flow such as if-else and the number of operations it supports is rather limited compared to for example ForwardDiff.jl and other established autodiff libraries.

The supported operations and functions are listed in the readme and in the documentation.

Installation

Simply

] add DiffMatic

Future

I originally started this because I wanted something like matrixcalculus.org but that would generate C++ functions for use in my research. This particular feature, as well as some operators and functions, is still missing but I’m hoping that the current features could already be useful.

Thanks for reading! Please let me know your thoughts!

9 Likes

I recently had to do a calculation similar to this package. Had to do this in Mathematica. But now that I know this package exist. I will ise Julia for sure!

3 Likes