C++类写的矩阵计算[1]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 twttalk 的 blog

#pragma once

//矩阵类
class matrix
{
private:
 double * p;
 //矩阵宽度
 long width;
 //矩阵高度
 long height;
public:
 long getheight();
 long getwidth();
 bool isvector();//如果是false,那就是一个数
 double arg();//求行列式
 bool isptv();//是否正定
 matrix  t();//转置
 matrix submatrix(long offset);
 void test();//测试函数
    /***********重载部分-overloaded part*****************/
 matrix operator+(matrix &);
 matrix operator-(matrix &);
 matrix operator*(matrix &);
 friend matrix operator*(double alpha,matrix &);//实数与矩阵相乘
 matrix operator*(double alpha);//矩阵与实数相乘
 matrix operator/(matrix &);//实际是实数相除或矩阵和实数相除
 matrix operator/(double sub);
 matrix operator+=(matrix &);
 matrix operator-=(matrix &);
 matrix operator*=(double alpha);//矩阵与实数相乘
 friend matrix  sqrt(matrix m);//开方
 friend double abs(matrix &);//取绝对值
 matrix &operator=(matrix &);
 double * operator[](long heightpos);//用于实现用[][]操作矩阵元素
 friend ostream & operator<<(ostream &,matrix &);
 matrix(void);//default constructor
 matrix(long n);//单位矩阵
 matrix(double * arraddress,long arrwidth);//构造一维矩阵
 matrix(double * arraddress,long arrwidth,long arrheight);
 matrix(const matrix &);//copy constructor
 ~matrix(void);//default destructor
 /***********重载部分-overloaded part*****************/
};
#include "stdafx.h"
#include ".\matrix.h"

matrix::matrix(void)
{
 p=new double[1];
 width=0;
 height=0;
}
matrix::matrix(long n)
{
 height=width=n;
 p=new double[n*n];
 for(long i=0;i<n;i++){
  for(long j=0;j<n;j++){
   if(i==j) *(p+n*i+j)=1;
   else *(p+n*i+j)=0;
  }
 }
}
matrix::matrix(double * arraddress,long arrwidth)
{
 long arrheight=1;
 p=new double[arrwidth*arrheight];
 for(long i=0;i<arrheight;i++){
  for(long j=0;j<arrwidth;j++){
   *(p+arrwidth*i+j)=*(arraddress+arrwidth*i+j);
  }
 }
 width=arrwidth;
 height=arrheight;
}
matrix::matrix(double * arraddress,long arrwidth,long arrheight)
{
 p=new double[arrwidth*arrheight];
 for(long i=0;i<arrheight;i++){
  for(long j=0;j<arrwidth;j++){
   *(p+arrwidth*i+j)=*(arraddress+arrwidth*i+j);
  }
 }
 width=arrwidth;
 height=arrheight;
}
matrix::matrix(const matrix & m)//copy constructor
{
 height=m.height;
 width=m.width;
 p=new double[height*width];
 for(long i=0;i<height;i++){
   for(long j=0;j<width;j++){
    *(p+width*i+j)=*(m.p+width*i+j);
   }
 }
}
long matrix::getwidth(){
 return width;
}
long matrix::getheight(){
 return height;
}
bool matrix::isvector()
{
 return !(width==1 && height==1);
}
matrix matrix::submatrix(long offset)
{
 assert(height==width && offset<=width && offset>=0);
 double * t=new double[offset*offset];
 for(long i=0;i<offset;i++){
  for(long j=0;j<offset;j++){
   *(t+offset*i+j)=*(p+width*i+j);
  }
 }
 matrix m(t,offset,offset);
 delete [] t;
 return m;
}
double matrix::arg()//矩阵的行列式
{
 assert(width==height);
 double result=1;
 double k;
 matrix m=*this;
 for(long i=0;i<height-1;i++){//gauss消去法,变换成上三角矩阵
  for(long j=i+1;j<height;j++){
   k=m[j][i]/m[i][i];
   m[j][i]=0;
   for(long n=i+1;n<width;n++){
    m[j][n]=m[j][n]-k*m[i][n];
   }
  }
 }
 for(long i=0;i<height;i++){
  for(long j=0;j<width;j++){
   if(i==j) result*=m[i][j];
  }
 }
 return result;
}

bool matrix::isptv()
{
 assert(width==height);//是方阵才可以计算
 bool result=true;
 matrix m;
 for(long i=1;i<=height;i++){
   m=this->submatrix(i);
   if(m.arg()<=0){
    result=false;
    break;
   }
 }
 return result;
}
matrix  matrix::t()
{
 double * t=new double[width*height];
 for(long i=0;i<height;i++){
  for(long j=0;j<width;j++){

本文关键:C++类写的矩阵计算
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top