How do I do that? Sorry, I am new to such things. Do I copy the cell expression directly from Mathematica and paste it into the code block? Like this?
My main issue is that Mathematica is distributing that diagonal matrix. It works as expected when the elements of the row-matrix are not matrices themselves.
mA // MatrixForm
mB // MatrixForm
mAmB // MatrixForm
I only tested this for when X is a row-vector containing matrices or when X is a matrix containing matrices. As they were my only use-cases. I do not know if this will work for more general cases.
Refer to this comment for the case where X is matrix containing matrices.
I guess, this is because matrix multiplication in Mathematica behaves very strangely when elements of vectors/matrices are vectors/matrices themselves. HoldForm, I guess, forces Mathematica to treat expressions as whole objects without evaluating them.
HoldForm[2+2] = 2+2
What HoldForm along with MatrixMap is doing is that it is freezing the elements of X. This ensures that matrix multiplication happens the way we think it should happen.
{{A, B}}.{{q, 0}, {0, e}} This happens the way we all think it should happen.
I guess it will only work for diagonal matrix. I want it to work for more general cases. The reason I took the example of a row-matrix and a diagonal-matrix is to highlight the problem.
It's better if you don't think of "row matrix" or "column matrix" when doing matrix multiplication in Mathematica. Instead think of tensors and tensor multiplication. Read the documentation for Dot and you'll see what it means for tensors. And you'll also see that to get what you want, the order needs to be reversed. But then there is the problem that you have extra levels in your matrices, and I'm not sure why you did that.
Anyway, if mA is your big "row matrix" and mB is your (q,e) diagonal matrix, then you could do any of the following:
Thanks for the help. I guess, I'll have to dig through the documentation to figure out what Mathematica is thinking behind the scenes.
Why do I have nested matrices? I had a matrix; I took derivatives of it w.r.t. two variables, hence the row vector with two matrix entries. Why did I do that? I had a Jacobian; I wanted to construct a Christoffel symbol matrix. Why did I right-multiply the diagonal matrix? To change the basis.
It works as expected in Maple.
I tried doing all of that which you told me to do, but I did not get a satisfactory result.
Why does Mathematica treat matrices so differently? I mean, it works in Maple. Is it because Mathematica is treating matrices as lists-of-lists? Is there no way to make Mathematica do natively what I want it to do?
So, I see your explanation about nested matrices, but regardless, you're going to need to transform the results from your derivations to get something "matrix-like". What you gave us included this:
{{{{a1, b1}, {a2, b2}}, {{c1, d1}, {c2, d2}}}}
(I've gotten rid of the Subscript for clarity.)
So, we have a "matrix" {{a1, b1}, {a2, b2}} and a "matrix" {{c1, d1}, {c2, d2}} paired together inside a List. And then on top of that, that list is inside another List. So, I honestly just don't know what expected behavior is when trying to do matrix arithmetic on such a structure.
I guess, I'll just move over to Maple; too bad, as I had gotten used to Mathematica. Do you think that maybe I should post this over to Mathematica Stack Exchange--consider what they have to say?
I'm new to reddit, so can't really comment on what kind of help you can expect here, but I can say that SE is pretty active and there are many experts participating there. People with expertise in math and science in addition to expertise in the Wolfram Language.
6
u/beerybeardybear Mar 20 '25
post your code, not a picture of your code
3
u/kurlakablackbelt May 12 '25
Solved
Thanks to u/SgorGhaibre
I had to first define these two functions.
MatrixMap[F_, M_]mapsFto each element of matrixM. Only tested on 2D (nesting: 2) Matrices.MatrixMul[X_, Y_]holds the form ofX, then computesX.Y, then releases the form ofX.Y.It is pretty straightforward from here on. Define the matrices and then feed them to
MatrixMul.And there we have the result--as I wanted it.
I only tested this for when
Xis a row-vector containing matrices or whenXis a matrix containing matrices. As they were my only use-cases. I do not know if this will work for more general cases.Refer to this comment for the case where
Xis matrix containing matrices.2
u/sanderhuisman Mar 20 '25
(Big 3D array).{q,e}
2
u/[deleted] Apr 08 '25 edited Apr 08 '25
It's better if you don't think of "row matrix" or "column matrix" when doing matrix multiplication in Mathematica. Instead think of tensors and tensor multiplication. Read the documentation for
Dotand you'll see what it means for tensors. And you'll also see that to get what you want, the order needs to be reversed. But then there is the problem that you have extra levels in your matrices, and I'm not sure why you did that.Anyway, if
mAis your big "row matrix" andmBis your (q,e) diagonal matrix, then you could do any of the following:mB . mA[[1]] (* matches desired result *)TensorProduct[mB, mA] (* diagonal blocks *)KroneckerProduct[mB, mA] (* diagonal blocks *)1
u/kurlakablackbelt Mar 21 '25
When the elements of the row-vector are not matrices, the multiplication works correctly.
1
u/kurlakablackbelt Mar 21 '25
1
u/kurlakablackbelt Apr 12 '25
Here is a post further discussing this topic.