Matrix Multiplication Template Class header file C ++

Source: Internet
Author: User

This is followed by an implementation of the multiplication sequence of the query matrix in the previous article. This class can accept input and calculate the final result.

Because I recently learned C ++, I naturally wrote this thing as a class and implemented it in template mode. It is indeed very neat.

// Matrix. h <br/> # ifndef matrix_h _ <br/> # define matrix_h _ </P> <p> # include <iostream> </P> <p> template <class t> class Matrix <br/>{< br/> PRIVATE: <br/> T ** m_matrix; <br/> int m_x, m_y; <br/> long m_count; <br/> // find the optimal order <br/> char ** m_matrixchainorder (const T * const matrixchain, const int mcsize ); <br/> // calculate <br/> matrix m_matrixoptimalparens (const char * const record, Const matrix * const marray, const int I, const Int J); <br/> Public: <br/> matrix (void ); <br/> matrix (const T * const input, const int isize, const int x = 1, const int y = 1 ); <br/> matrix (const Matrix & M); <br/> void optimalmultiplication (const matrix * const marray, const int msize ); <br/> Matrix & operator = (const Matrix & M); <br/> void show (void) const; <br/> ~ Matrix (void); <br/> friend Matrix Operator * (const Matrix & ml, const Matrix & Mr); <br/> }; </P> <p> template <class T> char ** matrix <t>: m_matrixchainorder (const T * const matrixchain, const int mcsize) <br/>{< br/> int rsize = mcsize-1; </P> <p> int ** storage = new int * [rsize]; <br/> for (INT I = 0; I <rsize; ++ I) <br/> storage [I] = new int [rsize]; <br/> for (INT I = 0; I <rsize; ++ I) <br/> Storage [I] [I] = 0; <br/> char ** record = new char * [rsize]; <br/> for (INT I = 0; I <rsize; ++ I) <br/> record [I] = new char [rsize]; <br/> for (INT I = 2; I <= rsize; ++ I) <br/>{< br/> for (Int J = 0; j <= rsize-I; ++ J) <br/> {<br/> storage [J] [J + I-1] = infinity; <br/> for (int K = J; k <j + I-1; ++ K) <br/> {<br/> int temp = storage [J] [k] + storage [k + 1] [J + I-1] + matrixchain [J] * matri Xchain [k + 1] * matrixchain [I + J]; <br/> If (temp <storage [J] [J + I-1]) <br/> {<br/> storage [J] [J + I-1] = temp; <br/> record [J] [J + I-1] = k + 1; <br/>}< br/> STD: cout <"optimal times: "<storage [0] [rsize-1] <STD: Endl; <br/> for (INT I = 0; I <rsize; ++ I) <br/> Delete [] storage [I]; <br/> Delete [] storage; </P> <p> return record; <br/>}</P> <p> template <class t> Matrix <t>: m_matrixoptimalparens (const char * const record, const matrix * const marray, const int I, const Int J) <br/>{< br/> if (I = J) <br/> return marray [I]; <br/> else <br/> return m_matrixoptimalparens (record, marray, I, record [I] [J]-1) * m_matrixoptimalparens (record, marray, record [I] [J], J ); <br/>}</P> <p> template <class T> matrix <t>: matrix (void) <br/>{< br/> m_x = M_y = 0; <br/> m_count = 0l; <br/> m_matrix = NULL; <br/>}</P> <p> template <class T> matrix <t>: matrix (const T * const input, const int isize, const int X, const int y) <br/>{< br/> If (x * y! = Isize) <br/>{< br/> m_x = m_y = 0; <br/> m_count = 0l; <br/> m_matrix = NULL; <br/> STD:: cout <"error [0]/n"; <br/>}< br/> m_matrix = new T * [x]; <br/> for (INT I = 0; I <X; ++ I) <br/> m_matrix [I] = new T [y]; <br/> int K = 0; <br/> for (INT I = 0; I <X; ++ I) <br/> {<br/> for (Int J = 0; j <Y; ++ J) <br/> m_matrix [I] [J] = input [k ++]; <br/>}< br/> m_x = x; <br/> m_y = y; <br/> m_count = 0l; <br/>} </P> <p> template <class T> matrix <t>: matrix (const Matrix & M) <br/>{< br/> m_x = m. m_x; <br/> m_matrix = new T * [m_x]; <br/> m_y = m. m_y; <br/> for (INT I = 0; I <m_x; ++ I) <br/> m_matrix [I] = new T [m_y]; <br/> for (INT I = 0; I <m_x; ++ I) <br/> {<br/> for (Int J = 0; j <m_y; ++ J) <br/> m_matrix [I] [J] = m. m_matrix [I] [J]; <br/>}< br/> m_count = m. m_count; <br/>}</P> <p> template <class T> void Matrix <T>: optimalmultiplication (const matrix * const marray, const int msize) <br/>{< br/> T * matrixchain = new T [msize + 1]; <br/> for (INT I = 0; I <msize; ++ I) <br/> matrixchain [I] = marray [I]. m_x; <br/> matrixchain [msize] = marray [msize-1]. m_y; <br/> char ** record = new char * [msize]; <br/> for (INT I = 0; I <msize; ++ I) <br/> record [I] = new char [msize]; <br/> record = m_matrixchainorder (MA Trixchain, msize + 1); <br/> Delete [] matrixchain; <br/> * This = m_matrixoptimalparens (record, marray, 0, msize-1 ); <br/> for (INT I = 0; I <msize; ++ I) <br/> Delete [] record [I]; <br/> Delete [] record; <br/>}</P> <p> template <class T> matrix <t> & matrix <t>: Operator = (const Matrix & M) <br/>{< br/> If (this = & M) <br/> return * This; <br/> for (INT I = 0; I <m_x; ++ I) <br/> Delete [] m_matrix [I]; <Br/> Delete [] m_matrix; <br/> m_x = m. m_x; <br/> m_matrix = new T * [m_x]; <br/> m_y = m. m_y; <br/> for (INT I = 0; I <m_x; ++ I) <br/> m_matrix [I] = new T [m_y]; <br/> for (INT I = 0; I <m_x; ++ I) <br/> {<br/> for (Int J = 0; j <m_y; ++ J) <br/> m_matrix [I] [J] = m. m_matrix [I] [J]; <br/>}< br/> m_count = m. m_count; </P> <p> return * This; <br/>}</P> <p> template <class T> void matrix <t> :: show (void) cons T <br/>{< br/> Using STD: cout; <br/> Using STD: Endl; <br/> for (INT I = 0; I <m_x; ++ I) <br/>{< br/> for (Int J = 0; j <m_y; ++ J) <br/> cout <m_matrix [I] [J] <""; <br/> cout <Endl; <br/>}< br/> cout <"actual times:" <m_count <Endl; <br/>}</P> <p> template <class T> matrix <t> ::~ Matrix (void) <br/>{< br/> for (INT I = 0; I <m_x; ++ I) <br/> Delete [] m_matrix [I]; <br/> Delete [] m_matrix; <br/>}</P> <p> matrix <int> operator * (const matrix <int> & ml, const matrix <int> & Mr) <br/>{< br/> matrix <int> newm; </P> <p> newm. m_x = ML. m_x; <br/> newm. m_matrix = new int * [newm. m_x]; <br/> newm. m_y = mr. m_y; <br/> for (INT I = 0; I <newm. m_x; ++ I) <br/> newm. m_matrix [I] = new int [newm. m_y]; <br/> newm. m_count = 0l; <br/> for (INT I = 0; I <ml. m_x; ++ I) <br/>{< br/> for (Int J = 0; j <mr. m_y; ++ J) <br/>{< br/> newm. m_matrix [I] [J] = 0; <br/> for (int K = 0; k <mr. m_x; ++ K) <br/>{< br/> newm. m_matrix [I] [J] + = ML. m_matrix [I] [k] * Mr. m_matrix [k] [J]; <br/> ++ newm. m_count; <br/>}< br/> newm. m_count + = ML. m_count + mr. m_count; </P> <p> return newm; <br/>}</P> <p> matrix <double> operator * (const matrix <double> & ml, const matrix <double> & Mr) <br/>{< br/> matrix <double> newm; </P> <p> newm. m_x = ML. m_x; <br/> newm. m_matrix = new double * [newm. m_x]; <br/> newm. m_y = mr. m_y; <br/> for (INT I = 0; I <newm. m_x; ++ I) <br/> newm. m_matrix [I] = new double [newm. m_y]; <br/> newm. m_count = 0l; <br/> for (INT I = 0; I <ml. m_x; ++ I) <br/>{< br/> for (Int J = 0; j <mr. m_y; ++ J) <br/>{< br/> newm. m_matrix [I] [J] = 0; <br/> for (int K = 0; k <mr. m_x; ++ K) <br/>{< br/> newm. m_matrix [I] [J] + = ML. m_matrix [I] [k] * Mr. m_matrix [k] [J]; <br/> ++ newm. m_count; <br/>}< br/> newm. m_count + = ML. m_count + mr. m_count; </P> <p> return newm; <br/>}</P> <p> # endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.