Page 1 of 2

Matrix Multipication

PostPosted: Oct 11, 2001 @ 7:26pm
by RwGast
I want to multiply two matrix's togather, that are stored in multidimensional arrays. But i want the user to be able to specify the size of the matrix. How could i "ReDim"(for those of you familiar with BASIC) a multidimensional array on runtime?

Re: Matrix Multipication

PostPosted: Oct 11, 2001 @ 10:59pm
by RwGast
That really sucks when you think someone like digby or dan has probably given you a nice intellgent answer. But instead its just a babling ideot.

Re: Matrix Multipication

PostPosted: Oct 11, 2001 @ 11:46pm
by Dan East
I don't think I follow your question 100%. It appears you want to know how to dynamically create a 2 dimensional array at runtime. When it comes to true multidimensional arrays in C / C++, you have to specify the array bounds at compile time (one dimensional arrays do not have that limitation - you can dynamically allocate them any size you like). So, since we can dynamically create one dimensional arrays, we can create an array of pointers, then create an array of elements for each pointer:<br>[fixed]<br>//A dynamic "pseudo 2 dimensional" array of ints<br>//Create the main array of int pointers<br>int **MyArray=new int *[xLimit];<br><br>//Now for each int pointer, allocate a new<br>//array of ints which are the actual elements<br>for (int i=0; i<xLimit; i++)<br>  MyArray[ i ]=new int[yLimit];<br><br>//Assign to the 5,3 element:<br>MyArray[5][3]=10;<br><br>//Clean up<br>//First delete all the actual elements<br>for (i=0; i<xLimit; i++)<br>  delete []MyArray[ i ];<br>//Now delete the array of int pointers<br>delete []MyArray;<br>[/fixed]<br><br>Now, that acts like a multidimensional array in the way it is accessed (MyArray[xElement][yElement]), even though it is an array of arrays.<br> The caveats are that more code is required to allocate / deallocate, and although I haven't looked at the actual assembly that example generates compared to a real 2 dimensional array, it probably requires more instructions to access an element than accessing an element from a true multidimensional array.<br><br>Dan East

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 12:06am
by Dan East
Here's another method. This gives you simpler allocation / deallocation, but requires a less intuitive method of accessing an element. Basically you just use a 1 dimensional array large enough to hold the number of elements of the two dimensional array, and do extra calculation to find the desired element:<br>[fixed]<br>//Allocate a one dimensional array large enough<br>//for all the elements of the two dimensional array.<br>int *MyArray=new int[xLimit*yLimit];<br><br>//Assign to the 5,3 element:<br>MyArray[5*xLimit+3]=10;<br><br>//Clean up<br>delete []MyArray;<br>[/fixed]<br><br>Both techniques have their advantages.<br><br>Dan East

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 12:22am
by RICoder

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 3:01am
by Digby
Use a buffer allocated with malloc and when you need to change its size, use realloc.<br>[fixed]<br>float *pMatrix = (float*)malloc(cnRows * cnCols * sizeof(float));<br>[/fixed]<br>Use the second technique Dan mentioned for accessing the elements of the matrix.<br><br>To resize use realloc:<br>[fixed]<br>pMatrix = (float*)realloc(pMatrix, cnNewRows * cnNewCols * sizeof(float));<br>[/fixed]<br><br>The code isn't high-performance or something that you'd like to put in your game loop but it works. Is this for a class assignment?<br><br>

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 11:22am
by RwGast
Thanks you guys, you pretty much told me all i needed to know. <br>Digby this is not for a class assignment, im just writing my first ppc app in eVC and this is what i decided to do.

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 4:00pm
by RICoder
Digby,<br>  Was it supposed to be a pun that you named the variable "pMatrix" ?<br><br>  I feel like such a geek for pointing that out.

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 5:15pm
by RwGast
Do you use alot of matrix';s when programming games? Like do you do matrix transfermations and stuff?

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 5:44pm
by Digby
RICoder,<br>You really should get away from the computer for a while.  Your geekitude is showing.  That's just Hungarian notation for a pointer variable.<br><br>Robert,<br>Yes, you use matrices quite a bit with 3D game programming.  Matrices make it easy to combine multiple transformations by concatenation.  Some hardware supports matrix operations directly so it's nice if your app uses matrices and can take advantage of it.<br>

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 6:57pm
by RwGast
What do you mean some hardware supports it directly? How would you take advantage of this?

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 9:02pm
by Digby
No current Pocket PC hardware supports it directly so if that's what you're targetting you can relax.<br><br>Modern PC display cards support matrix concatentation and multiplication by a vector.  The Hitachi SH4 supports some 4 component vector multiplications in hardware, as do the current crop of CPUs from Intel (via SSE) and AMD (3DNow!).<br><br>The StrongARM does have an integer multiply and accumulate instruction so that would help with matrix operations using fixed-point data.<br><br>

Re: Matrix Multipication

PostPosted: Oct 12, 2001 @ 9:21pm
by RwGast
No i mean how do you implement this if i wanted to do some matrix functions id just use multi dim arrays and right i function to multiply them, i hardly think that thats how you would do it the cpu suporrted way. Are these directX functions?

Re: Matrix Multipication

PostPosted: Oct 13, 2001 @ 3:34am
by MirekCz
robert:nor pocketpc nor pc cpu support hardware matrix transformations/whatever. Only modern graphics cards for PC (with t&l) support it. On cpu you either make it the "normal" way - using mul commands (all 80x86,all PPC cpus) or use an additional set of commands to speed things up (modern 80x86 processors - those commands sets are named SSE , 3DNOW and all their extensions...)

Re: Matrix Multipication

PostPosted: Oct 14, 2001 @ 4:27pm
by RICoder
Well, then there is the x86 stuff that was introduced with MMX for concatinated integer math...not really matrix, but single vector.