# How do I solve for a matrix in this linear algebra expression?

In the problem I am working on I have an expression that looks like

$$B^T\operatorname{diag}(Bx) y$$

where $B$ is a known $m \times n$ matrix, $x$ is an unknown $n \times 1$ vector of variables, and $y$ is a known $m \times 1$ vector, all of real numbers.

The expression above can always be rewritten in the form $Px$ by expanding and collecting like terms, where $P$ is an $n \times n$ matrix. I would like to know how to calculate $P$ using only matrix and vector products.

As an example, take $B = \left( \begin{array}{ccc} -1 & 1 & 0 \\ -1 & 0 & 1\end{array} \right), y = \left( \begin{array}{c} 2 \\ 3\end{array} \right)$.

Then

\begin{align} B^T \operatorname{diag}(Bx) y & = \left( \begin{array}{cc} -1 & -1 \\ 1 & 0 \\ 0 & 1\end{array} \right) \left( \begin{array}{ccc} -x_1 + x_2 & 0\\ 0 & -x_1 + x_3\end{array} \right) \left( \begin{array}{c} 2 \\ 3\end{array} \right) \\[10pt] & = \left( \begin{array}{c} -5x_1 -2x_2 -3x_3 \\ -2x_1 + 2x_2 \\ -3x_1 + 3x_3\end{array} \right) = \left( \begin{array}{cc} -5 & -2 & -3 \\ -2 & 2 & 0 \\ -3 & 0 & 3\end{array} \right) \left( \begin{array}{c} x_1 \\ x_2 \\ x_3\end{array} \right) \end{align}

Therefore $P = \left( \begin{array}{cc} -5 & -2 & -3 \\ -2 & 2 & 0 \\ -3 & 0 & 3\end{array} \right)$

But I do not know how to calculate $P$ as an expression of the other known quantities, $P = F(B,y)$. Thanks.

• I have long been mystified by the seeming universality of starting the word "where" in this context with a capital letter when it's clearly not the beginning of a new sentence but a continuation of a sentence already underway. That's not done by mathematicians but is seen daily here and in Wikipedia. Can you tell me why you did that? $\qquad$ Jun 15, 2016 at 21:00
• @MichaelHardy I find myself accidentally whatever comes after a full-line mathematical expression, since the next paragraph usually starts with a new sentence. Jun 15, 2016 at 21:03

Note that $x \mapsto B^T \operatorname{diag}(Bx)y$ is a linear transformation on $x$. To find the matrix of this transformation (that is, the matrix $P$ for which $Px = B^T \operatorname{diag}(Bx)y$), we can see what happens to the standard basis vectors in order to get the columns of $P$.
Long story short: let $B_k$ denote the $k$th column of $B$. Let $P_k = B^T \operatorname{diag}(B_k)y$. We then have $$P = [P_1 \quad P_2 \quad \cdots \quad P_n]$$ If we use $\circ$ to denote the Hadamard product, then we could also say $$P_k = B^T(B_k \circ y) = B^T(y \circ B_k) = B^T \operatorname{diag}(y) B_k$$ From which one may deduce that $$P = B^T \operatorname{diag}(y) B$$ (as inspired by the other answer)