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?
Matrix Multipication
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.
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:
[fixed]
//A dynamic "pseudo 2 dimensional" array of ints
//Create the main array of int pointers
int **MyArray=new int *[xLimit];
//Now for each int pointer, allocate a new
//array of ints which are the actual elements
for (int i=0; i<xLimit; i++)
MyArray[ i ]=new int[yLimit];
//Assign to the 5,3 element:
MyArray[5][3]=10;
//Clean up
//First delete all the actual elements
for (i=0; i<xLimit; i++)
delete []MyArray[ i ];
//Now delete the array of int pointers
delete []MyArray;
[/fixed]
Now, that acts like a multidimensional array in the way it is accessed (MyArray[xElement][yElement]), even though it is an array of arrays.
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.
Dan East
[fixed]
//A dynamic "pseudo 2 dimensional" array of ints
//Create the main array of int pointers
int **MyArray=new int *[xLimit];
//Now for each int pointer, allocate a new
//array of ints which are the actual elements
for (int i=0; i<xLimit; i++)
MyArray[ i ]=new int[yLimit];
//Assign to the 5,3 element:
MyArray[5][3]=10;
//Clean up
//First delete all the actual elements
for (i=0; i<xLimit; i++)
delete []MyArray[ i ];
//Now delete the array of int pointers
delete []MyArray;
[/fixed]
Now, that acts like a multidimensional array in the way it is accessed (MyArray[xElement][yElement]), even though it is an array of arrays.
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.
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:
[fixed]
//Allocate a one dimensional array large enough
//for all the elements of the two dimensional array.
int *MyArray=new int[xLimit*yLimit];
//Assign to the 5,3 element:
MyArray[5*xLimit+3]=10;
//Clean up
delete []MyArray;
[/fixed]
Both techniques have their advantages.
Dan East
[fixed]
//Allocate a one dimensional array large enough
//for all the elements of the two dimensional array.
int *MyArray=new int[xLimit*yLimit];
//Assign to the 5,3 element:
MyArray[5*xLimit+3]=10;
//Clean up
delete []MyArray;
[/fixed]
Both techniques have their advantages.
Dan East
I feel like I will look smart if I answer, so I will.
Dan is correct, as I understand the question, though I too am not sure EXACTLY what you want to know. Assuming that it is indeed C++ that you are using.
Though I would take the appoach of allocating space in as a singledimensional array and treat it like a multidimensional array. Not that it would be faster...because it may or may not be.
I guess the only issue is that there are 2 math operations to get to the desired location.
Dan is correct, as I understand the question, though I too am not sure EXACTLY what you want to know. Assuming that it is indeed C++ that you are using.
Though I would take the appoach of allocating space in as a singledimensional array and treat it like a multidimensional array. Not that it would be faster...because it may or may not be.
- Code:
//THE ARRAY
int *x = (int *)calloc(XSIZE * YSIZE, sizeof(int));
//ACCESSING
x[yLoc * XSIZE + xLoc] = 1;
I guess the only issue is that there are 2 math operations to get to the desired location.
Use a buffer allocated with malloc and when you need to change its size, use realloc.
[fixed]
float *pMatrix = (float*)malloc(cnRows * cnCols * sizeof(float));
[/fixed]
Use the second technique Dan mentioned for accessing the elements of the matrix.
To resize use realloc:
[fixed]
pMatrix = (float*)realloc(pMatrix, cnNewRows * cnNewCols * sizeof(float));
[/fixed]
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?
[fixed]
float *pMatrix = (float*)malloc(cnRows * cnCols * sizeof(float));
[/fixed]
Use the second technique Dan mentioned for accessing the elements of the matrix.
To resize use realloc:
[fixed]
pMatrix = (float*)realloc(pMatrix, cnNewRows * cnNewCols * sizeof(float));
[/fixed]
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?
Thanks you guys, you pretty much told me all i needed to know.
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.
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.
Digby,
Was it supposed to be a pun that you named the variable "pMatrix" ?
I feel like such a geek for pointing that out.
Was it supposed to be a pun that you named the variable "pMatrix" ?
I feel like such a geek for pointing that out.
Do you use alot of matrix';s when programming games? Like do you do matrix transfermations and stuff?
RICoder,
You really should get away from the computer for a while. Your geekitude is showing. That's just Hungarian notation for a pointer variable.
Robert,
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.
You really should get away from the computer for a while. Your geekitude is showing. That's just Hungarian notation for a pointer variable.
Robert,
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.
What do you mean some hardware supports it directly? How would you take advantage of this?
No current Pocket PC hardware supports it directly so if that's what you're targetting you can relax.
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!).
The StrongARM does have an integer multiply and accumulate instruction so that would help with matrix operations using fixed-point data.
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!).
The StrongARM does have an integer multiply and accumulate instruction so that would help with matrix operations using fixed-point data.
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?
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...)
Well, then there is the x86 stuff that was introduced with MMX for concatinated integer math...not really matrix, but single vector.
Why not use an "off-the-shelf" matrix library? Many moons ago I used one that I found on aminet, I did a quick search and they have a few libraries there now. I think the one I used was http://aminet.net/pub/aminet/dev/c/matrix.lha it supports dynamic as you would expect.
Chris
Chris