[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

12/02/2005 at 11:03PM PST, ID: 21651713
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.4

Matrix inverse and transpose ...

Asked by meow00 in C++ Programming Language

Tags: matrix, inverse, transpose

Hello experts,

    I am working on a matrix class for practice. I just added a inverse and transpose function, and have some questions about the class ? I would appreciate someone to give me some suggestions.

1. In the inverse function, I have problem to do :
    for(int i =0; ...) ... for the second time... so that I have to declare things like: int ii =0 , or iii =0 ...
    does anyone know why ? since sometimes I don't have this problem...

2. For the + and - operator member function, I decide to return by value ... because in C= a+b
   I do not want to change anything of a ! is this the best way to implement "+" and "-" ??? thanks.

3. in the inverse function, sometimes I have values like : 1.2e-15
    so ... when we print it out, it looks bad ... what would be a better way to print out things ?

4. any suggestions to improve the matrix class ? many thanks.

-----------------------------------------
// Matrix class ...

#ifndef _MatrixH_
#define _MatrixH_

#include <iostream>
#include <sstream>
#include <iomanip>


using namespace std;

//template <class T>

typedef double T;

class Matrix
{
      public:
            Matrix(size_t row =1 , size_t column=1):M(row),N(column), data(new T[M*N]){}

            Matrix(const Matrix& B):M(B.M),N(B.N), data(new T[M*N])
            {
            for(int i=0; i<M*N; i++)
                  {
                        data[i]= B.data[i];
                  }
            }

            Matrix& operator=(const Matrix& B)
            {      
                  for(int i=0; i<M*N; i++)
                  {
                        data[i]= B.data[i];
                  }

                  return *this;
            }

            Matrix operator+(const Matrix& B)
            {
                int B_M = B.getRow();
                int B_N = B.getColumn();
                  if(B_M != M || B_N!= N)
                  {
                        throw "Dimension do NOT match !" ;
            }
                  
                  Matrix C(M,N);
            for(int i=0; i<M*N; i++)
                  {
                        C.data[i] = data[i] + B.data[i];
                  }

                  return C;
            
            }
       

            
            Matrix operator-(const Matrix& B)
            {
                int B_M = B.getRow();
                int B_N = B.getColumn();
                  if(B_M != M || B_N!= N)
                  {
                        throw "Dimension do NOT match !" ;
            }
                  Matrix C(M,N);
            for(int i=0; i<M*N; i++)
                  {
                        C.data[i] = data[i]-B.data[i];
                  }

                  return C;
            
            }


            Matrix operator*(const Matrix& B)
            {
                int B_M = B.getRow();
                  int B_N = B.getColumn();
                  if( N!= B_M)
                  {
                        throw "Dimension for multiplication do NOT match !" ;
            }
                  
                  Matrix C(M,N);
         /*-------------------------------------------------------------
             //  Standard implemetion of matrix multiplication formula

                 for(int i=0; i<M; i++)
                        for(int j=0;j<B_N; j++)
                        {      
                              C.data[i*N+j]= 0;
                              for(int k=0; k<N;k++)
                              C.data[i*N+j]+= data[i*N+k]* B.data[k*N+j];
                        }
             //----------------------------------------------------------- */
         
             //-------------------------------------------------------------
             // An optimized way to speed up the multiplication calculation
             
                  for(int i=0, e=0; i<M; i++)
                for(int j=0;j<B_N; j++, e++)
                {    
                    T val = data[i*N] * B.data[j];
                    for(int k=1, c = i * N+1, d = j + B_N; k<N;k++, c++, d += B_N)
                       val += data[c]* B.data[d];
                   
                              C.data[e] = val;
                        }                  
          //---------------------------------------------------
              return C;
            }

            //----------------------------------------------------
            // A way to read & write matrix element, using (i,j)
            T operator()(size_t i, size_t j) const { return data[i * N + j]; }
            T& operator()(size_t i, size_t j) { return data[i * N + j]; }
        //-----------------------------------------------------

            //----------------------------------------------------
            // Another way to read & write matrix elemnt, using[i][j]

             T* operator[](size_t i) {return (data + (N*i));}
         T const*const operator[](size_t i) const{return (data + (N*i));}
     
             //----------------------------------------------------
             Matrix transpose()
             {
                   Matrix trans(N,M);
                   for(int j=0; j<N; j++)
                         for (int i=0; i<M; i++)
                         {
                               trans.data[j*M+i] = data[i*N+j];
                         }
                   return trans;
             }

             Matrix inverse()
             {
                 if(M!=N) throw "Only Square Matrix can have inverse!";
                   Matrix SI(M,2*M);

                   for(int i=0; i<M;i++)
                   {
                         for(int j=0; j<2*M; j++)
                         {
                               if(j<M)
                               {
                                    SI.data[i*2*M+j] = data[i*M+j];
                               }
                               else
                               {
                                    SI.data[i*2*M+j] = 0;
                               }

                             if(j==i+M) SI.data[i*2*M+j] =1.0;
                         }
                   }


                   for(int ii=0; ii<M-1; ii++)
                   {
                        for(int kk=ii+1; kk < M; kk++)
                        {
                              for(int jj=2*M-1; jj>=ii; jj--)
                              {
                                    SI.data[kk*2*M+jj] -= SI.data[ii*2*M+jj] *
                                                  (SI.data[kk*2*M+ii]/SI.data[ii*2*M+ii]);
                              }
                        }
                   }      
         
                   //-------------------------------------

                 for(int iii=M-1; iii>0; iii--)
                   {
                        for(int kkk=iii-1; kkk >=0; kkk--)
                        {
                              for(int jjj=2*M-1; jjj>=iii; jjj--)
                              {
                                    SI.data[kkk*2*M+jjj] -= SI.data[iii*2*M+jjj] *
                                                  (SI.data[kkk*2*M+iii]/SI.data[iii*2*M+iii]);
                              }
                        }
                   }      

                   for(int iiii=0; iiii<M; iiii++)
                   {
                        for(int jjjj=2*M-1; jjjj >=0; jjjj--)
                        {
                              SI.data[iiii*2*M+jjjj] =SI.data[iiii*2*M+jjjj]/SI.data[iiii*2*M+iiii];                     
                        }
                   }      

                   Matrix I(M,M);
                   for(int s = 0; s<M; s++)
                         for(int r=0; r<M; r++)
                         {
                               I.data[s*M+r] = SI.data[s*2*M+r+M];
                         }
                   return I;
                   //---------------------------------
             }

            //---------------------------------
            // A standard print() function, printing things to standard IO
            void print(ostream& ss)
            {
                  for (int i = 0, c = 0; i < M; i++) {
                        for (int j = 0; j < N; j++,c++) {

                              ss << setw(20) << data[c] ;
                        }
                        ss << endl;
                  }
            }
            //---------------------------------------------

            //-----------------------------------
            // A general print() function for possible future non-standard IO
            template<class Streamlike>
            void printStream(Streamlike s)
            {
                  for (int i = 0, c = 0; i < M; i++) {
                        for (int j = 0; j < N; j++,c++) {
                              s << setw(4) << data[c] ;
                        }
                        s << endl;
                  }
            }
            //------------------------------------

           ~Matrix()
            {      
                        delete [] data;
            }
            
      protected:
            int getRow() const {return M;}
            int getColumn() const {return N;}

      private:
            int N, M;
            T *data ;
};

#endif
[+][-]12/03/05 01:03 AM, ID: 15410482

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/03/05 01:13 AM, ID: 15410500

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/03/05 01:21 AM, ID: 15410517

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/03/05 01:29 AM, ID: 15410537

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]12/03/05 01:42 AM, ID: 15410569

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]12/03/05 06:13 AM, ID: 15411036

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/03/05 06:22 AM, ID: 15411060

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/03/05 06:30 AM, ID: 15411076

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/03/05 06:39 AM, ID: 15411103

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C++ Programming Language
Tags: matrix, inverse, transpose
Sign Up Now!
Solution Provided By: dbkruger
Participating Experts: 3
Solution Grade: A
 
 
[+][-]12/03/05 07:02 AM, ID: 15411185

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/03/05 01:07 PM, ID: 15412389

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/03/05 06:33 PM, ID: 15413350

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/03/05 09:03 PM, ID: 15413682

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/03/05 09:15 PM, ID: 15413704

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/04/05 05:18 AM, ID: 15414494

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/04/05 12:41 PM, ID: 15415897

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/04/05 12:50 PM, ID: 15415918

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/05/05 06:39 AM, ID: 15419550

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091028-EE-VQP-85