// LAB52.cpp : Defines the entry point for the console application. // #include
ID: 3606510 • Letter: #
Question
// LAB52.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "math.h"
using namespace std;
typedef double mattype;
#define myfabs(x) fabs(x)
class Matrix
{
private: // all data is private
mattype **m;
int NR, NC;
public:
Matrix() { NR = 0; NC = 0; m = 0; } //Constructor
Matrix(int rows, int cols) { allocate(rows, cols); } //Constructor
Matrix(const Matrix &a); // Constructor
~Matrix() { unallocate(); } //Destructor
void allocate(int rows, int cols);
void unallocate();
int getRows() { return NR; }
int getCols() { return NC; }
void zero();
void set(mattype val, int rows, int cols) { m[rows][cols] = val; }
mattype get(int rows, int cols) { return m[rows][cols]; }
int gaussjordan();
int invert();
void addcolumns(int cols); // Add columns for I-mat
void removecolumns(int cols); // Remove columns of I-mat
Matrix &operator=(const Matrix &a);
Matrix operator+(const Matrix &a);
Matrix operator-(const Matrix &a);
Matrix operator*(const Matrix &a);
Matrix operator*(const mattype &a); // Scalar multiply by "a"
Matrix operator/(const mattype &a); // Scalar divide by "a"
Matrix operator!(void); // Unary operation to "invert"
Matrix operator~(void); // Unary operation to "sove by gaussjordan"
};
void Matrix::allocate(int rows, int cols) {
if (rows>0 && cols>0) {
NR = rows;
NC = cols;
m = new mattype *[NR];
for (int i = 0; i<NR; i++) m[i] = new mattype[NC];
}
else {
NR = 0; NC = 0; m = 0;
}
}
void Matrix::unallocate() {
if (m) {
for (int i = 0; i<NR; i++) delete[] m[i];
delete[] m;
NR = 0; NC = 0; m = 0;
}
}
Matrix::Matrix(const Matrix &a) {
allocate(a.NR, a.NC);
for (int i = 0; i<NR; i++)
for (int j = 0; j<NC; j++)
m[i][j] = a.m[i][j];
}
void Matrix::zero() {
if (!m)
{
cout << "Matrix not sized yet." << endl;
}
else
{
for (int i = 0; i<NR; i++)
for (int j = 0; j<NC; j++)
m[i][j] = 0;
}
}
int Matrix::gaussjordan() {
int i, j, k;
mattype *t;
int BIGrow;
for (j = 0; j<NR; j++)
{
// Find Maximum Pivot
BIGrow = j;
for (i = j + 1; i<NR; i++)
{
if (myfabs(m[i][j])>myfabs(m[BIGrow][j]))
{
BIGrow = i;
}
}
if (myfabs(m[BIGrow][j])<1e-7) return 1;
t = m[j];
m[j] = m[BIGrow];
m[BIGrow] = t;
// Normalization
for (i = j + 1; i<NC; i++) m[j][i] /= m[j][j];
m[j][j] = 1.0;
// Elimination
for (k = 0; k<NR; k++)
{
if (k == j) continue; // Restart Loop
for (i = j + 1; i<NC; i++) m[k][i] -= m[j][i] * m[k][j];
m[k][j] = 0.0;
}
}
return 0;
}
void Matrix::addcolumns(int cols) {
mattype *t; int i, j;
if (!m) {
cout << "Matrix is not set." << endl;
return;
}
for (i = 0; i<NR; i++)
{
t = new mattype[NC + cols];
for (j = 0; j<NC; j++) t[j] = m[i][j];
for (; j<NC + cols; j++) t[j] = 0;
delete[] m[i];
m[i] = t;
}
NC += cols;
}
void Matrix::removecolumns(int cols) {
mattype *t; int i, j;
if (!m) {
cout << "Matrix is not set." << endl;
return;
}
NC -= cols;
for (i = 0; i<NR; i++)
{
t = new mattype[NC];
for (j = 0; j<NC; j++) t[j] = m[i][j + cols];
delete[] m[i];
m[i] = t;
}
}
int Matrix::invert() {
int i, j;
if (!m) {
cout << "Matrix is not set." << endl;
return 1;
}
if (NC != NR) {
cout << "Matrix must be square." << endl;
return 1;
}
addcolumns(j = NC);
for (i = 0; i<NR; i++) m[i][j + i] = 1;
i = gaussjordan();
removecolumns(j);
return i;
}
ostream& operator<<(ostream &os, Matrix a) {
for (int i = 0; i<a.getRows(); i++) {
for (int j = 0; j<a.getCols(); j++) os << a.get(i, j) << " ";
os << endl;
}
return os;
}
Matrix &Matrix::operator=(const Matrix &rhs) {
if (this != &rhs)
{
if (NR != rhs.NR || NC != rhs.NC)
{
unallocate();
allocate(rhs.NR, rhs.NC);
}
for (int i = 0; i < NR; i++)
for (int j = 0; j < NC; j++)
m[i][j] = rhs.m[i][j];
}
return *this;
}
Matrix Matrix::operator+(const Matrix &rhs) {
Matrix t;
if (NR != rhs.NR || NC != rhs.NC)
{
cout << "Matrices not the same size." << endl;
}
else
{
t.allocate(NR, NC);
for (int i = 0; i<NR; i++)
for (int j = 0; j<NC; j++)
t.m[i][j] = m[i][j] + rhs.m[i][j];
}
return t;
}
Matrix Matrix::operator-(const Matrix &rhs) {
Matrix t;
if (NR != rhs.NR || NC != rhs.NC)
{
cout << "Matrices not the same size." << endl;
}
else
{
t.allocate(NR, NC);
for (int i = 0; i<NR; i++)
for (int j = 0; j<NC; j++)
t.m[i][j] = m[i][j] - rhs.m[i][j];
}
return t;
}
Matrix Matrix::operator*(const Matrix &rhs) {
Matrix t;
if (NC != rhs.NR)
{
cout << "Matrices not the right size." << endl;
}
else
{
t.allocate(NR, rhs.NC);
t.zero();
//Add Code Here for Matrix multiplication
}
return t;
}
Matrix Matrix::operator*(const mattype &a) {
Matrix t(NR, NC);
// Add your code here
return t;
}
Matrix Matrix::operator/(const mattype &a) {
Matrix t(NR, NC);
if (mattype(a) == mattype(0))
{
cout << "Cannot divide by zero scalar." << endl;
t.zero();
}
else
{
// Add your code here
}
return t;
}
Matrix Matrix::operator!(void) {
Matrix t(*this);
// Add your code here
return t;
}
Matrix Matrix::operator~(void) {
Matrix t(NC, NR);
// Add your code here
return t;
}
int main(void)
{
Matrix A(2, 3);
A.set(1, 0, 0); A.set(2, 0, 1); A.set(3, 0, 2);
A.set(4, 1, 0); A.set(5, 1, 1); A.set(6, 1, 2);
Matrix B(2, 3);
B.set(7, 0, 0); B.set(8, 0, 1); B.set(9, 0, 2);
B.set(10, 1, 0); B.set(11, 1, 1); B.set(12, 1, 2);
cout << "Matrix A:" << endl << A;
cout << "Matrix A*10:" << endl << A * 10;
cout << "Matrix B:" << endl << B;
cout << "Matrix B/10:" << endl << B / 10;
Matrix C; C = A * (~B);
cout << "Matrix C = A * (~B):" << endl << C;
Matrix D; D = (~A) * B;
cout << "Matrix D = (~A) * B:" << endl << D;
cout << "Inversion of Matrix C:" << endl << !C;
cout << "Inversion of Matrix D:" << endl << !D;
Matrix E(A); E.gaussjordan(); cout << "Solution of Gauss-Jordan on A:" << endl << E;
Matrix F(B); F.gaussjordan(); cout << "Solution of Gauss-Jordan on B:" << endl << F;
return 0;
}
// in c++The question is...For matrix multiplication, if we are given two matrices A (N by M), matrix B (M by L) which result in matrix C (N by L). Write the newly added code for the “operator*” function below given Matrix B as an argument; Matrix A is the current object. The matrix checks and configuration are already done for you. C is a zero N by L matrix.
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "math.h"
using namespace std;
typedef double mattype;
#define myfabs(x) fabs(x)
class Matrix
{
private:
mattype **m;
int NR, NC;
public:
Matrix() { NR = 0; NC = 0; m = 0; }
Matrix(int rows, int cols) { allocate(rows, cols); }
Matrix(const Matrix &a);
~Matrix() { unallocate(); }
void allocate(int rows, int cols);
void unallocate();
int getRows() { return NR; }
int getCols() { return NC; }
void zero();
void set(mattype val, int rows, int cols) { m[rows][cols] = val; }
mattype get(int rows, int cols) { return m[rows][cols]; }
int gaussjordan();
int invert();
void addcolumns(int cols);
void removecolumns(int cols);
Matrix &operator=(const Matrix &a);
Matrix operator+(const Matrix &a);
Matrix operator-(const Matrix &a);
Matrix operator*(const Matrix &a);
Matrix operator*(const mattype &a);
Matrix operator/(const mattype &a);
Matrix operator!(void); // Unary operation to "invert"
Matrix operator~(void); // Unary operation to "sove by gaussjordan"
};
void Matrix::allocate(int rows, int cols) {
if (rows>0 && cols>0) {
NR = rows;
NC = cols;
m = new mattype *[NR];
for (int i = 0; i<NR; i++) m[i] = new mattype[NC];
}
else {
NR = 0; NC = 0; m = 0;
}
}
void Matrix::unallocate() {
if (m) {
for (int i = 0; i<NR; i++) delete[] m[i];
delete[] m;
NR = 0; NC = 0; m = 0;
}
}
Matrix::Matrix(const Matrix &a) {
allocate(a.NR, a.NC);
for (int i = 0; i<NR; i++)
for (int j = 0; j<NC; j++)
m[i][j] = a.m[i][j];
}
void Matrix::zero() {
if (!m)
{
cout << "Matrix not sized yet." << endl;
}
else
{
for (int i = 0; i<NR; i++)
for (int j = 0; j<NC; j++)
m[i][j] = 0;
}
}
int Matrix::gaussjordan() {
int i, j, k;
mattype *t;
int BIGrow;
for (j = 0; j<NR; j++)
{
BIGrow = j;
for (i = j + 1; i<NR; i++)
{
if (myfabs(m[i][j])>myfabs(m[BIGrow][j]))
{
BIGrow = i;
}
}
if (myfabs(m[BIGrow][j])<1e-7) return 1;
t = m[j];
m[j] = m[BIGrow];
m[BIGrow] = t;
for (i = j + 1; i<NC; i++) m[j][i] /= m[j][j];
m[j][j] = 1.0;
for (k = 0; k<NR; k++)
{
if (k == j) continue; // Restart Loop
for (i = j + 1; i<NC; i++) m[k][i] -= m[j][i] * m[k][j];
m[k][j] = 0.0;
}
}
return 0;
}
void Matrix::addcolumns(int cols) {
mattype *t; int i, j;
if (!m) {
cout << "Matrix is not set." << endl;
return;
}
for (i = 0; i<NR; i++)
{
t = new mattype[NC + cols];
for (j = 0; j<NC; j++) t[j] = m[i][j];
for (; j<NC + cols; j++) t[j] = 0;
delete[] m[i];
m[i] = t;
}
NC += cols;
}
void Matrix::removecolumns(int cols) {
mattype *t; int i, j;
if (!m) {
cout << "Matrix is not set." << endl;
return;
}
NC -= cols;
for (i = 0; i<NR; i++)
{
t = new mattype[NC];
for (j = 0; j<NC; j++) t[j] = m[i][j + cols];
delete[] m[i];
m[i] = t;
}
}
int Matrix::invert() {
int i, j;
if (!m) {
cout << "Matrix is not set." << endl;
return 1;
}
if (NC != NR) {
cout << "Matrix must be square." << endl;
return 1;
}
addcolumns(j = NC);
for (i = 0; i<NR; i++) m[i][j + i] = 1;
i = gaussjordan();
removecolumns(j);
return i;
}
ostream& operator<<(ostream &os, Matrix a) {
for (int i = 0; i<a.getRows(); i++) {
for (int j = 0; j<a.getCols(); j++) os << a.get(i, j) << " ";
os << endl;
}
return os;
}
Matrix &Matrix::operator=(const Matrix &rhs) {
if (this != &rhs)
{
if (NR != rhs.NR || NC != rhs.NC)
{
unallocate();
allocate(rhs.NR, rhs.NC);
}
for (int i = 0; i < NR; i++)
for (int j = 0; j < NC; j++)
m[i][j] = rhs.m[i][j];
}
return *this;
}
Matrix Matrix::operator+(const Matrix &rhs) {
Matrix t;
if (NR != rhs.NR || NC != rhs.NC)
{
cout << "Matrices not the same size." << endl;
}
else
{
t.allocate(NR, NC);
for (int i = 0; i<NR; i++)
for (int j = 0; j<NC; j++)
t.m[i][j] = m[i][j] + rhs.m[i][j];
}
return t;
}
Matrix Matrix::operator-(const Matrix &rhs) {
Matrix t;
if (NR != rhs.NR || NC != rhs.NC)
{
cout << "Matrices not the same size." << endl;
}
else
{
t.allocate(NR, NC);
for (int i = 0; i<NR; i++)
for (int j = 0; j<NC; j++)
t.m[i][j] = m[i][j] - rhs.m[i][j];
}
return t;
}
Matrix Matrix::operator*(const Matrix &rhs) {
Matrix t;
if (NC != rhs.NR)
{
cout << "Matrices not the right size." << endl;
}
else
{
t.allocate(NR, rhs.NC);
t.zero();
//Add Code Here for Matrix multiplication
}
return t;
}
Matrix Matrix::operator*(const mattype &a) {
Matrix t(NR, NC);
// Add your code here
return t;
}
Matrix Matrix::operator/(const mattype &a) {
Matrix t(NR, NC);
if (mattype(a) == mattype(0))
{
cout << "Cannot divide by zero scalar." << endl;
t.zero();
}
else
{
// Add your code here
}
return t;
}
Matrix Matrix::operator!(void) {
Matrix t(*this);
// Add your code here
return t;
}
Matrix Matrix::operator~(void) {
Matrix t(NC, NR);
// Add your code here
return t;
}
int main(void)
{
Matrix A(2, 3);
A.set(1, 0, 0); A.set(2, 0, 1); A.set(3, 0, 2);
A.set(4, 1, 0); A.set(5, 1, 1); A.set(6, 1, 2);
Matrix B(2, 3);
B.set(7, 0, 0); B.set(8, 0, 1); B.set(9, 0, 2);
B.set(10, 1, 0); B.set(11, 1, 1); B.set(12, 1, 2);
cout << "Matrix A:" << endl << A;
cout << "Matrix A*10:" << endl << A * 10;
cout << "Matrix B:" << endl << B;
cout << "Matrix B/10:" << endl << B / 10;
Matrix C; C = A * (~B);
cout << "Matrix C = A * (~B):" << endl << C;
Matrix D; D = (~A) * B;
cout << "Matrix D = (~A) * B:" << endl << D;
cout << "Inversion of Matrix C:" << endl << !C;
cout << "Inversion of Matrix D:" << endl << !D;
Matrix E(A); E.gaussjordan(); cout << "Solution of Gauss-Jordan on A:" << endl << E;
Matrix F(B); F.gaussjordan(); cout << "Solution of Gauss-Jordan on B:" << endl << F;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.