# GLKMatrix4Multiply?

Discussion in 'iOS Programming' started by samdev, Jul 15, 2012.

1. ### samdev macrumors regular

Joined:
Sep 16, 2011
#1
Does GLKMatrix4Multiply (from GLKit framework) return AxB or BxA?

Because the SDK docs says it returns AxB, but I'm getting BxA.

You can compare the results with this online matrix caculator:
http://www.meta-calculator.com/online/

2. Jul 15, 2012
Last edited: Jul 15, 2012

### lloyddean macrumors 6502a

Joined:
May 10, 2009
Location:
Des Moines, WA
#2
You could check the source in "GLKit/GLKMatrix4.h" yourself to determine the answer.

3. ### samdev thread starter macrumors regular

Joined:
Sep 16, 2011
#3
I already looked at it. The function is unrolled, so it doesn't tell me much in terms of order.

Code:
```GLKMatrix4Multiply
Returns the product of two matrices.

GLKMatrix4 GLKMatrix4Multiply (
GLKMatrix4 matrixLeft,
GLKMatrix4 matrixRight
);
```
I don't understand why Apple would create a matrix multiply function that gives a BxA result.
All of my OpenGL math is screwed up.

4. ### samdev thread starter macrumors regular

Joined:
Sep 16, 2011
#4
The code that produces the same result can be found in the SDK example pARk/ARView.m:

Code:
```void multiplyMatrixAndMatrix(mat4f_t c, const mat4f_t a, const mat4f_t b)
{
uint8_t col, row, i;
memset(c, 0, 16*sizeof(float));

for (col = 0; col < 4; col++) {
for (row = 0; row < 4; row++) {
for (i = 0; i < 4; i++) {
c[col*4+row] += a[i*4+row]*b[col*4+i];
}
}
}
}
```
This also creates a BxA matrix.

This line here:
Can be written as:
For AxB, it must do this:
The whole GLK framework must be backwards or something. Argh.

5. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Sailing beyond the sunset
#5

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#6
7. ### firewood macrumors 604

Joined:
Jul 29, 2003
Location:
Silicon Valley
#7
Is this one of those row column things inherited from supercomputer Fortran/LINPACK math crunching code rather than newbie C conventions.

8. ### samdev thread starter macrumors regular

Joined:
Sep 16, 2011
#8
LH and RH coordinate systems can use the same matrix functions.

Matrix functions shouldn't even know or care how the elements are stored in memory.
Ideally, they should request a (row,col) element and do whatever they want with it.

So, the problem here is not related to how matrix elements are stored in memory.
It just gives a backwards result.

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
#9
Yeah, I wasn't sure that was the answer, but I thought I'd throw it out there. It's been many (, many) years since I was in my linear-algebra class and did matrix-multiplication.

10. Jul 17, 2012
Last edited: Jul 17, 2012

### samdev thread starter macrumors regular

Joined:
Sep 16, 2011
#10
No problem, it's just lack of documentation. I don't see it as a bug, because I usually prepare my
GL matrices in ZYX order.

So, GLKMatrix4Multiply makes sense here, but it doesn't make sense for generic matrix math.
I wasn't expecting a backwards result.

Thanks! Problem solved.