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

📄 vector.cpp

📁 基于线性规划的回归支持向量机源程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//the realization of vector template class
#include "matrix.hpp"
#include "matlib.h"

//#include "materr.hpp"

extern char **Mwarningstring;
extern char **Merrorstring;
vector::vector()
{
	  p=new vrep;
	  p->length=0;
	  p->f=NULL;
	  p->refcnt=1;
}



vector::vector(int size,double init)
{
    if(size<=0)
    {
        cout<< Merrorstring[EMAT_INVALIDSIZE];
        throw Merrorstring[EMAT_INVALIDSIZE];
    }

    if(size>= UINT_MAX /sizeof(double))
    {
        cout<< Merrorstring[EMAT_EXCEEDCAPACITY]<<endl;
        throw Merrorstring[EMAT_EXCEEDCAPACITY];
    }
	  p=new vrep;
      p->f=new double[p->length=size];
	  for(int i=0; i<size; i++)
		  p->f[i]=init;
	  p->refcnt=1;
}

vector::initial(int size)
{
	int retn=1;
    if(size<=0)
    {
        cout<< Merrorstring[EMAT_INVALIDSIZE];
        throw Merrorstring[EMAT_INVALIDSIZE];
    }

    if(size>= UINT_MAX /sizeof(double))
    {
        cout<< Merrorstring[EMAT_EXCEEDCAPACITY]<<endl;
        throw Merrorstring[EMAT_EXCEEDCAPACITY];
    }
    if(p->refcnt==1)
	{
        if(p->length==size)
        return 1;
		if(p->f !=NULL)
        delete p->f;
	}
    else
    {
        p->refcnt--;
        p=new vrep;
        p->refcnt=1;
    }
    if(p->length=size)
    {
        p->f=new double[p->length];
    }
	return retn;
}


vector::vector(double* a,int length)
{
    if(length<=0)
    {
        cout<< Merrorstring[EMAT_INVALIDSIZE];
        throw Merrorstring[EMAT_INVALIDSIZE];
    }

    if(length>= UINT_MAX /sizeof(double))
    {
        cout<< Merrorstring[EMAT_EXCEEDCAPACITY]<<endl;
        throw Merrorstring[EMAT_EXCEEDCAPACITY];
    }

	  p=new vrep;
      p->f=new double[p->length=length];
	  for(int i=0; i<length; i++,a++)
		  p->f[i]=*a;
	  p->refcnt=1;
}


vector::vector(vector& a1, vector& a2)
{
	  int len1=a1.p->length;
	  int len2=a2.p->length;
	  int size=len1+len2;
    if(size>= UINT_MAX /sizeof(double))
    {
        cout<< Merrorstring[EMAT_EXCEEDCAPACITY]<<endl;
        throw Merrorstring[EMAT_EXCEEDCAPACITY];
    }
	  p=new vrep;
	  p->length=size;
      p->f=new double[size];
	  for(int i=0; i<len1; i++)
		  p->f[i]=a1[i];
	  for(int j=0; j<len2; j++)
		  p->f[len1+j]=a2[j];
	  p->refcnt=1;
}

//constructs an sub-vector using vector a. the element number=last-fist and
//index from first to last-1.

vector:: vector(vector& a,int first,int last)
{
	  int size=last-first;
      if(size<=0)
      {
        cout<<Merrorstring[EMAT_ASSIGNDATAERR];
        throw Merrorstring[EMAT_ASSIGNDATAERR];
    }
      if((first<0)||(last>a.vlen()))
      {
        cout<< Merrorstring[EMAT_OUTOFRANGE];
        throw Merrorstring[EMAT_OUTOFRANGE];
    }

	  p=new vrep;
	  p->length=size;
      p->f=new double[p->length];
	  for(int i=0; i<size; i++)
		  p->f[i]=a[first+i];
	  p->refcnt=1;
}



vector::vector(vector& x)
{
	x.p->refcnt++;
	p=x.p;
}



vector::~vector()
{
	if(--p->refcnt==0)
	{
		if(p->f !=NULL)
			delete p->f;
		delete p;
	}
}



vector copy(vector& m)
{
	int len=m.vlen();
    vector x(len);
    double *f1=x.p->f;
    double *f2=m.p->f;
	for(int i=0; i<len; i++, f1++, f2++)
		*f1=*f2;
	return x;
}



vector vector::copy()
{
	int len=vlen();
    vector x(len);
    double *f1=x.p->f;
    double *f2=p->f;
	for(int i=0; i<len; i++, f1++, f2++)
		*f1=*f2;
	return x;
}

int vector::copy(vector& m)
{
	int len=m.vlen();
    int f=initial(len);
    double *f1=p->f;
    double *f2=m.p->f;
	for(int i=0; i<len; i++, f1++, f2++)
		*f1=*f2;
	return f;
}

//v2=v1

vector& vector::operator=(vector& vec)
{
    if( this==&vec)
    return *this;
    vec.p->refcnt++;
    if(--p->refcnt==0)
    {
        delete p->f;
        delete p;
    }
    p=vec.p;
/*
    if( p->length!=vec() ){
            initial( vec() );
    }
    if(p->length){
            int i;
            for(i=0;i<p->length;i++){
                    p->f[i]=vec[i];
            }
    }
*/
	return *this;
}

//v=x

vector&  vector::operator=(double x)
{
	int len=p->length;
    if(p->refcnt>1)
	{
		p->refcnt--;
		p=new vrep;
		p->length=len;
		p->refcnt=1;
        p->f=new double[len];
	}

    double *f=p->f;
	for(int i=0; i<len; i++, f++)
		*f=x;
	return *this;
}

//v=v1+v2

vector operator+(vector& v1,vector& v2)
{
    //allocate the new vector
    if(v1()!=v2())
    {
        cout<<"vector's dim are not equal";
    }
	int len=MIN(v1.p->length,v2.p->length);
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
    double *f2=v2.p->f;
	for(int i=0; i<len; i++, f++, f1++, f2++)
		*f=*f1 + *f2;
	return v;
}

//v=v1-v2

vector operator-(vector& v1,vector& v2)
{
    //allocate the new vector
    if(v1()!=v2())
    {
        cout<<"vector's dim are not equal";
    }
	int len=MIN(v1.p->length,v2.p->length);
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
    double *f2=v2.p->f;
	for(int i=0; i<len; i++, f++, f1++, f2++)
		*f=*f1 - *f2;
	return v;
}

//v=v1*v2

vector    operator*(vector& v1,vector& v2)
{
    //allocate the new vector
    if(v1()!=v2())
    {
        cout<<"vector's dim are not equal";
    }
	int len=MIN(v1.p->length,v2.p->length);
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
    double *f2=v2.p->f;
	for(int i=0; i<len; i++, f++, f1++, f2++)
		*f=*f1 * *f2;
	return v;
}

//v=v1/v2

vector    operator/(vector& v1,vector& v2)
{
    //allocate the new vector
	int len=MIN(v1.p->length,v2.p->length);
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
    double *f2=v2.p->f;
	for(int i=0; i<len; i++, f++, f1++, f2++)
		*f=*f1 / *f2;
	return v;
}

//v=v1+x

vector    operator+(vector& v1,double x)
{
    //allocate the new vector
	int len=v1.p->length;
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f=*f1 + x;
	return v;
}

//v=v1-x

vector    operator-(vector& v1,double x)
{
    //allocate the new vector
	int len=v1.p->length;
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f=*f1 - x;
	return v;
}

//v=v1*x

vector    operator*(vector& v1,double x)
{
    //allocate the new vector
	int len=v1.p->length;
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f=*f1 * x;
	return v;
}

//v=v1/x

vector    operator/(vector& v1,double x)
{
    //allocate the new vector
	int len=v1.p->length;
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f=*f1 / x;
	return v;
}

//v=x+v1

vector    operator+(double x,vector& v1)
{
    //allocate the new vector
	int len=v1.p->length;
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f=x+ *f1;
	return v;
}

//v=x-v1

vector    operator-(double x,vector& v1)
{
    //allocate the new vector
	int len=v1.p->length;
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f=x- *f1;
	return v;
}

//v=x*v1

vector    operator*(double x,vector& v1)
{
    //allocate the new vector
	int len=v1.p->length;
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f=x * *f1;
	return v;
}

//v=x/v1

vector    operator/(double x,vector& v1)
{
    //allocate the new vector
	int len=v1.p->length;
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f=x/ *f1;
	return v;
}

//v=+v1

vector    operator+(vector& v1)
{
	return v1;
}

//v=-v1

vector    operator-(vector& v1)
{
    //allocate the new vector
	int len=v1.p->length;
    vector v(len);
	//do the operations
    double *f=v.p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f=- *f1;
	return v;
}

//v+=v1

vector& vector::operator+=(vector& v1)
{
    //allocate the new vector
    if(v1()!=vlen())
    {
        cout<<"vector's dim are not equal";
    }
	int len=MIN(v1.p->length, p->length);
	//do the operations
    double *f=p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f +=*f1;
	return *this;
}

//v-=v1

vector& vector::operator-=(vector& v1)
{
    //allocate the new vector
    if(v1()!=vlen())
    {
        cout<<"vector's dim are not equal";
    }
	int len=MIN(v1.p->length, p->length);
	//do the operations
    double *f=p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f -=*f1;
	return *this;
}

//v*=v1

vector& vector::operator*=(vector& v1)
{
    //allocate the new vector
    if(v1()!=vlen())
    {
        cout<<"vector's dim are not equal";
    }
	int len=MIN(v1.p->length, p->length);
	//do the operations
    double *f=p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f *=*f1;
	return *this;
}

//v/=v1

vector& vector::operator/=(vector& v1)
{
    //allocate the new vector
    if(v1()!=vlen())
    {
        cout<<"vector's dim are not equal";
    }
	int len=MIN(v1.p->length, p->length);
	//do the operations
    double *f=p->f;
    double *f1=v1.p->f;
	for(int i=0; i<len; i++, f++, f1++)
		*f /=*f1;
	return *this;
}

//v+=x

vector& vector::operator+=(double x)
{
    //allocate the new vector
	int len=p->length;
	//do the operations
    double *f=p->f;
	for(int i=0; i<len; i++, f++)
		*f +=x;
	return *this;
}

//v-=x

vector& vector::operator-=(double x)
{
    //allocate the new vector
	int len=p->length;
	//do the operations
    double *f=p->f;
	for(int i=0; i<len; i++, f++)
		*f -=x;
	return *this;
}

//v*=x

vector& vector::operator*=(double x)
{

⌨️ 快捷键说明

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