Examples

Creating Variables

using DiffMatic

@matrix A B C
@vector x y z
@scalar c

Creating Expressions

Matrix Multiplication and Transpose

expr = x' * A * x

# output

x₄A⁴₅x⁵
expr = x' * A' * x

# output

x₄A₅⁴x⁵
expr = A * B * x

# output

A¹₅B⁵₇x⁷

Hadamard/Element-wise Product

expr = A .* B .* C

# output

A¹₂B¹₂C¹₂
expr = x .* y .* z

# output

x¹y¹z¹

Powers and Element-wise Powers

expr = (x' * A * x)^2

# output

(x₄A⁴₅x⁵).^2
expr = (x .* y).^2

# output

(x¹y¹).^2
expr = (A * x).^2

# output

(A¹₄x⁴).^2

Trigonometric Functions

expr = sin.(A * x)

# output

sin(A¹₄x⁴)
expr = cos(x' * x)

# output

cos(x₃x³)

Absolute Value

expr = abs.(A * x)

# output

|A¹₄x⁴|
expr = abs(x' * x)

# output

|x₃x³|

Sum of a vector

expr = sum(x .* y)

# output

x¹y¹δ¹₁
expr = sum(A * x)

# output

A¹₄x⁴δ¹₁

Vector Norms

expr = norm2(A * x)

# output

((A¹₄x⁴).^2δ¹₁).^1//2
expr = norm1(A * x)

# output

|A¹₄x⁴|δ¹₁

Matrix Trace

expr = tr(A)

# output

A²₂
expr = tr(A*B*B'*C)

# output

B⁵₇A¹₅B₉⁷C⁹₁

Derivatives in Standard Notation

Gradient

to_std(gradient(tr(x * x'), x))

# output

"2x"
to_std(gradient(tr(x * x'), x))

# output

"2x"
to_std(gradient((x .* y)' * x, x))

# output

"2(x ⊙ y)"
to_std(gradient((x - y)' * x, x))

# output

"2x - y"
to_std(gradient(sin(tr(x * x')), x))

# output

"cos(xᵀx)2x"
to_std(gradient(abs(x' * x), x))

# output

"sgn(xᵀx)2x"
to_std(gradient(2 * sum(cos.(A * x + y)), x))

# output

"(-2)Aᵀsin(Ax + y)"
to_std(gradient((x' * A * x) ^ (-2), x))

# output

"(-2)(xᵀAᵀx)^(-3)(Aᵀx + Ax)"
to_std(gradient(((A .* (B .* C)) * C * x)' * x, x))

# output

"(B ⊙ C ⊙ A)Cx + Cᵀ(Bᵀ ⊙ Cᵀ ⊙ Aᵀ)x"
to_std(gradient(sum((A .* B) * C * x), x))

# output

"Cᵀ(Aᵀ ⊙ Bᵀ)vec(1)"

Jacobian

to_std(jacobian(A * x, x))

# output

"A"
to_std(jacobian(A' * x, x))

# output

"Aᵀ"
to_std(jacobian(sin.(A * x + y), x))

# output

"diag(cos(Ax + y))A"
to_std(jacobian(((A .* B) * C * x)' * x * x, x))

# output

"xᵀCᵀ(Aᵀ ⊙ Bᵀ)xI + x(xᵀCᵀ(Aᵀ ⊙ Bᵀ) + xᵀ(A ⊙ B)C)"