0

Here I am trying to write a library like function for the matrix multiplication. And this function should support all the datatype such as float, int, etc. that's why I have used Template here. But, I am having a difficult time passing 2d arrays to the function.

Q) void matrix_mul(T a[][2], T b[][2], T c[][2], ll p, ll q, ll r) how can I pass 2d arrays without any need to pass second parameter i.e., T a[][2]?

Q) I want to write these functions in another file and then import it into the main file, just like how we import standard libraries?

PS: I am new to the CPP. Kindly direct me to the resources if these questions have already been answered. TIA

template <class T>
void matrix_mul(T a[][2], T b[][2], T c[][2], ll p, ll q, ll r){
    for (ll i = 0; i < p; ++i)
        for (ll j = 0; j < q; ++j)
            for (ll k = 0; k < r; ++k)
                c[i][j] += a[i][k] * b[k][j];
}
int main(){
    io;
    ll p = 2, q = 2, r = 2;
    ll a[2][2] = {{1, 1}, {1, 1}};
    ll b[2][2] = {{1, 1}, {1, 1}};
    ll c[2][2] = {0};

    for (ll i = 0; i < p; ++i)
        for (ll j = 0; j < r; ++j)
            c[i][j] = 0;

    matrix_mul(a, b, c, p, q, r);

    for (ll i = 0; i < p; ++i){
        for (ll j = 0; j < r; ++j)
            cout << c[i][j] << "\t";
        cout << nl;
    }
    return 0;   
}

UPDATE: After giving C++ a read, I was able to come up with a working solution (find below).

ssp4all
  • 371
  • 2
  • 11
  • You could use [`std::array`](https://en.cppreference.com/w/cpp/container/array) – Indiana Kernick Feb 29 '20 at 03:13
  • Regarding both questions: sorry, C++ does not work this way. The types of all variables must be fully specified, and templates must be defined in header files. That's how C++ works. – Sam Varshavchik Feb 29 '20 at 03:17
  • 2
    Please learn C++ by working through a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Your second question especially suggest to me that you are lacking some basics about the language and the code is written in bad style suggesting that you are basing it of some "competitive programming" sites or similar. – walnut Feb 29 '20 at 03:21
  • Instead of `T a[][2]` of a fundamental type, look to using STL containers such as `std::vector`, e.g. `std::vector>`. The STL containers have the size information contained with the object itself. See [std::vector](https://en.cppreference.com/w/cpp/container/vector) – David C. Rankin Feb 29 '20 at 03:33

2 Answers2

0

Q) void matrix_mul(T a[][2], T b[][2], T c[][2], ll p, ll q, ll r) how can I pass 2d arrays without any need to pass second parameter i.e., T a[][2]?

Use references, and each dimension size as other template parameters

template<class T, ll  N, ll M>
void matrix_mul(T (&a)[N][2], T (&b)[N][M], T (&c)[N][M], ll p, ll q, ll r)

Values for these parametere will be deduced, the same way that type is deduced.

Q) I want to write these functions in another file and then import it into the main file, just like how we import standard libraries?

Standard libraries have all templates defined in header files, not in translation units that compile to linked object files. So you have to keep inline implementation in header file, not in .cpp file.

export template was meant to solve this, but it never worked and was removed from C++.

One possible way is to instantiate the template with all types you need in .cpp, and heave only declaration in header, but then it would fail with parameters you have not foreseen. For your case it is not an options, since you'll need to foresee all types and all sizes.

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
0

This is how I solve the above problem-

  • Define template in a header file .
  • Import the above-mentioned header file in the main.cpp #include "mat.hpp".
  • Used vector representation to overcome size specification.

I have posted a complete solution here https://github.com/ssp4all/Matrix-Multiplication-and-Transpose

ssp4all
  • 371
  • 2
  • 11