📄 intmat.cpp
字号:
//v=v1*m2;
IntVector operator*(IntVector & v1,IntMatrix & m2)
{
//check the dimensions
int i,j;
int m1cols=v1.vlen();
int m2rows=m2.mrow();
int m2cols=m2.mcol();
if(m1cols != m2rows)
{
cout<<Merrorstring[EMAT_INVALIDORDER];
throw Merrorstring[EMAT_INVALIDORDER];
}
IntVector v(m2cols);
IntMatrix flip(m2cols, m2rows);
for(i=0; i<m2rows; i++)
for(j=0; j<m2cols; j++)
flip[j][i]=m2[i][j];
//create the cross product
for(i=0; i<m2cols; i++){
v[i]=v1.dot(flip[i]);
}
return v;
}
//m=m1/x
IntMatrix operator/(IntMatrix & m1,int x)
{
//allocate the new IntMatrix
int len=m1.p->length;
int ven=vlen(m1.p->f[0]);
IntMatrix m(len,ven);
//do the operations
IntVector **f=m.p->f;
IntVector **f1=m1.p->f;
for(int i=0; i<len; i++)
*f[i]=*f1[i] / x;
return m;
}
//m=x+m1
IntMatrix operator+(int x, IntMatrix & m1)
{
//allocate the new IntMatrix
int len=m1.p->length;
int ven=vlen(m1.p->f[0]);
IntMatrix m(len,ven);
//do the operations
IntVector **f=m.p->f;
IntVector **f1=m1.p->f;
for(int i=0; i<len; i++)
*f[i]=x+ *f1[i];
return m;
}
//m=x-m1
IntMatrix operator-(int x, IntMatrix & m1)
{
//allocate the new IntMatrix
int len=m1.p->length;
int ven=vlen(m1.p->f[0]);
IntMatrix m(len,ven);
//do the operations
IntVector **f=m.p->f;
IntVector **f1=m1.p->f;
for(int i=0; i<len; i++)
*f[i]=x- *f1[i];
return m;
}
//m=x*m1
IntMatrix operator*(int x, IntMatrix & m1)
{
//allocate the new IntMatrix
int len=m1.p->length;
int ven=vlen(m1.p->f[0]);
IntMatrix m(len,ven);
//do the operations
IntVector **f=m.p->f;
IntVector **f1=m1.p->f;
for(int i=0; i<len; i++)
*f[i]=x* *f1[i];
return m;
}
//m=x/m1
IntMatrix operator/(int x, IntMatrix & m1)
{
//allocate the new IntMatrix
int len=m1.p->length;
int ven=vlen(m1.p->f[0]);
IntMatrix m(len,ven);
//do the operations
IntVector **f=m.p->f;
IntVector **f1=m1.p->f;
for(int i=0; i<len; i++)
*f[i]=x/ *f1[i];
return m;
}
//m=+m1
IntMatrix operator+(IntMatrix & m1)
{
return m1;
}
//m=-m1
IntMatrix operator-(IntMatrix & m1)
{
//allocate the new IntMatrix
int len=m1.p->length;
int ven=vlen(m1.p->f[0]);
IntMatrix m(len,ven);
//do the operations
IntVector **f=m.p->f;
IntVector **f1=m1.p->f;
for(int i=0; i<len; i++)
*f[i]=-*f1[i] ;
return m;
}
//m+=m1
IntMatrix & IntMatrix::operator+=(IntMatrix & m1)
{
//allocate the new IntMatrix
if(m1!=(*this))
{
cout<<"IntMatrix's dim are not equal";
}
int len=MIN(p->length,m1.p->length);
//do the operations
IntVector **f=p->f;
IntVector **f1=m1.p->f;
for(int i=0; i<len; i++)
*f[i] +=*f1[i] ;
return *this;
}
//m-=m1
IntMatrix & IntMatrix::operator-=(IntMatrix & m1)
{
//allocate the new IntMatrix
if(m1!=(*this))
{
cout<<"IntMatrix's dim are not equal";
}
int len=MIN(p->length,m1.p->length);
//do the operations
IntVector **f=p->f;
IntVector **f1=m1.p->f;
for(int i=0; i<len; i++)
*f[i] -=*f1[i] ;
return *this;
}
//m/=m1
IntMatrix & IntMatrix::operator/=(IntMatrix & m1)
{
//allocate the new IntMatrix
if(m1!=(*this))
{
cout<<"IntMatrix's dim are not equal";
}
int len=MIN(p->length,m1.p->length);
//do the operations
IntVector **f=p->f;
IntVector **f1=m1.p->f;
for(int i=0; i<len; i++)
*f[i] /=*f1[i] ;
return *this;
}
//m+=x
IntMatrix & IntMatrix::operator+=(int x)
{
//allocate the new IntMatrix
int len=p->length;
//do the operations
IntVector **f=p->f;
for(int i=0; i<len; i++)
*f[i] +=x ;
return *this;
}
//m-=x
IntMatrix & IntMatrix::operator-=(int x)
{
//allocate the new IntMatrix
int len=p->length;
//do the operations
IntVector **f=p->f;
for(int i=0; i<len; i++)
*f[i] -=x ;
return *this;
}
//m*=x
IntMatrix & IntMatrix::operator*=(int x)
{
//allocate the new IntMatrix
int len=p->length;
//do the operations
IntVector **f=p->f;
for(int i=0; i<len; i++)
*f[i] *=x ;
return *this;
}
//m/=x
IntMatrix & IntMatrix::operator/=(int x)
{
//allocate the new IntMatrix
int len=p->length;
//do the operations
IntVector **f=p->f;
for(int i=0; i<len; i++)
*f[i] /=x ;
return *this;
}
//m=m1.prod(m2)
IntMatrix IntMatrix::prod(IntMatrix & m2)
{
//allocate the new IntMatrix
if(m2!=(*this))
{
cout<<"IntMatrix's dim are not equal";
}
int len=MIN(p->length,m2.p->length);
int ven=MIN(vlen(p->f[0]),vlen(m2.p->f[0]));
IntMatrix m(len,ven);
//do the operations
IntVector **f=m.p->f;
IntVector **f1=p->f;
IntVector **f2=m2.p->f;
for(int i=0; i<len; i++)
*f[i]=*f1[i] * *f2[i];
return m;
}
//m=prod(m1,m2)
IntMatrix prod(IntMatrix & m1,IntMatrix & m2)
{
//allocate the new IntMatrix
if(m1!=m2)
{
cout<<"IntMatrix's dim are not equal";
}
int len=MIN(m1.p->length,m2.p->length);
int ven=MIN(vlen(m1.p->f[0]),vlen(m2.p->f[0]));
IntMatrix m(len,ven);
//do the operations
IntVector **f=m.p->f;
IntVector **f1=m1.p->f;
IntVector **f2=m2.p->f;
for(int i=0; i<len; i++)
*f[i]=*f1[i] * *f2[i];
return m;
}
//v=m[i]
IntVector & IntMatrix::operator[](int i)
{
if((i>=0)&&(i<p->length))
return *p->f[i];
else{
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
//next statement has no effect, the only function is avoid warnning message.
return *p->f[0];
}
}
int & IntMatrix::operator()(int i,int j)
{
if( (i>=0)&&(i<p->length)&&(j>=0)&&(j<vlen(p->f[0]) ) )
return (*p->f[i])[j];
else{
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
//next statement has no effect, the only function is avoid warnning message.
return (*p->f[0])[0];
}
}
//x=(m1==m2) :if m1==m2 returns 1. otherwise, returns 0.
int operator==(IntMatrix & m1, IntMatrix & m2)
{
if((m1.p->length !=m2.p->length) ||
(vlen(m1.p->f[0]) != vlen(m2.p->f[0])))
return 0;
else
// int len=m1.p->length;
//do the operations
// IntVector **f1=m1.p->f;
// IntVector **f2=m2.p->f;
// for(int i=0; i<len; i++)
// if(*f1[i] != *f2[i])
// return 0;
return 1;
}
//x=(m1 !=m2) :if m1!=m2 returns 1. otherwise, returns 0.
int operator!=(IntMatrix & m1, IntMatrix & m2)
{
return !(m2==m1);
}
//cin>>m1
istream & operator>>(istream & in, IntMatrix & m1)
{
int nrow=m1.mrow();
int ncol=m1.mcol();
for(int i=0; i<nrow; i++){
for(int j=0; j<ncol; j++)
in>>m1[i][j];
}
cout<<"\n";
return in;
}
//cout<<m1
ostream & operator<<(ostream & out, IntMatrix & m1)
{
int row=mrow(m1);
int col=mcol(m1);
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
out<<m1(i,j)<<" ";
out<<"\n ";
}
return out;
}
//returns a special row of a IntMatrix
IntVector IntMatrix::row(int n)
{
if((n>p->length)||(n<=0))
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
return ::copy(*(p->f[n-1]));
}
IntVector row(IntMatrix & m,int n)
{
if((n>m.mrow())||(n<=0))
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
return copy(*(m.p->f[n-1]));
}
//returns a special column of a IntMatrix
IntVector IntMatrix::col(int n)
{
int mrows=p->length;
int mcols=vlen(p->f[0]);
if((n>mcols)||(n<=0))
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
IntVector x(mrows);
for(int i=0; i<mrows; i++)
x[i]=(*this)[i][n-1];
return x;
}
IntVector col(IntMatrix & m, int n)
{
int mrows=m.mrow();
int mcols=m.mcol();
if((n>mcols)||(n<=0))
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
IntVector x(mrows);
for(int i=0; i<mrows; i++)
x[i]=m[i][n-1];
return x;
}
//mresult=x.slice(n,m): returns a sub IntMatrix of x. it is n*m and
//mresult[0][0]=x[0][0].
IntMatrix IntMatrix::slice(int row,int col)
{
int mrows=p->length;
int mcols=vlen(p->f[0]);
if((row>mrows)||(col>mcols)||(row<0)||(col<0))
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
IntMatrix x(row,col);
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
x[i][j]=(*this)[i][j];
return x;
}
IntMatrix slice(IntMatrix & m,int row,int col)
{
int mrows=m.mrow();
int mcols=m.mcol();
if((row>mrows)||(col>mcols)||(row<0)||(col<0))
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
IntMatrix x(row,col);
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
x[i][j]=m[i][j];
return x;
}
//mresult=x.slice(n1,m1,n2,m2): returns a subIntMatrix of x. it is
//n2*m2 and mresult[0][0]=x[n1][m1].
IntMatrix IntMatrix::slice(int row1,int col1,
int row2,int col2)
{
int mrows=p->length;
int mcols=vlen(p->f[0]);
if((row2<0)||(col2<0))
{
cout<<Merrorstring[EMAT_ASSIGNDATAERR];
throw Merrorstring[EMAT_ASSIGNDATAERR];
}
if((row1+row2>mrows)||(col1+col2>mcols)||(row1<0)||(col1<0))
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
IntMatrix x(row2,col2);
for(int i=row1; i<row1+row2; i++)
for(int j=col1; j<col1+col2; j++)
x[i-row1][j-col1]=(*this)[i][j];
return x;
}
IntMatrix slice(IntMatrix & m,int row1,
int col1,int row2,int col2)
{
int mrows=m.mrow();
int mcols=m.mcol();
if((row2<0)||(col2<0))
{
cout<<Merrorstring[EMAT_ASSIGNDATAERR];
throw Merrorstring[EMAT_ASSIGNDATAERR];
}
if((row1+row2>mrows)||(col1+col2>mcols)||(row1<0)||(col1<0))
{
cout<< Merrorstring[EMAT_OUTOFRANGE];
throw Merrorstring[EMAT_OUTOFRANGE];
}
IntMatrix x(row2,col2);
for(int i=row1; i<row1+row2; i++)
for(int j=col1; j<col1+col2; j++)
x[i-row1][j-col1]=m[i][j];
return x;
}
IntMatrix commat(IntMatrix & m1,
IntMatrix & m2, int select)
{
int i,j,m,n,minc,ninc;
int mrow1=m1.mrow();
int mcol1=m1.mcol();
int mrow2=m2.mrow();
int mcol2=m2.mcol();
switch(select)
{
case 0:
m=mrow1+mrow2;
n=mcol1+mcol2;
minc=mrow1;
ninc=mcol1;
break;
case 1:
m=MAX(mrow1,mrow2);
n=mcol1+mcol2;
minc=0;
ninc=mcol1;
break;
case 2:
m=mrow1+mrow2;
n=MAX(mcol1,mcol2);
minc=mrow1;
ninc=0;
break;
default:
{
cout<<Merrorstring[EMAT_ASSIGNDATAERR];
throw Merrorstring[EMAT_ASSIGNDATAERR];
}
;
}
IntMatrix x(m,n);
for(i=0; i<mrow1; i++)
for(j=0; j<mcol1; j++)
x[i][j]=m1[i][j];
for(i=0; i<mrow2; i++)
for(j=0; j<mcol2; j++)
x[i+minc][j+ninc]=m2[i][j];
return x;
}
IntMatrix IntMatrix::commat(IntMatrix & m2,
int select)
{
return ::commat(*this,m2,select);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -