CODE
CODE
#include <stdlib.h>
#include <iostream.h>
#include "matrix.h"
// allocate
void Matrix::allocate (){
mat=new Type*[rows];
for (int i=0;i<=rows;i++) {
mat[i]=new Type[cols];
for (int j=0;j<=cols;j++) mat[i][j]=Type(0);
}
}
// release
void Matrix::release (){
for (int i=0;i<=rows;i++) delete [] mat[i];
delete [] mat;
}
// copy matrix
void Matrix::copy (const Matrix& right){
for (int i=0;i<=rows;i++)
for (int j=0;j<=cols;j++) this->mat[i][j]=right.mat[i][j];
}
// constructor
Matrix::Matrix (int m, int n): rows(m), cols(n){
allocate();
}
// copy constructor
Matrix::Matrix (const Matrix& right): rows(right.rows),cols(right.cols){
allocate();
copy(right);
}
// destructor
Matrix::~Matrix (){
release ();
}
Matrix& Matrix:: operator = (const Matrix& right){
if (&right==this) return *this;
if (right.rows!=rows || right.cols!=cols) return *this;
release();
allocate();
copy(right);
return *this;
}
Type& Matrix::operator () (int i, int j) {
static Type error(0);
if (i<0 || i>rows || j<0 || j>cols) return error;
return mat[i-1][j-1];
}
int operator == (const Matrix& left, const Matrix& right) {
if (right.rows!=left.rows || right.cols!=left.cols) return 0;
for (int i=0;i<=left.rows;i++)
for (int j=0;j<=left.cols;j++)
if (left.mat[i][j]!=right.mat[i][j]) return 0;
return 1;
}
int operator != (const Matrix& left, const Matrix& right) {
return !(left==right);
}
Matrix operator + (const Matrix& m) {return m;}
Matrix operator - (const Matrix& m) {return -Type(1)*m;}
Matrix operator + (const Matrix& left, const Matrix& right) {
Matrix m=left;
return m+=right;
}
Matrix operator - (const Matrix& left, const Matrix& right) {
Matrix m=left;
return m-=right;
}
Matrix& Matrix :: operator+= (const Matrix& right) {
if (rows!=right.rows || cols!=right.cols) return *this;
for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
mat[i][j]+=right.mat[i][j];
return *this;
}
Matrix& Matrix :: operator-= (const Matrix& right) {
if (rows!=right.rows || cols!=right.cols) return *this;
for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
mat[i][j]-=right.mat[i][j];
return *this;
}
Matrix operator * (const Matrix& left, const Matrix& right) {
Matrix m (left.rows, right.cols);
if (left.cols!=right.rows) return m;
for (int i=0;i<left.rows;i++)
for (int j=0;j<right.cols;j++)
for (int k=0;k<left.cols;k++)
m.mat[i][j]+=left.mat[i][k]*right.mat[k][j];
return m;
}
Matrix& Matrix :: operator*= (const Type& t) {
for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
mat[i][j]*=t;
return *this;
}
Matrix operator * (const Matrix& left, const Type& t){
Matrix m=left;
return m*=t;
}
Matrix operator * (const Type& t, const Matrix& right){
return right*t;
}
Matrix& Matrix::operator *= (const Matrix& right) {
return *this=*this*right;
}
istream& operator >> (istream& is, Matrix& m) {
for (int i=0;i<m.rows;i++)
for (int j=0;j<m.cols;j++)
is>>m.mat[i][j];
return is;
}
ostream& operator << (ostream& os, const Matrix& m) {
os<<"{\n";
for (int i=0;i<m.rows;i++) {
os <<"{";
for (int j=0;j<m.cols;j++)
os << m.mat[i][j] << ((j<m.cols-1)?",":"}");
os << ((i<m.rows-1)?",\n":"\n}\n");
}
return os;
}
// transpose matrix
Matrix trans (const Matrix& m) {
Matrix temp(m.cols, m.rows);
for (int i=0;i<m.rows;i++)
for (int j=0;j<m.cols;j++)
temp.mat[j][i]=m.mat[i][j];
return temp;
}
// one row and col is out
Matrix compl (const Matrix& m, int r, int c) {
Matrix temp(m.rows-1, m.cols-1);
if (m.cols<1 || m.rows<1) return temp;
for (int i=0,x=0;i<m.rows;i++) {
if (i==r-1) continue;
for (int j=0,y=0;j<m.cols;j++) {
if (j==c-1) continue;
temp.mat[x][y]=m.mat[i][j];
y++;
}
x++;
}
return temp;
}
// det matrix
Type det (const Matrix& m) {
if (m.rows!=m.cols) return 0;
if (m.rows==1) return m.mat[0][0];
Type temp=Type(0);
for (int j=0,c=1;j<m.cols;j++,c*=-1)
temp+=(TypeŠ*m.mat[0][j]*det(compl(m,1,j+1)));
return temp;
}
// cofact
Type cofact (const Matrix& m, int r, int c) {
if (m.rows!=m.cols) return 0;
if ((r+c)%2!=0)
return (-det (compl(m,r,c)));
else
return (det (compl(m,r,c)));
}
// adj matrix
Matrix adj (const Matrix& m) {
Matrix temp(m.rows, m.cols);
if (m.rows!=m.cols) return temp;
for (int i=0;i<m.rows;i++)
for (int j=0;j<m.cols;j++)
temp.mat[i][j]=cofact(m,i+1,j+1);
return trans(temp);
}
// invers matrix
Matrix inv (const Matrix& m) {
Type temp=det(m);
if (temp!=0)
return (1/temp)*adj(m);
else
return Matrix(0,0);
}
void main () {
#define NMAX 4
Matrix m (NMAX,NMAX);
cin>>m;
const Matrix& m1=m;
const Matrix m2=inv (m1);
cout<<"m="<<m2<<'\n';
}
Where:
CODE
CODE
// Modul matrix.h
#ifndef _Matrix
#define _Matrix
class istream;
class ostream;
typedef int Type;
class Matrix{
public:
Matrix (int m, int n);
Matrix (const Matrix& mat);
~Matrix ();
Matrix& operator = (const Matrix&);
friend int operator == (const Matrix&, const Matrix&);
friend int operator != (const Matrix&, const Matrix&);
Type& operator () (int i, int j);
friend Matrix operator + (const Matrix&);
friend Matrix operator - (const Matrix&);
friend Matrix operator + (const Matrix&, const Matrix&);
friend Matrix operator - (const Matrix&, const Matrix&);
friend Matrix operator * (const Matrix&, const Matrix&);
friend Matrix operator * (const Matrix&, const Type&);
friend Matrix operator * (const Type&, const Matrix&);
friend Matrix trans (const Matrix&);
friend Matrix compl (const Matrix&, int i, int j);
friend Type det (const Matrix&);
friend Type cofact (const Matrix&, int i, int j);
friend Matrix adj (const Matrix&);
friend Matrix inv (const Matrix&);
Matrix& operator += (const Matrix&);
Matrix& operator -= (const Matrix&);
Matrix& operator *= (const Matrix&);
Matrix& operator *= (const Type&);
friend istream& operator >> (istream&, Matrix&);
friend ostream& operator << (ostream&, const Matrix&);
protected:
void allocate ();
void release ();
void copy (const Matrix&);
private:
int cols, rows;
Type **mat;
};
#endif
You can also see doc file.
I can figure out what is problem with main function. It allow me to write elements of matrix but not to see what is hepenning in output. Can somebody help me with this?
*mod edit - added code tags
This post has been edited by jjhaag: 6 Jan, 2008 - 10:44 AM