<matrix.cpp>
#include<fstream.h>
#include<process.h>
#include"d:\tcplus\hw\ma\matrix.h"
Matrix::Matrix()
{
m=n=0;
mpt=NULL;
}
Matrix::~Matrix()
{ delete [] mpt; }
Matrix::Matrix(unsigned m_in, unsigned n_in)
{
m=m_in;
n=n_in;
if ( (mpt=new double [m*n]) == NULL )
{
cerr << "Cann't get memory.\n";
exit(1);
}
for (int i=1; i<=m; i++) // Clean all element. //
for (int j=1; j<=n; j++)
change_elem(i,j,0);
}
Matrix::Matrix(const Matrix & m2)
{
unsigned i,j;
m=m2.m;
n=m2.n;
delete [] mpt;
mpt=new double [ m2.m*m2.n ];
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
change_elem(i, j, m2.show_elem(i, j));
}
double Matrix::show_elem(unsigned i, unsigned j) const
{
if ( i>m || j>n || i*j==0 )
{
cerr << "Wrong matrix subindex.\n";
exit(1);
}
return *( mpt+((i-1)*n+j-1) );
}
double Matrix::change_elem(unsigned i, unsigned j, double val)
{
if ( i>m || j>n || i*j==0 )
{
cerr << "Wrong matrix subindex.\n";
exit(1);
}
return ( *( mpt+((i-1)*n+j-1) )=val );
}
void Matrix::set_all_elem(void)
{
double val;
unsigned i,j;
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
cout << "a" << i << j << "=";
cin >> val;
change_elem(i,j,val);
}
cin.get();
}
void Matrix::set_all_elem(char * sourcefile)
{
ifstream fin;
fin.open(sourcefile, ios::in);
double val;
unsigned i,j;
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
fin >> val;
change_elem(i,j,val);
}
fin.close();
}
void Matrix::exchange_row(unsigned r1, unsigned r2)
{
double temp=0;
for (int j=1; j<=n; j++)
{
temp=show_elem(r1,j);
change_elem(r1, j, show_elem(r2,j));
change_elem(r2, j, temp);
}
}
void Matrix::exchange_column(unsigned c1, unsigned c2)
{
double temp=0;
for (int i=1; i<=m; i++)
{
temp=show_elem(i,c1);
change_elem(i, c1, show_elem(i,c2));
change_elem(i, c2, temp);
}
}
Matrix Matrix::operator*(const Matrix & m2) const
{
if ( n != m2.m )
{
cerr << "Wrong subindex of two multupling matrix.\n";
exit(1);
}
Matrix temp(m, m2.n);
double sum;
unsigned i,j,k;
for (i=1; i<=m; i++)
for (k=1; k<=m2.n; k++)
{
sum=0;
for (j=1; j<=m2.m; j++)
sum += ( show_elem(i,j) * m2.show_elem(j,k) );
temp.change_elem(i,k,sum);
}
return temp;
}
Matrix Matrix::operator*(double k) const
{
Matrix temp(m,n);
unsigned i,j;
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
temp.change_elem(i, j, k*show_elem(i,j));
return temp;
}
Matrix operator*(double k, const Matrix & m2)
{ return m2*k; }
Matrix Matrix::operator+(const Matrix & m2) const
{
if ( m!=m2.m || n!=m2.n )
{
cerr << "Wrong subindex of two adding matrix.\n";
exit(1);
}
Matrix temp(m,n);
unsigned i,j;
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
temp.change_elem(i, j, show_elem(i,j)+m2.show_elem(i,j));
return temp;
}
Matrix Matrix::operator-(const Matrix & m2) const
{
if ( m!=m2.m || n!=m2.n )
{
cerr << "Wrong subindex of two decreasing matrix.\n";
exit(1);
}
Matrix temp(m,n);
unsigned i,j;
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
temp.change_elem(i, j, show_elem(i,j)-m2.show_elem(i,j));
return temp;
}
Matrix Matrix::operator-() const
{ return (*this)*(-1); }
Matrix Matrix::operator=(const Matrix & m2)
{
unsigned i,j;
if ( this == &m2 )
{
Matrix temp(m,n);
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
temp.change_elem(i, j, show_elem(i,j));
return temp;
}
m=m2.m;
n=m2.n;
delete [] mpt;
mpt=new double [m*n];
Matrix temp(m,n);
for (i=1; i<=m; i++)
for (j=1; j<=m; j++)
{
change_elem(i, j, m2.show_elemilename) const
{
unsigned i,j;
ofstream fout;
fout.open(outfilename, ios::out|ios::app);
fout.precision(4);
fout.setf(ios::showpoint);
fout.setf(ios::fixed, ios::floatfield);
for (i=1; i<=m; i++)
{
for (j=1; j<=n; j++)
fout << show_elem(i,j) << ' ';
fout << '\n';
}
fout << '\n';
fout.close();
}
void Matrix::row_process(unsigned r1, double k1, unsigned r2, double k2)
{
if ( r1>m || r2>m || r1*r2*k1*k2==0 )
{
cerr << "Wrong subindex or multiply a row by zero.\n";
exit(1);
}
unsigned j;
for (j=1; j<=n; j++)
change_elem(r2, j, show_elem(r1,j)*k1+show_elem(r2,j)*k2);
}
void Matrix::row_process(unsigned r, double k)
{
if ( r>m || r==0 )
{
cerr << "Wrong subindex.\n";
exit(1);
}
if ( k==0 )
{
cerr << "Cann't multiply a row by zero.\n";
exit(1);
}
unsigned j;
for (j=1; j<=n; j++)
change_elem(r, j, show_elem(r,j)*k);
}
void Matrix::column_process(unsigned c1, double k1, unsigned c2, double k2)
{
if ( c1>n || c2>n || c1*c2*k1*k2==0 )
{
cerr << "Wrong subindex or multiply a column by zero.\n";
exit(1);
}
unsigned i;
for (i=1; i<=m; i++)
change_elem(i, c2, show_elem(i,c1)*k1+show_elem(i,c2)*k2);
}
void Matrix::column_process(unsigned c, double k)
{
if ( c>n || c==0 )
{
cerr << "Wrong subindex.\n";
exit(1);
}
if ( k==0 )
{
cerr << "Cann't multiply a column by zero.\n";
exit(1);
}
unsigned i;
for (i=1; i<=m; i++)
change_elem(i, c, show_elem(i,c)*k);
}
Matrix Matrix::transpose(void)
{
unsigned i,j;
unsigned long index=0;
Matrix temp(n,m);
for (i=1; i<=n; i++)
for (j=1; j<=m; j++,index++)
*(temp.mpt+index)=show_elem(j,i);
return temp;
}
double Matrix::determine(void) const
{
if ( m!=n )
{
cerr << "The matrix is not a square matrix and cann't determine its value.\n";
return NULL;
}
unsigned i,j,k;
double sum1,sum2,dec1,dec2;
sum2=dec2=0;
for (i=1; i<=m; i++)
{
sum1=1;
for (j=i,k=1; k<=n; j++,k++)
{
if (j>m)
j=j%m;
sum1*=show_elem(j,k);
}
sum2+=sum1;
}
for (i=3; i>0; i--)
{
dec1=1;
for (j=i, k=1; k<=n; k++,j--)
{
if (j == 0)
j=m;
dec1*=show_elem(j,k);
}
dec2+=dec1;
}
return sum2-dec2;
}
ostream & operator<<(ostream & os, Matrix & ma)
{
unsigned i,j;
os.precision(2);
os.setf(ios::showpoint);
os.setf(ios::fixed, ios::floatfield);
for (i=1; i<=ma.m; i++)
{
for (j=1; j<=ma.n; j++)
os << ma.show_elem(i,j) << ' ';
os << '\n';
}
os << '\n';
return os;
}
fstream & operator<<(fstream & fs, Matrix & ma)
{
unsigned i,j;
fs.precision(2);
fs.setf(ios::showpoint);
fs.setf(ios::fixed, ios::floatfield);
for (i=1; i<=ma.m; i++)
{
for (j=1; j<=ma.n; j++)
fs << ma.show_elem(i,j) << ' ';
fs << '\n';
}
fs << '\n';
return fs;
}(i,j));
temp.change_elem(i, j, m2.show_elem(i,j));
}
rn temp;
}
void Matrix::show_all(void) const
{
unsigned i,j;
cout << "Showing matrix...\n";
cout.precision(2);
cout.setf(ios::showpoint);
cout.setf(ios::fixed, ios::floatfield);
for (i=1; i<=m; i++)
{
for (j=1; j<=n; j++)
cout << show_elem(i,j) << ' ';
cout << '\n';
}
cout << '\n';
}
void Matrix::show_all(char * outfilename) const
{
unsigned i,j;
ofstream fout;
fout.open(outfilename, ios::out|ios::app);
fout.precision(4);
fout.setf(ios::showpoint);
fout.setf(ios::fixed, ios::floatfield);
for (i=1; i<=m; i++)
{
for (j=1; j<=n; j++)
fout << show_elem(i,j) << ' ';
fout << '\n';
}
fout << '\n';
fout.close();
}
void Matrix::row_process(unsigned r1, double k1, unsigned r2, double k2)
{
if ( r1>m || r2>m || r1*r2*k1*k2==0 )
{
cerr << "Wrong subindex or multiply a row by zero.\n";
exit(1);
}
unsigned j;
for (j=1; j<=n; j++)
change_elem(r2, j, show_elem(r1,j)*k1+show_elem(r2,j)*k2);
}
void Matrix::row_process(unsigned r, double k)
{
if ( r>m || r==0 )
{
cerr << "Wrong subindex.\n";
exit(1);
}
if ( k==0 )
{
cerr << "Cann't multiply a row by zero.\n";
exit(1);
}
unsigned j;
for (j=1; j<=n; j++)
change_elem(r, j, show_elem(r,j)*k);
}
void Matrix::column_process(unsigned c1, double k1, unsigned c2, double k2)
{
if ( c1>n || c2>n || c1*c2*k1*k2==0 )
{
cerr << "Wrong subindex or multiply a column by zero.\n";
exit(1);
}
unsigned i;
for (i=1; i<=m; i++)
change_elem(i, c2, show_elem(i,c1)*k1+show_elem(i,c2)*k2);
}
void Matrix::column_process(unsigned c, double k)
{
if ( c>n || c==0 )
{
cerr << "Wrong subindex.\n";
exit(1);
}
if ( k==0 )
{
cerr << "Cann't multiply a column by zero.\n";
exit(1);
}
unsigned i;
for (i=1; i<=m; i++)
change_elem(i, c, show_elem(i,c)*k);
}
Matrix Matrix::transpose(void)
{
unsigned i,j;
unsigned long index=0;
Matrix temp(n,m);
for (i=1; i<=n; i++)
for (j=1; j<=m; j++,index++)
*(temp.mpt+index)=show_elem(j,i);
return temp;
}
double Matrix::determine(void) const
{
if ( m!=n )
{
cerr << "The matrix is not a square matrix and cann't determine its value.\n";
return NULL;
}
unsigned i,j,k;
double sum1,sum2,dec1,dec2;
sum2=dec2=0;
for (i=1; i<=m; i++)
{
sum1=1;
for (j=i,k=1; k<=n; j++,k++)
{
if (j>m)
j=j%m;
sum1*=show_elem(j,k);
}
sum2+=sum1;
}
for (i=3; i>0; i--)
{
dec1=1;
for (j=i, k=1; k<=n; k++,j--)
{
if (j == 0)
j=m;
dec1*=show_elem(j,k);
}
dec2+=dec1;
}
return sum2-dec2;
}
ostream & operator<<(ostream & os, Matrix & ma)
{
unsigned i,j;
os.precision(2);
os.setf(ios::showpoint);
os.setf(ios::fixed, ios::floatfield);
for (i=1; i<=ma.m; i++)
{
for (j=1; j<=ma.n; j++)
os << ma.show_elem(i,j) << ' ';
os << '\n';
}
os << '\n';
return os;
}
fstream & operator<<(fstream & fs, Matrix & ma)
{
unsigned i,j;
fs.precision(2);
fs.setf(ios::showpoint);
fs.setf(ios::fixed, ios::floatfield);
for (i=1; i<=ma.m; i++)
{
for (j=1; j<=ma.n; j++)
fs << ma.show_elem(i,j) << ' ';
fs << '\n';
}
fs << '\n';
return fs;
}
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: t195-250.dialup.seed.net.tw