Matrix multiplication confusion

   5506   4   1
User Avatar
Member
9 posts
Joined: 7月 2006
Offline
I'm little bit confused with matrix multiplication, I think I understand what is going on but I would like to double check.

Consider following two matrices and a vector
matrix2 A = {{1,1}, {-1,1}};
matrix2 B = {{1,2}, { 0,1}};
vector2 v = {1,0};

The source of all problems is that is does not matter if I multiply a matrix by a vector from right or left, i.e.
 A*v == v*A 
It always gives answer as if I multiply the matrix from the left. Thus vex code `A*v` is equivalent to mathematical `transpose(v)*A`. However, multiplication between matrices works fine and there is a difference between `A*B` and `B*A`.

This leads to an absurd fact that multiplication is not associative, i.e.
A*B*v == (A*B)*v != A*(B*v)

If I always keep vector on the left side then there is no problem, i.e.
v*A*B == (v*A)*B == v*(A*B)

Do I understand the problem fully or is there an additional catch? Am I safe if I always multiply matrices by a vector from the left side?
User Avatar
Member
2042 posts
Joined: 9月 2015
Online
I get the same answer regardless of operation order.

Edit:

But that only works if I return the result to a variable that is equivalent in size to the lesser of the two used.

To understand why it doesn't work in other cases ( asking for a return to a variable having the same number of components of the greater ), needs to take a look at how matrix mutliplication is done.

If you consider a matrix to be defined by n x n ( rows x columns ) then in any multiplication the number of columns of the first matrix needs to match the number of rows of the second matrix.

In (n1 x n2) muliplied times ( n3 x n4 ), n2 needs to equal n3.

So in your case you have a matrix2 ( 2 x 2 ) multiplied by ( 1 x 2 ).

So since 2 does not equal 1, there is no meaningful result.

In the example hip, I do get a result, but I suspect I do so with no problems because I'm only using a vector2 for the return.

So Houdini can ‘fill’ a vector2 in this case because it has enough information to do so. Trying to return the result to a Matrix2 fails because there is not enough information to do so.

You might want to review some references to Matrix multiplication to decide how you want to set up your code, here's a couple links.

https://www.youtube.com/watch?v=ebdfJwHM5vo&list=PLHXZ9OQGMqxfUl0tcqPNTJsb7R6BqSLo6&index=15 [www.youtube.com]
https://www.khanacademy.org/math/precalculus/precalc-matrices/multiplying-matrices-by-matrices/v/multiplying-a-matrix-by-a-matrix [www.khanacademy.org]
Edited by BabaJ - 2018年9月12日 13:04:17

Attachments:
Order of Matrix Multiplication.hiplc (43.5 KB)

User Avatar
Member
9 posts
Joined: 7月 2006
Offline
I really want to multiply matrix by a vector from the right side, e.g. multiply a matrix 2x2 by a column vector 2x1. If I understand it correctly, Houdini considers vectors as row vectors, e.g. 1x2, thus I should transpose the vector to convert it to a column vector, e.g. 2x1. However, calling the function `transpose` gives and error that a matrix is expected as an input.

This is really stupid…
User Avatar
Member
2042 posts
Joined: 9月 2015
Online
The function transpose only works with matrix, not vector.

http://www.sidefx.com/docs/houdini/vex/functions/transpose.html [www.sidefx.com]


If you want to do that, create an empty matrix with your vector as the first row, then use the transpose on that matrix.

'Transposing' a vector2 from 1x2 to 2x1 is still the same two floats in the same order.

To make a differentiation between a 1x2 and 2x1 additional information/format must be used; which is why I suggest you create a matrix from the vector2 so it can be transposed (stored in a different format - row represented by column).


https://chortle.ccsu.edu/VectorLessons/vmch13/vmch13_14.html [chortle.ccsu.edu]
User Avatar
スタッフ
99 posts
Joined: 2月 2021
Offline
A vector to rotate
vector v = @P;
Rotates 120 degrees about {1, 1, 1}
matrix3 A = {{0, 1, 0}, {0, 0, 1}, {1, 0, 0}};
Rotates -90 degrees about {1, 0, 0}
matrix3 B = {{1, 0, 0}, {0, 0, -1}, {0, 1, 0}};

If you want to rotate the vector by 120 and then -90...

The convention is to write this from right to left, meaning the vector is transformed by A and then by B.
v = B * A * v;
You would expect to have: BAv == (BA)v == B(Av). But in VEX: BAv == (BA)v != B(Av).

VEX actually write this from left to right.
v = v * A * B;
So you actually have: vAB == v(AB) == (vA)B.

Surprisingly, VEX does not care about the order when it is a multiplication between a vector and a matrix.
Effectively: vAB == v(AB) == (vA)B == (Av)B == AvB.
Mai Ao
Senior Technical Lead of SideFX Labs
youtube.com/@notverydarkmagic
  • Quick Links