DiffMatic.jl

DiffMatic is a package for computing derivatives of matrix expressions, also known as matrix calculus. Provided are functions for computing gradients, Jacobians, Hessians and general matrix derivatives.

Installation

Installation from the general registry:

using Pkg; Pkg.add("DiffMatic")

Usage

Expressions are constructed from Julia syntax.

Creating a matrix and a vector:

using DiffMatic

@matrix A
@vector x

Creating an expression:

expr = x' * A * x

# output

x₄A⁴₅x⁵

The variable expr now contains an internal representation of the expression x' * A * x.

Compute the gradient and the Hessian with respect to the vector x.

g = gradient(expr, x)
H = hessian(expr, x)

Convert the gradient into standard notation using to_std:

to_std(g)

# output

"Aᵀx + Ax"

Convert the the Hessian into standard notation:

to_std(H)

# output

"Aᵀ + A"

Jacobians can be computed with jacobian:

to_std(jacobian(A * x, x))

# output

"A"

The function derivative can be used to compute arbitrary derivatives.

to_std(derivative(tr(A), A))

# output

"I"

The function to_std will throw an exception when given an expression that that cannot be converted to standard notation:

to_std(derivative(A, A))

# output

ERROR: DomainError