B
Bushido Hacks
I've come up with a good set of Matrix codes recently when I cam to a
road block. I would like to know if I should destroy an object to
create a new object with new dimmensions.
class Matrix{
public:
Matrix(int = 4, int = 4);
~Matrix();
Matrix &setDims(int, int);
Matrix &setRows(int);
Matrix &setCols(int);
Matrix &setVal(int,int,float);
Matrix &setInitVals();
Matrix &setIdentity();
Matrix &preMultiply(Matrix&);
int getRows() const;
int getCols() const;
float getVal(int,int) const;
const Matrix &operator=(const Matrix&);
private:
int rows, cols;
float** m;
};
Matrix::Matrix(int rows, int cols){
setDims(rows,cols);
m = new float*[getRows()];
for(int i = 0; i < getRows(); i++){
m = new float[getCols()];
};
setInitVals(); // this step is neccessary
};
Matrix::~Matrix(){
for(int i = getRows() - 1; i > -1; i--){
delete[] m;
};
delete[] m;
};
Matrix &Matrix::setDims(int rows,int
cols){setRows(rows);setCols(cols);return *this;};
Matrix &Matrix::setRows(int rows){this->rows = rows;return *this;};
Matrix &Matrix::setCols(int cols){this->cols = cols;return *this;};
Matrix &Matrix::setVal(int row, int col, float val){m[row][col] =
val;};
int Matrix::getRows() const {return rows;};
int Matrix::getCols() const {return cols;};
float Matrix::getVal(int row,int col) const {return m[row][col];};
Matrix &Matrix::setIdentity(){
for(int i = 0; i < this->getRows();i++){
for(int j = 0; j < this->getCols();j++){
this->setVal(i,j,static_cast<float>(i == j));
};
};
return *this;
};
Matrix &Matrix:reMultiply(Matrix &n){
// m[j] = m[k] * n[k][j]
try{
if(this->getCols() != n.getRows()) throw 1;
Matrix t(this->getRows(),n.getCols());
for(int i = 0; i < t.getRows(); i++){
for(int j = 0; j < t.getCols(); j++){
t.setVal(i,j,0.0);
for(int k = 0; k < t.getRows();k++){
t.setVal(i,j,t.getVal(i,j) + this->getVal(i,k) * n.getVal(k,j));
};
};
};
if(t.getRows() != n.getRows()) throw 2;
/* ========= AREA OF INTEREST ========== */
n = t; // Should I destroy object n so that a
// new n with t's dimmensions replaces it?
}
catch(int e){
cout << "Exception type " << e << " occured." << endl;
switch(e){
case 1:
case 2:
cout << "Insufficent matrix dimmesions." << endl;
break;
default:
cout << "Unknown exception type " << e << " occured." << endl;
};
}
catch(...){
cout << "Unknown exception occured." << endl;
};
};
const Matrix &Matrix:perator=(const Matrix &rhs){
for(int i = 0; i < rhs.getRows();i++){
for(int j = 0; j < rhs.getCols();j++){
this->setVal(i,j,rhs.getVal(i,j));
};
};
return *this;
};
road block. I would like to know if I should destroy an object to
create a new object with new dimmensions.
class Matrix{
public:
Matrix(int = 4, int = 4);
~Matrix();
Matrix &setDims(int, int);
Matrix &setRows(int);
Matrix &setCols(int);
Matrix &setVal(int,int,float);
Matrix &setInitVals();
Matrix &setIdentity();
Matrix &preMultiply(Matrix&);
int getRows() const;
int getCols() const;
float getVal(int,int) const;
const Matrix &operator=(const Matrix&);
private:
int rows, cols;
float** m;
};
Matrix::Matrix(int rows, int cols){
setDims(rows,cols);
m = new float*[getRows()];
for(int i = 0; i < getRows(); i++){
m = new float[getCols()];
};
setInitVals(); // this step is neccessary
};
Matrix::~Matrix(){
for(int i = getRows() - 1; i > -1; i--){
delete[] m;
};
delete[] m;
};
Matrix &Matrix::setDims(int rows,int
cols){setRows(rows);setCols(cols);return *this;};
Matrix &Matrix::setRows(int rows){this->rows = rows;return *this;};
Matrix &Matrix::setCols(int cols){this->cols = cols;return *this;};
Matrix &Matrix::setVal(int row, int col, float val){m[row][col] =
val;};
int Matrix::getRows() const {return rows;};
int Matrix::getCols() const {return cols;};
float Matrix::getVal(int row,int col) const {return m[row][col];};
Matrix &Matrix::setIdentity(){
for(int i = 0; i < this->getRows();i++){
for(int j = 0; j < this->getCols();j++){
this->setVal(i,j,static_cast<float>(i == j));
};
};
return *this;
};
Matrix &Matrix:reMultiply(Matrix &n){
// m[j] = m[k] * n[k][j]
try{
if(this->getCols() != n.getRows()) throw 1;
Matrix t(this->getRows(),n.getCols());
for(int i = 0; i < t.getRows(); i++){
for(int j = 0; j < t.getCols(); j++){
t.setVal(i,j,0.0);
for(int k = 0; k < t.getRows();k++){
t.setVal(i,j,t.getVal(i,j) + this->getVal(i,k) * n.getVal(k,j));
};
};
};
if(t.getRows() != n.getRows()) throw 2;
/* ========= AREA OF INTEREST ========== */
n = t; // Should I destroy object n so that a
// new n with t's dimmensions replaces it?
}
catch(int e){
cout << "Exception type " << e << " occured." << endl;
switch(e){
case 1:
case 2:
cout << "Insufficent matrix dimmesions." << endl;
break;
default:
cout << "Unknown exception type " << e << " occured." << endl;
};
}
catch(...){
cout << "Unknown exception occured." << endl;
};
};
const Matrix &Matrix:perator=(const Matrix &rhs){
for(int i = 0; i < rhs.getRows();i++){
for(int j = 0; j < rhs.getCols();j++){
this->setVal(i,j,rhs.getVal(i,j));
};
};
return *this;
};