#include<iostream.h>
#include<conio.h>
class Matrix
{
public:
Matrix()
{
p = NULL;
rows = 0;
cols = 0;
}
Matrix(const int row_count, const int column_count)
{
p = NULL;
if (row_count > 0 && column_count > 0)
{
rows = row_count;
cols = column_count;
p = new double*[rows];
for (int r = 0; r < rows; r++)
{
p[r] = new double[rows];
for (int c = 0; c < cols; c++)
{
p[r][c] = 0;
}
}
}
}
Matrix(const Matrix& a)
{
rows = a.rows;
cols = a.cols;
p = new double*[a.rows];
for (int r = 0; r < a.rows; r++)
{
p[r] = new double[a.cols];
for (int c = 0; c < a.cols; c++)
{
p[r][c] = a.p[r][c];
}
}
}
double& operator()(const int r, const int c)
{
if (p != NULL && r > 0 && r <= rows && c > 0 && c <= cols)
{
return p[r-1][c-1];
}
else
{
throw Exception("Subscript out of range");
}
}
Matrix& operator= (const Matrix& a)
{
rows = a.rows;
cols = a.cols;
p = new double*[a.rows];
for (int r = 0; r < a.rows; r++)
{
p[r] = new double[a.cols];
for (int c = 0; c < a.cols; c++)
{
p[r][c] = a.p[r][c];
}
}
return *this;
}
friend Matrix operator+(const Matrix& a, const Matrix& B)
{
{
Matrix res(a.rows, a.cols);
for (int r = 0; r < a.rows; r++)
{
for (int c = 0; c < a.cols; c++)
{
}
}
return res;
}
else
{
throw Exception("Dimensions does not match");
}
return Matrix();
}
Matrix& add(double v)
{
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
p[r][c] += v;
}
}
return *this;
}
Matrix& subtract(double v)
{
return add(-v);
}
Matrix& multiply(double v)
{
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
p[r][c] *= v;
}
}
return *this;
}
friend Matrix operator- (const Matrix& a, const Matrix& b)
{
if (a.rows == b.rows && a.cols == b.cols)
{
Matrix res(a.rows, a.cols);
for (int r = 0; r < a.rows; r++)
{
for (int c = 0; c < a.cols; c++)
{
res.p[r][c] = a.p[r][c] - b.p[r][c];
}
}
return res;
}
else
{
throw Exception("Dimensions does not match");
}
return Matrix();
}
friend Matrix operator- (const Matrix& a)
{
Matrix res(a.rows, a.cols);
for (int r = 0; r < a.rows; r++)
{
for (int c = 0; c < a.cols; c++)
{
res.p[r][c] = -a.p[r][c];
}
}
return res;
}
friend Matrix operator* (const Matrix& a, const Matrix& B)
{
if (a.cols == b.rows)
{
Matrix res(a.rows, b.cols);
for (int r = 0; r < a.rows; r++)
{
for (int c_res = 0; c_res < b.cols; c_res++)
{
for (int c = 0; c < a.cols; c++)
{
res.p[r][c_res] += a.p[r][c] * b.p[c][c_res];
}
}
}
return res;
}
else
{
throw Exception("Dimensions does not match");
}
return Matrix();
}
Matrix minor(const int row, const int col)
{
Matrix res;
if (row > 0 && row <= rows && col > 0 && col <= cols)
{
res = Matrix(rows - 1, cols - 1);
for (int r = 1; r <= (rows - (row >= rows)); r++)
{
for (int c = 1; c <= (cols - (col >= cols)); c++)
{
res(r - (r > row), c - (c > col)) = p[r-1][c-1];
}
}
}
else
{
throw Exception("Index for minor out of range");
}
return res;
}
int size(const int i)
{
if (i == 1)
{
return rows;
}
else if (i == 2)
{
return cols;
}
return 0;
}
int get_rows()
{
return rows;
}
int get_cols()
{
return cols;
}
void print()
{
if (p != NULL)
{
printf("[");
for (int r = 0; r < rows; r++)
{
if (r > 0)
{
printf(" ");
}
for (int c = 0; c < cols-1; c++)
{
printf("%.2f, ", p[r][c]);
}
if (r < rows-1)
{
printf("%.2f;\n", p[r][cols-1]);
}
else
{
printf("%.2f]\n", p[r][cols-1]);
}
}
}
else
{
printf("[ ]\n");
}
}
public:
~Matrix()
{
for (int r = 0; r < rows; r++)
{
delete p[r];
}
delete p;
p = NULL;
}
private:
int rows;
int cols;
double** p;
};
int size(Matrix& a, const int i)
{
return a.size(i);
}
Matrix operator+ (const Matrix& a, const double B)
{
Matrix res = a;
res.add(B);
return res;
}
Matrix operator+ (const double b, const Matrix& a)
{
Matrix res = a;
res.add(b);
return res;
}
Matrix operator- (const Matrix& a, const double B)
{
Matrix res = a;
res.subtract(B);
return res;
}
Matrix operator- (const double b, const Matrix& a)
{
Matrix res = -a;
res.add(b);
return res;
}
Matrix operator* (const Matrix& a, const double B)
{
Matrix res = a;
res.multiply(B);
return res;
}
Matrix operator* (const double b, const Matrix& a)
{
Matrix res = a;
res.multiply(b);
return res;
}
Matrix ones(const int rows, const int cols)
{
Matrix res = Matrix(rows, cols);
for (int r = 1; r < rows; r++)
{
for (int c = 1; c <= cols; c++)
{
res(r, c) = 1;
}
}
return res;
}
Matrix zeros(const int rows, const int cols)
{
return Matrix(rows, cols);
}
Matrix diag(const int n)
{
Matrix res = Matrix(n, n);
for (int i = 1; i <= n; i++)
{
res(i, i) = 1;
}
return res;
}
Matrix diag(Matrix& v)
{
Matrix res;
if (v.get_cols() == 1)
{
int rows = v.get_rows();
res = Matrix(rows, rows);
for (int r=1; r <= rows; r++)
{
res(r, r) = v(r, 1);
}
}
else if (v.get_rows() == 1)
{
int cols = v.get_cols();
res = Matrix(cols, cols);
for (int c=1; c <= cols; c++)
{
res(c, c) = v(1, c);
}
}
else
{
throw Exception("Parameter for diag must be a vector");
}
return res;
}
double det(Matrix& a)
{
double d = 0;
int rows = a.get_rows();
int cols = a.get_cols();
if (rows == cols)
{
if (rows == 1)
{
d = a(1, 1);
}
else if (rows == 2)
{
d = a(1, 1) * a(2, 2) - a(2, 1) * a(1, 2);
}
else
{
for (int c = 1; c <= cols; c++)
{
Matrix M = a.minor(1, c);
d += (c%2 + c%2 - 1) * a(1, c) * det(M);
}
}
}
else
{
throw Exception("Matrix must be square");
}
return d;
}
void swap(double& a, double& b)
{
double temp = a;
a = b;
b = temp;
}
Matrix inv(Matrix& a)
{
Matrix res;
double d = 0;
int rows = a.get_rows();
int cols = a.get_cols();
d = det(a);
if (rows == cols && d != 0)
{
if (rows == 1)
{
res = Matrix(rows, cols);
res(1, 1) = 1 / a(1, 1);
}
else if (rows == 2)
{
res = Matrix(rows, cols);
res(1, 1) = a(2, 2);
res(1, 2) = -a(1, 2);
res(2, 1) = -a(2, 1);
res(2, 2) = a(1, 1);
res = (1/d) * res;
}
else
{
res = diag(rows);
Matrix ai = a;
for (int c = 1; c <= cols; c++)
{
int r;
for (r = c; r <= rows && ai(r, c) == 0; r++)
{
}
if (r != c)
{
for (int s = 1; s <= cols; s++)
{
swap(ai(c, s), ai(r, s));
swap(res(c, s), res(r, s));
}
}
for (int r = 1; r <= rows; r++)
{
if(r != c)
{
if (ai(r, c) != 0)
{
double f = - ai(r, c) / ai(c, c);
for (int s = 1; s <= cols; s++)
{
ai(r, s) += f * ai(c, s);
res(r, s) += f * res(c, s);
}
}
}
else
{
double f = ai(c, c);
for (int s = 1; s <= cols; s++)
{
ai(r, s) /= f;
res(r, s) /= f;
}
}
}
}
}
}
else
{
if (rows == cols)
{
throw Exception("Matrix must be square");
}
else
{
throw Exception("Determinant of matrix is zero");
}
}
return res;
}
int main(int argc, char *argv[])
{
try
{
int cols = 3;
int rows = 3;
Matrix A = Matrix(cols, rows);
int count = 0;
for (int r = 1; r <= rows; r++)
{
for (int c = 1; c <= cols; c++)
{
count ++;
A(r, c) = count;
}
}
A(2,1) = 1.23;
double centervalue = A(2,2);
printf("centervalue = %f \n", centervalue);
printf("\n");
printf("A = \n");
A.print();
printf("\n");
Matrix B = ones(rows, cols) + diag(rows);
printf("B = \n");
B.print();
printf("\n");
Matrix B2 = Matrix(rows, 1);
count = 1;
for (int r = 1; r <= rows; r++)
{
count ++;
B2(r, 1) = count * 2;
}
printf("B2 = \n");
B2.print();
printf("\n");
Matrix C;
C = A + B;
printf("A + B = \n");
C.print();
printf("\n");
C = A - B;
printf("A - B = \n");
C.print();
printf("\n");
C = A * B2;
printf("A * B2 = \n");
C.print();
printf("\n");
Matrix E = diag(B2);
printf("E = \n");
E.print();
printf("\n");
Matrix D = Matrix(2, 2);
D(1,1) = 2;
D(1,2) = 4;
D(2,1) = 1;
D(2,2) = -2;
printf("D = \n");
D.print();
printf("det(D) = %f\n\n", det(D));
printf("A = \n");
A.print();
printf("\n");
printf("det(A) = %f\n\n", det(A));
Matrix F;
F = 3 - A ;
printf("3 - A = \n");
F.print();
printf("\n");
Matrix G = Matrix(2, 2);
G(1, 1) = 1;
G(1, 2) = 2;
G(2, 1) = 3;
G(2, 2) = 4;
printf("G = \n");
G.print();
printf("\n");
Matrix G_inv = inv(G);
printf("inv(G) = \n");
G_inv.print();
printf("\n");
Matrix A_inv = inv(A);
printf("inv(A) = \n");
A_inv.print();
printf("\n");
}
catch (Exception err)
{
printf("Error: %s\n", "err.msg");
}
catch (...)
{
printf("An error occured...\n");
}
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}