template multiplications of two matrix

I have the folowing code that allow me multiplicate matrix but I want it to void the size's of each matrix.

Something like that:

1
2
template<class T,class S,class R>
void mult(const T& A,const S& B, R& C); 


How I could calculate the sizes of each matrix?

Thanks.


This is my code:

1
2
3
4
5
6
7
8
9
10
11
//A*B=C sizeX(files de A), sizeY=columnes de B sizeY
template<class T,class S,class R>
void mult(const T& A,const S& B, R& C,int sizeAX,int sizeAY,int sizeBY){//,const double & B,double & C){

    for(int i=0;i<sizeAX;i++)
        for(int j=0;j<sizeBY;j++)
            for(int k=0;k<sizeAY;k++)
                C[i][j]+=A[i][k]*B[k][j];
        //imprimir(C,sizeAX,sizeBY);

}
Integers can be template arguments.
The matrix class could be
1
2
3
4
5
template<int W, int H>
struct Matrix{
   float m[W][H];
   //...
};
and the multiplication function could be
1
2
3
4
template<int W1, int H1, int W2> //H2 is equal to W1
void Multiply( const Matrix<W1, H1>& a, const Matrix<W2, W1>& b, Matrix<W2, H1>& c ){
   /...
}
With this struct how I can define?

This doesnt work =(

1
2
3

Matrix<4,1> B={1,2,3,4};
How does it not work? an error message or wrong values?
= {..} kind of initialization is only valid for classes that don't have constructors or virtual methods (there might be something about private members too. i don't remember.)
Though if you try Matrix<2, 2> a = {1, 2, 3, 4};, you'll get a matrix
1 3
2 4
,I think. In that case, switch W and H in the class member array definition.
Now al work.

Thanks you very much.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

template<int W, int H>
struct Matrix{
   double m[W][H];
};

template<int sizeX,int sizeY>
void imprimir(const Matrix<sizeX,sizeY> & A){
    for(int i=0;i<sizeX;i++){
        for(int j=0;j<sizeY;j++)
            cout << A.m[i][j]<<" ";
        cout << endl;
    }
}

//A*B=C sizeX(files de A), sizeY=columnes de B sizeY
template<int sizeAX,int sizeAY, int sizeBY>
void mult(const Matrix<sizeAX,sizeAY>& A,const Matrix<sizeAY,sizeBY>& B, Matrix<sizeAX,sizeBY>& C){//,const double & B,double & C){

    for(int i=0;i<sizeAX;i++)
        for(int j=0;j<sizeBY;j++)
            for(int k=0;k<sizeAY;k++)
                C.m[i][j]+=A.m[i][k]*B.m[k][j];
        imprimir(C);

}
int main(){
    Matrix<4,1> B={1,2,3,4};
    Matrix<4,4> A={0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
    Matrix<4,1> C={0,0,0,0};

    //cout << B.m[0][0];
    //A(4x4) B(4x1) C(4x1)
    mult(A,B,C);
    cout <<"\n";



}
Topic archived. No new replies allowed.