I have a set of quadratic forms.




where $u_{i=1,2,3}$ - 3$\times$1 vector;

where $J_{i=1,2,3}$ - 3$\times$3 matrix;

I need to pack their into a single operation in such a way that I get a matrix of the following form:

$\boldsymbol{L}=\begin{bmatrix}L_1 & 0 & 0\\0 & L_2 & 0\\0 & 0 & L_3\end{bmatrix}$

I'm assuming this can be done using a tensor operation as well as combining vectors into a matrix. Probably, something like that:

$\boldsymbol{L} \approx ? \boldsymbol{u}^T\boldsymbol{J}\boldsymbol{u}$ or $\boldsymbol{L} \approx \boldsymbol{u}^T \otimes ? \boldsymbol{J} \otimes ? \boldsymbol{u}$

But the problem turned out to be that, firstly, my $J$-matrices are different, and secondly, there are side off-diagonal elements in the result.So far, I haven't come up with a suitable operation.

TensorProduct[{Subscript[u, 1], Subscript[u, 2], Subscript[u, 3]}, 
 TensorProduct[{Subscript[u, 1], Subscript[u, 2], Subscript[u, 
   3]}, {J}]]

I need help and advice. Thank you for attention!

  • 1
    $\begingroup$ My deleted answer did not answer the question. You are asking for a 3x3 matrix with the quadratic forms on the the diagonal. My "answer" had $J_1$, $J_2$, $J_3$ on the diagonal, not the $L_i$'s. Sorry for the confusion. $\endgroup$
    – LouisB
    Apr 11, 2022 at 6:21
  • $\begingroup$ @LouisB never mind :) we'll think of something $\endgroup$
    – dtn
    Apr 11, 2022 at 6:31
  • $\begingroup$ Might this help? mathematica.stackexchange.com/a/153181/35390 Constructing block-diagonal matrices this way also works for non-square matrices. $\endgroup$
    – JEM_Mosig
    Apr 11, 2022 at 7:06

2 Answers 2


I am not sure if I understand the question correctly, but if what you want is to produce a block diagonal matrix from a list of u's and a list of J's then

us = {u1, u2, u3};
Js = {J1, J2, J3};
DD = DiagonalMatrix[Inner[Dot, us, Inner[Dot, Js, us, List], List]]

will produce

{{u1 . J1 . u1, 0, 0}, {0, u2 . J2 . u2, 0}, {0, 0, u3 . J3 . u3}}

as you want. It also works if the u's are Arrays of length n and the J's are nxn matrices. If you prefer to work with nx1 matrices then you will need to transpose the us in the outer Inner.

If you already have the L's then DiagonalMatrix is all you need.

  • $\begingroup$ In general, yes, you correctly caught my question. And I understand that, apparently, it is clearly possible to come up with more than one such ways. I will consider your option, just write, please, the formula by which this problem is solved. $\endgroup$
    – dtn
    Apr 11, 2022 at 14:12
  • $\begingroup$ That would be DiagonalMatrix[Inner[Dot, us, Inner[Dot, Js, us, List], List]]. Or do you still have something that needs to be addressed? $\endgroup$ Apr 11, 2022 at 20:36
  • $\begingroup$ Yes, please, write your line in the form of a mathematical formula. $\endgroup$
    – dtn
    Apr 12, 2022 at 5:26
  • 1
    $\begingroup$ This line is the formula: it is applying an inner product in which multiplication is Dot and sum is List twice, followed by the structural operation DiagonalMatrix that takes a list into a diagonal matrix. $\endgroup$ Apr 12, 2022 at 13:58

First define the ui and ji using helper functions:

u[i_] := {Subscript[u, i, 1], Subscript[u, i, 2], Subscript[u, i, 3]}
{u1, u2, u3} = u /@ {1, 2, 3};
j[i_] := Table[Subscript[j, i, i1, i2], {i1, 3}, {i2, 3}];
{j1, j2, j3} = j /@ {1, 2, 3};

Now it is easy to create the final matrix:

mat={{u1 . j1 . u1, 0, 0}, {0, u2 . j2 . u2, 0}, {0, 0, u3 . j3 . u3}};
mat // MatrixForm

enter image description here


If you want to create the matrix automatically instead by hand, you may write:

 MapThread[#1 . #2 . #1 &, {{u1, u2, u3}, {j1, j2, j3}}]] 
  • $\begingroup$ Daniel, yes, this method works. But the matrix is constructed forcibly, i.e. has the desired structure forcibly. I would like to get the same matrix, but using tensors, or possibly outer and inner products, probably... As suggested in another answer in this thread. $\endgroup$
    – dtn
    Apr 11, 2022 at 16:39
  • $\begingroup$ I added the code to create the diagonal matrix by code. $\endgroup$ Apr 11, 2022 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.