3
$\begingroup$

I am using a game engine where the library only provides 3x3 matrices with the multiplication and inverse operation. I could build my own matrix library to provide larger matrices, but it would be less efficient to do so. Is there any clever mathematical way I could use 3x3 matrices with the limited operations they provide to easily simulate some larger square matrix?

Edit: Technically they do support other operations than the ones I mentioned (such as initialization from quaternion), but I wasn't sure of its relevance. You can view its documentation here.

$\endgroup$
  • 1
    $\begingroup$ Exactly what dimension matrix do you want to use? If it's a multiple of $3$, then block matrices could work. Also, this looks like Lua and just Googling "lua matrix library" gives me Lua Matrix and Numeric Lua, so you might not need to build your own matrix library. $\endgroup$ – Noble Mushtak Jun 29 '16 at 2:15
  • $\begingroup$ A 9x9 matrix would work fine. $\endgroup$ – CaptainObvious Jun 29 '16 at 2:16
0
$\begingroup$

OK, so it's been a LONG time since I've written Lua code, so I'm just going to write this as best as I can, but there might be syntax errors here. Basically, the way, we're going to represent a 9x9 matrix is as a 3x3 matrix of 3x3 matrices. This is called a block matrix.

function block_matrix_sum(CFrame_matrix1, CFrame_matrix2):
    --[[
    CFrame_matrix1 and CFrame_matrix2 are both multi-dimensional 3x3
    arrays of CFrames, which I assume are 3x3 matrices.
    The return of this function is the product of these two matrices
    as 3x3 array of CFrames, just like the inputs.
    ]]--
    sum = {}
    for i=1,3 do
        --Add the ith row to sum:
        table.insert(sum, {})
        for j=1,3 do
            --Make the jth element of the ith row of the sum
            --simply the sum of the two corresponding elements in the inputs.
            table.insert(sum[i], CFrame_matrix1[i][j]+CFrame_matrix2[i][j])
        end
    end
    --Finally, return the sum.
    return sum

function block_matrix_multiplication(CFrame_matrix1, CFrame_matrix2):
    --[[
    CFrame_matrix1 and CFrame_matrix2 are both multi-dimensional 3x3
    arrays of CFrames, which I assume are 3x3 matrices.
    The return of this function is the product of these two matrices
    as 3x3 array of CFrames, just like the inputs.
    ]]--
    product = {}
    for i=1,3 do
        --Add the ith row to product:
        table.insert(product, {})
        for j=1,3 do
            --Add the jth element in the ith row to product:
            table.insert(product[i], CFrame.new(0, 0, 0))
            --Calculate this element of the matrix:
            for k=1,3 do
                product[i][j] += CFrame_matrix1[i][k]+CFrame_matrix2[k][j]
            end
        end
    end
    --Finally, return the product:
    return product

Inverses in block matrices are more complicated and I'm not quite sure how to do this with a 3x3 block partition.

| cite | improve this answer | |
$\endgroup$
0
$\begingroup$

Block matrix multiplication will work.

$\begin{bmatrix} A&B\\ C&D\end{bmatrix}\cdot \begin{bmatrix} E&F\\G&H\end{bmatrix} = \begin{bmatrix}AE+BG & AF+BH\\CE+DG&CF+DH\end{bmatrix}$

In the above, each of $A,B,\dots$ are matrices. Say, for example $3\times 3$ matrices. Building up from there, one could easily simulate products of matrices of dimensions which are multiples of three. One could also use a row and column (or rows and columns) of all zeroes except a $1$ on the main diagonal to narrow it further to a nonmultiple of three.

Inverses will prove to be more of a challenge.

Given some conditions on the dimensions of each and the singularity of each matrix, (A must be square and invertible, (D-CA^{-1}B)$ must be invertible)

$\begin{bmatrix}A&B\\C&D\end{bmatrix}^{-1} = \begin{bmatrix} (A-BD^{-1}C)^{-1}&-A^{-1}B(D-CA^{-1}B)^{-1}\\-D^{-1}C(A-BD^{-1}C)^{-1}&(D-CA^{-1}B)^{-1}\end{bmatrix}$

Addition should be straightforward as well.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.