# Is my 4x4 matrix multiplication on a 1D float array correct?

I'm trying to develop my own math library to use on a Voxel engine, but I'm worried about Matrices multiplication. Everywhere I saw people using 4x4 Matrices on a 2D Float Array but I'm using a 1D Float Array because of performance needs.

    public float[] multiplicate(float[] factor){
float[] result = new float[factor.length];
for (int i = 0, a = 0, b = 0; i < factor.length; i++){
a = (i/4)*4;
b = (i%4);
result[i] = (mat4f[a] * factor[b]) + (mat4f[a+1] * factor[b+4]) + (mat4f[a+2] * factor[b+8]) + (mat4f[a+3] * factor[b+12]);
}
return result;
}


EDIT: I'm using a 1D array to store the 4x4 Vector. If I use 16 float variables instead, should I gain in performance or memory usage? The code should be like that:

    r00 = a00*b00+a01*b10+a02*b20+a03*b30;
r01 = a00*b01+a01*b11+a02*b21+a03*b31;
r02 = a00*b02+a01*b12+a02*b22+a03*b32;
r03 = a00*b03+a01*b13+a02*b23+a03*b33;

r10 = a10*b00+a11*b10+a12*b20+a13*b30;
r11 = a10*b01+a11*b11+a12*b21+a13*b31;
r12 = a10*b02+a11*b12+a12*b22+a13*b32;
r13 = a10*b03+a11*b13+a12*b23+a13*b33;

r20 = a20*b00+a21*b10+a22*b20+a23*b30;
r21 = a20*b01+a21*b11+a22*b21+a23*b31;
r22 = a20*b02+a21*b12+a22*b22+a23*b32;
r23 = a20*b03+a21*b13+a22*b23+a23*b33;

r30 = a30*b00+a31*b10+a32*b20+a33*b30;
r31 = a30*b01+a31*b11+a32*b21+a33*b31;
r32 = a30*b02+a31*b12+a32*b22+a33*b32;
r33 = a30*b03+a31*b13+a32*b23+a33*b33;


Where r00~r33 are 16 float variables where I store the result and later set to my 16 float main fields...The variables a00~a33 and b00~b33 are the factors variables.

So if I use fixed fields instead of a float array, should I gain in performance or memory usage?

• in the init part of your loop: a= 0, b=0 is superfluous since you assign values right at the beginning of the loop body Commented Jun 18, 2013 at 22:10
• This is the same as if I declared the variables int a, b; before the loop...no? Commented Jun 18, 2013 at 22:40
• @AfonsoLage but it makes it harder to read your for loop. We get the idea that a and b affect the loop some how, but instead they are just local variables. There by making it harder to read. Commented Jun 19, 2013 at 1:45
• To answer your question about performance gains you could use a simple test to time how long it takes to do your multiplication. You'd want to have it perform the task a few thousand times to check for performance. In terms of memory though there is no difference (usually) since it will allocate the same amount of memory if it was a 1d array or a 16d array. In the end it is how many elements you have. Commented Jun 19, 2013 at 1:48

My final code is:

public float[] multiplicate(float[] factor){
int a = 0;
int b = 0;
float[] result = new float[factor.length];
for (int i = 0; i < factor.length; i++){
a = (i/4)*4;
b = (i%4);
result[i] = (mat4f[a] * factor[b]) + (mat4f[a+1] * factor[b+4]) + (mat4f[a+2] * factor[b+8]) + (mat4f[a+3] * factor[b+12]);
}
return result;
}


I'll keeping using 1D float array because I'll need to use a FloatBuffer to send it to OpenGL (using LWJGL). So i'll get less overhead by calling one FloatBuffer.put(myArrFloatVar) then FloatBuffer.put(new float[]{r00, r01...r33} or event calling FloatBuffer.put(r00) 16 times.