DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: C++ Matrix

  1. #1
    Join Date
    Nov 2007

    C++ Matrix

    Hi Guys,

    I want to create a matrix with a dynamic size, any idea how to do it?


  2. #2
    Join Date
    Dec 2003
    generally you will want to make a single dimensional array and index it as 2-d. This allows you to resize it (realloc or just make it huge to begin with), and if you need to re-arrange the dimensions its very very easy without any reallocations or anything, you just do it.

    Additionally, several packages use this format (matlab, lapack, etc) and many routines can be made more efficient with careful coding. For example matrix multiply can be arranged to pass through both arrays iteratively without double loops or page faults created by poor indexing.

    How to index:
    array[desired_row*max_cols + desired_col];

    You can do the same thing with a valarray or vector, of course, but there are good and bad things that tag along with the heavy hitting classes ... you will have to decide for yourself if things like slice on valarray or some of the vector toys are good enough to justify the complexity.

  3. #3
    Join Date
    Oct 2005
    if you are good enough with pointers use double pointer :
    int **p;
    this means a pointer that point to a pointer so I can write :
    p = new int*[7]; here I have a pointer that pointes to 7 different pointers ..
    later I should specify the width :
    for(int i=0; i<7; i++) p[i] = new int[WIDTH];

    can you imagine it now ? each p[i] has WIDTH number of colums, and i is from 0 to 6 , ie 7 so if WIDTH=5 say we have 7 X 5 matrix ... anb very easy to change the size at any time by just deleting some pointers ar reallocating new locations ... just you have a problem with the new allocation , be craful to not lose the matrix data for example :
    int *ptr=new int; //address say 0x4444 after this line write :
    int *ptr=new int[5]; //address not nessasry 0x4444

    to avoid this you may need before reallocating the pointer to use a dummy pointer that pointes to the old matrix, and after the relocation test if the address is the same (in most cases) so you are safe. otherwise you need to move the data first to the new location .
    Programmer&Cracker CS

  4. #4
    Join Date
    Dec 2003
    double pointers are almost impossible to resize(unless you have all day to wait on a realloc command, yuck), thats why I recommended a single dimensional. Unless you are in the realm of square matrices (and, a lot of them are!) you will often have to transpose or resize for various reasons.

    Again, if the pure array seems ugly a valarray with clever use of slices is a decent approach.

  5. #5
    Join Date
    Oct 2005
    I don't see a problem with double pointers, if I'm lucky enough and have free space the address won't change and no need to reallocations, this is most of the time ... but if I want to grow a static array as much as the user want I'll finally return again to the reallocation so the pointer and its flexibility may help more I think, if and only if I'm good using pointers and I know what's I'm doing.
    Programmer&Cracker CS

  6. #6
    Join Date
    Dec 2003
    the nature of linear algebra is, unless you in the square matrix world, you will have a lot of reshapes and resizes (reshape is dimension change, resize is raw number of bytes used to store data) due to multiply, transpose, etc. Many of the more efficient algorithms involve adding a column or row to the problem. It may not matter for school sized problems (50X50 or less) but when you get into 500X500 sized matrices (which are not that large, really, but are common enough in non specialized work), a resize or reshape with the double pointers will cost you more time than most of the math that you will do on it! But the main thing is that most of the common software expects a single dimensional array, all of lapack and blas etc. do as well as matlab if you interact with it.

  7. #7
    Join Date
    Dec 2003
    I took a crack at the valarray side just to see how it did. The result was a routine that is twice as slow as the C array version, largely due to the creation of all the objects in the loop. I do not know how to create a slice once and modify it as needed, nor how to take the sliced valarrays into pre-existing ones to reduce all the overhead of the object creation. If we can tweak it to do those things, odds are very good that it can compete. Any suggestions from our stl guru's? Naturally later we will want to wrap up a matrix class with rows and cols as members etc, but this was just a rough cut at it.

    void mul(valarray<double> x, int xr, int xc, valarray<double> y, int yr, int yc)
    int i,j;
    valarray<double> result;
    result.resize(xr*yc, 0.0);
    int dx = 0;

    for(i = 0; i < xr; i++)
    for(j = 0; j < yc; j++)
    slice s(j,yr,yc);
    valarray<double> YC = y[s]; //get a column of Y
    slice s2(i*xc,2,1);
    valarray<double> XR = x[s2]; /*Get a row of X (can we skip and just multiply XY using a part of X instead, since the rows are already in a friendly format??) */
    valarray<double> tmp = XR*YC; //multiply, sum, and save
    result[dx++] = tmp.sum();
    //for(i = 0; i < xr*yc; i++)
    // cout << result[i] << " ";

Similar Threads

  1. Matrix Problem
    By Peter_APIIT in forum C++
    Replies: 18
    Last Post: 06-09-2007, 04:32 AM
  2. sorting
    By krazykrisi in forum C++
    Replies: 3
    Last Post: 01-25-2007, 12:59 PM
  3. Matrix Problem in C++
    By emmanuella in forum C++
    Replies: 2
    Last Post: 11-23-2006, 06:07 PM
  4. matrix operations in VB
    By goldenOzaru1906 in forum VB Classic
    Replies: 0
    Last Post: 11-17-2005, 04:59 PM
  5. Free ASP.NET Web Matrix Design/Editor Tool Released
    By ASPSmith Training in forum dotnet.announcements
    Replies: 0
    Last Post: 06-18-2002, 03:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center