Fixing Dimension Mismatch in Matrix Multiplication #4179
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request addresses a dimension mismatch issue in matrix multiplication. The original code attempts to multiply a 4x4 transformation matrix (mat) by a 3-element vertex array (verts), resulting in an invalid operation. The proposed fix involves ensuring that the vertex array has a fourth element representing the homogeneous coordinate before performing the matrix multiplication.
Changes Made
The original code: np.array([mat @ np.array([verts[i], verts[i + 1], verts[i + 2]]) for i in range(0, len(verts), 3)])
the updated code: np.delete((mat @ np.hstack((verts, np.ones((len(verts), 1)))).T).T, -1, axis=1)
Explanation
To resolve the dimension mismatch, a modification has been made to ensure that each vertex array has a fourth element (homogeneous coordinate) before the matrix multiplication. This is achieved by appending a column of ones to the verts array.
np.ones((len(verts), 1)): This creates a column vector of ones with a length equal to the number of rows in the verts array. This is done to represent the homogeneous coordinates
np.hstack((verts, np.ones((len(verts), 1)))): This horizontally stacks the verts array with the column vector of ones. This is essentially adding an extra column to verts to represent homogeneous coordinates.
(mat @ np.hstack((verts, np.ones((len(verts), 1)))).T: The .T transposes the vertecies to match the dimension of the transformation matrix and perform a valid multiplication
mat @ np.hstack((verts, np.ones((len(verts), 1)))): This performs matrix multiplication between the matrix mat and the stacked array
(mat @ np.hstack((verts, np.ones((len(verts), 1)))).T).T: The second .T transposes the matrix back to its original form.
np.delete(..., -1, axis=1): This deletes the last column from the result of the matrix multiplication.
Example
mat [
[ 0.0, -1.0, 0.0, 0.0],
[ 1.0, 0.0, 0.0, -0.175],
[ 0.0, 0.0, 1.0, 0.0],
[ 0.0, 0.0, 0.0, 1.0]
]
verts [ [ 8.35, 0.175, 0. ],
[ 8.35, 0.175, 5.638],
[ 0. , 0.175, 5.638],
[ 0. , 0.175, 0. ],
[ 0. , -0.175, 5.638],
[ 0. , -0.175, 0. ],
[ 8.35, -0.175, 5.638],
[ 8.35, -0.175, 0. ]
]
result [ [-0.175, 8.175, 0. ],
[-0.175, 8.175, 5.638],
[-0.175, -0.175, 5.638],
[-0.175, -0.175, 0. ],
[ 0.175, -0.175, 5.638],
[ 0.175, -0.175, 0. ],
[ 0.175, 8.175, 5.638],
[ 0.175, 8.175, 0. ]
]
Testing
The fix has been tested with various vertex arrays and transformation matrices to ensure correct behavior.
Code compiles successfully
Tests pass
Documentation updating, not necessary
Other ways to solve the problem:
(mat @ np.hstack((verts, np.ones((len(verts), 1)))).T)[:3, :].T
(mat @ np.hstack((verts, np.ones((len(verts), 1)))).T).T[:, :-1]
(mat @ np.column_stack((verts, np.ones(verts.shape[0]))).T).T[:, :-1]