Somos uma comunidade de intercâmbio. Por favor, ajude-nos com a subida ** 1 ** um novo documento ou um que queremos baixar:

OU DOWNLOAD IMEDIATAMENTE

C++ Templates -- A Simple Example Rajanikanth Jammalamadaka This article describes a C++ code for performing basic operations on matrices using templates. That means we can create a matrix of any data type using one line and call the respective functions. The code listing opmatrix.h is the header file in which the matrix class is described. Note that the number of rows and columns is hard coded in the header file. So, in the current code, a matrix of two rows and two columns has been created. These numbers can be changed for matrices of bigger dimensions. Also for convenience’s sake, this code works only for square matrices (matrices which have the same number of rows and columns). The header file defines the matrix as a two dimensional vector. A vector is a container in C++ which is very similar to an array in the C language but is more sophisticated (it manages its own memory and you can call a number of functions to perform useful operations.) The functions readm() and printm() are used to read in the elements of the matrix and to print the matrix. Note that the readm() and printm() functions use the this pointer. The this pointer stores the address of the current object. For example, if we declare an object of the matrix class of type int like this matrix a; then the this pointer will contain the address of the object a, i.e. this = &a Therefore, saying a.readme() is equivalent to saying for(int i = 0; i < ROWS; i++) for(int j = 0; j < COLS; j++) cin >> (&a)->s[i][j]; The overloaded operators +,-,* and ~ are declared as friend functions of the matrix, so that they can access the elements of the matrix (which are private) in order to perform the operations. The overloaded operator ~ is the transpose operator. The + operator operates on two matrices and adds the corresponding elements of the two matrices and prints the result. The - operator is similar to the + operates except that it subtracts the elements of two matrices, instead of adding them. The * operator needs some explanation. Matrix multiplication works only if the number of columns of the first matrix is equal to the number of rows of the second matrix. For example, if we have to multiply matrices a and b, then the number of columns of matrix a must be equal to the number of rows of matrix b. In our case this is not a problem

because we are dealing with square matrices(whose sizes are fixed once the variables ROWS and COLUMNS are initialized), so the number of columns of first matrix will always be equal to the number of rows of the second matrix. In order to understand how the * operator works, let us consider a simple example a=

1 2 3 4

b=

5 6 7 8

19 22 43 50 Now, both a and b are of dimensions 2 by 2. Therefore, their product will be of dimension 2 by 2. The first element of a*b is gotten by summing the product of the corresponding elements of the first row of matrix a and first column of matrix b i.e. (a*b)11 = 1*5 + 2*7 = 5 + 14 = 19. (a*b)ij denotes the element at the intersection of ith row and jth column of the product matrix a*b. Similarly, the (a*b)12 is gotten by summing the product of the corresponding elements of the first row of matrix a and second column of matrix b. Similarly the other two elements can be obtained. Now, a*b =

The ~ operator transposes the elements of a matrix. By transpose, we mean interchanging the rows and columns of a matrix. So, a matrix (A)ij after the transpose operation becomes (A)ji. For example the matrix

1 2 after transposing becomes 3 4

1 3 . 2 4 The above examples may seem to be trivial, but they were purposefully made trivial in order to understand the concepts of basic matrix operations. If the matrices were of large dimensions, it wouldn' t be a trivial task to multiply them manually. In the results section, operations are performed on two matrices each of dimension 4 by 4. It is here that operator overloading proves to be most useful. For example, if we have to find the transpose of a+b*c, all we need to do is ~(a+b*c) and store this in a matrix and print the resultant matrix using the printm() function.

Also note that each operator accepts a constant reference to the matrix, this is because we want each operator to perform its function without modifying the original matrix which was given to it. Rajanikanth Jammalamadaka opmatrix.h // C++ header file #include #include using std::cin; using std::cout; using std::vector; using std::endl; const int ROWS = 2; const int COLS = 2; template class matrix { //declare a vector of vectors of type T vector< vector > s ; public: //Initialize the size of s to ROWS by COLS matrix(): s(ROWS, vector(COLS)) {} void readm(); void printm(); //declare the operators +,-,*,~ as friends and with return type matrix friend matrix operator+(const matrix&, const matrix&); friend matrix operator-(const matrix&, const matrix&); friend matrix operator*(const matrix&, const matrix&); friend matrix operator~(const matrix&); };

template void matrix::readm() { for(int i = 0; i < ROWS; i++) for(int j = 0; j < COLS; j++) cin >> this->s[i][j];

}

}

template void matrix::printm() { for(int i = 0; i < ROWS; i++) { for(int j = 0; j < COLS; j++) couts[i][j]

Lihat lebih banyak...
because we are dealing with square matrices(whose sizes are fixed once the variables ROWS and COLUMNS are initialized), so the number of columns of first matrix will always be equal to the number of rows of the second matrix. In order to understand how the * operator works, let us consider a simple example a=

1 2 3 4

b=

5 6 7 8

19 22 43 50 Now, both a and b are of dimensions 2 by 2. Therefore, their product will be of dimension 2 by 2. The first element of a*b is gotten by summing the product of the corresponding elements of the first row of matrix a and first column of matrix b i.e. (a*b)11 = 1*5 + 2*7 = 5 + 14 = 19. (a*b)ij denotes the element at the intersection of ith row and jth column of the product matrix a*b. Similarly, the (a*b)12 is gotten by summing the product of the corresponding elements of the first row of matrix a and second column of matrix b. Similarly the other two elements can be obtained. Now, a*b =

The ~ operator transposes the elements of a matrix. By transpose, we mean interchanging the rows and columns of a matrix. So, a matrix (A)ij after the transpose operation becomes (A)ji. For example the matrix

1 2 after transposing becomes 3 4

1 3 . 2 4 The above examples may seem to be trivial, but they were purposefully made trivial in order to understand the concepts of basic matrix operations. If the matrices were of large dimensions, it wouldn' t be a trivial task to multiply them manually. In the results section, operations are performed on two matrices each of dimension 4 by 4. It is here that operator overloading proves to be most useful. For example, if we have to find the transpose of a+b*c, all we need to do is ~(a+b*c) and store this in a matrix and print the resultant matrix using the printm() function.

Also note that each operator accepts a constant reference to the matrix, this is because we want each operator to perform its function without modifying the original matrix which was given to it. Rajanikanth Jammalamadaka opmatrix.h // C++ header file #include #include using std::cin; using std::cout; using std::vector; using std::endl; const int ROWS = 2; const int COLS = 2; template class matrix { //declare a vector of vectors of type T vector< vector > s ; public: //Initialize the size of s to ROWS by COLS matrix(): s(ROWS, vector(COLS)) {} void readm(); void printm(); //declare the operators +,-,*,~ as friends and with return type matrix friend matrix operator+(const matrix&, const matrix&); friend matrix operator-(const matrix&, const matrix&); friend matrix operator*(const matrix&, const matrix&); friend matrix operator~(const matrix&); };

template void matrix::readm() { for(int i = 0; i < ROWS; i++) for(int j = 0; j < COLS; j++) cin >> this->s[i][j];

}

}

template void matrix::printm() { for(int i = 0; i < ROWS; i++) { for(int j = 0; j < COLS; j++) couts[i][j]

Somos uma comunidade de intercâmbio. Por favor, ajude-nos com a subida ** 1 ** um novo documento ou um que queremos baixar:

OU DOWNLOAD IMEDIATAMENTE