1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include "matrixType.h"
#include <assert.h>
template<class elemType>
MatrixType<elemType>& MatrixType<elemType>::operator=(const MatrixType& other)
{
if(this == &other)
return *this;
else
{
delete [] matrix;
rowS = other.rowS;
colS = other.colS;
matrix = new elemType[rowS*colS]; //This is where the program
//crashes when it tries to make an array of 0
// elements, due to A+B deallocating before it can be
// used.
assert(matrix != NULL);
for(int k = 0; k < rowS; k++)
for(int i = 0; i < colS; i++)
matrix[k*colS+i] = other.matrix[k*colS+i];
}
return *this;
}
template<class elemType>
MatrixType<elemType>& MatrixType<elemType>::operator+= (const MatrixType& other)
{
if(this->rowS == other.rowS & this->colS == other.colS)
{
for(int i = 0; i < this->rowS; i++)
for(int k = 0; k < this->colS; k++)
this->matrix[i*colS+k] = this->matrix[i*colS+k]+other(i+1,k+1);
}
else
{
cerr << "Different Sizes." << endl;
}
return *this;
}
template<class elemType>
MatrixType<elemType>& MatrixType<elemType>::operator-= (const MatrixType& other)
{
if(this->rowS == other.rowS & this->colS == other.colS)
{
for(int i = 0; i < this->rowS; i++)
for(int k = 0; k < this->colS; k++)
this->matrix[i*colS+k] = this->matrix[i*colS+k]-other(i+1,k+1);
}
else
{
cerr << "Different Sizes." << endl;
}
return *this;
}
template<class elemType>
MatrixType<elemType>& MatrixType<elemType>::operator*= (const MatrixType& other)
{
if(this->colS == other.rowS)
{
MatrixType<elemType> temp(this->rowS, other.colS);
for(int i = 1 ; i <= temp.rowS ; i++)
for(int j = 1 ; j <= temp.colS ; j++)
{
temp(i,j) = 0;
for(int k = 1 ;k <= temp.colS ; k++)
temp(i,j) += (*this)(i,k)*temp(k,j);
}
*this = temp;
}
else
cerr << "Number of Columns in A and Rows in B are not the same." << endl;
return *this;
}
template<class elemType>
const MatrixType<elemType>& MatrixType<elemType>::operator+ (const MatrixType& other) const
{
return MatrixType<elemType>(*this) += other;
}
template<class elemType>
const MatrixType<elemType>& MatrixType<elemType>::operator- (const MatrixType& other) const
{
return MatrixType<elemType>(*this) -= other;
}
template<class elemType>
const MatrixType<elemType>& MatrixType<elemType>::operator* (const MatrixType& other) const
{
return MatrixType<elemType>(*this) *= other;
}
template<class elemType>
const MatrixType<elemType>& MatrixType<elemType>::operator/ (const MatrixType& other) const
{
if(this->colS == other.rowS)
{
MatrixType<elemType> result(this->rowS, other.colS);
for(int i = 1 ; i <= result.rowS ; i++)
for(int j = 1 ; j <= result.colS ; j++)
{
result(i,j) = 0;
for(int k = 1 ;k <= result.colS ; k++)
result(i,j) += (*this)(i,k)/other(k,j);
}
}
return result;
}
template<class elemType>
elemType& MatrixType<elemType>::operator() (const int row, const int col)
{
if(row < 1 || row > rowS || col < 1 || col > colS)
cerr << "Selection Out of Bounds: " << endl;
else
return matrix[(row-1)*colS+col-1];
}
template<class elemType>
elemType MatrixType<elemType>::operator() (const int row, const int col) const
{
if(row < 1 || row > rowS || col < 1 || col > colS)
cerr << "Selection Out of Bounds: " << endl;
else
return matrix[(row-1)*colS+col-1];
}
template<class elemType>
void MatrixType<elemType>::print() const
{
for(int i = 0; i < rowS+1; i++)
for(int k = 0; k < colS+1; k++)
cout << "[" << (i+1) << "," << (k+1) << "] " << matrix[i*colS+k] << endl;
}
//constructors
template<class elemType>
MatrixType<elemType>::MatrixType(int rows, int cols)
{
if(rows < 1 || cols < 1)
{
cerr << "The matrix must be atleast 1x1. Creating a 1x1 matrix."<<endl;
rowS = 1;
colS = 1;
}
else
{
rowS = rows;
colS = cols;
}
matrix = new elemType[rowS*colS];
assert(matrix != NULL);
}
template<class elemType>
MatrixType<elemType>::MatrixType(const MatrixType& other)
{
rowS = other.rowS;
colS = other.colS;
matrix = new elemType[rowS*colS];
assert(matrix != NULL);
for(int k = 0; k < rowS; k++)
for(int i = 0; i < colS; i++)
matrix[k*colS+i] = other.matrix[k*colS+i];
}
template<class elemType>
MatrixType<elemType>::~MatrixType()
{
delete [] matrix;
}
|