Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Use the attached file Matrices-3.h for the project specification. Store the elements of your matrix in a private 2D vector of doubles. Some of

C++

Use the attached file Matrices-3.himage text in transcribed for the project specification. Store the elements of your matrix in a private 2D vector of doubles. Some of the functions are already done, including an assignment operator to assign a(i,j), an access operator to read a(i,j), and accessors to get the number of rows and columns in the matrix. The following function headers:

double& operator()(int i, int j) const double& operator()(int i, int j) const

act as operators for access and assignment. The operator() syntax allows usage of the form a(i,j) in your code. The first line returns a reference to the double at row i, column j within the private vector so that element can be changed. The second line returns a const reference to the same element so it can be read. Since they are declared from within the class, the compiler will automatically use the calling object for the operation. These operators are done for you.

The rest of the functions you must implement:

  • (1 pt) Matrix(int _rows, int _cols)
    • Construct a matrix of the specified size
    • Initialize each element to 0
    • You will want to use the vector resize function here
  • (1 pt) Matrix operator+(const Matrix& a, const Matrix& b)
    • Add each corresponding element
    • Construct a local matrix to store the result and return it
    • usage: c = a + b;
    • If a and b do not have the same number of rows and columns, throw an error
    • Example:
b: 1 0.866025 1 0.5 0 0.5 1 0.866025 c = b + b: 2 1.73205 2 1 0 1 2 1.73205
  • (2 pts) Matrix operator*(const Matrix& a, const Matrix& b);
    • Perform a matrix multiplication
      • This will be the most difficult part of the project. It does NOT consist of just multiplying each corresponding element. You may have to do some research on what this operation is:
        • This Khan Academy (Links to an external site.) page explains it well
        • Wolfram (Links to an external site.) also has a good, but very concise, description
    • For a matrix a with row i and column j, and a matrix b with row j and column k, the product c of two matrices a and b is defined as:
    • c(i,k) = a(i,0) * b(0,k) + a(i,1) * b(1,k) + a(i,2) * b(2,k) + ...
      • The elements at row i of matrix a are multiplied and summed with the elements in column k of matrix b, storing the resulting sum in element (i,k) in matrix c.
        • This can be done with a triple nested for loop
          • Let the outermost loop control k, the column index for b
          • Let the loop inside of that control i, the row for a
          • The innermost loop will control j, which will determine which column to use from a and which row to use from b.
            • Multiply each a(i,j) * b(j,k) and create a running sum.
            • When the j loop is finished, store the resulting sum in c(i,k)
      • In order for this to work, the number of columns in a have to equal the number of rows in b. If they do not, throw an error.
      • Test your calculated results against handwritten examples to make sure it is working
    • Construct a local matrix to store the result and return it
    • usage: c = a * b;
    • In the example below,
      • c(0,0)=a(0,0)*b(0,0)+a(0,1)*b(1,0) = 0*1+(-1)*0 = 0
      • c(0,1)=a(0,0)*b(0,1)+a(0,1)*b(1,1) = 0*0.866025+-1*0.5 = -0.5
      • c(0,2)=a(0,0)*b(0,2)+a(0,1)*b(1,2) = 0*1+-1*1 = -1
      • ...
      • c(1,3)=a(1,0)*b(0,3)+a(1,1)*b(1,3) = 1*0.5+0*0.866025=0.5
a: 0 -1 1 0 b: 1 0.866025 1 0.5 0 0.5 1 0.866025 c = a * b: 0 -0.5 -1 -0.866025 1 0.866025 1 0.5
  • (1 pt) bool operator==(const Matrix& a, const Matrix& b)
    • If the rows and columns are not equal, return false
    • If any element (i,j) does not match, return false
    • Otherwise return true
  • (1 pt) bool operator!=(const Matrix& a, const Matrix& b)
    • Opposite of ==
  • (1 pt) ostream& operator
    • Output operator
    • Output matrices in the format shown above
      • Separate columns by ' ' and rows by ' '
    • If you want to you can specify the width of your columns using setw from
  • (2 pts) Output Test
    • Construct matrices a and b in your main.cpp file and output the following operations:
a: 0 -1 1 0 b: 1 0.866025 1 0.5 0 0.5 1 0.866025 c = b + b: 2 1.73205 2 1 0 1 2 1.73205 c = a * b: 0 -0.5 -1 -0.866025 1 0.866025 1 0.5

//////////////////////////////////////Matrices-3.h

#ifndef MATRIX_H_INCLUDED #define MATRIX_H_INCLUDED

#include "../std_lib_facilities.h"

namespace Matrices { class Matrix { public: ///Construct a matrix of the specified size. ///Initialize each element to 0. Matrix(int _rows, int _cols);

///************************************ ///inline accessors / mutators, these are done:

///Read element at row i, column j ///usage: double x = a(i,j); const double& operator()(int i, int j) const { return a.at(i).at(j); }

///Assign element at row i, column j ///usage: a(i,j) = x; double& operator()(int i, int j) { return a.at(i).at(j); }

int getRows() const{return rows;} int getCols() const{return cols;} ///************************************

private: vector> a; int rows; int cols; };

///Add each corresponding element. ///usage: c = a + b; Matrix operator+(const Matrix& a, const Matrix& b);

///Matrix multiply. See description. ///usage: c = a * b; Matrix operator*(const Matrix& a, const Matrix& b);

///Matrix comparison. See description. ///usage: a == b bool operator==(const Matrix& a, const Matrix& b);

///Matrix comparison. See description. ///usage: a != b bool operator!=(const Matrix& a, const Matrix& b);

///Output matrix. ///Separate columns by ' ' and rows by ' ' ostream& operator

#endif // MATRIX_H_INCLUDED

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions