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

📄 matrix.hpp

📁 基于线性规划的回归支持向量机源程序
💻 HPP
📖 第 1 页 / 共 2 页
字号:
//defines matrix template class which can used to every data type
#include "IntVect.hpp"
#ifndef __BMATPAC_HPP
#define __BMATPAC_HPP

class matrix;

class vector
{

	protected:

		//vector data structure
		struct vrep
		{
             double *f;
			 int refcnt;
			 int length;
		} *p;

	public:

		//iresult=v.vlen() or iresult=vlen(v) or iresult=vlen(pv)
		//retrieves dada member.
        friend int vlen(vector& v){ return v.p->length;}
        friend int vlen(vector   *v){ return v->p->length;}
		int vlen(){ return p->length;}


		//friend classes.
        friend class matrix;

		 //vector()
		 //default constructor.
		 vector();

		 //vector(i,c) or vector(i)
		 //constructor with the length and/or initial data.
         vector(int size, double init=0);
         initial(int size);

		 //vector(pt,i)
		 //constructor with a c/c++ array and its length.
         vector(double   *a,int length);

		 //vector(v1,v2)
		 //constructor with two vectors.
         vector(vector& a1,vector& a2);

		 //vector(v,i1,i2)
		 //constructor with part of a vector.
         vector(vector& a,int first,int last);

		 //vector(v)
		 //copy constructor.
         vector(vector& x);

		 //destructor.
		 ~vector();

		 //vresult=v
         vector&  operator=(vector& v);

		 //vresult=x
         vector&  operator=(double x);

		 //vresult=copy(v) or vresult=x.copy()
		 //produces a new vector which has the same order and elements.
		 //this vector has its own data structure.
         friend vector   copy(vector& v);
         vector    copy();
         int copy(vector& v);

		 //pt=v
         //converts vector to double *.
         operator double *(){return p->f;}
         int operator()(){return p->length;}

		//vresult=v1+v2
        friend vector    operator+(vector& v1, vector& v2);

		//vresult=v1-v2
        friend vector    operator-(vector& v1, vector& v2);

		//vresult=v1*v2
        friend vector    operator*(vector& v1, vector& v2);

		//vresult=v1/v2
        friend vector    operator/(vector& v1, vector& v2);

		//vresult=v1+x
        friend vector    operator+(vector& v1,double x);

		//vresult=v1-x
        friend vector    operator-(vector& v1,double x);

		//vresult=v1*x
        friend vector    operator*(vector& v1, double x);

		//vresult=v1/x
        friend vector    operator/(vector& v1, double x);

		//vresult=x+v1
        friend vector    operator+(double x, vector& v1);

		//vresult=x-v1
        friend vector    operator-(double x, vector& v1);

		//vresult=x*v1
        friend vector    operator*(double x, vector& v1);

		//vresult=x/v1
        friend vector    operator/(double x, vector& v1);

		//vresult=+v1
        friend vector    operator+(vector& v1);

		//vresult=-v1
        friend vector    operator-(vector& v1);

		//vresult+=v1
        vector&  operator+=(vector& v1);

		//vresult-=v1
        vector&  operator-=(vector& v1);

		//vresult*=v1
        vector&  operator*=(vector& v1);

		 //vresult/=v1
        vector&  operator/=(vector& v1);

		//vresult+=x
        vector&  operator+=(double x);

		//vresult-=x
        vector&  operator-=(double x);

		//vresult*=x
        vector&  operator*=(double x);

		//vresult/=x
        vector&  operator/=(double x);

		//xresult=v[i]
        double&  operator[](int i);

		//iresult=(v1==v2)
        friend int    operator==(vector& v1,
                     vector& v2);

		//iresult=(v1!=v2)
        friend int    operator!=(vector& v1,
                     vector& v2);

        //cin>>v
        friend istream&  operator>>(istream&,vector& v);

        //cout<<v
        friend ostream&  operator<<(ostream&,vector& v);

		//result=sum(v) or result=v.sum()
		//returns the sum of all elements.
        friend double    sum(vector& v);
        double    sum();

		//result=v1.dot(v2)
        double    dot(vector& v2);

		//vresult=v.slice(len) or  vresult=slice(v,len)
		//returns a subvector of v. it has len elements and vresult[0]=x[0].
        vector    slice(int len);
        friend vector    slice(vector& v,int len);

		//vresult=v.slice(lstart,length) or vresult=slice(v,lstart,length)
		//returns a subvector of v. it has length elements
		//and vresult[0]=v[lstart].
        vector    slice(int lstart,int length);
        friend vector    slice(vector& v, int lstart,
							int length);

		//vresult=v1.comvec(v2) or vresult=comvec(v1,v2)
		//combines two vectors.
        friend vector    comvec(vector& v1,vector& v2);
        vector    comvec(vector& v2);

         //result=any(v) or result=v.any()
		 //if all elements of vector are zero, returns 0. otherwise, returns 1.
         friend int  any(vector  & v);
         int  any();

		 //result=max(v) or result=v.max()
         friend double  vmax(vector  & v);
         double  vmax();

		 //vresult=max(v1,v2) or vresult=v1.max(v2)
		 //vresult[i]=max(v1[i],v2[i]).
         friend vector  vmax(vector  & v1,
                    vector  & v2);
         vector  vmax(vector  & v2);

		 //result=min(v) or result=v.min()
         friend double  vmin(vector  & v);
         double  vmin();

		 //vresult=min(v1,v2) or vresult=v1.min(v2)
		 //vresult[i]=min(v1[i],v2[i]).
         friend vector  vmin(vector  & v1,
                    vector  & v2);
         vector  vmin(vector  & v2);

		 //vresult=abs(v) or vresult=v.abs()
         friend vector  abs(vector  & v);
         vector  abs();

		//result=dot(v1,v2)
        friend double  dot(vector  & v1,vector  & v2);

		 //vresult=rand(v) or rand(v) or vresult=v.rand()
		 //fullfills every element of vector v with random numbers which
		 //are uniformly distributed in the range of (0.0, 1.0).
		 //WARNING: this function will destroy the old content of
		 //vector object which calls this function.
         vector  &  rand();
         friend vector  &  rand(vector  & v);

		 //vresult=v.rand(m)
		 //this function create a new vector which has m elements.
		 //there is no influence to vector which calls this function.
         vector  rand(int m);
        void rndnorm(double mu=0,double sigma=1);
        void SortByHeap(int flag=1);
        IntVector Sort(int flag=1);
};


class matrix
{

	protected:

		//matrix data structure
		struct mrep
		{
            vector **f;
			int refcnt;
			int length;
            double ** tmppointer;
		} *p;

	public:
		int copy(matrix&);


		//iresult=m.row() or iresult=row(m) or iresult=row(pm)
		//retrieves row number of the matrix.
        friend int mrow(matrix  & m){ return m.p->length;}
        friend int mrow(matrix  *m){ return m->p->length;}
		int mrow(){ return p->length;}

		//iresult=m.col() or iresult=col(m) or iresult=col(pm)
		//retrieves colum number of the matrix.
        friend int mcol(matrix  & m){ return vlen(m.p->f[0]);}
        friend int mcol(matrix  *m){ return vlen(m->p->f[0]);}
		int mcol(){ return vlen(p->f[0]);}

		 //matrix()
		 //default constructor.
		 matrix();

		 //matrix(i1,i2,c) or matrix(i1,i2)
		 //constructor with the order and/or initial data.
         matrix(int xsize, int ysize, double init=0.0);

		 //matrix(ppt,i1,i2)
		 //constructor with a c/c++ array.
         matrix(double  **a,int xlength,int ylength);

		 //matrix(m1,m2,select) or matrix(m1,m2)
		 //constructs a new matrix with matrix a1 and a2. the construction
		 //mode is controlled by variable select:
		 //select=0: 		  the matrix A1(n1*m1) and B1(n2*m2) will be
		 //					  placed at main diagnal and the dimension of
		 //					  new matrix is (n1+n2)*(m1+m2);
		 //select=1:	   	  combines matrix A1(n1*m1) and B1(n2*m2).
		 //					  the dimension of new matrix is n*(m1+m2)
		 //					  (n=MAX(n1,n2));
		 //select=2(default): combines matrix A1(n1*m1) and B1(n2*m2).
		 //					  the dimension of new matrix is (n1+n2)*m
		 //					  (m=MAX(m1,m2));
		 //other select:	  reports an error !
         matrix(matrix  & a1, matrix  & a2,int select=2);

		 //matrix(m,i1,i2,i3,i4)
		 //constructor with part of a matrix. the dimension of new matrix  is
		 //(xlast-xfirst)*(ylast-yfist). the first element is a[xfirst][yfirst].
         matrix(matrix  & a,int xfirst,int xlast,
				 int yfirst, int ylast);

		 //matrix(m)
		 //copy constructor.
         matrix(matrix  & x);
         int initial(int xsize,int ysize);
         int initrow(int xsize);

		 //destructor.
		 ~matrix();


		 //mresult=copy(m) or mresult=m.copy()
		 //produces an new matrix which has the same order and elements.
		 //this matrix has its own data structure.
         friend matrix copy(matrix & m);
         matrix copy();

⌨️ 快捷键说明

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