Method on @sym: trace (A)

Trace of symbolic matrix.

Example:

syms x
A = [1 2 x; 3 sym(pi) 4; 13 5 2*x]
  ⇒ A = (sym 3×3 matrix)
      ⎡1   2   x ⎤
      ⎢          ⎥
      ⎢3   π   4 ⎥
      ⎢          ⎥
      ⎣13  5  2⋅x⎦
trace(A)
  ⇒ ans = (sym) 2⋅x + 1 + π

As an example, we can check that the trace of the product is not the product of the traces:

A = sym([1 2; 3 4]);
B = sym([pi 3; 1 8]);
trace(A*B)
  ⇒ ans = (sym) π + 43
trace(A) * trace(B)
  ⇒ ans = (sym) 5⋅π + 40

However, such a property does hold if we use the Kronecker tensor product (see ‘@sym/trace’):

kron(A, B)
  ⇒ ans = (sym 4×4 matrix)
      ⎡ π   3   2⋅π  6 ⎤
      ⎢                ⎥
      ⎢ 1   8    2   16⎥
      ⎢                ⎥
      ⎢3⋅π  9   4⋅π  12⎥
      ⎢                ⎥
      ⎣ 3   24   4   32⎦
trace(kron(A, B))
  ⇒ ans = (sym) 5⋅π + 40
trace(A) * trace(B)
  ⇒ ans = (sym) 5⋅π + 40

See also: @sym/det.

Package: symbolic