Question
This is the code: #include #include #include math.h using namespace std; typedef double mattype; #define myfabs(x) fabs(x) class Matrix { private: // all data is
This is the code:
#include
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 "solve 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 void Matrix::unallocate() { if (m) { for (int i = 0; i Matrix::Matrix(const Matrix &a) { allocate(a.NR, a.NC); for (int i = 0; i void Matrix::zero() { if (!m) { cout int Matrix::gaussjordan() { int i, j, k; mattype *t; int BIGrow; for (j = 0; j // Normalization for (i = j + 1; i // Elimination for (k = 0; k void Matrix::addcolumns(int cols) { mattype *t; int i, j; if (!m) { cout for (i = 0; i void Matrix::removecolumns(int cols) { mattype *t; int i, j; if (!m) { cout NC -= cols; for (i = 0; i int Matrix::invert() { int i, j; if (!m) { cout addcolumns(j = NC); for (i = 0; i i = gaussjordan(); removecolumns(j); return i; } ostream& operator 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 Matrix Matrix::operator+(const Matrix &rhs) { Matrix t; if (NR != rhs.NR || NC != rhs.NC) { cout Matrix Matrix::operator-(const Matrix &rhs) { Matrix t; if (NR != rhs.NR || NC != rhs.NC) { cout Matrix Matrix::operator*(const Matrix &rhs) { Matrix t; if (NC != rhs.NR) { cout Matrix Matrix::operator*(const mattype &a) { Matrix t(NR, NC); for (int i = 0; i Matrix Matrix::operator/(const mattype &a) { Matrix t(NR, NC); if (mattype(a) == mattype(0)) { cout Matrix Matrix::operator!(void) { Matrix t(*this); t.invert(); return t; } Matrix Matrix::operator~(void) { Matrix t(NC, NR); for (int i = 0; i class Regression { private: // all data is private double *x, *y; unsigned int n; public: void allocate(unsigned int num); void unallocate(); Regression(unsigned int num=0, double *xi=0, double *yi=0); Regression(const Regression &a); ~Regression(); Regression &operator=(const Regression &rhs); double linear(double &a, double &b); double poly(unsigned int degree, double *a); Regression operator+(const Regression &rhs); Regression operator-(const double &rhs); void print(ostream &os); }; void Regression::allocate(unsigned int num) { n = num; if (num == 0) { x = y = 0; return; } x = new double[n]; y = new double[n]; } void Regression::unallocate() { if (x) delete[] x; if (y) delete[] y; } Regression::Regression(unsigned int num, double *xi, double *yi) { allocate(num); for (unsigned int i = 0; i Regression::Regression(const Regression &a) { allocate(a.n); for (unsigned int i = 0; i Regression::~Regression() { unallocate(); } Regression &Regression::operator=(const Regression &rhs) { if (this != &rhs) { unallocate(); allocate(rhs.n); for (unsigned int i = 0; i double Regression::linear(double &a, double &b) { double sumx = 0, sumy = 0, sumx2 = 0, sumxy = 0; if (n == 0) { cout double Regression::poly(unsigned int degree, double *av) { unsigned int i, j; double *sumxem, t; if (n == 0) { cout void Regression::print(ostream &os) { for (unsigned int i = 0; i ostream& operator Regression Regression::operator+(const Regression &rhs) { Regression t; unsigned int i,j; t.allocate(n + rhs.n); // Your code here return t; } Regression Regression::operator-(const double &rhs) { Regression t; unsigned int i, j; t.allocate(n); // Your code here return t; } int main(void) { unsigned int i, m; double e, a, b, *as; double x1[] = { 0, 1, 2, 3, 4 }; double y1[] = { 1, 4, -6, 5, -1 }; // change one of these numbers (keep them between 0 and +/-9) // y1[2] changed from 1 to -6 unsigned int n1 = sizeof(x1) / sizeof(x1[0]); double x2[] = { 5, 6, 7, 8, 9 }; double y2[] = { 2, 7, 1, -3, 2 }; // and change one of these numbers (keep them between 0 and +/-9) // y2[1] changed from 0 to 7 unsigned int n2 = sizeof(x2) / sizeof(x2[0]); Regression data1(n1, x1, y1); Regression data2(n2, x2, y2); cout e = data1.linear(a, b); cout m = 4; // set your order here //order changed from 2 to 4 as = new double[m + 1]; e = data1.poly(m, as); cout delete[] as; cout e = data2.linear(a, b); cout m = 5; // set your order here //order changed from 2 to 5 as = new double[m + 1]; e = data2.poly(m, as); cout delete[] as; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started