Thread: how to initialize a n-dimensional matrix and return it

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    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.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    start with a one dimensional array and go from there.

    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

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    FYI, it is generally better to use vector over dynamic arrays, but it doesn't sound like you have a choice in this assignment.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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).
    Last edited by anon; 07-28-2007 at 01:14 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    We're not talking about n-dimensional array but rather n-dimension matrices.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    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 !

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by robatino View Post
    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).
    Last edited by grumpy; 07-29-2007 at 03:00 AM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > 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?

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    Quote Originally Posted by robatino View Post
    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.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by bawet View Post
    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.
    That was answered in the second post in the thread.

  12. #12
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    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.
    Last edited by CodeMonkey; 07-29-2007 at 10:00 PM. Reason: yikes
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  13. #13
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    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?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM