Symbolic arrays

Symbolic array-valued expressions (symbolic arrays) are supported by Symbolics. Symbolic array expressions propagate useful metadata that depends on input arrays: array dimension, element type and shape.

You can create a symbolic array variable with the following syntax:

using Symbolics
@variables A[1:5, 1:3] b[1:3]
2-element Vector{Symbolics.Arr{Num}}:
 A[1:5,1:3]
 b[1:3]

Here A is a symbolic matrix of size (5, 3) and b is a symbolic vector of length 3.

size(A)
(5, 3)
size(b)
(3,)
ndims(A)
2
ndims(b)
1
eltype(A)
Real
eltype(b)
Real

Array operations

Operations on symbolic arrays return symbolic array expressions:

c = A * b

\[ \begin{equation} A b \end{equation} \]

size(c)
(5,)
eltype(c)
Real

Adjoints, matrix-matrix, and matrix-vector multiplications are supported. Dot product returns a scalar-valued expression:

b'b

\[ \begin{equation} \mathrm{adjoint}\left( b \right) b \end{equation} \]

size(b'b)
()

Outer product returns a matrix:

b * b'

\[ \begin{equation} b \mathrm{adjoint}\left( b \right) \end{equation} \]

size(b*b')
(3, 3)

Broadcast, map and reduce

A .* b'

\[ \begin{equation} \mathrm{broadcast}\left( *, A, \mathrm{adjoint}\left( b \right) \right) \end{equation} \]

map(asin, (A*b))

\[ \begin{equation} \mathrm{map}\left( asin, A b \right) \end{equation} \]

#sum(A) #latexify not working
typeof(sum(A))
Num
typeof(sum(A, dims=2))
Symbolics.Arr{Num, 2}

Indexing and delayed computation

Indexing array expressions is fairly flexible in Symbolics. Let's go through all the possible ways to index arrays.

Scalar indexing and scalarization

AAt = A*A'

\[ \begin{equation} A \mathrm{adjoint}\left( A \right) \end{equation} \]

AAt[2,3]

\[ \begin{equation} A * adjoint(A)_{2}ˏ_3 \end{equation} \]

Here we indexed for the element (2,3), but we got back a symbolic indexing expression. You may want to force the element to be computed in terms of the elements of A. This can be done, using scalarize function.

Symbolics.scalarize(AAt[2,3])

\[ \begin{equation} A_{2}ˏ_1 A_{3}ˏ_1 + A_{2}ˏ_2 A_{3}ˏ_2 + A_{2}ˏ_3 A_{3}ˏ_3 \end{equation} \]

@syms i::Int j::Int
Symbolics.scalarize(AAt[i,j])

\[ \begin{equation} A\left[i, 1\right] A\left[j, 1\right] + A\left[i, 2\right] A\left[j, 2\right] + A\left[i, 3\right] A\left[j, 3\right] \end{equation} \]

In general any scalar expression which is derived from array expressions can be scalarized.

#sum(A[:,1]) + sum(A[2,:])#latexify not working
Symbolics.scalarize(sum(A[:,1]) + sum(A[2,:]))

\[ \begin{equation} 2 A_{2}ˏ_1 + A_{1}ˏ_1 + A_{2}ˏ_2 + A_{2}ˏ_3 + A_{3}ˏ_1 + A_{4}ˏ_1 + A_{5}ˏ_1 \end{equation} \]