Define $n_A(G) = $ number of occurences of symbol $A$ in the grammar $G$ (RHS's of rules).
Then a smallest grammar minimizes $|G| = \sum_{A \in \Sigma} n_A(G)$ where $\Sigma$ is the alphabet of $G$ (all symbols occuring). Let $n_A(B) = 0$ if $A$ doesn't occur in $B$ and $n_A(B) = $ the number of times $A$ occurs in any rule with LHS $A$. Clearly $|G|$ is also $|G| = \sum_{A,B \in \Sigma} n_A(B)$.
Then every grammar has an associated square matrix $M$, where row and column indexes correspond to letters in $\Sigma$:
Ex. $G = \{S \to AABaa, A \to BBBa, B \to aaa\} \implies M = $ $$ \begin{pmatrix} \ & S & A & B & a \\ S & 0 & 0 & 0 & 0 \\ A & 2 & 0 & 0 & 0 \\ B & 1 & 3 & 0 & 0 \\ a & 2 & 1 & 3 & 0 \end{pmatrix} $$ where the sum of the entries in $M$ is equal to $|G|$, and each entry is $n_{\text{row}}(\text{column})$.
Can grammar operations then be written as matrix operations? Be creative...
For instance, the operation $\exp(A)$ (expand all occurences of variable $A$) is:
$S_c := S_c + 2A_c$ where $c, r$ will indicate column or row. $G$ in the example is not a smallest grammar of $s = a^{25}$, but $G'= \{S \to AAAAA, B \to aaaaa\}$ is. Can we transform $M$ into:
$$ \begin{pmatrix} 0 & 0 & 0 & 0 \\ 5 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 5 & 0 & 0 \end{pmatrix} $$
using row and column operations?
Add $-1$'s allong the diagonal of $M = (m_{i,j})$ used for subtracting $k$ below:
The grammar-constrained operations would be:
$$ C_j := C_j + k \cdot C_i , \\ k \leq m_{i,j}\\ m_{i,j} := m_{i,j} + k \\ \equiv \text{expanding variable } i \text{ inside } j $$
We can accomplish this by multiplying $M$ on the right by an identity matrix with a $k$ at $m_{j,i}$ :
$$ \begin{pmatrix} 1 & 0 & 0 & 0 \\ k & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} $$
Thus expansion of $k$ $A's$ inside $S$ can be done with an invertible elementary column matrix. But when $m_{i,j}$ drops to zero for all $j$ we do not sum in (non-diagonal) column $i$ entries and we can delete that colum/row (symbol $i$). Alternatively, strictly constrain $k \lt m_{i,j}$ and work only with grammars on a fixed $n$ variables at any one time.