I have some linear algebra equations, which are a few succinct lines of pytorch code, where the matrices involved have dimensions N and D, and N and D are usually very large. I wanted to really understand what they are doing, so started working through them for N=3, D=2. So I started with this:
|a b| |p r| |ap + bq ar + bs|
|c d| x |q s| = |cp + dq cr + ds|
|e f| |ep + fq er + fs|
A couple of long, tedious, calculations later, and after gathering terms, I had this:
aa(pt+rv) + ab(pu+rw) + ab(qt+sv) + bb(qu+sw) ac(pt+rv) + ad(pu+rw) + bc(qt+sv) + bd(qu+sw) ae(pt+rv) + af(pu+rw) + be(qt+sv) + bf(qu+sw)
ac(pt+rv) + bc(pu+rw) + ad(qt+sv) + bd(qu+sw) cc(pt+rv) + cd(pu+rw) + cd(qt+sv) + dd(qu+sw) ce(pt+rv) + cf(pu+rw) + de(qt+sv) + df(qu+sw)
ae(pt+rv) + be(pu+rw) + af(qt+sv) + bf(qu+sw) ce(pt+rv) + de(pu+rw) + cf(qt+sv) + df(qu+sw) ee(pt+rv) + ef(pu+rw) + ef(qt+sv) + ff(qu+sw)
Which was very helpful, but I'd cut one of the calculations out, and also made me see I want to know what it looks like when D and N are slightly bigger.
Is there any software or library I can use to have the computer do is this hard work for me? I.e. where I would write this code:
x = torch.tensor([ [1.0,2], [3,4], [5,6] ])
M = torch.tensor([ [0.1, 0.3], [0.7, 0.5] ])
y = x.matmul(M)
I want to write something like:
x = torch.tensor([ ['a', 'b'], ['c', 'd'], ['e', 'f'] ])
M = torch.tensor([ ['p', 'r'], ['q', 's'] ])
y = x.matmul(M)
If it gathered like terms for me that would be a bonus, but it is not essential. Not tied to a particular computer language, so any of Python, R, Octave/Matlab, C++, etc.
Thanks to the hint in the comments, I took a look at Sage, specifically inside CoCalc.
To do symbolic linear algebra you define your matrices like this:
(That last line was just to confirm I can something other than a..z; in fact only valid Python identifiers are allowed. Which means lots of Unicode is available, but things like circled numbers, negative letters and emoji were all disallowed.)
*does matrix multiplication as you'd expect, and there is a function to expand out brackets, and a choice of functions to simplify.(I look at Sage first, as it is open source, and based on Python; there appear to be a number of other choices.)