矩阵类我写好了一个框架,大家补全一下落!
比如说线形代数的运算有求方程的枳,有求解线形方程组得方法,比如高斯法,可以写一个线形类来操作矩阵类作为矩阵类的友元类!或者其他的只要可以实现九可以了,不考虑效率和时间问题!
// Matrix.h: interface for the Matrix class. // //////////////////////////////////////////////////////////////////////
#if !defined(AFX_MATRIX_H__40822523_B76E_4E7C_A2FF_E276919FBD9A__INCLUDED_) #define AFX_MATRIX_H__40822523_B76E_4E7C_A2FF_E276919FBD9A__INCLUDED_
#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000
class Matrix { public: Matrix(); virtual ~Matrix(); Matrix( int rSize, int cSize, float init = 0 ); Matrix( Matrix& A ); friend ostream & operator << ( ostream & out, Matrix & A ); friend Matrix operator + ( Matrix & A, Matrix & B ); friend Matrix operator - ( Matrix & A, Matrix & B ); friend Matrix operator * ( Matrix & A, Matrix & B ); Matrix & operator = ( Matrix & A ); float & operator()( int mid, int nid ); Matrix Transpose(); bool IsEmpty(); protected: float * elem; int m; int n; float elemEmpty; };
#endif // !defined(AFX_MATRIX_H__40822523_B76E_4E7C_A2FF_E276919FBD9A__INCLUDED_) 这个市vc中添加的矩阵类Matrix中的头文件
// Matrix.cpp: implementation of the Matrix class. // //////////////////////////////////////////////////////////////////////
#include <iostream.h> #include <iomanip.h>
#include "Matrix.h"
////////////////////////////////////////////////////////////////////// // Construction/Destruction //////////////////////////////////////////////////////////////////////
Matrix::Matrix() { m = 0; n = 0; elem = NULL; }
Matrix::Matrix( int rSize, int cSize, float init ) { m = rSize; n = cSize; elem = new float[ m * n ]; for ( int i = 0; i < m * n; i++ ) { elem[i] = init; } }
Matrix::Matrix( Matrix & A ) { m = A.m; n = A.n; elem = new float[ m * n ]; for ( int i = 0; i < m * n; i++ ) { elem[i] = A.elem[i]; } }
Matrix::~Matrix() { if ( !IsEmpty() ) { delete [] elem; } }
bool Matrix::IsEmpty() { if ( elem == NULL ) { return true; } else { return false; } }
float & Matrix::operator ()( int mid,int nid ) { if ( IsEmpty() || mid >= m || nid >= n) { elemEmpty = 0; return elemEmpty; } else { return elem[ mid * m + nid ]; } }
ostream & operator << ( ostream & out, Matrix & A ) { int width = out.width();
for ( int i = 0; i < A.m; i++ ) { for ( int j = 0; j < A.n; j++ ) { out << setw( width ) << A( i, j ); } out << endl; } return out; }
Matrix operator + ( Matrix & A, Matrix & B ) { Matrix result( A.m, A.n ); for ( int i = 0; i < result.m; i++ ) { for ( int j = 0; j < result.n; j++ ) { result( i, j ) = A( i, j ) + B( i, j ); } } return result; }
Matrix operator - ( Matrix & A, Matrix & B ) { Matrix result( A.m, A.n ); for ( int i = 0; i < result.m; i++ ) { for ( int j = 0; j < result.n; j++ ) { result( i, j ) = A( i, j ) - B( i, j ); } } return result; }
Matrix operator * ( Matrix & A, Matrix & B ) { Matrix result( A.m, B.n ); int i, j, k; int maxN = A.n > B.m ? A.n : B.m;
for ( i = 0; i < result.m; i++ ) { for ( j = 0; j < result.n; j++ ) { for( k = 0; k < maxN; k++ ) { result( i, j ) += A( i, k ) * B( k, j ); } } } return result; }
Matrix & Matrix::operator = ( Matrix & A ) { m = A.m; n = A.n; if ( !IsEmpty() ) { delete [] elem; } elem = new float[ m * n ]; for ( int i = 0; i < m * n; i++ ) { elem[i] = A.elem[i]; }
return *this; }
Matrix Matrix::Transpose() { Matrix result( n, m );
for( int i = 0; i < n; i++ ) { for ( int j = 0; j < m; j++ ) { result( i, j ) = (*this)( j, i ); } } return result; } 这个是Matrix.cpp中的实现细节