0

I'm creating a simple library function for basic matrix operations and one of it is matrix addition,as IMO the function seems to have too many parameters passed to it, but on the other hand i feel all the parameters passed are required and there is no other way to get them in the function directly.

so here's the code snippet :

  void mat_add(int r1, int c1, int m1[][c1], int r2, int c2, int m2[][c2],int res[][c1])
{
    if (r1 == r2 && c1 == c2)
    {
        for (int i = 0; i < r1; i++)
        {
            for (int j = 0; j < c1; j++)
            {
                res[i][j] = m1[i][j] + m2[i][j];
            }
        }
    }
    else
    {
        fprintf(stderr, "ERROR:matrix sizes are not same\n");
        exit(-1);
    }
}

Of course, its the real necessary parameters should be passed,
but I really dont know what i am doing is just too much,it can be reduced or not,
so is there any way you can reduce the number of parameters passed and get the required data(#rows and #columns) from those itself in the function?

  • Shouldn't that be `if (r1 == c2 && c1 == r2)`? I.e., rows of m1 must match columns of m2 and cols of m1 must match rows of m2? – Paul Ogilvie Apr 16 '16 at 18:05
  • Pick one (1) language. It makes a difference. In any case, the minimum number is zero (0). – Ulrich Eckhardt Apr 16 '16 at 18:05
  • Please consider using some punctuation in the future. – MikeC Apr 16 '16 at 18:07
  • 1
    You could make a matrix a `struct` with the number of columns and rows in there that would cut four different parameters in your function – DarthRubik Apr 16 '16 at 18:08
  • @PaulOgilvie No it's just addition now ..so basically the dimensions of both the matrix should be same.. – Sumanth K gowda Apr 16 '16 at 18:13
  • @UlrichEckhardt Yeah I guess Java makes it easier...but I want to do it in c.. But r u sure that at any case min number would be 0..? – Sumanth K gowda Apr 16 '16 at 18:15
  • @MikeC Pardon me for that, I'll try to take care of it in future....thanks – Sumanth K gowda Apr 16 '16 at 18:16
  • Yes, I'm sure, you can reduce the number to zero using globals. What did that answer give you? Maybe you're asking the wrong question or need to make it a bit more precise. While at it, remove the C++ tag then! – Ulrich Eckhardt Apr 16 '16 at 18:17
  • @DarthRubik Yeah using structure was one of the option I had,but every time you are creating an matrix you just should do it in a struct(create it ) and pass it..I was just thinking of going in usual way like..people have matrix or while they create a matrix thy just create 2d arrays directly ,so for any operations they just send the 2d array but not the struct..but yeah using struct is one of the good opinion.. – Sumanth K gowda Apr 16 '16 at 18:23
  • @UlrichEckhardt Actually I'm creating a library function for this,so it would be necessary to pass some parameters as far as I know....why isn't this appropriate for c++, it's just creating header files right which even exists in c++ and almost similar to c..sorry if I'm wrong.. – Sumanth K gowda Apr 16 '16 at 18:28
  • By your logic, you should add every language's tag that is in some way able to interface with C. No. You should make your goal to write good C code so that should be the only language tag present. – Ulrich Eckhardt Apr 16 '16 at 18:36
  • @UlrichEckhardt Haha fine... I'll change it..thanks.. actually I know nothing about c++..lol.. – Sumanth K gowda Apr 16 '16 at 18:38
  • @UlrichEckhardt Is matrix tag OK with this ques.? – Sumanth K gowda Apr 16 '16 at 18:40
  • If this is to be a part of a library, you really should look into describing each matrix using a structure. I've had excellent results with [mine](https://stackoverflow.com/a/34862940/1475978). – Nominal Animal Apr 16 '16 at 20:40
  • @NominalAnimal I got almost of it..it was great..but if you don't mind can you please explain exactly what owner sturct is and it's attributes.. And even what is rowstride and colstride ..? – Sumanth K gowda Apr 17 '16 at 07:26
  • With my structures, several matrices can refer to the exact same data. (GSL supports "views", but mine are first-class matrices, not a special type.) The data itself is reference-counted, and stored in the owner structure. Each matrix must be destroyed when it is no longer needed, but the owner structures are destroyed when the last matrix referring to it is destroyed. Colstride is the pointer delta when advancing one column, and rowstride is the delta when advancing one row, in the matrix data. – Nominal Animal Apr 17 '16 at 08:09
  • @NominalAnimal OK so you are basically storing the 2d array in 1d array in data[] attribute of owner struct by any one of the row major or column major order..? If so then how will you initialize the array for exact elements..say you are taking the input from the user.. so he/she should do it in your way specified there...? – Sumanth K gowda Apr 17 '16 at 08:57
  • Yes, but not limited to row or column major order. Any regular rectangular order will work. Use row major order by default, unless it is going to be used as a right matrix in matrix multiplication. I'd read the number of rows and number of columns first, then use a double loop (outer over rows, inner over columns) to read each element. For `matrix m;`, element on row `r`, column `c`, is `m.origin[r*m.rowstride + c*m.colstride]`. It is a `double`, so you can use either `fscanf(input, "%lf", &(m.origin[r*m.rowstride+c*m.colstride]))` or `strtod()`. – Nominal Animal Apr 18 '16 at 01:32
  • @NominalAnimal Ohh k ...great it is an amazing answer.. I even shared it with some of my friends they felt it was a great approach...thank you soo much....see you around.. – Sumanth K gowda Apr 18 '16 at 15:06
  • It is especially powerful when you do either linear algebra (and would love to have one or more diagonals as vectors), or some kind of kernel filtering (in which case you can use a larger matrix as the original source, and the real data as a smaller window inside it, so that you can actually access negative rows and columns). The reference counting makes using the functions really easy and intuitive: if you remember to destroy each matrix when you no longer need it, you won't leak memory, no matter how complicated your matrix "views" are. It All Just Works. In my experience, that is. – Nominal Animal Apr 19 '16 at 01:56
  • @NominalAnimal yeah it is a good approach... Thank you.. – Sumanth K gowda Apr 19 '16 at 02:43

1 Answers1

1

The minimum number of parameters is 1. A struct or a pointer to a struct with all parameters that you need to perform an operation.

Andrei Smeltsov
  • 528
  • 6
  • 14
  • @DarthRubik Yeah using structure was one of the option I had,but every time you are creating an matrix you just should do it in a struct(create it ) and pass it..I was just thinking of going in usual way like..people have matrix or while they create a matrix thy just create 2d arrays directly ,so for any operations they just send the 2d array but not the struct..but yeah using struct is one of the good opinion....so I'm just seeing if doing it normally(using just 2d arrays) and reducing the parameters can be done or not.. – Sumanth K gowda Apr 16 '16 at 18:24