Let $A \in \mathbb{R}^{n \times n}$, and let $B \in \mathbb{R}^{n \times n}$, where $B$ is a diagonal matrix with $b > 0$ along the diagonal.

Then we have $$tr(AB) = tr(BA) = b\* tr(A),$$
where we can factor out $b$ from the trace operator.

Now suppose $B$ is no longer a diagonal matrix. Suppose it has $b > 0$ along the diagonal and $b' \in \mathbb{R}$ in the off-diagonals.

If we're interested in computing $tr(AB)$, is it still possible to still factor out some function of $b$ from the trace operator?

Here's a simple example I tried:

$A = \begin{bmatrix}1 & 2 \\ 3 & 9 \end{bmatrix}, B = \begin{bmatrix}b & b' \\ b' & b\end{bmatrix}$

then we have
\begin{align}
tr(BA) &= tr\left(\begin{bmatrix}b & b' \\ b' & b\end{bmatrix}\begin{bmatrix}1 & 2 \\ 3 & 9 \end{bmatrix}\right)\\
&= tr\left(\begin{bmatrix}b + 3b' & 2b + 9b' \\ 3b + b' & 9b + 2b'\end{bmatrix}\right)\\
&= 10b + 5b'
\end{align}

\begin{align}
tr(A) &= tr\left(\begin{bmatrix}1 & 2 \\ 3 & 9 \end{bmatrix}\right)\\
&= 10
\end{align}

So it looks like $$tr(AB) = b \* tr(A) + (a\_{12} + a\_{21}) \* b'$$

where $a\_{12}, a\_{21}$ are the off-diagonal entries in $A$.