Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ANSWER IN C++ CSC331 project In this project you will be defining a Template Mat

ID: 3902631 • Letter: A

Question

ANSWER IN C++



CSC331 project In this project you will be defining a Template Matrix class in C++ and equip the class with a few matrix operations. If the code does not compile- no credit is given. The code must compile, and the main function execute successfully The Matrix class must have the following private data members at least: 1. A variable to represent rows for the number of rows of the matrix 2. A variable column for the number of columns of the matrix 3. A pointer variable data to point to the matrix data. The Matrix class must also have the following member functions I. A default constructor this constructor initializes the row and column of the matrix to 3, then dynamically allocates memory for the elements of the matrix. The constructor also initializes the Matrix elements to random integers between 0 and 10. A constructor with parameters The parameters to the constructor are used to initialize the row and column variables. This constructor dynamically allocates memory for the elements of the matrix. The constructor also initializes the Matrix elements to random integers between 0 and 10. 3. A correct destructor 4. A method called display0 that displays the matrix in proper row/column form 5. The method get Element(int i, int j) that will return the (ij) element in the matrix. Include exception handling with object out of range for the case when i or j or both are out of bounds 6. The method setElement ( int i, int j, int value) that set the (ij) element of the matrix to the value. 7. The method for the addition of one matric object to another add(Matrix a) that receives a Matrix 8· The method multiply multiply ( Matrix a) that receives a Matrix object and multiplies it to the multiplies (incompatible dimensions). An algorithm for matrix multiplication is given on page 10. Overload the in stream operator"> for the Matrix class to receive elements for a Matrix object. Include exception handling with object out of range for the case when i or j or both are out of bounds and adds it to the implicit object and returns another Matrix object. Include exceptions if matrices cannot be added (e.g. in the case of incompatible types or dimensions) implicit object and returns another Matrix object, Include exceptions if matrices cannot be 205 of your textbook. t stream operator "for the Matrix class to display a Matrix Object 11. Make proper use of const and references. Write a main ..) method to text the Matrix class. The main(...) method must include: 1. Declare two 3x3 matrices 2. Test setElement) and getElement) methods 3. Test the exceptions by using invalid parameters 4. Use the operator " change the values of one of the matrices. P 6. Call the add method and display the result. 7. Call the multiply method and display the result. rint the new matrix.

Explanation / Answer

#ifndef matrix_h

#define matrix_h

#include <stdexcept>

#include <vector>

#include <function>

// undefine to disable range checking

#define RANGE_CHECK

class overdetermined : public std::domain_error

{

public:

overdetermined()

: std::domain_error("solution is over-determined")

{}

};

class underdetermined : public std::domain_error

{

public:

underdetermined()

: std::domain_error("solution is under-determined")

{}

};

template<class T>

class kahn_sum { // implements Kahn Summation method

public:

kahn_sum() : sum(0.0), cor(0.0) {}

kahn_sum<T>& operator+=( const T& val ) {

T old_sum = sum;

T next = val-cor;

cor = ( (sum += next) - old_sum ) - next;

return *this;

}

kahn_sum<T>& operator-=( const T& val ) {

T old_sum = sum;

T next = val+cor;

cor = ( (sum -= val) - old_sum ) + next;

return *this;

}

operator T&() { return sum; }

private:

T sum; // running sum

T cor; // correction term

};

template<class T>

class matrix {

private:

std::vector<T> elements; // array of elements

public:

const unsigned rows; // number of rows

const unsigned cols; // number of columns

  

protected:

// range check function for matrix access

void range_check( unsigned i, unsigned j ) const;

public:

T& operator()( unsigned i, unsigned j ) {

#ifdef RANGE_CHECK

range_check(i,j);

#endif

return elements[i*cols+j];

}

const T& operator()( unsigned i, unsigned j ) const {

#ifdef RANGE_CHECK

range_check(i,j);

#endif

return elements[i*cols+j];

}

const T& element(unsigned i, unsigned j) const {

#ifdef RANGE_CHECK

range_check(i,j);

#endif

return elements[i*cols+j];

}

T& element(unsigned i, unsigned j) {

#ifdef RANGE_CHECK

range_check(i,j);

#endif

return elements[i*cols+j];

}

public:

// constructors

matrix( unsigned rows, unsigned cols, const T* elements = 0 );

matrix( const matrix<T>& );

// destructor

~matrix();

// assignment

matrix<T>& operator=( const matrix<T>& );

// comparison

bool operator==( const matrix<T>& ) const;

bool iszero() const;

bool operator!() const {

return iszero();

}

// scalar multiplication/division

matrix<T>& operator*=( const T& a );

matrix<T> operator*( const T& a ) const {

return matrix<T>(*this).operator*=(a);

}

matrix<T>& operator/=( const T& a );

matrix<T> operator/( const T& a ) {

return matrix<T>(*this).operator/=(a);

}

matrix<T> operator-() const;

matrix<T> operator+() const;

// addition/subtraction

matrix<T>& operator+=( const matrix<T>& );

matrix<T>& operator-=( const matrix<T>& );

matrix<T> operator+( const matrix<T>& M ) const {

return matrix<T>(*this).operator+=(M);

}

matrix<T> operator-( const matrix<T>& M ) const {

return matrix<T>(*this).operator-=(M);

}

// matrix multiplication

matrix<T> operator*( const matrix<T>& ) const;

matrix<T>& operator*=( const matrix<T>& M ) {

return *this = *this * M;

}

// matrix division

matrix<T> leftdiv( const matrix<T>& ) const;

matrix<T> rightdiv( const matrix<T>& D ) const {

return transpose().leftdiv(D.transpose()).transpose();

}

matrix<T> operator/( const matrix<T>& D ) const {

return rightdiv(D);

}

matrix<T>& operator/=( const matrix<T>& M ) {

return *this = *this/M;

}

// determinants

matrix<T> minor( unsigned i, unsigned j ) const;

T det() const;

T minor_det( unsigned i, unsigned j ) const;

// these member functions are only valid for squares

matrix<T> inverse() const;

matrix<T> pow( int exp ) const;

matrix<T> identity() const;

bool isidentity() const;

// vector operations

matrix<T> getrow( unsigned j ) const;

matrix<T> getcol( unsigned i ) const;

matrix<T>& setcol( unsigned j, const matrix<T>& C );

matrix<T>& setrow( unsigned i, const matrix<T>& R );

matrix<T> delrow( unsigned i ) const;

matrix<T> delcol( unsigned j ) const;

matrix<T> transpose() const;

matrix<T> operator~() const {

return transpose();

}

};

template<class T>

matrix<T>::matrix( unsigned rows, unsigned cols, const T* elements = 0 )

: rows(rows), cols(cols), elements(rows*cols,T(0.0))

{

if( rows == 0 || cols == 0 )

throw std::range_error("attempt to create a degenerate matrix");

// initialze from array

if(elements)

for(unsigned i=0;i<rows*cols;i++)

this->elements[i] = elements[i];

};

template<class T>

matrix<T>::matrix( const matrix<T>& cp )

: rows(cp.rows), cols(cp.cols), elements(cp.elements)

{

}

template<class T>

matrix<T>::~matrix()

{

}

template<class T>

matrix<T>& matrix<T>::operator=( const matrix<T>& cp )

{

if(cp.rows != rows && cp.cols != cols )

throw std::domain_error("matrix op= not of same order");

for(unsigned i=0;i<rows*cols;i++)

elements[i] = cp.elements[i];

return *this;

}

template<class T>

void matrix<T>::range_check( unsigned i, unsigned j ) const

{

if( rows <= i )

throw std::range_error("matrix access row out of range");

if( cols <= j )

throw std::range_error("matrix access col out of range");

}

//not shown: operator==(const matrix<T>& A) const, iszero() const,

//operator*=(const T& a), operator/=(const T& a), operator-() const,

//operator+() const, operator*(const T& a, const matrix<T>& M),

//operator+=(const matrix<T>& M), operator-=(const matrix<T>& M),

//minor(unsigned i, unsigned j) const,

//minor_det(unsigned i, unsigned j) const, det() const -- mb

//operator*( const matrix<T>& B) const ... mb

template<class T>

matrix<T> matrix<T>::leftdiv( const matrix<T>& D ) const

{

const matrix<T>& N = *this;

if( N.rows != D.rows )

throw std::domain_error("matrix divide: incompatible orders");

matrix<T> Q(D.cols,N.cols); // quotient matrix

if( N.cols > 1 ) {

// break this down for each column of the numerator

for(unsigned j=0;j<Q.cols;j++)

Q.setcol( j, N.getcol(j).leftdiv(D) ); // note: recursive

return Q;

}

// from here on, N.col == 1

if( D.rows < D.cols )

throw underdetermined();

if( D.rows > D.cols ) {

bool solution = false;

for(unsigned i=0;i<D.rows;i++) {

matrix<T> D2 = D.delrow(i); // delete a row from the matrix

matrix<T> N2 = N.delrow(i);

matrix<T> Q2(Q);  

try {

Q2 = N2.leftdiv(D2);

}

catch( underdetermined x ) {

continue; // try again with next row

}

if( !solution ) {

// this is our possible solution

solution = true;

Q = Q2;

} else {

// do the solutions agree?

if( Q != Q2 )

throw overdetermined();

}

}

if( !solution )

throw underdetermined();

return Q;

}

// D.rows == D.cols && N.cols == 1

// use Kramer's Rule

//

// D is a square matrix of order N x N

// N is a matrix of order N x 1

const T T0(0.0); // additive identity

if( D.cols<=3 ) {

T ddet = D.det();

if( ddet == T0 )

throw underdetermined();

for(unsigned j=0;j<D.cols;j++) {

matrix<T> A(D); // make a copy of the D matrix

// replace column with numerator vector

A.setcol(j,N);

Q(j,0) = A.det()/ddet;

}

} else {

// this method optimizes the determinant calculations

// by saving a cofactors used in calculating the

// denominator determinant.

kahn_sum<T> sum;

vector<T> cofactors(D.cols); // save cofactors

for(unsigned j=0;j<D.cols;j++) {

T c = D.minor_det(0,j);

cofactors[j] = c;

T a = D(0,j);

if( a != T0 ) {

a *= c;

if(j%2)

sum -= a;

else

sum += a;

}

}

T ddet = sum;

if( ddet == T0 )

throw underdetermined();

for(unsigned j = 0;j<D.cols;j++) {

matrix<T> A(D);

A.setcol(j,N);

kahn_sum<T> ndet;

for(unsigned k=0;k<D.cols;k++) {

T a = A(0,k);

if( a != T0 ) {

if(k==j)

a *= cofactors[k]; // use previously calculated

// cofactor

else

a *= A.minor_det(0,k); // calculate minor's determinant

if(k%2)

ndet -= a;

else

ndet += a;

}

}

Q(j,0) = T(ndet)/ddet;

}

}

return Q;

}

//not shown: inverse() const, getrow(unsigned i) const,

//getcol(unsigned j) const, delcol(unsigned j) const,

//delrow(unsigned i) const, setcol(unsigned j, const matrix<T>& C)

//setrow(unsigned i, const matrix<T>& R), identity() const,

//isidentity() const, pow(int exp) const,

//pow(const matrix<T>& M, int exp) --mb

// ...

#endif

/* End of File */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote