# how to initialize a n-dimensional matrix and return it

• 07-28-2007
bawet
how to initialize a n-dimensional matrix and return it
Can anybody help me write a function like
Code:

```func ( int n) { }```
and inside the function I need to initialize an n-dimensional matrix, and return the same.

or if any one can refer me some book or links where i can learn to do the above it will be of great help to me.
• 07-28-2007
robwhit

you're going to have to use loops and dynamic memory allocation.

http://www.cprogramming.com/tutorial/lesson3.html
http://www.cprogramming.com/tutorial/lesson6.html
http://www.cprogramming.com/tutorial/lesson8.html
• 07-28-2007
Daved
FYI, it is generally better to use vector over dynamic arrays, but it doesn't sound like you have a choice in this assignment.
• 07-28-2007
anon
Is it even possible? Something could be done recursively, but what would this function return? A simple pointer and not "n levels of indirection" pointer?

Also, what would be the size of each dimension?

Are you sure you aren't misreading the assignment?

Now I might be wrong, but I'm completely dumbfounded by this assignment... I can't even think of a sane situation where one would use a n-dimensional array where n is a variable...

The most I have used was a 3-dimensional static array (hides in shame).
• 07-28-2007
Desolation
We're not talking about n-dimensional array but rather n-dimension matrices.
• 07-29-2007
bawet
First of all its a self imposed assignment.

second thing I need to initialize n-dimensional matrix not array.

third I do give myself liberty to use array or vector, and I am expecting from myself a recursive solution and from you guys a little help :)

and so far no luck with my neurons !
• 07-29-2007
robatino
By definition, a matrix is 2-dimensional. Presumably you're talking about an n x n matrix. Since you're free to do it any way you want, you should consider defining a Matrix class, and letting the dimensions m and n be data members (or just use one data member if you're only considering square matrices). You can handle allocation/deallocation in the constructor/destructor. If efficiency matters, you should use a 1-dimensional array or vector with m*n elements, and pretend it's two-dimensional by letting a[i][j] in the matrix correspond to i + j*n in the array/vector. This simplifies memory management and reduces memory fragmentation in case you want to have millions of matrices simultaneously.
• 07-29-2007
grumpy
Quote:

Originally Posted by robatino
By definition, a matrix is 2-dimensional.

As a matter of fact, the mathematical definition of a matrix is "a rectangular array", and the corresponding mathematical definition of "rectangular" is "designating a geometric coordinate system with mutually perpendicular axes" or the equivalent "having a base or section in the form of a rectangle". A matrix can therefore be n-dimensional where n is zero or more (a zero dimensional matrix represents a single value).
• 07-29-2007
robatino
> A matrix can therefore be n-dimensional where n exceeds 0 (a one dimensional matrix is a single value).
Wouldn't a single value correspond to n == 0, a row or column matrix to n == 1 (if the length of the row/column is > 1), and any other matrix to n == 2?
• 07-29-2007
bawet
Quote:

Originally Posted by robatino
By definition, a matrix is 2-dimensional. Presumably you're talking about an n x n matrix. Since you're free to do it any way you want, you should consider defining a Matrix class, and letting the dimensions m and n be data members (or just use one data member if you're only considering square matrices). You can handle allocation/deallocation in the constructor/destructor. If efficiency matters, you should use a 1-dimensional array or vector with m*n elements, and pretend it's two-dimensional by letting a[i][j] in the matrix correspond to i + j*n in the array/vector. This simplifies memory management and reduces memory fragmentation in case you want to have millions of matrices simultaneously.

Thanks for some explicit explanations, which i assumed the readers to know already.
But your explanation didnt help me to get anywhere ... I am still stuck !

I am talking about initializing n-dimensional matrix (square matrix), As far as i can see it can be done with vector or arrays.

I followed your idea of using a matrix class
Code:

```class matrix { private : int n; public : matrix(int i) { } };```
But I still dont know what to write inside the constructor so that i will have n-dimensional matrix (where each dimension each of length n). And there is a difference between millions of matrices and a matrix with million dimension.
• 07-29-2007
grumpy
Quote:

Originally Posted by bawet
But I still dont know what to write inside the constructor so that i will have n-dimensional matrix (where each dimension each of length n). And there is a difference between millions of matrices and a matrix with million dimension.

• 07-29-2007
CodeMonkey
If your aim is to simulate the mathematical object called a "matrix" of n dimensions, then I would suggest creating a class that keeps track of those "points" only that the user has modified. Operations could then be simulated with formulae. This way, you don't have the problem of how to arrange this arbitrary dimension in memory.
Then you're only problem is the irritating mathematics that will be involved with your very generalized operations (products, transformations); and, of course, should you reference many points, the memory load will become great.

Code:

```template <class T, const unsigned int D> class basic_tuplet {       T components[D];     public:             const unsigned int dimension;             basic_tuplet() : dimension(D) {/*empty*/}             T & x(unsigned int index)       {       return components[index - 1];       } }; //operator == //operator <```
and then
Code:

```template<class T1, class T2, const unsigned int D> class basic_matrix {     std::map< basic_tuplet<T1,D> , T2  > data;     public:         T2 & operator() (basic_tuplet<T1,D> & pos) { return data[pos]; }         T2 & operator() (T1, ...);  //can you mix variable arguments with templates? That would be cool.         T2 resultant();  //generalized to n dimensions? Good luck. }; //inner_product() //dot_product()```
That's how I'd do it. I'm just a hobbyist. Hopefully someone has a better idea.
• 07-30-2007
CodeMonkey
Say. As I read, I seem to find that the use of <cstdarg> in C++ frowned upon. I can see why -- it's not necessary, and is not typesafe. However, suppose I wanted to implement my a matrix class such that operator() will take as many arguments as there are dimensions specified by the template. So that:
Code:

```matrix<dimension(5), double>  tesseract; tesseract(1,5,3,0,1) = 4.5567;```
Is safe code? I implemented it with va_args, but I have no way of knowing how many arguments the user stuck in. This seems to be an unavoidable truth.

I figured I could have the function accept as arguments a type of class that casts implicity as an integer, and can also serve as an "arguments stop here" specifier. But this adds notational inconvenience.
Code:

`tesseract(1,5,3,0,1,end_components) = 4.5567;`
I don't like this. If I were to go through that much trouble, then I might as well use the obvious C++ solution:
Code:

```typedef basic_tuplet<5> tuplet; tesseract(tuplet(1,5,3,0,1)) = 4.5567;```
Yet I prefer (aesthetically) the old non-typesafe code. Where is the compromise?