Implementation of linear algebra-matrix-point multiplication C and C + +

Source: Internet
Author: User

Principle Analysis:

Matrix multiplication is divided into point multiplication and fork multiplication, and this summary introduces the point multiplication. We know that A is multiplied by B to get a new set of values by multiplying each row of a by a column of B.

(The fill diagram here)

C + + language:

We first have to have a member function that extracts a row or column: (Please expand view)

    BOOL Getspecifiedrow (int index,vector<t> *vec);      Gets the index line element    bool Getspecifiedcolumn (int index,vector<t> *vec);//Gets the index column element

Get row:

View Code

Get column:

View Code

Next start the dot multiplication:

1, for (MxN) a matrix multiplied (NXJ) of the target matrix (m*j), the number of columns of a matrix is equal to the number of rows of the B matrix in order to carry out the point multiplication, so first of all to do two matrices to meet the requirements of the judgment.

2, for the 3*3 matrix: We first extract the first row of a matrix and the 123th column of the b matrix multiplied, get the first row of the target matrix to extract the second row of a matrix, and the b matrix, respectively, the 123th column, the second row of the target matrix ...

In conclusion, the point multiplication is divided into two steps:

    • Judging the legality of two matrices;
    • The K line of the A matrix is extracted and multiplied by the column I of the b Matrix, and the first column I of the target matrix is obtained.

The following two kinds of writing are the above ideas, the first one faster, occupy less memory, the second closer to People's thinking (the second folding please expand).

Template <typename t>Matrix<T> Matrix<t>::operator* (matrix<t> &matrix)//operator Overloading * Overloading for point multiplication{    /*Matrix leagality Check*/    if( This->m_icolumns! =matrix.getrows ()) {cout<<"operator* (): Input Ileagal"<<Endl; return* This; }        /*caculate Point Multiply*/Matrix<T>Outputmatrix; Vector<T>Tempvec;    T TempData;  for(intj=0; j<m_irows;j++)    {         for(intk=0; k<matrix.m_icolumns;k++) {TempData=0;  for(intI=0; i<m_icolumns;i++) {TempData+= This->m_vecmatrix[j][i] *Matrix.m_vecmatrix[i][k];        } tempvec.push_back (TempData);        } outputmatrix.addonerowtoback (Tempvec); Tempvec.clear (); //clear for next rows adding    }    returnOutputmatrix;}

View Code

C Language:

Implementation of linear algebra-matrix-point multiplication C and C + +

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.