Confused about row major matrix and pre multiplication

Started by
5 comments, last by uglybdavis 9 years, 1 month ago
I'm hoping someone could clear up a bit of matrix confusion i'm having. The breakdown might be because of my understanding of math or direct x but here goes.
As i understand Direct3D is often using a left handed coordinate system, row major matrices and row vectors with pre multiplication.
pre multiplication meaning that the vector being multiplied goes before the matrix.
IE: transformedVector = vector * matrix;
Now as far as i understand given matrices A * B, to calculate the matrix product of element 2, 3 you take the dot product of the second row of A and 3rd column of B. This means that A and B's inner dimensions must match, and their outer dimensions will be the size of the resulting matrix.
With the above rules in mind given a row vector (4, 1) and a transformation matrix (4, 4) the only way to get a row vector as the result is to use post multiplication. That is transformedVector = matrix * vector.
So, given this information how on earth does DirectX use pre multiplication? Do they not follow the row / column rules of the dot product and in effect transpose the vector or matrix?
Context:

float4 VertexShader_Tutorial_1(float4 inPos : POSITION ) : POSITION
{
    return mul(inPos, WorldViewProj );
};

Advertisement

Do they not follow the row / column rules of the dot product and in effect transpose the vector or matrix?

In HLSL the transfromation of a vector, when you multiply a matrix and a vector, the vector itself si not disinguised wheather it is a column vector 1xn or nx1. Technicaly when you multiply a vector and matrix, the switched order between them algebraisticly relates to A*b=A*b and b*A=AT*b. By switching order you transpose the transfrormation.

I see, thank you for clearing that up.
If i understand correctly, the transpose is a result of treating the vector as a column vector; hence the matrix is never actually transposed. Is that correct?
I have one more question regarding pre / post multiplication. I always assumed that the whole row v column order was what determined if pre or post multiplication is used (Due largely to my understanding of linear algebra being based on the differences in OpenGL & Direct X).
Now that i'm trying to build some more solid mathematical foundations i find my old understanding very chalenged.
If we have Matrix T (a translation matrix that translates by 10, 20 and 30)
and matrix S (A scale matrix that is a uniform scale of two)
And we want to concatinate the transformation so that the matrix translates first, and scales second we must calculate it as S x T.
This is assuming the same multiplication method as in the first post. I think because we scaled first then translated this is using post multiplication?
If correct, how come post multiplying two row major matrices works in math but Direct 3D uses pre-multiplication? Is this due to some implicit like the vector X matrix multiplication? Am i missing something trivial in my mental model of how this works?
Or perhaps is my pre / post terminology wrong when it comes to multiplying matrices?

If i understand correctly, the transpose is a result of treating the vector as a column vector; hence the matrix is never actually transposed. Is that correct?
I have one more question regarding pre / post multiplication. I always assumed that the whole row v column order was what determined if pre or post multiplication is used (Due largely to my understanding of linear algebra being based on the differences in OpenGL & Direct X).
Now that i'm trying to build some more solid mathematical foundations i find my old understanding very chalenged.
If we have Matrix T (a translation matrix that translates by 10, 20 and 30)
and matrix S (A scale matrix that is a uniform scale of two)
And we want to concatinate the transformation so that the matrix translates first, and scales second we must calculate it as S x T.
This is assuming the same multiplication method as in the first post. I think because we scaled first then translated this is using post multiplication?
If correct, how come post multiplying two row major matrices works in math but Direct 3D uses pre-multiplication? Is this due to some implicit like the vector X matrix multiplication? Am i missing something trivial in my mental model of how this works?
Or perhaps is my pre / post terminology wrong when it comes to multiplying matrices?

Yes, that's correct that matrix itself does not transform, only the resulting vector is transformed as by transponed matrix. The multipliction of matricies itself does reflect the actual multiply order as for the resulting transformation that is A(B(v))=B*A*v but HLSL and GLSL, though having the same technical multiply operator, can differ on picking wheather row-up way or colum-up way to pick columns to multiply the pre/post vector with. If they differ, what I do not know becouse I haven't used HLSL for ages, then in GLSL you might have (A(B(v)))=B*A*v and in HLSL (A(B(v)))=v*B*A.

That's very helpful.

If i understand what you said correctly, if two matrices (A and B) together; to find element 2,3 of the resulting matrix

Row major multiplication: take the dot product row 2 of matrix A and column 3 of matrix B

Column major multiplication: take the dot product of column 2 of matrix A, and row 3 of matrix B

----------------------------------------

So, last question (i hope). Does the above mean that the formula for matrix product (For matrices F & G):

image17.gif?_subject_uid=48598159&w=AACC

This will always take dot product the i-th row and j-th column of the two matrices, the only difference being that row major = FG, while column major = GF

OR, do the subscripts get a different meaning? Like in row major it's the i-th row and j-th column, where with column major it's the j-th row and i-th column?

matricies multiplication is consistent in algebraic sence, it does not make any big point to think wheather they are row or column multilpied, since the vector transformation can be transpone, so that in case the matrix multiplication is first case, you have row matricies, or column matricies in second, but this affect only transpone character of transformed vectors, so you can decide wheather you use v*M or M*v to synchronize with any of cases of algebraic columns in matricies, what stems from the way you multiply them.

If you have column matricies, meaning you transform a vector by doting rows with the vector (adding columns multiplied by scalar from vector), you should use column dot row when multiiplying the matricies. If you use the row-dot-column, just transform the vector by adding rows then, to keep in tact the algebraic intent.

Thank you!

That has answered all of my questions. Thanks again, you have no idea how helpful that all was!

This topic is closed to new replies.

Advertisement