⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsmatrixt.h

📁 提供基本榘阵 ( Matrix ) 运算 ( product 、 sum 、 difference、 transpose、 traceof ) 的非可视构件 ( 1.0 版
💻 H
字号:
//*****************************************************//
//
// Unit: RsMatrix.h
//
// Purpose: Template class for basic matrix operations
//
// Inherits From: nothing
//
// Project: Misc
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Revisions
//
// Copyright Rapid Systesms 2000
//******************************************************//
//---------------------------------------------------------------------------
#ifndef RsMatrixTH
#define RsMatrixTH
//---------------------------------------------------------------------------
// For C++ Builder.. of course this might be a problem with the old Borland C++
// unsure if Exception was a standard Exception class with OWL ??
#ifdef __BORLANDC__
  #define RS_EXCEPTION(p) Exception(p)
#else
  #define RS_EXCPETION(p) p
#endif
template <class T>
class TRsMatrixT
{
public:

void Product(const T **m1 , long rows1 ,long cols1 ,
                        const T **m2 , long rows2 , long cols2 ,
                        T ***m3 , long *rows3 , long *cols3)
{
    //TODO: Add your source code here
    if (cols1 !=rows2)
       throw RS_EXCEPTION("Undefined product as matricies cannot be multiplied");

    // allocate memory for the matrix to be returned
    *m3 = new T*[rows1];
    for(int counter=0;counter<rows1;counter++)
       ((*m3)[counter]) = new T[cols2];

    *rows3 = rows1;
    *cols3 = cols2;

    //the product will be a rows1 x cols2 matrix   m3(11) = sum of m1(row1) x m2(col1)
    int i,j,k;

    T sum ;
    for(i=0;i<rows1;i++)
    {
      for(j=0;j<cols2;j++)
      {
        sum = 0 ;
        for(k=0;k<rows2;k++)
        {
          sum+=(m1[i][k] * m2[k][j]) ;
        }

       (*m3)[i][j] = sum;

      }
    }

}
void Sum( const T **m1 , long rows1 ,long cols1 ,const T **m2 ,
                    long  rows2 , long cols2 , T ***m3 )
{
  if (rows1 !=rows2)
    throw RS_EXCEPTION ("Matricies are not the same size, sum undefined");

  if (cols1 != cols2)
     throw RS_EXCEPTION ("Matricies are not the same size , sum undefined");

  *m3 = new T* [rows1];

  int i,j;

  for (i=0;i<rows1;i++)
    ((*m3)[i]) = new T[cols1];

  for(i=0;i<rows1;i++)
  {
     for(j=0;j<cols1;j++)
     {
       (*m3)[i][j] = (m1[i][j]) + (m2[i][j]);
     }
  }


}

void Difference(const T**m1 , long rows1 ,long cols1,
                          const T **m2 , long rows2 , long cols2 , T ***m3 )
{
  if (rows1 !=rows2)
    throw RS_EXCEPTION ("Matricies are not the same size, difference undefined");

  if (cols1 != cols2)
     throw RS_EXCEPTION ("Matricies are not the same size , difference undefined");

  *m3 = new T* [rows1];

  int i,j;

  for (i=0;i<rows1;i++)
    ((*m3)[i]) = new T[cols1];

  for(i=0;i<rows1;i++)
  {
     for(j=0;j<cols1;j++)
     {
       (*m3)[i][j] = (m1[i][j]) - (m2[i][j]);
     }
  }


}

T TraceOf(const T **m1 , long rows , long cols)
{
   T ret;

   if (rows !=cols)
      throw RS_EXCEPTION("Not a square matrix , Trace of matrix is undefined");

   ret = 0;
   for(int i=0;i<rows;i++)
      ret +=m1[i][i];

   return ret;

}

void Transpose(const T **m1 , long rows , long cols , T ***transposed)
{
   int i,j;

   // transposition shifts the rows and columns
   (*transposed) = new T*[cols];
   for (i=0;i<cols;i++)
      (*transposed)[i] = new T[rows];

   for(i=0;i<rows;i++)
   {
      for(j=0;j<cols;j++)
        (*transposed)[j][i] = m1[i][j];
   }

}

};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -