Help with matrix multiplication

Started by
3 comments, last by duckflock 9 years, 5 months ago

I wasn't sure which topic to put this in, it is math but also programming. I was wondering if someone would be good enough to write a little c function, or java, etc. to show me how multiply matrices in this order. My request is please do NOT use an array of arrays. Just a single float[16] or float[4];

Matrices are 4 x 4 column-vector matrices stored in column-major order:

m[offset + 0] m[offset + 4] m[offset + 8] m[offset + 12]
m[offset + 1] m[offset + 5] m[offset + 9] m[offset + 13]
m[offset + 2] m[offset + 6] m[offset + 10] m[offset + 14]
m[offset + 3] m[offset + 7] m[offset + 11] m[offset + 15]

Vectors are 4 x 1 column vectors stored in order: v[offset + 0]
v[offset + 1]
v[offset + 2]
v[offset + 3]

The reason why I need this is because android MatrixMM and MatrixMV are written in native code and I need to reproduce them.

http://developer.android.com/reference/android/opengl/Matrix.html

Advertisement

To further elaborate, these are the android native methods In need to reproduce(in plain english so to speak), found here http://androidxref.com/source/xref/frameworks/base/core/jni/android/opengl/util.cpp

void multiplyMM(float* r, const float* lhs, const float* rhs)

{

for (int i=0 ; i<4 ; i++) {

register const float rhs_i0 = rhs[ I(i,0) ];

register float ri0 = lhs[ I(0,0) ] * rhs_i0;

register float ri1 = lhs[ I(0,1) ] * rhs_i0;

register float ri2 = lhs[ I(0,2) ] * rhs_i0;

register float ri3 = lhs[ I(0,3) ] * rhs_i0;

for (int j=1 ; j<4 ; j++) {

register const float rhs_ij = rhs[ I(i,j) ];

ri0 += lhs[ I(j,0) ] * rhs_ij;

ri1 += lhs[ I(j,1) ] * rhs_ij;

ri2 += lhs[ I(j,2) ] * rhs_ij;

ri3 += lhs[ I(j,3) ] * rhs_ij;

}

r[ I(i,0) ] = ri0;

r[ I(i,1) ] = ri1;

r[ I(i,2) ] = ri2;

r[ I(i,3) ] = ri3;

}

}

static

void util_multiplyMM(JNIEnv *env, jclass clazz,

jfloatArray result_ref, jint resultOffset,

jfloatArray lhs_ref, jint lhsOffset,

jfloatArray rhs_ref, jint rhsOffset) {

FloatArrayHelper resultMat(env, result_ref, resultOffset, 16);

FloatArrayHelper lhs(env, lhs_ref, lhsOffset, 16);

FloatArrayHelper rhs(env, rhs_ref, rhsOffset, 16);

bool checkOK = resultMat.check() && lhs.check() && rhs.check();

if ( !checkOK ) {

return;

}

resultMat.bind();

lhs.bind();

rhs.bind();

multiplyMM(resultMat.mData, lhs.mData, rhs.mData);

resultMat.commitChanges();

}

I believe these have already been wrapped inside the Java matrix library in the link you already posted.

If you are storing your matrix in a float array you can just use MatrixMM to have it computed with a provided float array. You shouldn't have to write any extraneous code as there are already wrappers over the native code on the link you provided. I personally haven't done much Java on Android but that method is static now inside the android.opengl.matrix class.

Does that answer your question or do you need more information?

As I said I need to reconstruct it.

I believe these have already been wrapped inside the Java matrix library in the link you already posted.

If you are storing your matrix in a float array you can just use MatrixMM to have it computed with a provided float array. You shouldn't have to write any extraneous code as there are already wrappers over the native code on the link you provided. I personally haven't done much Java on Android but that method is static now inside the android.opengl.matrix class.

Does that answer your question or do you need more information?

I don't understand the question (well, let's pretend it is a question). You've posted code to perform a matrix-vector multiply for a 4 dimensional space and now you want us to write similar code again?

Have you troubles understanding anything specific in multiplyMM ?

Do you know how to multiply 2 matrices?

Do you know how to map indices (x,y) into a linear index for an array?

What is your specific problem?

This topic is closed to new replies.

Advertisement