Close

January 7, 2020

Julia – Symbolic Math and Matrices

Started porting some Octave code over to Julia. Nice to see the SymPy package working nicely. This short post shows how to create a 2 dimensional Array, containing symbols.

Here is a small snippet that creates a 3×2 Array of symbols with the appropriate subscripts. ie: Array{Sym,2}. Notice the similarity to Python’s List comprehension syntax.

using SymPy
A = [Sym("a$i$j") for i=1:3 for j=1:2].reshape(3,2)

Shown below is an excerpt from one of my Jupyter Notebooks showing how to construct the Array of symbols and some basic Matrix operations (Transpose, Multiplication). Good for all those folks studying Linear Algebra also 🙂

If you want a simple function to make theses arrays containing sub-scripted elements, you could use something like the following.

function makeSymArray(m::Number, n::Number, elname::String="a")
    A = [Sym("$elname$i$j") for i=1:m for j=1:n].reshape(m,n)
    return A
end                             # end makeSymArray

I still use Octave, but I am impressed with Julia and its Jupyter integration via IJulia/notebook. Head on over to the Julia site and check it out.