I have an expression for a scalar, which is the double sum over vector-matrix-vector products. All terms are real-values vectors/matrices and dimensions are below each term:
$$\sum_{i=1}^N \sum_{j=1}^N \underbrace{a_i^T}_{1 \times k} \underbrace{B_i}_{k \times k} \underbrace{K_i}_{k \times p} \underbrace{K_j^T}_{p \times k} \underbrace{B_j^T}_{k \times k} \underbrace{a_j}_{k \times 1} $$
I'd like to simplify this expression such that the double sums are absorbed and I have a single vector-matrix-vector product, maybe something like:
$$\underbrace{x^T}_{1 \times kN} \quad \underbrace{P}_{kN \times kN} \quad \underbrace{x}_{kN \times 1}$$
where $P$ depends on the $B_i, K_i$ matrices and $x$ depends on the $a_i$ vectors.
But I don't know how to collapse $B_i, K_i$ to form $P$. Can someone please help me? I think the Kronecker product might be useful, but I'm not sure and I'm not too familiar with it.
If this question has already been asked/answered, please point me towards that!
Edit: Made clear that $P$ should depend only on $B_i, K_i$ and $x$ should depend only on $a_i$s. I need this because I want to understand how $B_i, K_i$ together determine the eigenspectrum of $P$.
Edit 2: All I want is $\begin{bmatrix}a_1\\ \vdots\\ a_N\end{bmatrix}^T \begin{bmatrix} B_1 K_1 K_1^T B_1^T & \dots & B_1 K_1 K_N^T B_N^T\\ \vdots & \ddots & \\ B_N K_N K_1^T B_1^T & & B_N K_N K_N^T B_N^T \end{bmatrix} \begin{bmatrix}a_1\\ \vdots\\ a_N\end{bmatrix}$. Is there no way to express this using a Kronecker product or some other operation?