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
(A*b)[Base.OneTo(5)]
size(c)
(5,)
eltype(c)
Real
Adjoints, matrix-matrix, and matrix-vector multiplications are supported. Dot product returns a scalar-valued expression:
b'b
(adjoint(b)*b)[1]
size(b'b)
()
Outer product returns a matrix:
b * b'
(b*adjoint(b))[Base.OneTo(3),Base.OneTo(3)]
size(b*b')
(3, 3)
Broadcast, map and reduce
A .* b'
(broadcast(*, A, adjoint(b)))[Base.OneTo(5),Base.OneTo(3)]
map(asin, (A*b))
(map(asin, A*b))[Base.OneTo(5)]
#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'
(A*adjoint(A))[Base.OneTo(5),Base.OneTo(5)]
AAt[2,3]
(A*adjoint(A))[2, 3]
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 the scalarize
function.
Symbolics.scalarize(AAt[2,3])
A[2, 1]*A[3, 1] + A[2, 2]*A[3, 2] + A[2, 3]*A[3, 3]
@syms i::Int j::Int
Symbolics.scalarize(AAt[i,j])
A[i, 1]*A[j, 1] + A[i, 2]*A[j, 2] + A[i, 3]*A[j, 3]
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,:]))
A[1, 1] + 2A[2, 1] + A[2, 2] + A[2, 3] + A[3, 1] + A[4, 1] + A[5, 1]