📄 rsmatrixc.cpp
字号:
//*****************************************************//
//
// Unit: RsMatrixC.cpp
//
// Purpose: Component for simple matrix operations
//
// Inherits From: TComponent
//
// Project: Rapid Misc
//
// Author: Jeff Hiscock -- jhiscock@rapidsi.com
//
// Date: February 2000
//
// Revisions
//
// Copyright www.rapidsi.com 2000
//******************************************************//
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "RsMatrixC.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TRsMatrixC *)
{
new TRsMatrixC(NULL);
}
//---------------------------------------------------------------------------
__fastcall TRsMatrixC::TRsMatrixC(TComponent* Owner)
: TComponent(Owner)
{
}
//---------------------------------------------------------------------------
namespace Rsmatrixc
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TRsMatrixC)};
RegisterComponents("Rapid - Misc", classes, 0);
}
}
//---------------------------------------------------------------------------
//*******************************************************//
//
// Method: Product
//
// Purpose: Multiply two matricies
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown:
//
// Parameter Description: m1 (in)- first matrix
// m2 (in)- second matrix
// m3 (in/out)- product returned
// rows1 (in)- number of rows in matrix 1
// rows2 (in)- number of rows in matrix 2
// rows3 (in/out) - number of rows in product
// cols1 (in)- number of columns in matrix 1
// cols2 (in)- number of columns in matrix 2
// cols3 (in/out) - number of columns in product
// Simmilar scheme for all other methods
// Revisions:
//
// Scope: public
//*********************************************************//
void TRsMatrixC::Product( const double **m1 , long rows1 ,long cols1 ,
const double **m2 , long rows2 , long cols2 ,double ***m3 ,
long *rows3 , long *cols3)
{
//TODO: Add your source code here
if (cols1 !=rows2)
{
if (FOnProductError)
{
OnProductError(this);
return;
}
else
throw Exception("Undefined product as matricies cannot be multiplied");
}
// allocate memory for the matrix to be returned
*m3 = new double*[rows1];
for(int counter=0;counter<rows1;counter++)
(*m3)[counter] = new double[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;
long double sum = 0;
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;
}
}
}
//*******************************************************//
//
// Method: Sum
//
// Purpose: Compute Sum of two matricies
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown:
//
// Parameter Description:
//
// Revisions:
//
// Scope: public
//*********************************************************//
void __fastcall TRsMatrixC::Sum( const double **m1 , long rows1 ,long cols1 ,const double **m2 ,
long rows2 , long cols2 , double ***m3 )
{
bool ok = true;
if (rows1 !=rows2)
ok = false;
if (cols1 != cols2)
ok = false;
if (!ok)
{
if (FOnSumError)
{
OnSumError(this);
return;
}
else
{
throw Exception ("Matricies are not the same size, sum undefined");
}
}
*m3 = new double* [rows1];
int i,j;
for (i=0;i<rows1;i++)
((*m3)[i]) = new double[cols1];
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
{
(*m3)[i][j] = (m1[i][j]) + (m2[i][j]);
}
}
}
//*******************************************************//
//
// Method: Difference
//
// Purpose: Subtract two matricies
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown:
//
// Parameter Description:
//
// Revisions:
//
// Scope: public
//*********************************************************//
void __fastcall TRsMatrixC::Difference( const double **m1 , long rows1 ,long cols1,
const double **m2 , long rows2 , long cols2 , double ***m3 )
{
bool ok = true;
if (rows1 !=rows2)
ok = false;
if (cols1 != cols2)
ok = false;
if (!ok)
{
if (FOnDifferenceError)
{
OnDifferenceError(this);
return;
}
else
{
throw Exception ("Matricies are not the same size, difference undefined");
}
}
*m3 = new double* [rows1];
int i,j;
for (i=0;i<rows1;i++)
((*m3)[i]) = new double[cols1];
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
{
(*m3)[i][j] = (m1[i][j]) - (m2[i][j]);
}
}
}
//*******************************************************//
//
// Method: TraceOf
//
// Purpose: Compute trace of a matrix
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown:
//
// Parameter Description:
//
// Revisions:
//
// Scope: public
//*********************************************************//
double __fastcall TRsMatrixC::TraceOf(const double **m1 , long rows , long cols)
{
long double ret = 0;
if (rows !=cols)
{
if (FOnTraceOfError)
{
OnTraceOfError(this);
return 0;
}
else
throw Exception("Not a square matrix , Trace of matrix is undefined");
}
for(int i=0;i<rows;i++)
ret +=(m1[i][i]);
return ret;
}
//*******************************************************//
//
// Method: Transpose
//
// Purpose: Transpose a matrix
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown:
//
// Parameter Description:
//
// Revisions:
//
// Scope: public
//*********************************************************//
void __fastcall TRsMatrixC::Transpose(const double **m1 , long rows , long cols , double ***transposed)
{
int i,j;
// transposition shifts the rows and columns
(*transposed) = new double*[cols];
for (i=0;i<cols;i++)
(*transposed)[i] = new double[rows];
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
(*transposed)[j][i] = m1[i][j];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -