I'd like to use the syms package to do some algebra for me, but the baseline assumption seems to be that variables are scalars. I would like to denote some variables as matrices. This will change the symbolic output/calculations. For example: inverse is not division, operations are not all commutative, and things can be transposed. Below I've illustrated a little bit of what I'd like to be able to do.

syms x
b=(x' * x)^(-1)
b=(x' * x)^(-1) * x

I want this code to treat 'x' as a matrix, such that I should get $(X'X)^{-1}X$, and there would be no simplification. However, the output is 1/x^2 for the first line and 1/x for the second line. Is there a way to do this in octave?

  • $\begingroup$ I've corrected what I thought they were typos in your code and added some LaTeX / Mathjax formatting. Please check if it is what you intended to write. $\endgroup$ – Ertxiem - reinstate Monica May 1 '19 at 21:44
  • $\begingroup$ @Ertxiem I'm not sure if those were typos. My understanding was that .' denotes transpose, and * multiplies. It looks like just ' is fine and I do not need the . before it to transpose. I do not want the element-by-element, and have changed this part back. $\endgroup$ – amquack May 4 '19 at 17:41
  • 1
    $\begingroup$ Not sure you can do what you want in Octave. MATLAB, or even MAPLE. Options which can, include matrixcalculus.org and perhaps SymPy. $\endgroup$ – Mark L. Stone May 4 '19 at 23:27
  • $\begingroup$ And apparently. per link in @Federico Poloni 's answer, Mathematica (which I didn't mention in my previous comment because I didn't know one way or a another. $\endgroup$ – Mark L. Stone May 5 '19 at 21:50

I don't think Octave's syms package supports this kind of matrix algebra operations, but you can do that in Mathematica.

EDIT: and also in Sympy.

| cite | improve this answer | |
  • $\begingroup$ Octave's symbolic package uses SymPy. $\endgroup$ – nicoguaro May 6 '19 at 13:37
  • 1
    $\begingroup$ @nicoguaro True, but looking at the documentation I don't see a binding to MatrixSymbol implemented by Octave. $\endgroup$ – Federico Poloni May 6 '19 at 17:24

You can define the elements of the matrix and then do the rest of the computations as intended.

For instance:

syms a b c d
x = [a b; c d]
y = (x' .* x)^(-1)
z = y * x

Edit: I've found a related post, which I used in the first part of this edit.

Another approach is to create a matrix of a determined size:

a = sym('a' ,[2 3])

The transpose a' works as expected.

I've also tried:

n = sym('n')
m = sym('m')
a = sym('a' ,[n m])

and it created the matrix a. However, when I tried a' I got an error message.

| cite | improve this answer | |
  • $\begingroup$ But I do not know what is in the matrix X. I just want it to treat the sym variable as a matrix rather than a scalar - what you've done is shown how to create a matrix out of scalar syms and then work with that. This is useful, but not in situations where matrix contents are unknown. $\endgroup$ – amquack May 4 '19 at 17:43
  • $\begingroup$ For example, I'm thinking something along the lines of matrixcalculus.org, where you can denote a variable to be scalar, matrix, symmetric matrix, vector, etc. I'd even be fine if I had to put in dimensions of matrices (assuming I could use syms for dimensions). $\endgroup$ – amquack May 4 '19 at 17:45
  • $\begingroup$ @amquack: I've edited my post with two other examples. $\endgroup$ – Ertxiem - reinstate Monica May 4 '19 at 22:53
  • $\begingroup$ that is an interesting post you found, which may start to get at the question. However based on your example I'm not sure how reliable it is (with the transpose), and it looks like the use of a different platform (SymPy or Mathematica as mentioned above) may be necessary to get at the symbolic matrix calculations I was interested in. $\endgroup$ – amquack May 6 '19 at 16:36

Your Answer

