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

📄 intmat.hpp

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



class IntMatrix
{

	protected:

        //IntMatrix data structure
		struct mrep
		{
            IntVector **f;
			int refcnt;
			int length;
            int ** tmppointer;
		} *p;

	public:


		//iresult=m.row() or iresult=row(m) or iresult=row(pm)
        //retrieves row number of the IntMatrix.
        friend int mrow(IntMatrix  & m){ return m.p->length;}
        friend int mrow(IntMatrix  *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 IntMatrix.
        friend int mcol(IntMatrix  & m){ return vlen(m.p->f[0]);}
        friend int mcol(IntMatrix  *m){ return vlen(m->p->f[0]);}
		int mcol(){ return vlen(p->f[0]);}

         //IntMatrix()
		 //default constructor.
         IntMatrix();

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

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

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

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

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

		 //destructor.
         ~IntMatrix();


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

		 //ppt=m
         //converts IntMatrix to int **.
         operator int **();

		 //mresult=m
         IntMatrix &  operator=(IntMatrix & m);

		 //mresult=v
         IntMatrix &  operator=(IntVector  & v);

		 //mresult=x
         IntMatrix &  operator=(int x);

		//mresult=m1+m2
        friend IntMatrix operator+(IntMatrix & m1,
                    IntMatrix & m2);

		//mresult=m1-m2
        friend IntMatrix operator-(IntMatrix & m1,
                    IntMatrix & m2);

		//mresult=m1*m2
        friend IntMatrix operator*(IntMatrix & m1,
                    IntMatrix & m2);

		//mresult=m1/m2
        friend IntMatrix operator/(IntMatrix & m1,
                    IntMatrix & m2);

		//mresult=m1+x
        friend IntMatrix operator+(IntMatrix & m1, int x);

		//mresult=m1-x
        friend IntMatrix operator-(IntMatrix & m1, int x);

		//mresult=m1*x
        friend IntMatrix operator*(IntMatrix & m1, int x);

		//vresult=m1*v2
        friend IntVector operator*(IntMatrix & m1,
                                IntVector & v2);
		//vresult=v1*m2
        friend IntVector operator*(IntVector & v1,
                                 IntMatrix & m2);
		//mresult=m1/x
        friend IntMatrix operator/(IntMatrix & m1, int x);
        friend IntVector operator/(IntVector& v,IntMatrix& a);//col(a)=vlen(v),x*a=v,vlen(x)=mrow(a)
        friend IntVector operator%(IntVector& v,IntMatrix& a);//mrow(a)=vlen(v),a*x=v,vlen(x)=mcol(a)

		//mresult=x+m1
        friend IntMatrix operator+(int x, IntMatrix & m1);

		//mresult=x-m1
        friend IntMatrix operator-(int x, IntMatrix & m1);

		//mresult=x*m1
        friend IntMatrix operator*(int x, IntMatrix & m1);

		//mresult=x/m1
        friend IntMatrix operator/(int x, IntMatrix & m1);

		//mresult=+m1
        friend IntMatrix operator+(IntMatrix & m1);

		//mresult=-m1
        friend IntMatrix operator-(IntMatrix & m1);

		//mresult+=m1
        IntMatrix &  operator+=(IntMatrix & m1);

		//mresult-=m1
        IntMatrix &  operator-=(IntMatrix & m1);

		//mresult/=m1
        IntMatrix &  operator/=(IntMatrix & m1);

		//mresult+=x
        IntMatrix &  operator+=(int x);

		//mresult-=x
        IntMatrix &  operator-=(int x);

		//mresult*=x
        IntMatrix &  operator*=(int x);

		//mresult/=x
        IntMatrix &  operator/=(int x);

		//mresult=m1.prod(m2) or mresult=prod(m1,m2)
        IntMatrix prod(IntMatrix & m2);
        friend IntMatrix prod(IntMatrix & m1,IntMatrix & m2);

		//vresult=m[i]
        IntVector &  operator[](int i);
        int &  operator()(int i,int j);

		//iresult=(m1==m2)
        friend int operator==(IntMatrix & m1, IntMatrix & m2);

		//iresult=(m1!=m2)
        friend int operator!=(IntMatrix & m1, IntMatrix & m2);

		//cin>>m
        friend istream &  operator>>(istream &, IntMatrix & m);

		//cout<<m
        friend ostream &  operator<<(ostream &, IntMatrix & m);

		//vresult=m.row(n) or vresult=row(m,n)
        //returns a special row of a IntMatrix.
        IntVector row(int n);
        friend IntVector row(IntMatrix & m,int n);

		//vresult=m.col(n) or vresult=col(m,n)
        //returns a special column of a IntMatrix.
        IntVector col(int n);
        friend IntVector col(IntMatrix & m,int n);

		//mresult=m.slice(row,col) or  mresult=slice(m,row,col)
        //returns a subIntMatrix of m. it is row*col and mresult[0][0]=x[0][0].
        IntMatrix slice(int row,int col);
        friend IntMatrix slice(IntMatrix & m,int row,int col);

		//mresult=m.slice(row1,col1,row2,col2) or mresult=slice(m,row1,col1,
        //row2,col2)       returns a subIntMatrix of m. it is row2*col2 and
		//mresult[0][0]=v[row1][col1].
        IntMatrix slice(int row1,int col1,int row2,int col2);
        friend IntMatrix slice(IntMatrix & m, int row1,
					int col1,int row2,int col2);

		//mresult=m1.commat(m2,select) or mresult=commat(m1,m2,select)
		//mresult=m1.commat(m2) or mresult=commat(m1,m2)
		//combines two matrices:
        //select=0:           the IntMatrix A(n1*m1) and B(n2*m2) will be placed
		//					  at main diagnal line and result is a
        //                    (n1+n2)*(m1+m2) IntMatrix;
        //select=1:           combines a n1*m1 IntMatrix with a n2*m2 IntMatrix. the
        //                    result is a n*(m1+m2) IntMatrix (n=MAX(n1,n2));
        //select=2(default):  combines a n1*m1 IntMatrix with a n2*m2 IntMatrix. the
        //                    result is a (n1+n2)*m IntMatrix (m=MAX(m1,m2));
		//other select:		  reports an error !
        friend IntMatrix commat(IntMatrix & m1,IntMatrix & m2,
							int select=2);
        IntMatrix commat(IntMatrix & m2,
							int select=2);

		//mresult=m1.commat(v2,select) or mresult=commat(m1,v2,select)
		//mresult=m1.commat(v2) or mresult=commat(m1,v2)
        //combines a IntMatrix with a IntVector to form a new IntMatrix:
        //select=1:             combines a n1*m1 IntMatrix with a n2 element
        //                      column-IntVector. the result is a n*(m1+1)
        //                      IntMatrix (n=MAX(n1,n2));
        //select=2(default):    combines a n1*m1 IntMatrix with a m2 element
        //                      row-IntVector. the result is a (n1+1)*m
        //                      IntMatrix (m=MAX(m1,m2));
		//other select:			will report an error !
        friend IntMatrix commat(IntMatrix & m1,
                        IntVector & v2,int select=2);
        IntMatrix commat(IntVector & v2,int select=2);

		//mresult=v1.commat(m2,select) or mresult=commat(v1,m2,select)
		//mresult=v1.commat(m2) or mresult=commat(v1,m2)
        //same as above function, but the IntVector is placed at first row or
		//first column.
        friend IntMatrix commat(IntVector & v1,
                        IntMatrix & m2,int select=2);

		//mresult=v1.commat(v2,select) or mresult=commat(v1,v2,select)
        //combines two IntVectors to form a new IntMatrix:
        //select=1:             combines a n1 element column-IntVector with a n2
        //                      element column-IntVector. the result is a n*2
        //                      IntMatrix (n=MAX(n1,n2));
        //select=2(default):    combines a m1 element row-IntVector with a m2
        //                      element row-IntVector. the result is a 2*m
        //                      IntMatrix (m=MAX(m1,m2));
		//other select:		    reports an error !
        friend IntMatrix commat(IntVector & v1,
                        IntVector & v2,int select=2);

		//mresult=m1.commat(m2,srow,scol) or mresult=commat(m1,m2,srow,scol)
		//combines two matrices with arbitry manner. the limitation is that
        //any element of the two matrices can not be overlapped. the IntMatrix
        //A(n1*m1) is placed at left-top corner. the first element of IntMatrix
        //B(n2*m2) is placed at (srow,scol). the dimension of result IntMatrix
		//is max(srow+n2,n1)*max(scol+m2,m1).
        friend IntMatrix commat(IntMatrix & m1,IntMatrix & m2,
							int srow,int scol);
        IntMatrix commat(IntMatrix & m2,
							int srow,int scol);

		//mresult=diag(v2)
        //returns a IntMatrix which uses input IntVector as diagnal elements.
        friend IntMatrix diag(IntVector & v2);

		//mresult=diag(v2,k)
        //returns a IntMatrix which uses the element of IntVector as the k-th
		//diagnal. if k>0, the subdiagnal is above main diagnal. otherwise,
        //it is under the main diagnal. the dimension of return IntMatrix
		//is (n+|k|)*(n+|k|).
        friend IntMatrix diag(IntVector & v2,int k);

		//vresult=diag(m2) or vresult=m2.diag()
        //returns the main diagnal element of input IntMatrix.
        friend IntVector diag(IntMatrix & m2);
        IntVector diag();

		//vresult=diag(m2,k) or vresult=m2.diag(k)
        //returns the k-th subdiagnal of input IntMatrix. if k>0, the subdiagnal
		//is above main diagnal. otherwise, it is under the main diagnal.
        friend IntVector diag(IntMatrix & m2,int k);
        IntVector diag(int k);

		//vresult=sum(m) or vresult=m.sum()
        //returns the sum of every element in the same row of input IntMatrix.
        friend IntVector sum(IntMatrix & m);
        IntVector sum();

        //mresult=trans(m) or mresult=m.trans()
        IntMatrix trans();
        friend IntMatrix trans(IntMatrix & m);

		//mresult=abs(m) or mresult=m.abs()
        IntMatrix abs();
        friend IntMatrix abs(IntMatrix & m);

		//vresult=any(m) or vresult=m.any()
        //returns a IntVector. if the i-th row has any non-zero element. the i-th
        //element of return IntVector is 1. otherwise, it is 0.
        IntVector any();
        friend IntVector any(IntMatrix & m);

		 //vresult=max(m) or vresult=m.max()
         IntVector maxofrow();
         friend IntVector maxofrow(IntMatrix & m);

         IntVector maxofcol();
         friend IntVector maxofcol(IntMatrix & m);

		 //mresult=max(v1,v2) or mresult=v1.max(v2)
		 //mresult[i][j]=max(v1(i,j),v2(i,j)).
         IntMatrix mmax(IntMatrix & v2);
         friend IntMatrix mmax(IntMatrix & v1,IntMatrix & v2);

		 //vresult=min(m) or vresult=m.min()
         IntVector minofrow();
         friend IntVector minofrow(IntMatrix & m);

         IntVector minofcol();
         friend IntVector minofcol(IntMatrix & m);

		 //mresult=min(v1,v2) or mresult=v1.min(v2)
		 //mresult[i][j]=min(v1(i,j),v2(i,j)).
         IntMatrix mmin(IntMatrix & v2);
         friend IntMatrix mmin(IntMatrix & v1,IntMatrix & v2);

		//mresult=eye(n)
        //returns a n*n E IntMatrix.

		//mresult=eye(m) or mresult=m.eye()
        //returns a IntMatrix which has the same order with input IntMatrix.
		//the main diagnal elements are 1, and  other elements are 0.
        friend IntMatrix eye(IntMatrix & m);
        IntMatrix eye();

        void matmult(IntVector& u,IntVector& v);
};


//#if defined( __BCOPT__ ) && !defined( _ALLOW_po )
//#pragma option -po.
//#endif
//#pragma option -Vo.

#endif //__BMATPAC_HPP

⌨️ 快捷键说明

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