Does it matter if you're multiplying a matrix by a matrix rather than a matrix by a vector with the same information?

179 Views Asked by At

I'm really new to all of this and I'm 13 learning OpenGL. So I was writing a rotation function (3D) for an OpenGL project and I encountered a couple websites that claim that if I multiply the rotation matrix by a vector I would get the correct result. To do this I got the matrix that I was going to rotate and 'translated' its information into a vector. e.g $$ \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & 4 & 0 & 0\\ 0 & 0 & 2 & 0\\ 0 & 0 & 0 & 1 \end{bmatrix}\rightarrow \begin{bmatrix} 1 & 4 & 2 & 1 \end{bmatrix}. $$ After that I would multiply the rotation matrix by the vector and it did not work as intended. After this, I simply multiplied the Original Matrix by the Rotation Matrix and it worked as intended. Could I get an explanation on why this would happen when the vector has the same information as the original matrix?

Example: $$ \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & cos(45) & sin(45) & 0\\ 0 & -sin(45) & cos(45) & 0\\ 0 & 0 & 0 & 1 \end{bmatrix}* \begin{bmatrix} 0 & 4 & 1 & 1\\ \end{bmatrix} $$ $$ \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & 4 & 0 & 0\\ 0 & 0 & 2 & 0\\ 0 & 0 & 0 & 1 \end{bmatrix}* \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & cos(45) & sin(45) & 0\\ 0 & -sin(45) & cos(45) & 0\\ 0 & 0 & 0 & 1 \end{bmatrix} $$

Thanks.

Edit: After I finished multiplying the Matrix and the Vector (which I got a vector for the answer) I tried to convert it back to its old matrix form. e.g Matrix[0][0] = Vector[0]

2

There are 2 best solutions below

3
On BEST ANSWER

There’s no reason (that I know) why the two computations should give the same answer. In fact, the two computations don’t even give the same kind of result: the matrix-times-vector computation gives a vector result, and the matrix-times-matrix one gives a matrix.

Even if you convert your vector result back to a matrix (as in your edited question), there’s still no reason to expect the same results.

Just because two computations involve the same numbers, you shouldn’t expect them to give you the same results. After all, the computations “2+3” and “2*3” both involve 2 and 3, but they obviously don’t give the same result.

To transform points and vectors in OpenGL, you need to multiply them by matrices, not by vectors.

0
On

If you have a diagonal matrix (all the entries are on the diagonal), then you can get those diagonal entries by multiplying by a vector which is all 1s: $$\begin{bmatrix} a & 0 & 0 & 0\\ 0 & b & 0 & 0\\ 0 & 0 & c & 0\\ 0 & 0 & 0 & d \end{bmatrix}*\begin{bmatrix}1\\1\\1\\1\end{bmatrix} =\begin{bmatrix}a\\b\\c\\d \end{bmatrix} $$

Let's give these things some names: $$D = \begin{bmatrix} a & 0 & 0 & 0\\ 0 & b & 0 & 0\\ 0 & 0 & c & 0\\ 0 & 0 & 0 & d \end{bmatrix}$$ $$w = \begin{bmatrix}1\\1\\1\\1\end{bmatrix}$$ $$v = \begin{bmatrix}a\\b\\c\\d\end{bmatrix}$$ And let $M$ be some matrix we would like to apply to (i.e. multiply by) the vector $v$.

So you could directly calculate $M*v$, or since $v = D*w$, you could calculate $M*D*w$. Note that matrix multiplication is associative (i.e. $(M*D)*w = M*(D*w)$). One way to calculate this is to multiply $M*D$, then multiply the result by the vector $w$, which seems to be almost what you're trying to do. Note that when you multiply $M*D$, the result will probably not be a diagonal matrix anymore. Multiplying any matrix by $w$ adds up each row of the matrix to produce a vector, so in short you can recapture the entries of $M*v$ by multiplying $M*D$ and then adding up the rows to form a vector.

On the other hand, you're very close to an important fact about matrices: If you multiply a matrix $M$ by another matrix $N$ then you're in effect multiplying $M$ by each column of $N$.

In other words, if you represented the vector $v = \begin{bmatrix}a\\b\\c\\d\end{bmatrix}$ as the matrix $C = \begin{bmatrix} a & 0 & 0 & 0\\ b & 0 & 0 & 0\\ c & 0 & 0 & 0\\ d & 0 & 0 & 0 \end{bmatrix}$, then you can calculate $M*v$ by just calculating $M*C$ and taking the first column.

$M*C$ will be exactly the matrix $\begin{bmatrix} e & 0 & 0 & 0\\ f & 0 & 0 & 0\\ g & 0 & 0 & 0\\ h & 0 & 0 & 0 \end{bmatrix}$ where $M*v = \begin{bmatrix}e\\f\\g\\h\end{bmatrix}$.

On the third hand, these are mostly just mathematical amusements. I encourage you to make sure you can get the correct answer out of multiplying the rotation matrix by a vector.